Micropolis
utilities.cpp
Go to the documentation of this file.
1 /* utilities.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 
79 
80 
81 #include "micropolis.h"
82 
83 
85 
86 
87 /* comefrom: drawTaxesCollected incBoxValue decBoxValue drawCurrentFunds
88  drawActualBox updateFunds updateCurrentCost */
89 std::string Micropolis::makeDollarDecimalStr(const std::string &numStr)
90 {
91  short leftMostSet;
92  short numOfDigits;
93  short numOfChars;
94  short numOfCommas;
95  short dollarIndex = 0;
96  short numIndex = 0;
97  short x;
98 
99  numOfDigits = numStr.length();
100 
101  std::string dollarStr;
102 
103  dollarStr += '$';
104  if (numOfDigits == 1) {
105  dollarStr += numStr[0];
106  return dollarStr;
107  } else if (numOfDigits == 2) {
108  dollarStr += numStr[0];
109  dollarStr += numStr[1];
110  return dollarStr;
111  } else if (numOfDigits == 3) {
112  dollarStr += numStr[0];
113  dollarStr += numStr[1];
114  dollarStr += numStr[2];
115  return dollarStr;
116  } else {
117  leftMostSet = numOfDigits % 3;
118 
119  if (leftMostSet == 0) {
120  leftMostSet = 3;
121  }
122 
123  numOfCommas = (numOfDigits - 1) / 3;
124 
125  /* add 1 for the dollar sign */
126  numOfChars = numOfDigits + numOfCommas + 1;
127 
128  for (x = 1; x <= leftMostSet; x++) {
129  dollarStr += numStr[numIndex++];
130  }
131 
132  for (x = 1; x <= numOfCommas; x++) {
133  dollarStr += ',';
134  dollarStr += numStr[numIndex++];
135  dollarStr += numStr[numIndex++];
136  dollarStr += numStr[numIndex++];
137  }
138 
139  }
140 
141  return dollarStr;
142 }
143 
149 {
150  if (!simPaused) {
151  simPausedSpeed = simSpeedMeta;
152  setSpeed(0);
153  simPaused = true;
154  }
155 
156  // Call back even if the state did not change.
157  callback->updatePaused(this, callbackVal, simPaused);
158 }
159 
165 {
166  if (simPaused) {
167  simPaused = false;
168  setSpeed(simPausedSpeed);
169  }
170 
171  // Call back even if the state did not change.
172  callback->updatePaused(this, callbackVal, simPaused);
173 }
174 
175 
176 void Micropolis::setSpeed(short speed)
177 {
178  if (speed < 0) {
179  speed = 0;
180  } else if (speed > 3) {
181  speed = 3;
182  }
183 
184  simSpeedMeta = speed;
185 
186  if (simPaused) {
187  simPausedSpeed = simSpeedMeta;
188  speed = 0;
189  }
190 
191  simSpeed = speed;
192 
193  callback->updateSpeed(this, callbackVal, speed);
194 }
195 
196 
197 void Micropolis::setPasses(int passes)
198 {
199  simPasses = passes;
200  simPass = 0;
201  callback->updatePasses(this, callbackVal, passes);
202 }
203 
209 {
210  switch (level) {
211 
212  default:
213  case LEVEL_EASY:
214  setFunds(20000);
216  break;
217 
218  case LEVEL_MEDIUM:
219  setFunds(10000);
221  break;
222 
223  case LEVEL_HARD:
224  setFunds(5000);
226  break;
227 
228  }
229 }
230 
231 
236 {
237  assert(level >= LEVEL_FIRST && level <= LEVEL_LAST);
238  gameLevel = level;
239  updateGameLevel();
240 }
241 
242 
245 {
246  callback->updateGameLevel(this, callbackVal, gameLevel);
247 }
248 
249 
250 void Micropolis::setCityName(const std::string &name)
251 {
252  std::string cleanName;
253 
254  int i;
255  int n = name.length();
256  for (i = 0; i < n; i++) {
257  char ch = name[i];
258  if (!isalnum(ch)) {
259  ch = '_';
260  }
261  cleanName.push_back(ch);
262  }
263 
264  setCleanCityName(cleanName);
265 }
266 
267 
272 void Micropolis::setCleanCityName(const std::string &name)
273 {
274  cityName = name;
275 
276  callback->updateCityName(this, callbackVal, cityName);
277 }
278 
279 
280 void Micropolis::setYear(int year)
281 {
282  // Must prevent year from going negative, since it screws up the non-floored modulo arithmetic.
283  if (year < startingYear) {
284  year = startingYear;
285  }
286 
287  year = (year - startingYear) - (cityTime / 48);
288  cityTime += year * 48;
289  doTimeStuff();
290 }
291 
292 
298 {
299  return (cityTime / 48) + startingYear;
300 }
301 
302 
307 {
308  callback->newGame(this, callbackVal);
309 }
310 
311 
318 {
319  enableDisasters = value;
320  mustUpdateOptions = true;
321 }
322 
323 
329 {
330  autoBudget = value;
331  mustUpdateOptions = true;
332 }
333 
334 
343 {
344  autoBulldoze = value;
345  mustUpdateOptions = true;
346 }
347 
348 
356 void Micropolis::setAutoGoto(bool value)
357 {
358  autoGoto = value;
359  mustUpdateOptions = true;
360 }
361 
362 
371 {
372  enableSound = value;
373  mustUpdateOptions = true;
374 }
375 
376 
385 {
386  doAnimation = value;
387  mustUpdateOptions = true;
388 }
389 
390 
399 {
400  doMessages = value;
401  mustUpdateOptions = true;
402 }
403 
404 
412 void Micropolis::setDoNotices(bool value)
413 {
414  doNotices = value;
415  mustUpdateOptions = true;
416 }
417 
418 
425  float *resDemandResult,
426  float *comDemandResult,
427  float *indDemandResult)
428 {
429  *resDemandResult = (float)resValve / (float)RES_VALVE_RANGE;
430  *comDemandResult = (float)comValve / (float)COM_VALVE_RANGE;
431  *indDemandResult = (float)indValve / (float)IND_VALVE_RANGE;
432 }
433 
434 
void setGameLevelFunds(GameLevel level)
Definition: utilities.cpp:208
void setDoAnimation(bool value)
Definition: utilities.cpp:384
void setDoNotices(bool value)
Definition: utilities.cpp:412
GameLevel gameLevel
Difficulty level of the game (0..2)
Definition: micropolis.h:2334
Callback * callback
Definition: micropolis.h:954
Quad cityTime
Definition: micropolis.h:1092
void getDemands(float *resDemandResult, float *comDemandResult, float *indDemandResult)
Definition: utilities.cpp:424
void setAutoGoto(bool value)
Definition: utilities.cpp:356
bool doAnimation
Definition: micropolis.h:1887
bool enableDisasters
Enable disasters.
Definition: micropolis.h:2346
std::string cityName
Name of the city.
Definition: micropolis.h:1909
void setFunds(int dollars)
void updateGameLevel()
Definition: utilities.cpp:244
int currentYear()
Definition: utilities.cpp:297
void setAutoBulldoze(bool value)
Definition: utilities.cpp:342
void setEnableSound(bool value)
Definition: utilities.cpp:370
bool mustUpdateOptions
Options displayed at user need updating.
Definition: micropolis.h:2639
void setEnableDisasters(bool value)
Definition: utilities.cpp:317
bool doNotices
Definition: micropolis.h:1891
bool doMessages
Definition: micropolis.h:1889
void pause()
Definition: utilities.cpp:148
bool simPaused
Definition: micropolis.h:1877
short startingYear
Definition: micropolis.h:1109
bool enableSound
Enable sound.
Definition: micropolis.h:2344
void setGameLevel(GameLevel level)
Definition: utilities.cpp:235
bool autoBudget
Definition: micropolis.h:2330
void resume()
Definition: utilities.cpp:164
void setCleanCityName(const std::string &name)
Definition: utilities.cpp:272
bool autoGoto
Definition: micropolis.h:1960
void doNewGame()
Definition: utilities.cpp:306
void setDoMessages(bool value)
Definition: utilities.cpp:398
bool autoBulldoze
Definition: micropolis.h:2323
void setAutoBudget(bool value)
Definition: utilities.cpp:328
Header file for Micropolis game engine.
GameLevel
Definition: micropolis.h:762
@ LEVEL_FIRST
First game level value.
Definition: micropolis.h:769
@ LEVEL_MEDIUM
Intermediate game level.
Definition: micropolis.h:764
@ LEVEL_EASY
Simple game level.
Definition: micropolis.h:763
@ LEVEL_HARD
Difficult game level.
Definition: micropolis.h:765
@ LEVEL_LAST
Last game level value.
Definition: micropolis.h:770