Debian Codegraph @

src/savegame.cpp

raw
1 /**
2 * Load and save game progress
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2012, 2014, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink 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.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #ifdef __EMSCRIPTEN__
29 #include <emscripten.h>
30 #endif
31
32 #include <string.h>
33
34 #include "game_engine.h"
35 #include "live_sprites_manager.h"
36 #include "DMod.h"
37 #include "dinkc.h"
38 #include "sfx.h"
39 #include "bgm.h"
40 #include "log.h"
41 #include "paths.h"
42 #include "input.h"
43 #include "gfx.h"
44 #include "gfx_palette.h"
45 #include "ImageLoader.h"
46
47 /*bool*/int add_time_to_saved_game(int num)
48 {
49 FILE *f = NULL;
50
51 f = paths_savegame_fopen(num, "rb");
52 if (!f)
53 {
54 log_error("Couldn't load save game %d", num);
55 return /*false*/0;
56 }
57
58 int minutes = 0;
59 int minutes_offset = 200;
60 fseek(f, minutes_offset, SEEK_SET);
61 minutes = read_lsb_int(f);
62 fclose(f);
63
64 //great, now let's resave it with added time
65 log_info("Ok, adding time.");
66 time_t ct;
67
68 time(&ct);
69 minutes += (int) (difftime(ct,time_start) / 60);
70
71 f = paths_savegame_fopen(num, "rb+");
72 if (f)
73 {
74 fseek(f, minutes_offset, SEEK_SET);
75 write_lsb_int(minutes, f);
76 fclose(f);
77 }
78 log_info("Wrote it.(%d of time)", minutes);
79
80 return /*true*/1;
81 }
82
83
84 /*bool*/int load_game(int num)
85 {
86 char skipbuf[10000]; // more than any fseek we do
87 char dinkdat[50] = "";
88 char mapdat[50] = "";
89
90 FILE *f = NULL;
91
92 //lets get rid of our magic and weapon scripts
93 if (weapon_script != 0)
94 {
95 if (locate(weapon_script, "DISARM"))
96 {
97 run_script(weapon_script);
98 }
99 }
100
101 if (magic_script != 0 && locate(magic_script, "DISARM"))
102 run_script(magic_script);
103
104 bow.active = /*false*/0;
105 weapon_script = 0;
106 magic_script = 0;
107 midi_active = /*true*/1;
108
109 if (last_saved_game > 0)
110 {
111 log_info("Modifying saved game.");
112 if (!add_time_to_saved_game(last_saved_game))
113 log_error("Error modifying saved game.");
114 }
115 StopMidi();
116
117 f = paths_savegame_fopen(num, "rb");
118 if (!f)
119 {
120 log_error("Couldn't load save game %d", num);
121 return /*false*/0;
122 }
123
124
125 /* Portably load struct player_info play from disk */
126 int i = 0;
127 // TODO: check 'version' field and warn/upgrade/downgrade if
128 // savegame version != dversion
129 fread(skipbuf, 4, 1, f);
130 fread(skipbuf, 77+1, 1, f); // skip save_game_info, cf. save_game_small(...)
131 fread(skipbuf, 118, 1, f); // unused
132 // offset 200
133 play.minutes = read_lsb_int(f);
134 spr[1].x = read_lsb_int(f);
135 spr[1].y = read_lsb_int(f);
136 fread(skipbuf, 4, 1, f); // unused 'die' field
137 spr[1].size = read_lsb_int(f);
138 spr[1].defense = read_lsb_int(f);
139 spr[1].dir = read_lsb_int(f);
140 spr[1].pframe = read_lsb_int(f);
141 spr[1].pseq = read_lsb_int(f);
142 spr[1].seq = read_lsb_int(f);
143 spr[1].frame = read_lsb_int(f);
144 spr[1].strength = read_lsb_int(f);
145 spr[1].base_walk = read_lsb_int(f);
146 spr[1].base_idle = read_lsb_int(f);
147 spr[1].base_hit = read_lsb_int(f);
148 spr[1].que = read_lsb_int(f);
149 // offset 264
150
151 // skip first originally unused mitem entry
152 fread(skipbuf, 20, 1, f);
153 for (i = 0; i < NB_MITEMS; i++)
154 {
155 play.mitem[i].active = fgetc(f);
156 fread(play.mitem[i].name, 11, 1, f);
157 /* The item script could overflow, overwriting 'seq'; if */
158 play.mitem[i].name[11-1] = '\0'; // safety
159 play.mitem[i].seq = read_lsb_int(f);
160 play.mitem[i].frame = read_lsb_int(f);
161 }
162 // skip first originally unused item entry
163 fread(skipbuf, 20, 1, f);
164 for (i = 0; i < NB_ITEMS; i++)
165 {
166 play.item[i].active = fgetc(f);
167 fread(play.item[i].name, 11, 1, f);
168 play.item[i].name[11-1] = '\0'; // safety
169 play.item[i].seq = read_lsb_int(f);
170 play.item[i].frame = read_lsb_int(f);
171 }
172 // offset 784
173
174 play.curitem = read_lsb_int(f) - 1;
175 fread(skipbuf, 4, 1, f); // reproduce unused 'unused' field
176 fread(skipbuf, 4, 1, f); // reproduce unused 'counter' field
177 fread(skipbuf, 1, 1, f); // reproduce unused 'idle' field
178 fread(skipbuf, 3, 1, f); // reproduce memory alignment
179 // offset 796
180
181 for (i = 0; i < 769; i++)
182 {
183 /* Thoses are char arrays, not null-terminated strings */
184 int j = 0;
185 fread(play.spmap[i].type, 100, 1, f);
186 for (j = 0; j < 100; j++)
187 play.spmap[i].seq[j] = read_lsb_short(f);
188 fread(play.spmap[i].frame, 100, 1, f);
189 play.spmap[i].last_time = read_lsb_uint(f);
190 }
191
192 /* Here's we'll perform a few tricks to respect a misconception in
193 the original savegame format */
194 // skip first originally unused play.button entry
195 fread(skipbuf, 4, 1, f);
196 // first play.var entry (cf. below) was overwritten by
197 // play.button[10], writing 10 play.button entries:
198 for (i = 0; i < 10; i++) // use fixed 10 rather than NB_BUTTONS
199 input_set_button_action(i, read_lsb_int(f));
200 // skip the rest of first unused play.var entry
201 fread(skipbuf, 32-4, 1, f);
202
203 // reading the rest of play.var
204 for (i = 1; i < MAX_VARS; i++)
205 {
206 play.var[i].var = read_lsb_int(f);
207 fread(play.var[i].name, 20, 1, f);
208 play.var[i].name[20-1] = '\0'; // safety
209 play.var[i].scope = read_lsb_int(f);
210 play.var[i].active = fgetc(f);
211 fread(skipbuf, 3, 1, f); // reproduce memory alignment
212 }
213
214 play.push_active = fgetc(f);
215 fread(skipbuf, 3, 1, f); // reproduce memory alignment
216 play.push_dir = read_lsb_int(f);
217
218 play.push_timer = read_lsb_int(f);
219
220 play.last_talk = read_lsb_int(f);
221 play.mouse = read_lsb_int(f);
222 play.item_type = (enum item_type)fgetc(f);
223 fread(skipbuf, 3, 1, f); // reproduce memory alignment
224 play.last_map = read_lsb_int(f);
225 fread(skipbuf, 4, 1, f); // reproduce unused 'crap' field
226 fread(skipbuf, 95 * 4, 1, f); // reproduce unused 'buff' field
227 fread(skipbuf, 20 * 4, 1, f); // reproduce unused 'dbuff' field
228 fread(skipbuf, 10 * 4, 1, f); // reproduce unused 'lbuff' field
229
230 /* v1.08: use wasted space for storing file location of map.dat,
231 dink.dat, palette, and tiles */
232 /* char cbuff[6000]; */
233 /* Thoses are char arrays, not null-terminated strings */
234 fread(mapdat, 50, 1, f);
235 fread(dinkdat, 50, 1, f);
236 fread(play.palette, 50, 1, f);
237
238 for (i = 0; i < GFX_TILES_NB_SETS+1; i++)
239 {
240 fread(play.tile[i].file, 50, 1, f);
241 play.tile[i].file[50-1] = '\0'; // safety
242 }
243 for (i = 0; i < 100; i++)
244 {
245 fread(play.func[i].file, 10, 1, f);
246 play.func[i].file[10-1] = '\0'; // safety
247 fread(play.func[i].func, 20, 1, f);
248 play.func[i].func[20-1] = '\0'; // safety
249 }
250 /* Remains 750 unused chars at the end of the file. */
251 /* fread(play.cbuff, 750, 1, f); */
252
253 fclose(f);
254
255
256 if (dversion >= 108)
257 {
258 // new map, if exist
259 if (strlen (mapdat) > 0 && strlen (dinkdat) > 0)
260 {
261 g_dmod.map = EditorMap(dinkdat, mapdat);
262 g_dmod.map.load();
263 }
264
265 // load palette
266 if (strlen(play.palette) > 0)
267 {
268 if (gfx_palette_set_from_bmp(play.palette) < 0)
269 log_error("Couldn't load palette from '%s': %s", play.palette, SDL_GetError());
270 gfx_palette_get_phys(GFX_ref_pal);
271 }
272
273 /* Reload tiles */
274 g_dmod.bgTilesets.loadDefault();
275
276 /* Replace with custom tiles if needed */
277 for (i = 1; i <= GFX_TILES_NB_SETS; i++)
278 if (strlen(play.tile[i].file) > 0)
279 g_dmod.bgTilesets.loadSlot(i, play.tile[i].file);
280 }
281
282
283 spr[1].damage = 0;
284 walk_off_screen = 0;
285 spr[1].nodraw = 0;
286 push_active = 1;
287
288 time(&time_start);
289
290 int script = load_script("main", 0);
291 locate(script, "main");
292 run_script(script);
293 //lets attach our vars to the scripts
294
295 attach();
296 log_debug("Attached vars.");
297 dinkspeed = 3;
298
299 if (*pcur_weapon >= 1 && *pcur_weapon <= NB_ITEMS)
300 {
301 if (play.item[*pcur_weapon - 1].active == 0)
302 {
303 *pcur_weapon = 1;
304 weapon_script = 0;
305 log_error("Loadgame error: Player doesn't have armed weapon - changed to 1.");
306 }
307 else
308 {
309 weapon_script = load_script(play.item[*pcur_weapon - 1].name, 1000);
310 if (locate(weapon_script, "DISARM"))
311 run_script(weapon_script);
312 weapon_script = load_script(play.item[*pcur_weapon - 1].name, 1000);
313 if (locate(weapon_script, "ARM"))
314 run_script(weapon_script);
315 }
316 }
317 if (*pcur_magic >= 1 && *pcur_magic <= NB_MITEMS)
318 {
319 if (play.item[*pcur_magic - 1].active == /*false*/0)
320 {
321 *pcur_magic = 0;
322 magic_script = 0;
323 log_error("Loadgame error: Player doesn't have armed magic - changed to 0.");
324 }
325 else
326 {
327
328 magic_script = load_script(play.mitem[*pcur_magic - 1].name, 1000);
329 if (locate(magic_script, "DISARM"))
330 run_script(magic_script);
331 magic_script = load_script(play.mitem[*pcur_magic - 1].name, 1000);
332 if (locate(magic_script, "ARM"))
333 run_script(magic_script);
334 }
335 }
336 kill_repeat_sounds_all();
337 game_load_screen(g_dmod.map.loc[*pplayer_map]);
338 log_info("Loaded map.");
339 draw_screen_game();
340 log_info("Map drawn.");
341
342 last_saved_game = num;
343
344 return /*true*/1;
345 }
346
347
348 void save_game(int num)
349 {
350 char skipbuf[10000]; // more than any fseek we do
351 memset(skipbuf, 0, 10000);
352
353 FILE *f;
354
355 //lets set some vars first
356 time_t ct;
357 time(&ct);
358 play.minutes += (int) (difftime(ct,time_start) / 60);
359 //reset timer
360 time(&time_start);
361
362 last_saved_game = num;
363 f = paths_savegame_fopen(num, "wb");
364 if (f == NULL)
365 {
366 perror("Cannot save game");
367 return;
368 }
369
370 /* Portably dump struct player_info play to disk */
371 int i = 0;
372 write_lsb_int(dversion, f);
373 // set_save_game_info() support:
374 {
375 char* info_temp = strdup(save_game_info);
376 decipher_string(&info_temp, 0);
377 fwrite(info_temp, 77+1, 1, f);
378 free(info_temp);
379 }
380 fwrite(skipbuf, 118, 1, f); // unused
381 // offset 200
382 write_lsb_int(play.minutes, f);
383 write_lsb_int(spr[1].x, f);
384 write_lsb_int(spr[1].y, f);
385 fwrite(skipbuf, 4, 1, f); // unused 'die' field
386 write_lsb_int(spr[1].size, f);
387 write_lsb_int(spr[1].defense, f);
388 write_lsb_int(spr[1].dir, f);
389 write_lsb_int(spr[1].pframe, f);
390 write_lsb_int(spr[1].pseq, f);
391 write_lsb_int(spr[1].seq, f);
392 write_lsb_int(spr[1].frame, f);
393 write_lsb_int(spr[1].strength, f);
394 write_lsb_int(spr[1].base_walk, f);
395 write_lsb_int(spr[1].base_idle, f);
396 write_lsb_int(spr[1].base_hit, f);
397 write_lsb_int(spr[1].que, f);
398 // offset 264
399
400 // skip first originally unused mitem entry
401 fwrite(skipbuf, 20, 1, f);
402 for (i = 0; i < NB_MITEMS; i++)
403 {
404 fputc(play.mitem[i].active, f);
405 fwrite(play.mitem[i].name, 11, 1, f);
406 write_lsb_int(play.mitem[i].seq, f);
407 write_lsb_int(play.mitem[i].frame, f);
408 }
409 // skip first originally unused item entry
410 fwrite(skipbuf, 20, 1, f);
411 for (i = 0; i < NB_ITEMS; i++)
412 {
413 fputc(play.item[i].active, f);
414 fwrite(play.item[i].name, 11, 1, f);
415 write_lsb_int(play.item[i].seq, f);
416 write_lsb_int(play.item[i].frame, f);
417 }
418 // offset 784
419
420 write_lsb_int(play.curitem + 1, f);
421 fwrite(skipbuf, 4, 1, f); // reproduce unused 'unused' field
422 fwrite(skipbuf, 4, 1, f); // reproduce unused 'counter' field
423 fwrite(skipbuf, 1, 1, f); // reproduce unused 'idle' field
424 fwrite(skipbuf, 3, 1, f); // reproduce memory alignment
425 // offset 796
426
427 for (i = 0; i < 769; i++)
428 {
429 int j = 0;
430 fwrite(play.spmap[i].type, 100, 1, f);
431 for (j = 0; j < 100; j++)
432 write_lsb_short(play.spmap[i].seq[j], f);
433 fwrite(play.spmap[i].frame, 100, 1, f);
434 write_lsb_uint(play.spmap[i].last_time, f);
435 }
436
437 /* Here's we'll perform a few tricks to respect a misconception in
438 the original savegame format */
439 // skip first originally unused play.button entry
440 fwrite(skipbuf, 4, 1, f);
441 // first play.var entry (cf. below) was overwritten by
442 // play.button[10], writing 10 play.button entries:
443 for (i = 0; i < 10; i++) // use fixed 10 rather than NB_BUTTONS
444 write_lsb_int(input_get_button_action(i), f);
445 // skip the rest of first unused play.var entry
446 fwrite(skipbuf, 32-4, 1, f);
447
448 // writing the rest of play.var
449 for (i = 1; i < MAX_VARS; i++)
450 {
451 write_lsb_int(play.var[i].var, f);
452 fwrite(play.var[i].name, 20, 1, f);
453 write_lsb_int(play.var[i].scope, f);
454 fputc(play.var[i].active, f);
455 fwrite(skipbuf, 3, 1, f); // reproduce memory alignment
456 }
457
458 fputc(play.push_active, f);
459 fwrite(skipbuf, 3, 1, f); // reproduce memory alignment
460 write_lsb_int(play.push_dir, f);
461
462 write_lsb_int(play.push_timer, f);
463
464 write_lsb_int(play.last_talk, f);
465 write_lsb_int(play.mouse, f);
466 fputc(play.item_type, f);
467 fwrite(skipbuf, 3, 1, f); // reproduce memory alignment
468 write_lsb_int(play.last_map, f);
469 fwrite(skipbuf, 4, 1, f); // reproduce unused 'crap' field
470 fwrite(skipbuf, 95 * 4, 1, f); // reproduce unused 'buff' field
471 fwrite(skipbuf, 20 * 4, 1, f); // reproduce unused 'dbuff' field
472 fwrite(skipbuf, 10 * 4, 1, f); // reproduce unused 'lbuff' field
473
474 /* v1.08: use wasted space for storing file location of map.dat,
475 dink.dat, palette, and tiles */
476 /* char cbuff[6000];*/
477 fwrite(g_dmod.map.map_dat.c_str(), 50, 1, f);
478 fwrite(g_dmod.map.dink_dat.c_str(), 50, 1, f);
479 fwrite(play.palette, 50, 1, f);
480
481 for (i = 0; i < GFX_TILES_NB_SETS+1; i++)
482 fwrite(play.tile[i].file, 50, 1, f);
483 for (i = 0; i < 100; i++)
484 {
485 fwrite(play.func[i].file, 10, 1, f);
486 fwrite(play.func[i].func, 20, 1, f);
487 }
488 fwrite(skipbuf, 750, 1, f);
489
490 fclose(f);
491
492 #ifdef __EMSCRIPTEN__
493 // Flush changes to IDBFS
494 EM_ASM(
495 FS.syncfs(false, function(err) { if (err) { console.trace(); console.log(err); } })
496 );
497 #endif
498 }