]>
git.zerfleddert.de Git - micropolis/blob - src/sim/s_fileio.c
37c2a98b9f1b19e03d295748d35586fb9c0652e9
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
64 void DoSaveCityAs(void);
65 void DidSaveCity(void);
66 void DidntSaveCity(char *msg
);
67 void DidLoadCity(void);
68 void DidntLoadCity(char *msg
);
69 void DidLoadScenario(void);
72 #define SWAP_SHORTS(a,b) _swap_shorts(a,b)
73 #define SWAP_LONGS(a,b) _swap_longs(a,b)
74 #define HALF_SWAP_LONGS(a,b) _half_swap_longs(a,b)
76 #define NOOP_ON_BE { int test = 1; if (!(*(unsigned char*) (&test))) return; }
79 _swap_shorts(short *buf
, int len
)
85 /* Flip bytes in each short! */
86 for (i
= 0; i
< len
; i
++) {
87 *buf
= ((*buf
& 0xFF) <<8) | ((*buf
&0xFF00) >>8);
94 _swap_longs(long *buf
, int len
)
100 /* Flip bytes in each long! */
101 for (i
= 0; i
< len
; i
++) {
104 ((l
& 0x000000ff) << 24) |
105 ((l
& 0x0000ff00) << 8) |
106 ((l
& 0x00ff0000) >> 8) |
107 ((l
& 0xff000000) >> 24);
114 _half_swap_longs(long *buf
, int len
)
120 /* Flip bytes in each long! */
121 for (i
= 0; i
< len
; i
++) {
124 ((l
& 0x0000ffff) << 16) |
125 ((l
& 0xffff0000) >> 16);
131 _load_short(short *buf
, int len
, FILE *f
)
133 if (fread(buf
, sizeof(short), len
, f
) != len
)
136 SWAP_SHORTS(buf
, len
); /* to intel */
144 _load_long(long *buf
, int len
, FILE *f
)
146 if (fread(buf
, sizeof(long), len
, f
) != len
)
149 SWAP_LONGS(buf
, len
); /* to intel */
157 _save_short(short *buf
, int len
, FILE *f
)
160 SWAP_SHORTS(buf
, len
); /* to MAC */
162 if (fwrite(buf
, sizeof(short), len
, f
) != len
)
165 SWAP_SHORTS(buf
, len
); /* back to intel */
173 _save_long(long *buf
, int len
, FILE *f
)
176 SWAP_LONGS(buf
, len
); /* to MAC */
178 if (fwrite(buf
, sizeof(long), len
, f
) != len
)
181 SWAP_LONGS(buf
, len
); /* back to intel */
190 _load_file(char *filename
, char *dir
)
198 sprintf(path
, "%s\\%s", dir
, filename
);
201 if ((f
= fopen(filename
, "rb")) == NULL
) {
206 sprintf(path
, "%s/%s", dir
, filename
);
209 if ((f
= fopen(filename
, "r")) == NULL
) {
214 fseek(f
, 0L, SEEK_END
);
216 fseek(f
, 0L, SEEK_SET
);
219 case 27120: /* Normal city */
222 case 99120: /* 2x2 city */
225 case 219120: /* 3x3 city */
232 if ((_load_short(ResHis
, HISTLEN
/ 2, f
) == 0) ||
233 (_load_short(ComHis
, HISTLEN
/ 2, f
) == 0) ||
234 (_load_short(IndHis
, HISTLEN
/ 2, f
) == 0) ||
235 (_load_short(CrimeHis
, HISTLEN
/ 2, f
) == 0) ||
236 (_load_short(PollutionHis
, HISTLEN
/ 2, f
) == 0) ||
237 (_load_short(MoneyHis
, HISTLEN
/ 2, f
) == 0) ||
238 (_load_short(MiscHis
, MISCHISTLEN
/ 2, f
) == 0) ||
239 (_load_short((&Map
[0][0]), WORLD_X
* WORLD_Y
, f
) < 0)) {
241 /* TODO: report error */
251 int loadFile(char *filename
)
255 if (_load_file(filename
, NULL
) == 0)
258 /* total funds is a long..... MiscHis is array of shorts */
259 /* total funds is being put in the 50th & 51th word of MiscHis */
260 /* find the address, cast the ptr to a lontPtr, take contents */
262 l
= *(QUAD
*)(MiscHis
+ 50);
263 HALF_SWAP_LONGS(&l
, 1);
266 l
= *(QUAD
*)(MiscHis
+ 8);
267 HALF_SWAP_LONGS(&l
, 1);
270 autoBulldoze
= MiscHis
[52]; /* flag for autoBulldoze */
271 autoBudget
= MiscHis
[53]; /* flag for autoBudget */
272 autoGo
= MiscHis
[54]; /* flag for autoGo */
273 UserSoundOn
= MiscHis
[55]; /* flag for the sound on/off */
274 CityTax
= MiscHis
[56];
275 SimSpeed
= MiscHis
[57];
276 // sim_skips = sim_skip = 0;
278 MustUpdateOptions
= 1;
282 l
= *(QUAD
*)(MiscHis
+ 58);
283 HALF_SWAP_LONGS(&l
, 1);
284 policePercent
= l
/ 65536.0;
286 l
= *(QUAD
*)(MiscHis
+ 60);
287 HALF_SWAP_LONGS(&l
, 1);
288 firePercent
= l
/ 65536.0;
290 l
= *(QUAD
*)(MiscHis
+ 62);
291 HALF_SWAP_LONGS(&l
, 1);
292 roadPercent
= l
/ 65536.0;
294 policePercent
= (*(QUAD
*)(MiscHis
+ 58)) / 65536.0; /* and 59 */
295 firePercent
= (*(QUAD
*)(MiscHis
+ 60)) / 65536.0; /* and 61 */
296 roadPercent
=(*(QUAD
*)(MiscHis
+ 62)) / 65536.0; /* and 63 */
300 if ((CityTax
> 20) || (CityTax
< 0))
302 if ((SimSpeed
< 0) || (SimSpeed
> 3))
310 /* set the scenario id to 0 */
323 int saveFile(char *filename
)
329 if ((f
= fopen(filename
, "wb")) == NULL
) {
331 if ((f
= fopen(filename
, "w")) == NULL
) {
333 /* TODO: report error */
337 /* total funds is a long..... MiscHis is array of ints */
338 /* total funds is bien put in the 50th & 51th word of MiscHis */
339 /* find the address, cast the ptr to a lontPtr, take contents */
342 HALF_SWAP_LONGS(&l
, 1);
343 (*(QUAD
*)(MiscHis
+ 50)) = l
;
346 HALF_SWAP_LONGS(&l
, 1);
347 (*(QUAD
*)(MiscHis
+ 8)) = l
;
349 MiscHis
[52] = autoBulldoze
; /* flag for autoBulldoze */
350 MiscHis
[53] = autoBudget
; /* flag for autoBudget */
351 MiscHis
[54] = autoGo
; /* flag for autoGo */
352 MiscHis
[55] = UserSoundOn
; /* flag for the sound on/off */
353 MiscHis
[57] = SimSpeed
;
354 MiscHis
[56] = CityTax
; /* post release */
358 l
= (int)(policePercent
* 65536);
359 HALF_SWAP_LONGS(&l
, 1);
360 (*(QUAD
*)(MiscHis
+ 58)) = l
;
362 l
= (int)(firePercent
* 65536);
363 HALF_SWAP_LONGS(&l
, 1);
364 (*(QUAD
*)(MiscHis
+ 60)) = l
;
366 l
= (int)(roadPercent
* 65536);
367 HALF_SWAP_LONGS(&l
, 1);
368 (*(QUAD
*)(MiscHis
+ 62)) = l
;
370 if ((_save_short(ResHis
, HISTLEN
/ 2, f
) == 0) ||
371 (_save_short(ComHis
, HISTLEN
/ 2, f
) == 0) ||
372 (_save_short(IndHis
, HISTLEN
/ 2, f
) == 0) ||
373 (_save_short(CrimeHis
, HISTLEN
/ 2, f
) == 0) ||
374 (_save_short(PollutionHis
, HISTLEN
/ 2, f
) == 0) ||
375 (_save_short(MoneyHis
, HISTLEN
/ 2, f
) == 0) ||
376 (_save_short(MiscHis
, MISCHISTLEN
/ 2, f
) == 0) ||
377 (_save_short((&Map
[0][0]), WORLD_X
* WORLD_Y
, f
) < 0)) {
379 /* TODO: report error */
390 LoadScenario(short s
)
392 char *name
= NULL
, *fname
= NULL
;
394 if (CityFileName
!= NULL
) {
395 ckfree(CityFileName
);
401 if ((s
< 1) || (s
> 8)) s
= 1;
408 CityTime
= ((1900 - 1900) * 48) + 2;
412 name
= "San Francisco";
415 CityTime
= ((1906 - 1900) * 48) + 2;
422 CityTime
= ((1944 - 1900) * 48) + 2;
429 CityTime
= ((1965 - 1900) * 48) + 2;
436 CityTime
= ((1957 - 1900) * 48) + 2;
443 CityTime
= ((1972 - 1900) * 48) + 2;
450 CityTime
= ((2010 - 1900) * 48) + 2;
454 name
= "Rio de Janeiro";
457 CityTime
= ((2047 - 1900) * 48) + 2;
462 setAnyCityName(name
);
463 // sim_skips = sim_skip = 0;
468 gettimeofday(&start_time
, NULL
);
470 _load_file(fname
, ResourceDir
);
486 DidLoadScenario(void)
488 Eval("UIDidLoadScenario");
492 int LoadCity(char *filename
)
497 if (loadFile(filename
)) {
498 if (CityFileName
!= NULL
)
499 ckfree(CityFileName
);
500 CityFileName
= (char *)ckalloc(strlen(filename
) + 1);
501 strcpy(CityFileName
, filename
);
503 if ((cp
= (char *)rindex(filename
, '.')))
506 if ((cp
= (char *)rindex(filename
, '\\')))
508 if ((cp
= (char *)rindex(filename
, '/')))
513 filename
= (char *)ckalloc(strlen(cp
) + 1);
514 strcpy(filename
, cp
);
515 setCityName(filename
);
516 gettimeofday(&start_time
, NULL
);
523 sprintf(msg
, "Unable to load a city from the file named \"%s\". %s",
524 filename
? filename
: "(null)",
525 errno
? strerror(errno
) : "");
535 Eval("UIDidLoadCity");
540 DidntLoadCity(char *msg
)
543 sprintf(buf
, "UIDidntLoadCity {%s}", msg
);
553 if (CityFileName
== NULL
) {
556 if (saveFile(CityFileName
))
559 sprintf(msg
, "Unable to save the city to the file named \"%s\". %s",
560 CityFileName
? CityFileName
: "(null)",
561 errno
? strerror(errno
) : "");
571 Eval("UISaveCityAs");
578 Eval("UIDidSaveCity");
583 DidntSaveCity(char *msg
)
586 sprintf(buf
, "UIDidntSaveCity {%s}", msg
);
592 SaveCityAs(char *filename
)
597 if (CityFileName
!= NULL
)
598 ckfree(CityFileName
);
599 CityFileName
= (char *)ckalloc(strlen(filename
) + 1);
600 strcpy(CityFileName
, filename
);
602 if (saveFile(CityFileName
)) {
603 if ((cp
= (char *)rindex(filename
, '.')))
606 if ((cp
= (char *)rindex(filename
, '\\')))
608 if ((cp
= (char *)rindex(filename
, '/')))
613 filename
= (char *)ckalloc(strlen(cp
) + 1);
614 strcpy(filename
, cp
);
618 sprintf(msg
, "Unable to save the city to the file named \"%s\". %s",
619 CityFileName
? CityFileName
: "(null)",
620 errno
? strerror(errno
) : "");