]>
git.zerfleddert.de Git - micropolis/blob - src/sim/s_fileio.c
37637f7ac484b4979b4362ba6126710099c11a0f
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.
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.
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/>.
21 * ADDITIONAL TERMS per GNU GPL Section 7
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.
29 * Any propagation or conveyance of this program must include this
30 * copyright notice and these terms.
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.
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.
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
65 #define SWAP_SHORTS(a,b) _swap_shorts(a,b)
66 #define SWAP_LONGS(a,b) _swap_longs(a,b)
67 #define HALF_SWAP_LONGS(a,b) _half_swap_longs(a,b)
69 #define NOOP_ON_BE { int test = 1; if (!(*(unsigned char*) (&test))) return; }
72 _swap_shorts(short *buf
, int len
)
78 /* Flip bytes in each short! */
79 for (i
= 0; i
< len
; i
++) {
80 *buf
= ((*buf
& 0xFF) <<8) | ((*buf
&0xFF00) >>8);
86 _swap_longs(long *buf
, int len
)
92 /* Flip bytes in each long! */
93 for (i
= 0; i
< len
; i
++) {
96 ((l
& 0x000000ff) << 24) |
97 ((l
& 0x0000ff00) << 8) |
98 ((l
& 0x00ff0000) >> 8) |
99 ((l
& 0xff000000) >> 24);
105 _half_swap_longs(long *buf
, int len
)
111 /* Flip bytes in each long! */
112 for (i
= 0; i
< len
; i
++) {
115 ((l
& 0x0000ffff) << 16) |
116 ((l
& 0xffff0000) >> 16);
122 _load_short(short *buf
, int len
, FILE *f
)
124 if (fread(buf
, sizeof(short), len
, f
) != len
)
127 SWAP_SHORTS(buf
, len
); /* to intel */
134 _load_long(long *buf
, int len
, FILE *f
)
136 if (fread(buf
, sizeof(long), len
, f
) != len
)
139 SWAP_LONGS(buf
, len
); /* to intel */
146 _save_short(short *buf
, int len
, FILE *f
)
149 SWAP_SHORTS(buf
, len
); /* to MAC */
151 if (fwrite(buf
, sizeof(short), len
, f
) != len
)
154 SWAP_SHORTS(buf
, len
); /* back to intel */
161 _save_long(long *buf
, int len
, FILE *f
)
164 SWAP_LONGS(buf
, len
); /* to MAC */
166 if (fwrite(buf
, sizeof(long), len
, f
) != len
)
169 SWAP_LONGS(buf
, len
); /* back to intel */
177 _load_file(char *filename
, char *dir
)
185 sprintf(path
, "%s\\%s", dir
, filename
);
188 if ((f
= fopen(filename
, "rb")) == NULL
) {
193 sprintf(path
, "%s/%s", dir
, filename
);
196 if ((f
= fopen(filename
, "r")) == NULL
) {
201 fseek(f
, 0L, SEEK_END
);
203 fseek(f
, 0L, SEEK_SET
);
206 case 27120: /* Normal city */
209 case 99120: /* 2x2 city */
212 case 219120: /* 3x3 city */
219 if ((_load_short(ResHis
, HISTLEN
/ 2, f
) == 0) ||
220 (_load_short(ComHis
, HISTLEN
/ 2, f
) == 0) ||
221 (_load_short(IndHis
, HISTLEN
/ 2, f
) == 0) ||
222 (_load_short(CrimeHis
, HISTLEN
/ 2, f
) == 0) ||
223 (_load_short(PollutionHis
, HISTLEN
/ 2, f
) == 0) ||
224 (_load_short(MoneyHis
, HISTLEN
/ 2, f
) == 0) ||
225 (_load_short(MiscHis
, MISCHISTLEN
/ 2, f
) == 0) ||
226 (_load_short((&Map
[0][0]), WORLD_X
* WORLD_Y
, f
) < 0)) {
228 /* TODO: report error */
238 int loadFile(char *filename
)
242 if (_load_file(filename
, NULL
) == 0)
245 /* total funds is a long..... MiscHis is array of shorts */
246 /* total funds is being put in the 50th & 51th word of MiscHis */
247 /* find the address, cast the ptr to a lontPtr, take contents */
249 l
= *(QUAD
*)(MiscHis
+ 50);
250 HALF_SWAP_LONGS(&l
, 1);
253 l
= *(QUAD
*)(MiscHis
+ 8);
254 HALF_SWAP_LONGS(&l
, 1);
257 autoBulldoze
= MiscHis
[52]; /* flag for autoBulldoze */
258 autoBudget
= MiscHis
[53]; /* flag for autoBudget */
259 autoGo
= MiscHis
[54]; /* flag for autoGo */
260 UserSoundOn
= MiscHis
[55]; /* flag for the sound on/off */
261 CityTax
= MiscHis
[56];
262 SimSpeed
= MiscHis
[57];
263 // sim_skips = sim_skip = 0;
265 MustUpdateOptions
= 1;
269 l
= *(QUAD
*)(MiscHis
+ 58);
270 HALF_SWAP_LONGS(&l
, 1);
271 policePercent
= l
/ 65536.0;
273 l
= *(QUAD
*)(MiscHis
+ 60);
274 HALF_SWAP_LONGS(&l
, 1);
275 firePercent
= l
/ 65536.0;
277 l
= *(QUAD
*)(MiscHis
+ 62);
278 HALF_SWAP_LONGS(&l
, 1);
279 roadPercent
= l
/ 65536.0;
281 policePercent
= (*(QUAD
*)(MiscHis
+ 58)) / 65536.0; /* and 59 */
282 firePercent
= (*(QUAD
*)(MiscHis
+ 60)) / 65536.0; /* and 61 */
283 roadPercent
=(*(QUAD
*)(MiscHis
+ 62)) / 65536.0; /* and 63 */
287 if ((CityTax
> 20) || (CityTax
< 0))
289 if ((SimSpeed
< 0) || (SimSpeed
> 3))
297 /* set the scenario id to 0 */
310 int saveFile(char *filename
)
316 if ((f
= fopen(filename
, "wb")) == NULL
) {
318 if ((f
= fopen(filename
, "w")) == NULL
) {
320 /* TODO: report error */
324 /* total funds is a long..... MiscHis is array of ints */
325 /* total funds is bien put in the 50th & 51th word of MiscHis */
326 /* find the address, cast the ptr to a lontPtr, take contents */
329 HALF_SWAP_LONGS(&l
, 1);
330 (*(QUAD
*)(MiscHis
+ 50)) = l
;
333 HALF_SWAP_LONGS(&l
, 1);
334 (*(QUAD
*)(MiscHis
+ 8)) = l
;
336 MiscHis
[52] = autoBulldoze
; /* flag for autoBulldoze */
337 MiscHis
[53] = autoBudget
; /* flag for autoBudget */
338 MiscHis
[54] = autoGo
; /* flag for autoGo */
339 MiscHis
[55] = UserSoundOn
; /* flag for the sound on/off */
340 MiscHis
[57] = SimSpeed
;
341 MiscHis
[56] = CityTax
; /* post release */
345 l
= (int)(policePercent
* 65536);
346 HALF_SWAP_LONGS(&l
, 1);
347 (*(QUAD
*)(MiscHis
+ 58)) = l
;
349 l
= (int)(firePercent
* 65536);
350 HALF_SWAP_LONGS(&l
, 1);
351 (*(QUAD
*)(MiscHis
+ 60)) = l
;
353 l
= (int)(roadPercent
* 65536);
354 HALF_SWAP_LONGS(&l
, 1);
355 (*(QUAD
*)(MiscHis
+ 62)) = l
;
357 if ((_save_short(ResHis
, HISTLEN
/ 2, f
) == 0) ||
358 (_save_short(ComHis
, HISTLEN
/ 2, f
) == 0) ||
359 (_save_short(IndHis
, HISTLEN
/ 2, f
) == 0) ||
360 (_save_short(CrimeHis
, HISTLEN
/ 2, f
) == 0) ||
361 (_save_short(PollutionHis
, HISTLEN
/ 2, f
) == 0) ||
362 (_save_short(MoneyHis
, HISTLEN
/ 2, f
) == 0) ||
363 (_save_short(MiscHis
, MISCHISTLEN
/ 2, f
) == 0) ||
364 (_save_short((&Map
[0][0]), WORLD_X
* WORLD_Y
, f
) < 0)) {
366 /* TODO: report error */
376 LoadScenario(short s
)
380 if (CityFileName
!= NULL
) {
381 ckfree(CityFileName
);
387 if ((s
< 1) || (s
> 8)) s
= 1;
394 CityTime
= ((1900 - 1900) * 48) + 2;
398 name
= "San Francisco";
401 CityTime
= ((1906 - 1900) * 48) + 2;
408 CityTime
= ((1944 - 1900) * 48) + 2;
415 CityTime
= ((1965 - 1900) * 48) + 2;
422 CityTime
= ((1957 - 1900) * 48) + 2;
429 CityTime
= ((1972 - 1900) * 48) + 2;
436 CityTime
= ((2010 - 1900) * 48) + 2;
440 name
= "Rio de Janeiro";
443 CityTime
= ((2047 - 1900) * 48) + 2;
448 setAnyCityName(name
);
449 // sim_skips = sim_skip = 0;
454 gettimeofday(&start_time
, NULL
);
456 _load_file(fname
, ResourceDir
);
473 Eval("UIDidLoadScenario");
477 int LoadCity(char *filename
)
482 if (loadFile(filename
)) {
483 if (CityFileName
!= NULL
)
484 ckfree(CityFileName
);
485 CityFileName
= (char *)ckalloc(strlen(filename
) + 1);
486 strcpy(CityFileName
, filename
);
488 if (cp
= (char *)rindex(filename
, '.'))
491 if (cp
= (char *)rindex(filename
, '\\'))
493 if (cp
= (char *)rindex(filename
, '/'))
498 filename
= (char *)ckalloc(strlen(cp
) + 1);
499 strcpy(filename
, cp
);
500 setCityName(filename
);
501 gettimeofday(&start_time
, NULL
);
508 sprintf(msg
, "Unable to load a city from the file named \"%s\". %s",
509 filename
? filename
: "(null)",
510 errno
? strerror(errno
) : "");
519 Eval("UIDidLoadCity");
523 DidntLoadCity(char *msg
)
526 sprintf(buf
, "UIDidntLoadCity {%s}", msg
);
535 if (CityFileName
== NULL
) {
538 if (saveFile(CityFileName
))
541 sprintf(msg
, "Unable to save the city to the file named \"%s\". %s",
542 CityFileName
? CityFileName
: "(null)",
543 errno
? strerror(errno
) : "");
552 Eval("UISaveCityAs");
558 Eval("UIDidSaveCity");
562 DidntSaveCity(char *msg
)
565 sprintf(buf
, "UIDidntSaveCity {%s}", msg
);
570 SaveCityAs(char *filename
)
575 if (CityFileName
!= NULL
)
576 ckfree(CityFileName
);
577 CityFileName
= (char *)ckalloc(strlen(filename
) + 1);
578 strcpy(CityFileName
, filename
);
580 if (saveFile(CityFileName
)) {
581 if (cp
= (char *)rindex(filename
, '.'))
584 if (cp
= (char *)rindex(filename
, '\\'))
586 if (cp
= (char *)rindex(filename
, '/'))
591 filename
= (char *)ckalloc(strlen(cp
) + 1);
592 strcpy(filename
, cp
);
596 sprintf(msg
, "Unable to save the city to the file named \"%s\". %s",
597 CityFileName
? CityFileName
: "(null)",
598 errno
? strerror(errno
) : "");