]>
cvs.zerfleddert.de Git - micropolis/blob - src/sim/s_fileio.c
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 #if defined(MSDOS) || defined(OSF1) || defined(IS_INTEL)
67 #define SWAP_SHORTS(a,b) _swap_shorts(a,b)
68 #define SWAP_LONGS(a,b) _swap_longs(a,b)
69 #define HALF_SWAP_LONGS(a,b) _half_swap_longs(a,b)
72 _swap_shorts(short *buf
, int len
)
76 /* Flip bytes in each short! */
77 for (i
= 0; i
< len
; i
++) {
78 *buf
= ((*buf
& 0xFF) <<8) | ((*buf
&0xFF00) >>8);
84 _swap_longs(long *buf
, int len
)
88 /* Flip bytes in each long! */
89 for (i
= 0; i
< len
; i
++) {
92 ((l
& 0x000000ff) << 24) |
93 ((l
& 0x0000ff00) << 8) |
94 ((l
& 0x00ff0000) >> 8) |
95 ((l
& 0xff000000) >> 24);
101 _half_swap_longs(long *buf
, int len
)
105 /* Flip bytes in each long! */
106 for (i
= 0; i
< len
; i
++) {
109 ((l
& 0x0000ffff) << 16) |
110 ((l
& 0xffff0000) >> 16);
117 #define SWAP_SHORTS(a, b)
118 #define SWAP_LONGS(a, b)
119 #define HALF_SWAP_LONGS(a, b)
125 _load_short(short *buf
, int len
, FILE *f
)
127 if (fread(buf
, sizeof(short), len
, f
) != len
)
130 SWAP_SHORTS(buf
, len
); /* to intel */
137 _load_long(long *buf
, int len
, FILE *f
)
139 if (fread(buf
, sizeof(long), len
, f
) != len
)
142 SWAP_LONGS(buf
, len
); /* to intel */
149 _save_short(short *buf
, int len
, FILE *f
)
152 SWAP_SHORTS(buf
, len
); /* to MAC */
154 if (fwrite(buf
, sizeof(short), len
, f
) != len
)
157 SWAP_SHORTS(buf
, len
); /* back to intel */
164 _save_long(long *buf
, int len
, FILE *f
)
167 SWAP_LONGS(buf
, len
); /* to MAC */
169 if (fwrite(buf
, sizeof(long), len
, f
) != len
)
172 SWAP_LONGS(buf
, len
); /* back to intel */
180 _load_file(char *filename
, char *dir
)
188 sprintf(path
, "%s\\%s", dir
, filename
);
191 if ((f
= fopen(filename
, "rb")) == NULL
) {
196 sprintf(path
, "%s/%s", dir
, filename
);
199 if ((f
= fopen(filename
, "r")) == NULL
) {
204 fseek(f
, 0L, SEEK_END
);
206 fseek(f
, 0L, SEEK_SET
);
209 case 27120: /* Normal city */
212 case 99120: /* 2x2 city */
215 case 219120: /* 3x3 city */
222 if ((_load_short(ResHis
, HISTLEN
/ 2, f
) == 0) ||
223 (_load_short(ComHis
, HISTLEN
/ 2, f
) == 0) ||
224 (_load_short(IndHis
, HISTLEN
/ 2, f
) == 0) ||
225 (_load_short(CrimeHis
, HISTLEN
/ 2, f
) == 0) ||
226 (_load_short(PollutionHis
, HISTLEN
/ 2, f
) == 0) ||
227 (_load_short(MoneyHis
, HISTLEN
/ 2, f
) == 0) ||
228 (_load_short(MiscHis
, MISCHISTLEN
/ 2, f
) == 0) ||
229 (_load_short((&Map
[0][0]), WORLD_X
* WORLD_Y
, f
) < 0)) {
231 /* TODO: report error */
241 int loadFile(char *filename
)
245 if (_load_file(filename
, NULL
) == 0)
248 /* total funds is a long..... MiscHis is array of shorts */
249 /* total funds is being put in the 50th & 51th word of MiscHis */
250 /* find the address, cast the ptr to a lontPtr, take contents */
252 l
= *(QUAD
*)(MiscHis
+ 50);
253 HALF_SWAP_LONGS(&l
, 1);
256 l
= *(QUAD
*)(MiscHis
+ 8);
257 HALF_SWAP_LONGS(&l
, 1);
260 autoBulldoze
= MiscHis
[52]; /* flag for autoBulldoze */
261 autoBudget
= MiscHis
[53]; /* flag for autoBudget */
262 autoGo
= MiscHis
[54]; /* flag for autoGo */
263 UserSoundOn
= MiscHis
[55]; /* flag for the sound on/off */
264 CityTax
= MiscHis
[56];
265 SimSpeed
= MiscHis
[57];
266 // sim_skips = sim_skip = 0;
268 MustUpdateOptions
= 1;
272 l
= *(QUAD
*)(MiscHis
+ 58);
273 HALF_SWAP_LONGS(&l
, 1);
274 policePercent
= l
/ 65536.0;
276 l
= *(QUAD
*)(MiscHis
+ 60);
277 HALF_SWAP_LONGS(&l
, 1);
278 firePercent
= l
/ 65536.0;
280 l
= *(QUAD
*)(MiscHis
+ 62);
281 HALF_SWAP_LONGS(&l
, 1);
282 roadPercent
= l
/ 65536.0;
284 policePercent
= (*(QUAD
*)(MiscHis
+ 58)) / 65536.0; /* and 59 */
285 firePercent
= (*(QUAD
*)(MiscHis
+ 60)) / 65536.0; /* and 61 */
286 roadPercent
=(*(QUAD
*)(MiscHis
+ 62)) / 65536.0; /* and 63 */
290 if ((CityTax
> 20) || (CityTax
< 0))
292 if ((SimSpeed
< 0) || (SimSpeed
> 3))
300 /* set the scenario id to 0 */
313 int saveFile(char *filename
)
319 if ((f
= fopen(filename
, "wb")) == NULL
) {
321 if ((f
= fopen(filename
, "w")) == NULL
) {
323 /* TODO: report error */
327 /* total funds is a long..... MiscHis is array of ints */
328 /* total funds is bien put in the 50th & 51th word of MiscHis */
329 /* find the address, cast the ptr to a lontPtr, take contents */
332 HALF_SWAP_LONGS(&l
, 1);
333 (*(QUAD
*)(MiscHis
+ 50)) = l
;
336 HALF_SWAP_LONGS(&l
, 1);
337 (*(QUAD
*)(MiscHis
+ 8)) = l
;
339 MiscHis
[52] = autoBulldoze
; /* flag for autoBulldoze */
340 MiscHis
[53] = autoBudget
; /* flag for autoBudget */
341 MiscHis
[54] = autoGo
; /* flag for autoGo */
342 MiscHis
[55] = UserSoundOn
; /* flag for the sound on/off */
343 MiscHis
[57] = SimSpeed
;
344 MiscHis
[56] = CityTax
; /* post release */
348 l
= (int)(policePercent
* 65536);
349 HALF_SWAP_LONGS(&l
, 1);
350 (*(QUAD
*)(MiscHis
+ 58)) = l
;
352 l
= (int)(firePercent
* 65536);
353 HALF_SWAP_LONGS(&l
, 1);
354 (*(QUAD
*)(MiscHis
+ 60)) = l
;
356 l
= (int)(roadPercent
* 65536);
357 HALF_SWAP_LONGS(&l
, 1);
358 (*(QUAD
*)(MiscHis
+ 62)) = l
;
360 if ((_save_short(ResHis
, HISTLEN
/ 2, f
) == 0) ||
361 (_save_short(ComHis
, HISTLEN
/ 2, f
) == 0) ||
362 (_save_short(IndHis
, HISTLEN
/ 2, f
) == 0) ||
363 (_save_short(CrimeHis
, HISTLEN
/ 2, f
) == 0) ||
364 (_save_short(PollutionHis
, HISTLEN
/ 2, f
) == 0) ||
365 (_save_short(MoneyHis
, HISTLEN
/ 2, f
) == 0) ||
366 (_save_short(MiscHis
, MISCHISTLEN
/ 2, f
) == 0) ||
367 (_save_short((&Map
[0][0]), WORLD_X
* WORLD_Y
, f
) < 0)) {
369 /* TODO: report error */
379 LoadScenario(short s
)
383 if (CityFileName
!= NULL
) {
384 ckfree(CityFileName
);
390 if ((s
< 1) || (s
> 8)) s
= 1;
397 CityTime
= ((1900 - 1900) * 48) + 2;
401 name
= "San Francisco";
404 CityTime
= ((1906 - 1900) * 48) + 2;
411 CityTime
= ((1944 - 1900) * 48) + 2;
418 CityTime
= ((1965 - 1900) * 48) + 2;
425 CityTime
= ((1957 - 1900) * 48) + 2;
432 CityTime
= ((1972 - 1900) * 48) + 2;
439 CityTime
= ((2010 - 1900) * 48) + 2;
443 name
= "Rio de Janeiro";
446 CityTime
= ((2047 - 1900) * 48) + 2;
451 setAnyCityName(name
);
452 // sim_skips = sim_skip = 0;
457 gettimeofday(&start_time
, NULL
);
459 _load_file(fname
, ResourceDir
);
476 Eval("UIDidLoadScenario");
480 int LoadCity(char *filename
)
485 if (loadFile(filename
)) {
486 if (CityFileName
!= NULL
)
487 ckfree(CityFileName
);
488 CityFileName
= (char *)ckalloc(strlen(filename
) + 1);
489 strcpy(CityFileName
, filename
);
491 if (cp
= (char *)rindex(filename
, '.'))
494 if (cp
= (char *)rindex(filename
, '\\'))
496 if (cp
= (char *)rindex(filename
, '/'))
501 filename
= (char *)ckalloc(strlen(cp
) + 1);
502 strcpy(filename
, cp
);
503 setCityName(filename
);
504 gettimeofday(&start_time
, NULL
);
511 sprintf(msg
, "Unable to load a city from the file named \"%s\". %s",
512 filename
? filename
: "(null)",
513 errno
? strerror(errno
) : "");
522 Eval("UIDidLoadCity");
526 DidntLoadCity(char *msg
)
529 sprintf(buf
, "UIDidntLoadCity {%s}", msg
);
538 if (CityFileName
== NULL
) {
541 if (saveFile(CityFileName
))
544 sprintf(msg
, "Unable to save the city to the file named \"%s\". %s",
545 CityFileName
? CityFileName
: "(null)",
546 errno
? strerror(errno
) : "");
555 Eval("UISaveCityAs");
561 Eval("UIDidSaveCity");
565 DidntSaveCity(char *msg
)
568 sprintf(buf
, "UIDidntSaveCity {%s}", msg
);
573 SaveCityAs(char *filename
)
578 if (CityFileName
!= NULL
)
579 ckfree(CityFileName
);
580 CityFileName
= (char *)ckalloc(strlen(filename
) + 1);
581 strcpy(CityFileName
, filename
);
583 if (saveFile(CityFileName
)) {
584 if (cp
= (char *)rindex(filename
, '.'))
587 if (cp
= (char *)rindex(filename
, '\\'))
589 if (cp
= (char *)rindex(filename
, '/'))
594 filename
= (char *)ckalloc(strlen(cp
) + 1);
595 strcpy(filename
, cp
);
599 sprintf(msg
, "Unable to save the city to the file named \"%s\". %s",
600 CityFileName
? CityFileName
: "(null)",
601 errno
? strerror(errno
) : "");