Micropolis
graph.cpp
Go to the documentation of this file.
1 /* graph.cpp
2  *
3  * Micropolis, Unix Version. This game was released for the Unix platform
4  * in or about 1990 and has been modified for inclusion in the One Laptop
5  * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
6  * you need assistance with this program, you may contact:
7  * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or (at
12  * your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program. If
19  * not, see <http://www.gnu.org/licenses/>.
20  *
21  * ADDITIONAL TERMS per GNU GPL Section 7
22  *
23  * No trademark or publicity rights are granted. This license does NOT
24  * give you any right, title or interest in the trademark SimCity or any
25  * other Electronic Arts trademark. You may not distribute any
26  * modification of this program using the trademark SimCity or claim any
27  * affliation or association with Electronic Arts Inc. or its employees.
28  *
29  * Any propagation or conveyance of this program must include this
30  * copyright notice and these terms.
31  *
32  * If you convey this program (or any modifications of it) and assume
33  * contractual liability for the program to recipients of it, you agree
34  * to indemnify Electronic Arts for any liability that those contractual
35  * assumptions impose on Electronic Arts.
36  *
37  * You may not misrepresent the origins of this program; modified
38  * versions of the program must be marked as such and not identified as
39  * the original program.
40  *
41  * This disclaimer supplements the one included in the General Public
42  * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
43  * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
44  * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
45  * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
46  * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
47  * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
48  * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
49  * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
50  * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
51  * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
52  * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
53  * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
54  * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
55  * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
56  * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
57  * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
58  * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
59  * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
60  * NOT APPLY TO YOU.
61  */
62 
78 
79 
80 #include "micropolis.h"
81 
82 
84 
85 
86 //char *Micropolis::histName[] = {
87 // "Residential", "Commercial", "Industrial",
88 // "Cash Flow", "Crime", "Pollution"
89 //};
90 
91 //unsigned char Micropolis::histColor[] = {
92 // COLOR_LIGHTGREEN, COLOR_DARKBLUE, COLOR_YELLOW,
93 // COLOR_DARKGREEN, COLOR_RED, COLOR_OLIVE
94 //};
95 
96 
98 
99 
107 void Micropolis::drawMonth(short *hist, unsigned char *s, float scale)
108 {
109  int val, x;
110 
111  for (x = 0; x < 120; x++) {
112  val = (int)(hist[x] * scale);
113  s[119 - x] = (unsigned char)clamp(val, 0, 255);
114  }
115 }
116 
117 
123 {
124  censusChanged = true;
125 }
126 
127 
133 {
134  if (censusChanged) {
135  callback->updateHistory(this, callbackVal);
136  censusChanged = false;
137  }
138 }
139 
140 
143 {
144  if (!historyInitialized) {
145  historyInitialized = true;
146  }
147 }
148 
149 
152 {
153  int x;
154 
155  resHist10Max = 0;
156  comHist10Max = 0;
157  indHist10Max = 0;
158 
159  for (x = 118; x >= 0; x--) {
160 
161  if (resHist[x] < 0) {
162  resHist[x] = 0;
163  }
164  if (comHist[x] < 0) {
165  comHist[x] = 0;
166  }
167  if (indHist[x] < 0) {
168  indHist[x] = 0;
169  }
170 
174 
175  }
176 
177  graph10Max =
180  indHist10Max));
181 
182  resHist120Max = 0;
183  comHist120Max = 0;
184  indHist120Max = 0;
185 
186  for (x = 238; x >= 120; x--) {
187 
188  if (resHist[x] < 0) {
189  resHist[x] = 0;
190  }
191  if (comHist[x] < 0) {
192  comHist[x] = 0;
193  }
194  if (indHist[x] < 0) {
195  indHist[x] = 0;
196  }
197 
201 
202  }
203 
204  graph120Max =
207  indHist120Max));
208 }
209 
210 
218 void Micropolis::getHistoryRange(int historyType, int historyScale,
219  short *minValResult, short *maxValResult)
220 {
221  if (historyType < 0 || historyType >= HISTORY_TYPE_COUNT
222  || historyScale < 0 || historyScale >= HISTORY_SCALE_COUNT) {
223  *minValResult = 0;
224  *maxValResult = 0;
225  return;
226  }
227 
228  short *history = NULL;
229  switch (historyType) {
230  case HISTORY_TYPE_RES:
231  history = resHist;
232  break;
233  case HISTORY_TYPE_COM:
234  history = comHist;
235  break;
236  case HISTORY_TYPE_IND:
237  history = indHist;
238  break;
239  case HISTORY_TYPE_MONEY:
240  history = moneyHist;
241  break;
242  case HISTORY_TYPE_CRIME:
243  history = crimeHist;
244  break;
246  history = pollutionHist;
247  break;
248  default:
249  NOT_REACHED();
250  break;
251  }
252 
253  int offset = 0;
254  switch (historyScale) {
255  case HISTORY_SCALE_SHORT:
256  offset = 0;
257  break;
258  case HISTORY_SCALE_LONG:
259  offset = 120;
260  break;
261  default:
262  NOT_REACHED();
263  break;
264  }
265 
266  short minVal = 32000;
267  short maxVal = -32000;
268 
269  for (int i = 0; i < HISTORY_COUNT; i++) {
270  short val = history[i + offset];
271 
272  minVal = min(val, minVal);
273  maxVal = max(val, maxVal);
274  }
275 
276  *minValResult = minVal;
277  *maxValResult = maxVal;
278 }
279 
280 
288 short Micropolis::getHistory(int historyType, int historyScale,
289  int historyIndex)
290 {
291  if (historyType < 0 || historyType >= HISTORY_TYPE_COUNT
292  || historyScale < 0 || historyScale >= HISTORY_SCALE_COUNT
293  || historyIndex < 0 || historyIndex >= HISTORY_COUNT) {
294  return 0;
295  }
296 
297  short *history = NULL;
298  switch (historyType) {
299  case HISTORY_TYPE_RES:
300  history = resHist;
301  break;
302  case HISTORY_TYPE_COM:
303  history = comHist;
304  break;
305  case HISTORY_TYPE_IND:
306  history = indHist;
307  break;
308  case HISTORY_TYPE_MONEY:
309  history = moneyHist;
310  break;
311  case HISTORY_TYPE_CRIME:
312  history = crimeHist;
313  break;
315  history = pollutionHist;
316  break;
317  default:
318  NOT_REACHED();
319  break;
320  }
321 
322  int offset = 0;
323  switch (historyScale) {
324  case HISTORY_SCALE_SHORT:
325  offset = 0;
326  break;
327  case HISTORY_SCALE_LONG:
328  offset = 120;
329  break;
330  default:
331  NOT_REACHED();
332  break;
333  }
334 
335  short result = history[historyIndex + offset];
336 
337  return result;
338 }
339 
340 
348 void Micropolis::setHistory(int historyType, int historyScale,
349  int historyIndex, short historyValue)
350 {
351  if (historyType < 0 || historyType >= HISTORY_TYPE_COUNT
352  || historyScale < 0 || historyScale >= HISTORY_SCALE_COUNT
353  || historyIndex < 0 || historyIndex >= HISTORY_COUNT) {
354  return;
355  }
356 
357  short *history = NULL;
358  switch (historyType) {
359  case HISTORY_TYPE_RES:
360  history = resHist;
361  break;
362  case HISTORY_TYPE_COM:
363  history = comHist;
364  break;
365  case HISTORY_TYPE_IND:
366  history = indHist;
367  break;
368  case HISTORY_TYPE_MONEY:
369  history = moneyHist;
370  break;
371  case HISTORY_TYPE_CRIME:
372  history = crimeHist;
373  break;
375  history = pollutionHist;
376  break;
377  default:
378  NOT_REACHED();
379  break;
380  }
381 
382  int offset = 0;
383  switch (historyScale) {
384  case HISTORY_SCALE_SHORT:
385  offset = 0;
386  break;
387  case HISTORY_SCALE_LONG:
388  offset = 120;
389  break;
390  default:
391  NOT_REACHED();
392  break;
393  }
394 
395  history[historyIndex + offset] = historyValue;
396 }
397 
398 
void initGraphs()
Definition: graph.cpp:142
short indHist10Max
Definition: micropolis.h:1145
void setHistory(int historyType, int historyScale, int historyIndex, short historyValue)
Definition: graph.cpp:348
short * resHist
Definition: micropolis.h:1341
void changeCensus()
Definition: graph.cpp:122
Callback * callback
Definition: micropolis.h:954
short graph10Max
Definition: micropolis.h:1807
void graphDoer()
Definition: graph.cpp:132
short * moneyHist
Definition: micropolis.h:1356
short graph120Max
Definition: micropolis.h:1812
bool historyInitialized
Definition: micropolis.h:1802
short resHist120Max
Definition: micropolis.h:1130
short * crimeHist
Definition: micropolis.h:1366
short * pollutionHist
Definition: micropolis.h:1361
short getHistory(int historyType, int historyScale, int historyIndex)
Definition: graph.cpp:288
void drawMonth(short *hist, unsigned char *s, float scale)
Definition: graph.cpp:107
void getHistoryRange(int historyType, int historyScale, short *minValResult, short *maxValResult)
Definition: graph.cpp:218
bool censusChanged
Definition: micropolis.h:1157
short * indHist
Definition: micropolis.h:1351
short comHist10Max
Definition: micropolis.h:1135
short * comHist
Definition: micropolis.h:1346
void initGraphMax()
Definition: graph.cpp:151
short comHist120Max
Definition: micropolis.h:1140
short resHist10Max
Definition: micropolis.h:1125
short indHist120Max
Definition: micropolis.h:1150
Header file for Micropolis game engine.
@ HISTORY_TYPE_CRIME
Crime history type.
Definition: micropolis.h:287
@ HISTORY_TYPE_MONEY
Money history type.
Definition: micropolis.h:286
@ HISTORY_TYPE_POLLUTION
Pollution history type.
Definition: micropolis.h:288
@ HISTORY_TYPE_IND
Industry history type.
Definition: micropolis.h:285
@ HISTORY_TYPE_COUNT
Number of history types.
Definition: micropolis.h:290
@ HISTORY_TYPE_RES
Residiential history type.
Definition: micropolis.h:283
@ HISTORY_TYPE_COM
Commercial history type.
Definition: micropolis.h:284
static const int HISTORY_COUNT
Definition: micropolis.h:221
#define NOT_REACHED()
Definition: micropolis.h:847
@ HISTORY_SCALE_SHORT
Short scale data (10 years)
Definition: micropolis.h:297
@ HISTORY_SCALE_COUNT
Number of history scales available.
Definition: micropolis.h:300
@ HISTORY_SCALE_LONG
Long scale data (120 years)
Definition: micropolis.h:298
static T min(const T a, const T b)
Definition: micropolis.h:783
static T clamp(const T val, const T lower, const T upper)
Definition: micropolis.h:808
static T max(const T a, const T b)
Definition: micropolis.h:795