Debian Codegraph @

Descent3/gamesave.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/gamesave.cpp $
21 * $Revision: 75 $
22 * $Date: 9/05/01 12:26p $
23 * $Author: Matt $
24 *
25 *
26 *
27 * $Log: /DescentIII/main/gamesave.cpp $
28 *
29 * 75 9/05/01 12:26p Matt
30 * Flush low-level mouse click queue before bringing up buddy, save, and
31 * load menus since these menus would otherwise get clicks that occurred
32 * before they existed.
33 *
34 * 74 4/19/00 5:14p Matt
35 * From Duane for 1.4
36 * Mac-only change: ensure dialog is cleared after saving screenshot.
37 *
38 * 73 10/22/99 10:57a Matt
39 * Mac merge
40 *
41 * 72 7/22/99 11:46a Chris
42 *
43 * 71 7/21/99 7:17p Chris
44 * Fixed an attach related crash in the load/save game code
45 *
46 * 70 5/24/99 7:23p Samir
47 * pass current string length to DoEditDialog. crash otherwise for long
48 * savegames.
49 *
50 * 69 5/21/99 11:14p Samir
51 * savegames save hud timer and special text states. restore too.
52 *
53 * 68 5/20/99 2:48a Matt
54 * Auto-waypoints now stored & used per player. Manual waypoints will all
55 * players, once Jason makes it work.
56 *
57 * 67 5/12/99 6:01p Jeff
58 * flip order of saving/loading objects and players
59 *
60 * 66 5/12/99 12:25p Kevin
61 * save & restore Current_mission.game_state_flags
62 *
63 * 65 5/06/99 6:11p Kevin
64 * fixes for save/load game syste. Also require CD again and added some
65 * heat.net stuff
66 *
67 * 64 5/02/99 7:18p Samir
68 * changed titles on messageboxes and don't put up loadgame dialog if
69 * there are no savegames.
70 *
71 * 63 4/30/99 3:05p Kevin
72 * doh
73 *
74 * 62 4/30/99 3:03p Kevin
75 * improved times restored count
76 *
77 * 61 4/29/99 11:25a Kevin
78 * Added room damage to save game
79 *
80 * 60 4/28/99 2:14a Samir
81 * save and load Game messages in savegame.
82 *
83 * 59 4/26/99 4:33p Samir
84 * made stringtable change.
85 *
86 * 58 4/23/99 10:28p Kevin
87 * fixed problems with minimal install and demo/savegames
88 *
89 * 57 4/23/99 8:28p Kevin
90 * trying to get doors working in save/load games
91 *
92 * 56 4/23/99 7:33p Kevin
93 * save weather effects and reset the HUD to show the correct ship
94 *
95 * 55 4/22/99 4:13p Matt
96 * Deleted sounds array from the object struct, since it was never used.
97 *
98 * 54 4/20/99 11:45a Samir
99 * added autosave
100 *
101 * 53 4/16/99 12:39a Matt
102 * Disable Soar on non-Windows systems.
103 *
104 * 52 4/15/99 5:40p Samir
105 * save current music region and restore it.
106 *
107 * 51 4/15/99 1:39a Jeff
108 * changes for linux compile
109 *
110 * 50 4/14/99 2:50a Jeff
111 * fixed some case mismatched #includes
112 *
113 * 49 4/06/99 10:44p Samir
114 * Fixed residue from changes to UID_OK and UID_CANCEL.
115 *
116 * 48 4/05/99 10:54a Matt
117 * Added auto-waypoint system
118 *
119 * 47 3/23/99 6:09p Kevin
120 * Marker text saved
121 *
122 * 46 3/23/99 10:24a Samir
123 * check valid bitmap handle in LoadGame.
124 *
125 * 45 3/22/99 5:12p Samir
126 * added snapshot to savegame.
127 *
128 * 44 3/11/99 9:09p Jeff
129 * fixed remaining known bugs in demo system (DemoWriteObjCreate being
130 * called twice and </<= problems with
131 * Highest_room_index/Highest_object_index).
132 *
133 * 43 3/10/99 6:20p Jeff
134 * many fixes to demo system. Fixed IGC so cameras move...fixed osiris to
135 * be restored correctly, and it handles errors on restore
136 *
137 * 42 3/10/99 2:25p Kevin
138 * Save/Load and Demo file fixes
139 *
140 * 41 3/08/99 10:52a Jason
141 * save/load texture changes on room faces
142 *
143 * 40 3/04/99 6:30p Jeff
144 * saves out current waypoint and matcens
145 *
146 * 39 3/04/99 6:19p Kevin
147 * was using the wrong string
148 *
149 * 38 3/03/99 7:20p Jeff
150 * save/restore some ai path information
151 *
152 * 37 3/03/99 12:56a Kevin
153 * Added some HUD messages. Needs to be localized.
154 *
155 * 36 3/02/99 6:39p Jeff
156 * level goals working in save/load game
157 *
158 * 35 3/02/99 4:41p Jeff
159 * Fixed inventory save/load
160 *
161 * 34 3/02/99 2:49p Samir
162 * will not allow savegames with no names.
163 *
164 * 33 3/02/99 1:48p Kevin
165 * Fixed collision with external rooms
166 *
167 * 32 3/01/99 9:03p Kevin
168 * OEM Beta 4
169 *
170 * 31 3/01/99 4:38p Samir
171 * made done button cancel in gamesave menus.
172 *
173 * 30 3/01/99 11:41a Kevin
174 * More progress...
175 *
176 * 29 3/01/99 4:59a Samir
177 * redid load and save game menus.
178 *
179 * 28 2/28/99 10:50p Kevin
180 *
181 * 26 2/27/99 4:41p Kevin
182 * Fixed(?) some load/save bugs
183 *
184 * 25 2/26/99 6:48p Kevin
185 * Fixes to load/save game, and change minor version for OEM beta 3
186 *
187 * 24 2/24/99 9:57a Kevin
188 * Added error messages if you try to save a game while in multi
189 *
190 * 23 2/23/99 9:58p Kevin
191 * trying to get attach working
192 *
193 * 22 2/23/99 5:13p Kevin
194 * Fixed it so objects with lightmaps will retain their lightmaps when a
195 * game is saved & loaded.
196 *
197 * 21 2/22/99 2:04p Jason
198 * added different damages for players and generics
199 *
200 * 20 2/07/99 1:17a Jeff
201 * peppered UI dialogs that were missing NEWUIRES_FORCEQUIT to handle it
202 *
203 * 19 2/02/99 12:43p Kevin
204 * Fixes to the Save/Load game system, and the demo system to work with
205 * the new OSIRIS
206 *
207 * 18 1/29/99 12:47p Matt
208 * Rewrote the doorway system
209 *
210 * 17 1/23/99 10:10p Kevin
211 * Added the start of osiris support into the demo system
212 *
213 * 16 1/22/99 8:53p Jeff
214 * added custom-default script overrides
215 *
216 * 15 1/18/99 2:46p Matt
217 * Combined flags & flags2 fields in object struct
218 *
219 * 14 1/13/99 6:37a Jeff
220 * fixed object.h. There were numerous struct declarations that were the
221 * same name as the instance of the struct (gcc doesn't like this).
222 * Changed the struct name. Also added some #ifdef's for linux build,
223 * along with fixing case-sensitive includes
224 *
225 * 13 1/08/99 2:55p Samir
226 * Ripped out OSIRIS1.
227 *
228 * 12 11/11/98 2:46p Kevin
229 * Demo recording system work
230 *
231 * 11 10/06/98 5:45p Kevin
232 * Added new configuration for demo
233 *
234 * 10 9/22/98 3:56p Samir
235 * special demo code doesn't allow pilot and mission stuff.
236 *
237 * 9 8/25/98 3:44p Samir
238 * make sure and return false if no save games.
239 *
240 * 8 8/20/98 6:45p Samir
241 * new interfaces for load-save.
242 *
243 * 7 8/20/98 10:52a Samir
244 * fixed some ui probs
245 *
246 * 6 8/19/98 5:35p Samir
247 * saved triggers, spew, viseffects, weapons, room data xcept texture per
248 * face changes, one real strange bug where robots in the same room as you
249 * don't appear but fire at you.
250 *
251 * 5 8/18/98 3:50p Matt
252 * Maybe fixed bug
253 *
254 * 4 8/18/98 3:05p Samir
255 * save game with no weapons or fireballs.
256 *
257 * 3 8/18/98 11:17a Samir
258 * for the sake of the team.
259 *
260 * 2 8/18/98 1:12a Samir
261 * rudimentary savegame. pretty damn buggy when loading.
262 *
263 * $NoKeywords: $
264 */
265
266 #include <cstring>
267 #include <filesystem>
268
269 #include "gamesave.h"
270 #include "descent.h"
271 #include "newui.h"
272 #include "cfile.h"
273 #include "Mission.h"
274 #include "gamesequence.h"
275 #include "gameevent.h"
276 #include "gameloop.h"
277 #include "game.h"
278 #include "stringtable.h"
279 #include "object.h"
280 #include "objinfo.h"
281 #include "gametexture.h"
282 #include "bitmap.h"
283 #include "ddio.h"
284 #include "door.h"
285 #include "doorway.h"
286 #include "ship.h"
287 #include "soar.h"
288 #include "hud.h"
289 #include "weapon.h"
290 #include "viseffect.h"
291 #include "room.h"
292 #include "trigger.h"
293 #include "spew.h"
294 #include "osiris_dll.h"
295 #include "levelgoal.h"
296 #include "aistruct.h"
297 #include "matcen.h"
298 #include "hud.h"
299 #include "marker.h"
300 #include "d3music.h"
301 #include "weather.h"
302
303 // function prototypes.
304
305 void SGSSnapshot(CFILE *fp);
306
307 #define SAVE_HOTSPOT_ID 0x1000
308 #define N_SAVE_SLOTS 8
309 #define GAMESAVE_WND_W 448
310 #define GAMESAVE_WND_H 384
311 #define GAMESAVE_SLOT_W 336
312 #define GAMESAVE_SLOT_H 18
313 #define GAMESAVE_SLOT_H2 22
314 #define GAMESAVE_HELP_Y 2
315 #define GAMESAVE_HELP_X 12
316 #define GAMESAVE_SLOT_Y 140
317 #define GAMESAVE_SLOT_Y2 110
318 #define GAMESAVE_SLOT_X 12
319
320 // we need this directory to load the savegame from
321 static char LGS_Path[_MAX_PATH];
322
323 extern int Times_game_restored;
324 // available for all.
325 int Quicksave_game_slot = -1;
326
327 void QuickSaveGame() {
328 if (Game_mode & GM_MULTI) {
329 DoMessageBox(TXT_ERROR, TXT_CANT_SAVE_MULTI, MSGBOX_OK);
330 return;
331 }
332
333 #ifdef _DEBUG
334 if (GetFunctionMode() == EDITOR_GAME_MODE || !Current_mission.filename) {
335 DoMessageBox(TXT_ERROR, "You need to be playing a mission.", MSGBOX_OK);
336 return;
337 }
338 #endif
339
340 if (Quicksave_game_slot == -1) {
341 SaveGameDialog();
342 } else {
343 // verify savegame still exists in the appropriate slot, if not just run dialog, if so then save
344 char filename[PSFILENAME_LEN + 1];
345 char pathname[_MAX_PATH];
346 char desc[GAMESAVE_DESCLEN + 1];
347 FILE *fp;
348 int i;
349
350 i = Quicksave_game_slot;
351
352 snprintf(filename, sizeof(filename), "saveg00%d", i);
353 ddio_MakePath(pathname, Base_directory_write, "savegame", filename, NULL);
354
355 fp = fopen(pathname, "rb");
356 if (fp) {
357 // slot valid, save here.
358 fclose(fp);
359 if (GetGameStateInfo(pathname, desc)) {
360 if (SaveGameState(pathname, desc)) {
361 AddHUDMessage(TXT_QUICKSAVE);
362 return; // we're okay. game saved. put up hud message?
363 }
364 DoMessageBox(TXT_ERROR, TXT_SAVEGAMEFAILED, MSGBOX_OK);
365 }
366 }
367 Quicksave_game_slot = -1;
368 SaveGameDialog();
369 }
370 }
371
372 void SaveGameDialog() {
373 newuiTiledWindow wnd;
374 newuiSheet *sheet;
375 int i, res;
376
377 char savegame_dir[_MAX_PATH];
378 char pathname[_MAX_PATH];
379 char filename[PSFILENAME_LEN + 1];
380 char desc[GAMESAVE_DESCLEN + 1];
381 bool occupied_slot[N_SAVE_SLOTS];
382
383 if (Game_mode & GM_MULTI) {
384 DoMessageBox(TXT_ERROR, TXT_CANT_SAVE_MULTI, MSGBOX_OK);
385 return;
386 }
387
388 #ifdef _DEBUG
389 if (!Current_mission.filename) {
390 DoMessageBox(TXT_ERROR, "You need to be playing a mission.", MSGBOX_OK);
391 return;
392 }
393 #endif
394
395 // setup paths.
396 ddio_MakePath(savegame_dir, Base_directory_write, "savegame", NULL);
397 // ddio_MakePath(pathname, savegame_dir, "*.sav", NULL); -unused
398
399 // create savegame directory if it didn't exist before.
400 std::error_code ec;
401 if (!std::filesystem::create_directories(savegame_dir, ec)) {
402 if (ec) {
403 DoMessageBox(TXT_ERROR, TXT_ERRCREATEDIR, MSGBOX_OK);
404 return;
405 }
406 }
407
408 // open window
409 wnd.Create(TXT_SAVEGAME, 0, 0, GAMESAVE_WND_W, GAMESAVE_WND_H);
410 wnd.Open();
411 sheet = wnd.GetSheet();
412
413 sheet->NewGroup(NULL, GAMESAVE_HELP_X, GAMESAVE_HELP_Y);
414 sheet->AddText(TXT_SAVEGAMEHELP);
415
416 sheet->NewGroup(NULL, GAMESAVE_SLOT_X, GAMESAVE_SLOT_Y2);
417 // generate save slots.
418 for (i = 0; i < N_SAVE_SLOTS; i++) {
419 FILE *fp;
420 bool ingroup = (i == 0 || i == (N_SAVE_SLOTS - 1)) ? true : false;
421
422 snprintf(filename, sizeof(filename), "saveg00%d", i);
423 ddio_MakePath(pathname, savegame_dir, filename, NULL);
424
425 occupied_slot[i] = false;
426
427 fp = fopen(pathname, "rb");
428 if (fp) {
429 fclose(fp);
430
431 if (GetGameStateInfo(pathname, desc)) {
432 sheet->AddHotspot(desc, GAMESAVE_SLOT_W, GAMESAVE_SLOT_H2, SAVE_HOTSPOT_ID + i, ingroup);
433 occupied_slot[i] = true;
434 } else {
435 sheet->AddHotspot(TXT_ILLEGALSAVEGAME, GAMESAVE_SLOT_W, GAMESAVE_SLOT_H2, SAVE_HOTSPOT_ID + i, ingroup);
436 }
437 } else {
438 sheet->AddHotspot(TXT_EMPTY, GAMESAVE_SLOT_W, GAMESAVE_SLOT_H2, SAVE_HOTSPOT_ID + i, ingroup);
439 }
440 }
441
442 sheet->NewGroup(NULL, GAMESAVE_WND_W - 148, GAMESAVE_WND_H - 100);
443 sheet->AddButton(TXT_CANCEL, UID_CANCEL);
444
445 // Mouse clicks from gameplay will be read by the dialog without this flush
446 ddio_MouseQueueFlush();
447
448 // do ui.
449 do {
450 res = wnd.DoUI();
451 if (res == NEWUIRES_FORCEQUIT) {
452 break;
453 } else if (res >= SAVE_HOTSPOT_ID && res < (SAVE_HOTSPOT_ID + N_SAVE_SLOTS)) {
454 newuiHotspot *hot;
455 const char *hot_desc;
456 int slot = res - SAVE_HOTSPOT_ID;
457 bool do_save = false;
458
459 // fill edit box with current description.
460 hot = (newuiHotspot *)sheet->GetGadget(res);
461 ASSERT(hot);
462 hot_desc = hot->GetTitle();
463
464 if (occupied_slot[slot]) {
465 strcpy(desc, hot_desc);
466 } else {
467 desc[0] = 0;
468 }
469
470 reenter_save:
471 if (DoEditDialog(TXT_DESCRIPTION, desc, sizeof(desc) - 1)) {
472 // perform check for duplicate names
473 // do not allow for empty or space only descriptions
474 if (strlen(desc) == 0) {
475 DoMessageBox("", TXT_SAVEGAMENAME, MSGBOX_OK);
476 goto reenter_save;
477 }
478 for (i = 0; i < N_SAVE_SLOTS; i++) {
479 hot = (newuiHotspot *)sheet->GetGadget(SAVE_HOTSPOT_ID + i);
480 hot_desc = hot->GetTitle();
481
482 if (occupied_slot[i] && stricmp(hot_desc, desc) == 0 && slot != i) {
483 DoMessageBox("", TXT_SAVEGAMEDUP, MSGBOX_OK);
484 goto reenter_save;
485 }
486 }
487
488 do_save =
489 occupied_slot[slot] ? (DoMessageBox(TXT_WARNING, TXT_OVERWRITESAVE, MSGBOX_YESNO) ? true : false) : true;
490
491 if (do_save) {
492 snprintf(filename, sizeof(filename), "saveg00%d", slot);
493 ddio_MakePath(pathname, savegame_dir, filename, NULL);
494 if (!SaveGameState(pathname, desc)) {
495 DoMessageBox("", TXT_SAVEGAMEFAILED, MSGBOX_OK);
496 } else {
497 AddHUDMessage(TXT_QUICKSAVE);
498 res = UID_CANCEL;
499 Quicksave_game_slot = slot;
500 }
501 }
502 }
503 }
504 } while (res != UID_CANCEL);
505
506 wnd.Close();
507 wnd.Destroy();
508 }
509
510 ////////////////////////////////////////////////////////////////////////////////////////
511 struct tLoadGameDialogData {
512 int cur_slot;
513 chunked_bitmap chunk;
514 };
515
516 #if defined(POSIX)
517 void LoadGameDialogCB(newuiTiledWindow *wnd, void *data)
518 #else
519 void __cdecl LoadGameDialogCB(newuiTiledWindow *wnd, void *data)
520 #endif
521 {
522 tLoadGameDialogData *cb_data = (tLoadGameDialogData *)data;
523 UIGadget *cur_gadget;
524 int id;
525
526 cur_gadget = wnd->GetFocus();
527
528 for (id = SAVE_HOTSPOT_ID; id < (SAVE_HOTSPOT_ID + N_SAVE_SLOTS); id++) {
529 if (cur_gadget->GetID() == id) {
530 break;
531 }
532 }
533
534 if (id < (SAVE_HOTSPOT_ID + N_SAVE_SLOTS) && id != cb_data->cur_slot) {
535 // new bitmap to be displayed!
536 char filename[PSFILENAME_LEN + 1];
537 char pathname[_MAX_PATH];
538 char savegame_dir[_MAX_PATH];
539 char desc[GAMESAVE_DESCLEN + 1];
540 int bm_handle;
541
542 if (cb_data->chunk.bm_array) {
543 bm_DestroyChunkedBitmap(&cb_data->chunk);
544 }
545
546 mprintf(0, "savegame slot=%d\n", id - SAVE_HOTSPOT_ID);
547
548 ddio_MakePath(savegame_dir, Base_directory_write, "savegame", NULL);
549 snprintf(filename, sizeof(filename), "saveg00%d", (id - SAVE_HOTSPOT_ID));
550 ddio_MakePath(pathname, savegame_dir, filename, NULL);
551
552 if (GetGameStateInfo(pathname, desc, &bm_handle)) {
553 if (bm_handle > 0) {
554 bm_CreateChunkedBitmap(bm_handle, &cb_data->chunk);
555 bm_FreeBitmap(bm_handle);
556 }
557 }
558 cb_data->cur_slot = id;
559 }
560
561 // draw bitmap if there is one
562 if (cb_data->chunk.bm_array) {
563 UIBitmapItem bm_item;
564 int x, y;
565 bm_item.set_chunked_bitmap(&cb_data->chunk);
566 x = (wnd->W() - bm_item.width()) / 2;
567 y = (wnd->H() - bm_item.height()) / 4;
568 bm_item.draw(x, y);
569 }
570 }
571
572 bool LoadGameDialog() {
573 tLoadGameDialogData lgd_data;
574 newuiTiledWindow wnd;
575 newuiSheet *sheet;
576 int i, res;
577 bool retval = true;
578
579 char savegame_dir[_MAX_PATH];
580 char pathname[_MAX_PATH];
581 char filename[PSFILENAME_LEN + 1];
582 char desc[GAMESAVE_DESCLEN + 1];
583 bool occupied_slot[N_SAVE_SLOTS], loadgames_avail = false;
584
585 if (Game_mode & GM_MULTI) {
586 DoMessageBox(TXT_ERROR, TXT_CANT_LOAD_MULTI, MSGBOX_OK);
587 return false;
588 }
589
590 // setup paths.
591 ddio_MakePath(savegame_dir, Base_directory_write, "savegame", NULL);
592 ddio_MakePath(pathname, savegame_dir, "*.sav", NULL);
593
594 // create savegame directory if it didn't exist before.
595 if (!std::filesystem::is_directory(savegame_dir)) {
596 DoMessageBox(TXT_ERROR, TXT_ERRNOSAVEGAMES, MSGBOX_OK);
597 return false;
598 }
599
600 // open window
601 wnd.Create(TXT_LOADGAME, 0, 0, GAMESAVE_WND_W, GAMESAVE_WND_H);
602 wnd.Open();
603 sheet = wnd.GetSheet();
604
605 sheet->NewGroup(NULL, GAMESAVE_HELP_X, GAMESAVE_HELP_Y);
606 sheet->AddText(TXT_LOADGAMEHELP);
607
608 sheet->NewGroup(NULL, GAMESAVE_SLOT_X, GAMESAVE_SLOT_Y);
609
610 // generate save slots.
611 lgd_data.cur_slot = SAVE_HOTSPOT_ID;
612 lgd_data.chunk.bm_array = NULL;
613
614 for (i = 0; i < N_SAVE_SLOTS; i++) {
615 FILE *fp;
616 bool ingroup = (i == 0 || i == (N_SAVE_SLOTS - 1)) ? true : false;
617
618 snprintf(filename, sizeof(filename), "saveg00%d", i);
619 ddio_MakePath(pathname, savegame_dir, filename, NULL);
620
621 occupied_slot[i] = false;
622
623 fp = fopen(pathname, "rb");
624 if (fp) {
625 int bm_handle = -1;
626 int *pbm_handle;
627 fclose(fp);
628
629 if (lgd_data.cur_slot == (SAVE_HOTSPOT_ID + i)) {
630 pbm_handle = &bm_handle;
631 } else {
632 pbm_handle = NULL;
633 }
634
635 if (GetGameStateInfo(pathname, desc, pbm_handle)) {
636 sheet->AddHotspot(desc, GAMESAVE_SLOT_W, GAMESAVE_SLOT_H, SAVE_HOTSPOT_ID + i, ingroup);
637 occupied_slot[i] = true;
638 loadgames_avail = true;
639
640 // create chunk
641 if (pbm_handle && bm_handle > 0) {
642 if (lgd_data.chunk.bm_array) {
643 bm_DestroyChunkedBitmap(&lgd_data.chunk);
644 }
645 bm_CreateChunkedBitmap(bm_handle, &lgd_data.chunk);
646 bm_FreeBitmap(bm_handle);
647 }
648 } else {
649 sheet->AddHotspot(TXT_ILLEGALSAVEGAME, GAMESAVE_SLOT_W, GAMESAVE_SLOT_H, SAVE_HOTSPOT_ID + i, ingroup);
650 }
651 } else {
652 sheet->AddHotspot(TXT_EMPTY, GAMESAVE_SLOT_W, GAMESAVE_SLOT_H, SAVE_HOTSPOT_ID + i, ingroup);
653 }
654 }
655
656 if (!loadgames_avail) {
657 wnd.Close();
658 DoMessageBox("", TXT_ERRNOSAVEGAMES, MSGBOX_OK);
659 wnd.Open();
660 retval = false;
661 goto loadgame_fail;
662 }
663
664 sheet->NewGroup(NULL, GAMESAVE_WND_W - 148, GAMESAVE_WND_H - 100);
665 sheet->AddButton(TXT_CANCEL, UID_CANCEL);
666
667 wnd.SetData(&lgd_data);
668 wnd.SetOnDrawCB(LoadGameDialogCB);
669
670 // Mouse clicks from gameplay will be read by the dialog without this flush
671 ddio_MouseQueueFlush();
672
673 // do ui.
674 do {
675 res = wnd.DoUI();
676 if (res == NEWUIRES_FORCEQUIT) {
677 retval = false;
678 break;
679 } else if (res >= SAVE_HOTSPOT_ID && res < (SAVE_HOTSPOT_ID + N_SAVE_SLOTS)) {
680 int slot = res - SAVE_HOTSPOT_ID;
681
682 if (occupied_slot[slot]) {
683 snprintf(filename, sizeof(filename), "saveg00%d", slot);
684 ddio_MakePath(pathname, savegame_dir, filename, NULL);
685 strcpy(LGS_Path, pathname);
686 SetGameState(GAMESTATE_LOADGAME);
687 res = UID_CANCEL;
688 }
689 } else if (res == UID_CANCEL) {
690 retval = false;
691 }
692 } while (res != UID_CANCEL);
693
694 loadgame_fail:
695 if (lgd_data.chunk.bm_array) {
696 bm_DestroyChunkedBitmap(&lgd_data.chunk);
697 }
698
699 wnd.Close();
700 wnd.Destroy();
701
702 return retval;
703 }
704
705 //////////////////////////////////////////////////////////////////////////////
706
707 // loads savegame as specified from LoadGameDialog.
708 bool LoadCurrentSaveGame() {
709 int retval = LoadGameState(LGS_Path);
710 if (retval != LGS_OK) {
711 Int3();
712 DoMessageBox(TXT_ERROR, TXT_LOADGAMEFAILED, MSGBOX_OK);
713 return false;
714 }
715 AddHUDMessage(TXT_GAMERESTORED);
716 return true;
717 }
718
719 //////////////////////////////////////////////////////////////////////////////
720
721 // give a description and slot number (0 to GAMESAVE_SLOTS-1)
722 bool SaveGameState(const char *pathname, const char *description) {
723 CFILE *fp;
724 char buf[GAMESAVE_DESCLEN + 1];
725 int16_t pending_music_region;
726
727 fp = cfopen(pathname, "wb");
728 if (!fp)
729 return false;
730
731 // Delete the old games restored count.
732 char countpath[_MAX_PATH * 2];
733 strcpy(countpath, pathname);
734 strcat(countpath, ".cnt");
735 CFILE *countfp;
736 countfp = cfopen(countpath, "wb");
737 if (countfp) {
738 cf_WriteInt(countfp, Times_game_restored);
739 cfclose(countfp);
740 }
741
742 // save out header
743 START_VERIFY_SAVEFILE(fp);
744 ASSERT(strlen(description) < sizeof(buf));
745 strcpy(buf, description);
746 cf_WriteBytes((uint8_t *)buf, sizeof(buf), fp);
747 cf_WriteShort(fp, GAMESAVE_VERSION);
748
749 SGSSnapshot(fp); // Save snapshot? MUST KEEP THIS HERE.
750
751 // write out translation tables
752 SGSXlateTables(fp);
753
754 // write out gamemode information
755
756 // write out mission level information
757 cf_WriteShort(fp, (int16_t)Current_mission.cur_level);
758
759 if (Current_mission.filename && (stricmp("d3_2.mn3", Current_mission.filename) == 0)) {
760 cf_WriteString(fp, "d3.mn3");
761 } else {
762 cf_WriteString(fp, Current_mission.filename ? Current_mission.filename : "");
763 }
764
765 cf_WriteInt(fp, Current_mission.game_state_flags);
766
767 cf_WriteFloat(fp, Gametime);
768 cf_WriteInt(fp, FrameCount);
769 cf_WriteInt(fp, Current_waypoint);
770
771 pending_music_region = D3MusicGetPendingRegion();
772 if (pending_music_region < 0) {
773 pending_music_region = D3MusicGetRegion();
774 }
775 cf_WriteShort(fp, pending_music_region);
776
777 // cf_WriteInt(fp,Times_game_restored);
778 // Save weather
779 cf_WriteInt(fp, sizeof(Weather));
780 cf_WriteBytes((uint8_t *)&Weather, sizeof(Weather), fp);
781
782 // Save active doorways
783 cf_WriteInt(fp, MAX_ACTIVE_DOORWAYS);
784 cf_WriteInt(fp, Num_active_doorways);
785
786 for (int d = 0; d < MAX_ACTIVE_DOORWAYS; d++) {
787 cf_WriteInt(fp, Active_doorways[d]);
788 }
789
790 // save out room information.
791 SGSRooms(fp);
792
793 // save out triggers
794 SGSTriggers(fp);
795
796 // save out object information.
797 SGSObjects(fp);
798
799 // players
800 SGSPlayers(fp);
801
802 // save out matcens
803 SGSMatcens(fp);
804
805 // save out viseffects
806 SGSVisEffects(fp);
807
808 // save out spew
809 SGSSpew(fp);
810
811 // Save OSIRIS stuff
812 Osiris_SaveSystemState(fp);
813
814 // Save Level goal info
815 Level_goals.SaveLevelGoalInfo(fp);
816
817 // save out game messages from console. (must occur AFTER players are written out!!!)
818 SGSGameMessages(fp);
819
820 // save out special hud item states.
821 SGSHudState(fp);
822
823 // end
824 END_VERIFY_SAVEFILE(fp, "Total save");
825 cfclose(fp);
826
827 return true;
828 }
829
830 //////////////////////////////////////////////////////////////////////////////
831
832 #define SAVE_DATA_TABLE(_nitems, _array) \
833 do { \
834 highest_index = -1; \
835 for (i = 0; i < (_nitems); i++) \
836 if (_array[i].used) \
837 highest_index = i; \
838 gs_WriteShort(fp, highest_index + 1); \
839 for (i = 0; i <= highest_index; i++) \
840 cf_WriteString(fp, _array[i].used ? _array[i].name : ""); \
841 } while (0)
842
843 // writes out translation tables.
844 void SGSXlateTables(CFILE *fp) {
845 START_VERIFY_SAVEFILE(fp);
846 // create object info translation table
847 int16_t i, highest_index = -1;
848
849 for (i = 0; i < MAX_OBJECT_IDS; i++)
850 if (Object_info[i].type != OBJ_NONE)
851 highest_index = i;
852
853 gs_WriteShort(fp, highest_index + 1);
854
855 for (i = 0; i <= highest_index; i++)
856 cf_WriteString(fp, (Object_info[i].type != OBJ_NONE) ? Object_info[i].name : "");
857
858 // write out polymodel list.
859 SAVE_DATA_TABLE(MAX_POLY_MODELS, Poly_models);
860
861 // save out door list
862 SAVE_DATA_TABLE(MAX_DOORS, Doors);
863
864 // save out ship list
865 SAVE_DATA_TABLE(MAX_SHIPS, Ships);
866
867 // save out weapons list
868 SAVE_DATA_TABLE(MAX_WEAPONS, Weapons);
869
870 // save out textures list
871 SAVE_DATA_TABLE(MAX_TEXTURES, GameTextures);
872
873 // write out bitmap handle list. look at all Objects that are fireballs and
874 // save their handles-names.
875 for (i = 0; i <= Highest_object_index; i++)
876 if (Objects[i].type == OBJ_FIREBALL) {
877 if (Objects[i].ctype.blast_info.bm_handle > -1) {
878 gs_WriteShort(fp, Objects[i].ctype.blast_info.bm_handle);
879 cf_WriteString(fp, GameBitmaps[Objects[i].ctype.blast_info.bm_handle].name);
880 }
881 }
882 gs_WriteShort(fp, -1); // terminate bitmap list
883 cf_WriteString(fp, "");
884 END_VERIFY_SAVEFILE(fp, "Xlate save");
885 }
886
887 extern uint8_t AutomapVisMap[MAX_ROOMS];
888 // initializes rooms
889 void SGSRooms(CFILE *fp) {
890 int i, f, p;
891
892 gs_WriteShort(fp, (int16_t)Highest_room_index);
893
894 gs_WriteShort(fp, MAX_ROOMS);
895
896 for (i = 0; i < MAX_ROOMS; i++) {
897 gs_WriteByte(fp, AutomapVisMap[i]);
898 }
899
900 for (i = 0; i <= Highest_room_index; i++) {
901 gs_WriteByte(fp, Rooms[i].used);
902 if (Rooms[i].used) {
903 // we need to save some room info out.
904 gs_WriteInt(fp, Rooms[i].flags);
905 gs_WriteByte(fp, Rooms[i].pulse_time);
906 gs_WriteByte(fp, Rooms[i].pulse_offset);
907 gs_WriteVector(fp, Rooms[i].wind);
908 gs_WriteFloat(fp, Rooms[i].last_render_time);
909 gs_WriteFloat(fp, Rooms[i].fog_depth);
910 gs_WriteFloat(fp, Rooms[i].fog_r);
911 gs_WriteFloat(fp, Rooms[i].fog_g);
912 gs_WriteFloat(fp, Rooms[i].fog_b);
913 gs_WriteFloat(fp, Rooms[i].damage);
914
915 //?? gs_WriteFloat(fp, Rooms[i].ambient_sound); // need to save an index of sounds.
916
917 // save additional face information here.
918 // save texture changes
919 int num_changed = 0;
920 for (f = 0; f < Rooms[i].num_faces; f++) {
921 if (Rooms[i].faces[f].flags & FF_TEXTURE_CHANGED)
922 num_changed++;
923 }
924 cf_WriteShort(fp, num_changed);
925 for (f = 0; f < Rooms[i].num_faces; f++) {
926 if (Rooms[i].faces[f].flags & FF_TEXTURE_CHANGED) {
927 cf_WriteShort(fp, f);
928 cf_WriteShort(fp, Rooms[i].faces[f].tmap);
929 }
930 }
931
932 for (p = 0; p < Rooms[i].num_portals; p++) {
933 gs_WriteInt(fp, Rooms[i].portals[p].flags);
934 }
935
936 // save doorway info
937 if (Rooms[i].flags & RF_DOOR) {
938 doorway *dp = Rooms[i].doorway_data;
939 ASSERT(dp != NULL);
940 gs_WriteByte(fp, dp->state);
941 gs_WriteByte(fp, dp->flags);
942 gs_WriteByte(fp, dp->keys_needed);
943 gs_WriteFloat(fp, dp->position);
944 gs_WriteFloat(fp, dp->dest_pos);
945 gs_WriteInt(fp, dp->sound_handle);
946 gs_WriteInt(fp, dp->activenum);
947 gs_WriteInt(fp, dp->doornum);
948 }
949 }
950 }
951 }
952
953 // saves out events
954 void SGSEvents(CFILE *fp) {}
955
956 // saves out triggers
957 void SGSTriggers(CFILE *fp) {
958 int i;
959
960 gs_WriteShort(fp, (int16_t)Num_triggers);
961
962 for (i = 0; i < Num_triggers; i++) {
963 gs_WriteShort(fp, Triggers[i].flags);
964 gs_WriteShort(fp, Triggers[i].activator);
965
966 // write script info
967 //@@ SGSScript(fp, &Triggers[i].script);
968 }
969 }
970
971 // players
972 void SGSPlayers(CFILE *fp) {
973 // player struct needs savin
974 player *plr = &Players[0];
975
976 gs_WriteShort(fp, sizeof(player));
977 cf_WriteBytes((uint8_t *)plr, sizeof(player), fp);
978 if (plr->guided_obj)
979 gs_WriteInt(fp, plr->guided_obj->handle);
980
981 // save inventory and countermeasures
982 plr->inventory.SaveInventory(fp);
983 plr->counter_measures.SaveInventory(fp);
984 }
985
986 // save viseffects
987 void SGSVisEffects(CFILE *fp) {
988 int i, count = 0;
989
990 // count up all viseffects to write out.
991 for (i = 0; i <= Highest_vis_effect_index; i++)
992 if (VisEffects[i].type != VIS_NONE)
993 count++;
994
995 gs_WriteShort(fp, (int16_t)count);
996
997 for (i = 0; i <= Highest_vis_effect_index; i++) {
998 if (VisEffects[i].type != VIS_NONE)
999 cf_WriteBytes((uint8_t *)&VisEffects[i], sizeof(vis_effect), fp);
1000 }
1001 }
1002
1003 extern int Physics_NumLinked;
1004 extern int PhysicsLinkList[MAX_OBJECTS];
1005 extern char MarkerMessages[MAX_PLAYERS * 2][MAX_MARKER_MESSAGE_LENGTH];
1006 extern int Marker_message;
1007 void InsureSaveGame(CFILE *fp) { cf_WriteInt(fp, 0xF00D4B0B); }
1008
1009 #define INSURE_SAVEFILE
1010 // InsureSaveGame(fp)
1011
1012 // saves out objects
1013 void SGSObjects(CFILE *fp) {
1014 int i, j;
1015
1016 START_VERIFY_SAVEFILE(fp);
1017
1018 // Save marker info (text)
1019 cf_WriteInt(fp, Marker_message);
1020 cf_WriteShort(fp, (int16_t)MAX_PLAYERS * 2);
1021 for (i = 0; i < MAX_PLAYERS * 2; i++) {
1022 cf_WriteShort(fp, strlen(MarkerMessages[i]) + 1);
1023 cf_WriteBytes((uint8_t *)MarkerMessages[i], strlen(MarkerMessages[i]) + 1, fp);
1024 }
1025
1026 // this method should maintain the object list as it currently stands in the level
1027 cf_WriteShort(fp, (int16_t)Highest_object_index);
1028
1029 // save what objects are stuck to each other
1030 cf_WriteInt(fp, MAX_OBJECTS);
1031 cf_WriteInt(fp, Physics_NumLinked);
1032 for (i = 0; i < MAX_OBJECTS; i++) {
1033 cf_WriteInt(fp, PhysicsLinkList[i]);
1034 }
1035
1036 // save AI information
1037 //////////////////////////////////
1038 cf_WriteInt(fp, MAX_DYNAMIC_PATHS);
1039 cf_WriteInt(fp, MAX_NODES);
1040
1041 int s;
1042 for (i = 0; i < MAX_DYNAMIC_PATHS; i++) {
1043 cf_WriteShort(fp, AIDynamicPath[i].num_nodes);
1044 cf_WriteShort(fp, AIDynamicPath[i].use_count);
1045 cf_WriteInt(fp, AIDynamicPath[i].owner_handle);
1046
1047 for (s = 0; s < MAX_NODES; s++) {
1048 cf_WriteFloat(fp, AIDynamicPath[i].pos[s].x);
1049 cf_WriteFloat(fp, AIDynamicPath[i].pos[s].y);
1050 cf_WriteFloat(fp, AIDynamicPath[i].pos[s].z);
1051 cf_WriteInt(fp, AIDynamicPath[i].roomnum[s]);
1052 }
1053 }
1054
1055 cf_WriteInt(fp, MAX_ROOMS);
1056 cf_WriteInt(fp, AIAltPathNumNodes);
1057
1058 for (i = 0; i < MAX_ROOMS; i++) {
1059 cf_WriteInt(fp, AIAltPath[i]);
1060 }
1061
1062 ///////////////////////////////////
1063
1064 for (i = 0; i <= Highest_object_index; i++) {
1065 object *op = &Objects[i];
1066 poly_model *pm = &Poly_models[op->rtype.pobj_info.model_num];
1067
1068 gs_WriteInt(fp, 0xBADB0B);
1069 // we don't save deleted objects or room objects since they're reconstructed on loadlevel
1070 gs_WriteByte(fp, (int8_t)op->type);
1071
1072 if (op->type == OBJ_NONE)
1073 continue;
1074 gs_WriteByte(fp, (int8_t)op->lighting_render_type);
1075
1076 // Store whether or not we have a pointer to lighting_info
1077 gs_WriteByte(fp, op->lighting_info ? 1 : 0);
1078 if (op->lighting_info) {
1079 cf_WriteBytes((uint8_t *)op->lighting_info, sizeof(*op->lighting_info), fp);
1080 }
1081
1082 // these objects FOR NOW won't be saved
1083 gs_WriteInt(fp, op->handle);
1084 ASSERT((op->handle & HANDLE_OBJNUM_MASK) == i);
1085
1086 // type and handle info.
1087 gs_WriteByte(fp, (int8_t)op->dummy_type);
1088 // positional information
1089 gs_WriteInt(fp, op->roomnum);
1090 gs_WriteVector(fp, op->pos);
1091 gs_WriteVector(fp, op->last_pos);
1092 gs_WriteMatrix(fp, op->orient);
1093
1094 // write out object name
1095 int ii;
1096 ii = (op->name) ? strlen(op->name) : 0;
1097 gs_WriteByte(fp, ii);
1098 if (ii > 0)
1099 cf_WriteBytes((uint8_t *)op->name, ii, fp);
1100
1101 // data universal to all objects that need to be saved.
1102 gs_WriteShort(fp, (int16_t)op->id);
1103 gs_WriteInt(fp, static_cast<int32_t>(op->flags));
1104 gs_WriteByte(fp, (int8_t)op->control_type);
1105 gs_WriteByte(fp, (int8_t)op->movement_type);
1106 gs_WriteByte(fp, (int8_t)op->render_type);
1107
1108 gs_WriteShort(fp, (int16_t)op->renderframe);
1109 gs_WriteFloat(fp, op->size);
1110 gs_WriteFloat(fp, op->shields);
1111 gs_WriteByte(fp, op->contains_type);
1112 gs_WriteByte(fp, op->contains_id);
1113 gs_WriteByte(fp, op->contains_count);
1114 gs_WriteFloat(fp, op->creation_time);
1115 gs_WriteFloat(fp, op->lifeleft);
1116 gs_WriteFloat(fp, op->lifetime);
1117 gs_WriteInt(fp, op->parent_handle);
1118
1119 // attachment info.
1120 gs_WriteInt(fp, op->attach_ultimate_handle);
1121 gs_WriteInt(fp, op->attach_parent_handle);
1122 if ((op->attach_ultimate_handle) && (OBJECT_HANDLE_NONE != op->attach_ultimate_handle)) {
1123 mprintf(0, "Object %d has an ultimate parent of %d (%d)\n", i, OBJNUM(ObjGet(op->attach_ultimate_handle)),
1124 op->attach_parent_handle);
1125 }
1126 if ((op->attach_ultimate_handle) && (OBJECT_HANDLE_NONE != op->attach_parent_handle)) {
1127 mprintf(0, "Object %d has a parent of %d (%d)\n", i, OBJNUM(ObjGet(op->attach_parent_handle)),
1128 op->attach_parent_handle);
1129 }
1130
1131 gs_WriteInt(fp, pm->n_attach);
1132 if (pm->n_attach) {
1133 mprintf(0, "Object %d has %d attach points.\n", i, pm->n_attach);
1134
1135 if (op->attach_children) {
1136 gs_WriteInt(fp, 1);
1137
1138 for (j = 0; j < pm->n_attach; j++)
1139 gs_WriteInt(fp, op->attach_children[j]);
1140 } else {
1141 gs_WriteInt(fp, 0);
1142 }
1143 }
1144
1145 INSURE_SAVEFILE;
1146
1147 gs_WriteByte(fp, op->attach_type);
1148 gs_WriteShort(fp, op->attach_index);
1149 gs_WriteFloat(fp, op->attach_dist);
1150 gs_WriteVector(fp, op->min_xyz);
1151 gs_WriteVector(fp, op->max_xyz);
1152 gs_WriteFloat(fp, op->impact_size);
1153 gs_WriteFloat(fp, op->impact_time);
1154 gs_WriteFloat(fp, op->impact_player_damage);
1155 gs_WriteFloat(fp, op->impact_generic_damage);
1156 gs_WriteFloat(fp, op->impact_force);
1157
1158 // write out custom default script info
1159 ii = (op->custom_default_script_name) ? strlen(op->custom_default_script_name) : 0;
1160 gs_WriteByte(fp, ii);
1161 if (ii > 0)
1162 cf_WriteBytes((uint8_t *)op->custom_default_script_name, ii, fp);
1163
1164 ii = (op->custom_default_module_name) ? strlen(op->custom_default_module_name) : 0;
1165 gs_WriteByte(fp, ii);
1166 if (ii > 0)
1167 cf_WriteBytes((uint8_t *)op->custom_default_module_name, ii, fp);
1168
1169 INSURE_SAVEFILE;
1170
1171 gs_WriteShort(fp, (int16_t)op->position_counter);
1172
1173 INSURE_SAVEFILE;
1174
1175 // write out all structures here.
1176 // movement info.
1177 gs_WriteShort(fp, sizeof(op->mtype));
1178 cf_WriteBytes((uint8_t *)&op->mtype, sizeof(op->mtype), fp);
1179
1180 INSURE_SAVEFILE;
1181
1182 // Control info, determined by CONTROL_TYPE
1183 gs_WriteShort(fp, sizeof(op->ctype));
1184 cf_WriteBytes((uint8_t *)&op->ctype, sizeof(op->ctype), fp);
1185
1186 INSURE_SAVEFILE;
1187
1188 // save ai information.
1189 SGSObjAI(fp, op->ai_info);
1190
1191 INSURE_SAVEFILE;
1192 // save out rendering information
1193 gs_WriteShort(fp, sizeof(op->rtype));
1194 cf_WriteBytes((uint8_t *)&op->rtype, sizeof(op->rtype), fp);
1195
1196 cf_WriteFloat(fp, op->size);
1197 if (op->render_type == RT_POLYOBJ) {
1198 // Do Animation stuff
1199 custom_anim multi_anim_info;
1200 ObjGetAnimUpdate(i, &multi_anim_info);
1201 cf_WriteBytes((uint8_t *)&multi_anim_info, sizeof(multi_anim_info), fp);
1202 }
1203
1204 INSURE_SAVEFILE;
1205
1206 // dynamic weapon battery info!!
1207 SGSObjWB(fp, op, (op->type == OBJ_PLAYER) ? MAX_WBS_PER_OBJ : pm->num_wbs);
1208
1209 INSURE_SAVEFILE;
1210
1211 // save effect info!
1212 SGSObjEffects(fp, op);
1213
1214 INSURE_SAVEFILE;
1215
1216 // save script stuff.
1217 //@@ SGSScript(fp, &op->script);
1218
1219 // special things local to object
1220 SGSObjSpecial(fp, op);
1221 }
1222 mprintf(0, "highest obj index = %d, ", Highest_object_index);
1223 END_VERIFY_SAVEFILE(fp, "Objects save");
1224 }
1225
1226 // saves ai
1227 void SGSObjAI(CFILE *fp, const ai_frame *ai) {
1228 gs_WriteByte(fp, (ai ? 1 : 0));
1229 if (!ai)
1230 return;
1231
1232 gs_WriteShort(fp, sizeof(ai_frame));
1233 cf_WriteBytes((uint8_t *)ai, sizeof(ai_frame), fp);
1234 }
1235
1236 // saves script
1237 //@@void SGSScript(CFILE *fp, const script_info *script)
1238 //@@{
1239 //@@ int i,j;
1240 //@@
1241 //@@// ugh, write out script info.
1242 //@@ gs_WriteByte(fp, (script->thread ? 1 : 0));
1243 //@@
1244 //@@ if (script->name)
1245 //@@ cf_WriteString(fp, script->name);
1246 //@@ else
1247 //@@ cf_WriteString(fp, "");
1248 //@@
1249 //@@ gs_WriteShort(fp, script->num_parms);
1250 //@@ gs_WriteShort(fp, script->is_custom);
1251 //@@ for (i = 0; i < script->num_parms; i++)
1252 //@@ {
1253 //@@ gs_WriteByte(fp, script->parms[i].type);
1254 //@@ gs_WriteFloat(fp, script->parms[i].val.x);
1255 //@@ gs_WriteFloat(fp, script->parms[i].val.y);
1256 //@@ gs_WriteFloat(fp, script->parms[i].val.z);
1257 //@@ }
1258 //@@
1259 //@@// write out thread data if necessary
1260 //@@ if (script->thread) {
1261 //@@ const vector *mem = D3XGetThreadMem(script->thread->mem_handle);
1262 //@@ uint16_t mem_size = script->thread->prog->map[script->thread->prog_idx].mem;
1263 //@@
1264 //@@ gs_WriteShort(fp, mem_size);
1265 //@@ if (mem) {
1266 //@@ for (j = 0; j < mem_size; j++)
1267 //@@ gs_WriteVector(fp, mem[j]);
1268 //@@ }
1269 //@@ }
1270 //@@}
1271
1272 // saves fx
1273 void SGSObjEffects(CFILE *fp, const object *op) {
1274 effect_info_s *ei = op->effect_info;
1275
1276 gs_WriteByte(fp, (ei ? 1 : 0));
1277 if (ei) {
1278 gs_WriteShort(fp, sizeof(effect_info_s));
1279 cf_WriteBytes((uint8_t *)ei, sizeof(effect_info_s), fp);
1280 }
1281 }
1282
1283 // saves wb
1284 void SGSObjWB(CFILE *fp, object *op, int num_wbs) {
1285 int i;
1286
1287 if (op->dynamic_wb) {
1288 gs_WriteByte(fp, (int8_t)num_wbs);
1289 for (i = 0; i < num_wbs; i++) {
1290 dynamic_wb_info *dwb = &op->dynamic_wb[i];
1291 cf_WriteBytes((uint8_t *)dwb, sizeof(dynamic_wb_info), fp);
1292 }
1293 } else {
1294 gs_WriteByte(fp, 0);
1295 }
1296 }
1297
1298 // saves special object info
1299 void SGSObjSpecial(CFILE *fp, const object *op) {}
1300
1301 // load spew
1302 void SGSSpew(CFILE *fp) {
1303 int i;
1304
1305 gs_WriteShort(fp, (int16_t)spew_count);
1306 for (i = 0; i < MAX_SPEW_EFFECTS; i++) {
1307 gs_WriteByte(fp, SpewEffects[i].inuse ? true : false);
1308 if (SpewEffects[i].inuse)
1309 cf_WriteBytes((uint8_t *)&SpewEffects[i], sizeof(spewinfo), fp);
1310 }
1311 }
1312
1313 // save matcens
1314 void SGSMatcens(CFILE *fp) {
1315 cf_WriteInt(fp, Num_matcens);
1316
1317 for (int i = 0; i < Num_matcens; i++) {
1318 ASSERT(Matcen[i]);
1319
1320 Matcen[i]->SaveData(fp);
1321 }
1322 }
1323
1324 #define HUD_RENDER_ZOOM 0.56f
1325 void SGSSnapshot(CFILE *fp) {
1326 extern void ResetFacings(); // render.cpp
1327
1328 const int SGSSNAP_WIDTH = 160, SGSSNAP_HEIGHT = 120;
1329 int oldfilepos, bm_handle;
1330
1331 oldfilepos = cftell(fp);
1332
1333 // Set up for rendering
1334 StartFrame(0, 0, Max_window_w, Max_window_h);
1335 g3_StartFrame(&Viewer_object->pos, &Viewer_object->orient, HUD_RENDER_ZOOM);
1336
1337 // Reset facings for mine stuff
1338 ResetFacings();
1339
1340 // Render the world
1341 GameRenderWorld(Viewer_object, &Viewer_object->pos, Viewer_object->roomnum, &Viewer_object->orient, Render_zoom,
1342 false);
1343
1344 // Done rendering
1345 g3_EndFrame();
1346 EndFrame();
1347 rend_Flip();
1348 bm_handle = bm_AllocBitmap(Max_window_w, Max_window_h, 0);
1349
1350 cf_WriteByte(fp, (bm_handle > 0) ? 1 : 0);
1351
1352 if (bm_handle > 0) {
1353 // Tell our renderer lib to take a screen shot
1354 rend_Screenshot(bm_handle);
1355 bm_ChangeSize(bm_handle, SGSSNAP_WIDTH, SGSSNAP_HEIGHT);
1356
1357 try {
1358 if (bm_SaveBitmap(fp, bm_handle) < 0) {
1359 cfseek(fp, oldfilepos, SEEK_SET);
1360 cf_WriteByte(fp, 0);
1361 }
1362 } catch (cfile_error) {
1363 cfseek(fp, oldfilepos, SEEK_SET);
1364 cf_WriteByte(fp, 0);
1365 }
1366
1367 bm_FreeBitmap(bm_handle);
1368 }
1369 }
1370
1371 //@@ switch (op->control_type)
1372 //@@ {
1373 //@@ case CT_NONE:
1374 //@@ case CT_AI:
1375 //@@ case CT_FLYING: //the player is flying
1376 //@@ case CT_FLYTHROUGH: //the flythrough system
1377 //@@ case CT_SLEW: //slewing
1378 //@@ case CT_PARTICLE: //Particle
1379 //@@ break;
1380 //@@ case CT_EXPLOSION: //explosion sequencer
1381 //@@ gs_WriteFloat(fp, op_expl->spawn_time);
1382 //@@ gs_WriteFloat(fp, op_expl->delete_time);
1383 //@@ gs_WriteShort(fp, op_expl->delete_objnum);
1384 //@@ gs_WriteShort(fp, op_expl->attach_parent);
1385 //@@ gs_WriteShort(fp, op_expl->prev_attach);
1386 //@@ gs_WriteShort(fp, op_expl->next_attach);
1387 //@@ break;
1388 //@@ case CT_DEBRIS: //this is a piece of debris
1389 //@@ gs_WriteFloat(fp, op->ctype.dying_info.delay_time);
1390 //@@ gs_WriteBool(fp, op->ctype.dying_info.f_death_anim);
1391 //@@ break;
1392 //@@ case CT_POWERUP: //animating powerup blob
1393 //@@ gs_WriteInt(fp, op->ctype.powerup_info.count);
1394 //@@ gs_WriteInt(fp, op->ctype.powerup_info.flags);
1395 //@@ break;
1396 //@@ case CT_SPLINTER: //Splinter
1397 //@@ gs_WriteByte(fp, (int8_t)op_splint->subobj_num);
1398 //@@ gs_WriteShort(fp, op_splint->facenum);
1399 //@@ for (j = 0; j < MAX_VERTS_PER_SPLINTER; j++)
1400 //@@ gs_WriteVector(fp, op_splint->verts[j]);
1401 //@@ gs_WriteVector(fp, op_splint->center);
1402 //@@ break;
1403 //@@ case CT_WEAPON: //laser, etc.
1404 //@@ gs_WriteShort(fp, op_wpn->parent_type);
1405 //@@ gs_WriteShort(fp, op_wpn->src_gun_num);
1406 //@@ gs_WriteInt(fp, op_wpn->last_hit_handle);
1407 //@@ gs_WriteInt(fp, op_wpn->track_handle);
1408 //@@ gs_WriteInt(fp, op_wpn->hit_status);
1409 //@@ gs_WriteVector(fp, op_wpn->hit_pnt);
1410 //@@ gs_WriteVector(fp, op_wpn->hit_wall_pnt);
1411 //@@ gs_WriteVector(fp, op_wpn->hit_wall_normal);
1412 //@@ gs_WriteInt(fp, op_wpn->hit_room);
1413 //@@ gs_WriteInt(fp, op_wpn->hit_pnt_room);
1414 //@@ gs_WriteShort(fp, op_wpn->hit_face);
1415 //@@ gs_WriteFloat(fp, op_wpn->multiplier);
1416 //@@ gs_WriteFloat(fp, op_wpn->thrust_left);
1417 //@@ gs_WriteFloat(fp, op_wpn->last_drop_time);
1418 //@@ break;
1419 //@@
1420 //@@ default:
1421 //@@ Int3();
1422 //@@ }
1423
1424 //@@ if (op->type == OBJ_FIREBALL) {
1425 //@@ gs_WriteFloat(fp, op->ctype.blast_info.max_size);
1426 //@@ gs_WriteInt(fp, op->ctype.blast_info.bm_handle);
1427 //@@ }
1428
1429 //@@ const int N_SHARD_VERTS = 3;
1430 //@@ switch (op->render_type)
1431 //@@ {
1432 //@@ case RT_NONE:
1433 //@@ case RT_EDITOR_SPHERE:
1434 //@@ case RT_FIREBALL:
1435 //@@ case RT_LINE:
1436 //@@ case RT_PARTICLE:
1437 //@@ case RT_SPLINTER:
1438 //@@ case RT_ROOM:
1439 //@@ break;
1440 //@@ case RT_WEAPON:
1441 //@@ if (!(op->flags & OF_POLYGON_OBJECT))
1442 //@@ break;
1443 //@@ case RT_POLYOBJ:
1444 //@@ gs_WriteShort(fp, pobji->model_num);
1445 //@@ gs_WriteShort(fp, pobji->dying_model_num);
1446 //@@ gs_WriteFloat(fp, pobji->anim_start_frame);
1447 //@@ gs_WriteFloat(fp, pobji->anim_frame);
1448 //@@ gs_WriteFloat(fp, pobji->anim_end_frame);
1449 //@@ gs_WriteFloat(fp, pobji->anim_time);
1450 //@@ gs_WriteInt(fp, (int)pobji->anim_flags);
1451 //@@ gs_WriteFloat(fp, pobji->max_speed);
1452 //@@ gs_WriteInt(fp, (int)pobji->subobj_flags);
1453 //@@ gs_WriteInt(fp, pobji->tmap_override);
1454 //@@ //!! multi_anim_save here
1455 //@@ break;
1456 //@@
1457 //@@ case RT_SHARD:
1458 //@@ for (j = 0; j < N_SHARD_VERTS; j++)
1459 //@@ gs_WriteVector(fp, op->rtype.shard_info.points[j]);
1460 //@@ for (j = 0; j < N_SHARD_VERTS; j++)
1461 //@@ gs_WriteFloat(fp, op->rtype.shard_info.u[j]);
1462 //@@ for (j = 0; j < N_SHARD_VERTS; j++)
1463 //@@ gs_WriteFloat(fp, op->rtype.shard_info.v[j]);
1464 //@@ gs_WriteVector(fp, op->rtype.shard_info.normal);
1465 //@@ gs_WriteShort(fp, op->rtype.shard_info.tmap);
1466 //@@ break;
1467 //@@
1468 //@@ default:
1469 //@@ Int3();
1470 //@@ }
1471
1472 // AI WRITES
1473 //@@// the FUN begins.
1474 //@@ gs_WriteByte(fp, ai->ai_class);
1475 //@@ gs_WriteByte(fp, ai->ai_type);
1476 //@@
1477 //@@// write path info
1478 //@@ const ai_path_info *path = &ai->path;
1479 //@@ gs_WriteInt(fp, path->flags);
1480 //@@ gs_WriteShort(fp, path->cur_path);
1481 //@@ gs_WriteShort(fp, path->cur_node);
1482 //@@ gs_WriteShort(fp, path->num_paths);
1483 //@@ gs_WriteShort(fp, path->goal_index);
1484 //@@
1485 //@@ for (i = 0; i < MAX_JOINED_PATHS; i++)
1486 //@@ {
1487 //@@ gs_WriteByte(fp, path->path_id[i]);
1488 //@@ gs_WriteByte(fp, path->path_type[i]);
1489 //@@ gs_WriteShort(fp, path->path_start_node[i]);
1490 //@@ gs_WriteShort(fp, path->path_end_node[i]);
1491 //@@ gs_WriteShort(fp, path->path_flags[i]);
1492 //@@ }
1493 //@@
1494 //@@// continue
1495 //@@ gs_WriteFloat(fp, ai->max_velocity);
1496 //@@ gs_WriteFloat(fp, ai->max_delta_velocity);
1497 //@@ gs_WriteFloat(fp, ai->max_turn_rate);
1498 //@@ gs_WriteFloat(fp, ai->max_delta_turn_rate);
1499 //@@ gs_WriteFloat(fp, ai->attack_vel_percent);
1500 //@@ gs_WriteFloat(fp, ai->flee_vel_percent);
1501 //@@ gs_WriteFloat(fp, ai->dodge_vel_percent);
1502 //@@ gs_WriteFloat(fp, ai->circle_distance);
1503 //@@ gs_WriteFloat(fp, ai->dodge_percent);
1504 //@@
1505 //@@ for (i = 0; i < 2; i++)
1506 //@@ {
1507 //@@ gs_WriteFloat(fp, ai->melee_damage[i]);
1508 //@@ gs_WriteFloat(fp, ai->melee_latency[i]);
1509 //@@ }
1510 //@@
1511 //@@ for (i = 0; i < MAX_AI_SOUNDS; i++)
1512 //@@ {
1513 //@@ gs_WriteInt(fp, ai->sound[i]);
1514 //@@ gs_WriteFloat(fp, ai->last_sound_time[i]);
1515 //@@ }
1516 //@@ gs_WriteShort(fp, ai->last_played_sound_index);
1517 //@@
1518 //@@ gs_WriteByte(fp, ai->movement_type);
1519 //@@ gs_WriteByte(fp, ai->movement_subtype);
1520 //@@ gs_WriteByte(fp, ai->animation_type);
1521 //@@ gs_WriteByte(fp, ai->next_animation_type);
1522 //@@ gs_WriteByte(fp, ai->next_movement);
1523 //@@ gs_WriteByte(fp, ai->current_wb_firing);
1524 //@@ gs_WriteByte(fp, ai->last_wb_firing);
1525 //@@
1526 //@@// goals
1527 //@@ for (i = 0;i < MAX_GOALS; i++)
1528 //@@ SGSObjAIGoal(fp, &ai->goals[i]);
1529 //@@
1530 //@@// continue
1531 //@@ gs_WriteInt(fp, ai->target_handle);
1532 //@@ gs_WriteFloat(fp, ai->next_target_update_time);
1533 //@@ gs_WriteFloat(fp, ai->dist_to_target);
1534 //@@ gs_WriteVector(fp, ai->vec_to_target);
1535 //@@ gs_WriteFloat(fp, ai->last_check_see_target_time);
1536 //@@ gs_WriteVector(fp, ai->last_see_target_pos);
1537 //@@ gs_WriteFloat(fp, ai->last_see_target_time);
1538 //@@ gs_WriteFloat(fp, ai->weapon_speed);
1539 //@@ gs_WriteFloat(fp, ai->next_melee_time);
1540 //@@ gs_WriteFloat(fp, ai->last_render_time);
1541 //@@ gs_WriteFloat(fp, ai->next_flinch_time);
1542 //@@ gs_WriteInt(fp, ai->status_reg);
1543 //@@ gs_WriteInt(fp, ai->flags);
1544 //@@ gs_WriteInt(fp, ai->notify_flags);
1545 //@@
1546 //@@// notify events (MUST BE WRITTEN OUT AT A LATER DATE)
1547 //@@
1548 //@@// Normalized movement and facing information
1549 //@@ gs_WriteVector(fp, ai->movement_dir);
1550 //@@ gs_WriteVector(fp, ai->rot_thrust_vector);
1551 //@@ gs_WriteFloat(fp, ai->fov);
1552 //@@ gs_WriteInt(fp, ai->anim_sound_handle);
1553 //@@ gs_WriteFloat(fp, ai->frustration);
1554 //@@ gs_WriteFloat(fp, ai->curiousity);
1555 //@@ gs_WriteFloat(fp, ai->fire_spread);
1556 //@@ gs_WriteFloat(fp, ai->agression);
1557 //@@ gs_WriteFloat(fp, ai->night_vision);
1558 //@@ gs_WriteFloat(fp, ai->fog_vision);
1559 //@@ gs_WriteFloat(fp, ai->lead_accuracy);
1560 //@@ gs_WriteFloat(fp, ai->lead_varience);
1561 //@@ gs_WriteFloat(fp, ai->fight_team);
1562 //@@ gs_WriteFloat(fp, ai->fight_same);
1563 //@@ gs_WriteFloat(fp, ai->hearing);
1564 //@@ gs_WriteFloat(fp, ai->roaming);
1565 //@@ gs_WriteFloat(fp, ai->life_preservation);
1566 //@@
1567 //@@ gs_WriteShort(fp, ai->awareness);
1568
1569 //@@void SGSObjAIGoal(CFILE *fp, const goal *g)
1570 //@@{
1571 //@@ int i;
1572 //@@
1573 //@@ gs_WriteByte(fp, (g->used ? 1 : 0));
1574 //@@ if (!g->used)
1575 //@@ return;
1576 //@@
1577 //@@ gs_WriteInt(fp, g->type);
1578 //@@ gs_WriteByte(fp, g->activation_level);
1579 //@@ gs_WriteShort(fp, g->influence);
1580 //@@
1581 //@@// write goal info
1582 //@@ switch (g->type)
1583 //@@ {
1584 //@@ case AIG_HIDE_FROM_OBJ:
1585 //@@ gs_WriteInt(fp, g->g_info.handle);
1586 //@@ gs_WriteFloat(fp, g->g_info.time);
1587 //@@ break;
1588 //@@
1589 //@@ case AIG_WANDER_AROUND:
1590 //@@ gs_WriteInt(fp, g->g_info.roomnum);
1591 //@@ break;
1592 //@@
1593 //@@ case AIG_DODGE_OBJ:
1594 //@@ case AIG_MOVE_RELATIVE_OBJ:
1595 //@@ gs_WriteInt(fp, g->g_info.handle);
1596 //@@ break;
1597 //@@
1598 //@@ case AIG_MOVE_RELATIVE_OBJ_VEC:
1599 //@@ gs_WriteInt(fp, g->g_info.handle);
1600 //@@ gs_WriteInt(fp, g->g_info.subtype);
1601 //@@ break;
1602 //@@
1603 //@@ case AIG_GUARD_AREA:
1604 //@@ gs_WriteVector(fp, g->g_info.pos);
1605 //@@ break;
1606 //@@
1607 //@@ case AIG_GET_TO_OBJ:
1608 //@@ gs_WriteInt(fp, g->g_info.handle);
1609 //@@ gs_WriteVector(fp, g->g_info.pos);
1610 //@@ break;
1611 //@@
1612 //@@ case AIG_GET_TO_POS:
1613 //@@ gs_WriteInt(fp, g->g_info.roomnum);
1614 //@@ gs_WriteVector(fp, g->g_info.pos);
1615 //@@ break;
1616 //@@
1617 //@@ case AIG_FOLLOW_PATH: // This must be fixed -- chrishack
1618 //@@ gs_WriteInt(fp, g->g_info.id);
1619 //@@ break;
1620 //@@ }
1621 //@@
1622 //@@// write goal enabler
1623 //@@ gs_WriteByte(fp, g->num_enablers);
1624 //@@ for (i = 0; i < g->num_enablers; i++)
1625 //@@ {
1626 //@@ gs_WriteByte(fp, g->enabler[i].enabler_type);
1627 //@@
1628 //@@ switch (g->enabler[i].enabler_type)
1629 //@@ {
1630 //@@ case AIE_AI_STATUS_FLAG:
1631 //@@ gs_WriteInt(fp, g->enabler[i].flags);
1632 //@@ break;
1633 //@@ }
1634 //@@
1635 //@@ gs_WriteFloat(fp, g->enabler[i].percent_enable);
1636 //@@ gs_WriteFloat(fp, g->enabler[i].check_interval);
1637 //@@ gs_WriteFloat(fp, g->enabler[i].last_check_time);
1638 //@@ gs_WriteByte(fp, g->enabler[i].bool_next_enabler_op);
1639 //@@ }
1640 //@@
1641 //@@// continue
1642 //@@ gs_WriteFloat(fp, g->circle_distance);
1643 //@@ gs_WriteInt(fp, g->status_reg);
1644 //@@ gs_WriteFloat(fp, g->start_time);
1645 //@@ gs_WriteFloat(fp, g->next_path_time);
1646 //@@ gs_WriteFloat(fp, g->dist_to_goal);
1647 //@@ gs_WriteVector(fp, g->vec_to_target);
1648 //@@ gs_WriteFloat(fp, g->last_check_see_target_time);
1649 //@@ gs_WriteVector(fp, g->last_see_target_pos);
1650 //@@ gs_WriteFloat(fp, g->last_see_target_time);
1651 //@@ gs_WriteFloat(fp, g->next_target_update_time);
1652 //@@ gs_WriteShort(fp, g->flags);
1653 //@@}
1654 //@@
1655
1656 // EFFECTS INFO STRUCTURE
1657 //@@ gs_WriteInt(fp, ei->type_flags);
1658 //@@ gs_WriteFloat(fp, ei->alpha);
1659 //@@
1660 //@@// if (op->type == OBJ_POWERUP) {
1661 //@@ gs_WriteFloat(fp, ei->last_object_hit_time);
1662 //@@ gs_WriteInt(fp, ei->last_object_hit);
1663 //@@// }
1664 //@@// if (ei->type_flags & EF_DEFORM) {
1665 //@@ gs_WriteFloat(fp, ei->deform_range);
1666 //@@ gs_WriteFloat(fp, ei->deform_time);
1667 //@@// }
1668 //@@// if (ei->type_flags & EF_VOLUME_CHANGING) {
1669 //@@ gs_WriteFloat(fp, ei->volume_change_time);
1670 //@@ gs_WriteVector(fp, ei->volume_old_pos);
1671 //@@ gs_WriteFloat(fp, ei->volume_old_room);
1672 //@@// }
1673 //@@// if (ei->type_flags & EF_VOLUME_LIT) {
1674 //@@ gs_WriteByte(fp, (int8_t)ei->dynamic_this_frame);
1675 //@@ gs_WriteFloat(fp, ei->dynamic_red);
1676 //@@ gs_WriteFloat(fp, ei->dynamic_green);
1677 //@@ gs_WriteFloat(fp, ei->dynamic_blue);
1678 //@@// }
1679 //@@// if (ei->type_flags & EF_CLOAKED) {
1680 //@@ gs_WriteFloat(fp, ei->cloak_time);
1681 //@@// }
1682 //@@// if (ei->type_flags & EF_COLORED) {
1683 //@@ gs_WriteFloat(fp, ei->color_time);
1684 //@@ gs_WriteFloat(fp, ei->r);
1685 //@@ gs_WriteFloat(fp, ei->g);
1686 //@@ gs_WriteFloat(fp, ei->b);
1687 //@@// }
1688 //@@// if (ei->type_flags & EF_NAPALMED) {
1689 //@@ gs_WriteFloat(fp, ei->damage_time);
1690 //@@ gs_WriteFloat(fp, ei->damage_per_second);
1691 //@@ gs_WriteFloat(fp, ei->last_damage_time);
1692 //@@ gs_WriteInt(fp, ei->damage_handle);
1693 //@@// }
1694 //@@// if (ei->type_flags & EF_FREEZE) {
1695 //@@ gs_WriteFloat(fp, ei->freeze_scalar);
1696 //@@// }
1697 //@@// if (ei->type_flags & EF_LINE_ATTACH) {
1698 //@@ gs_WriteInt(fp, ei->attach_line_handle);
1699 //@@// }
1700
1701 // Weapon Battery Info
1702 //@@ gs_WriteFloat(fp, dwb->last_fire_time);
1703 //@@ gs_WriteByte(fp, dwb->cur_firing_mask);
1704 //@@
1705 //@@ for (j = 0; j < MAX_WB_TURRETS; j++)
1706 //@@ {
1707 //@@ gs_WriteFloat(fp, dwb->norm_turret_angle[j]);
1708 //@@ gs_WriteFloat(fp, dwb->turret_next_think_time[j]);
1709 //@@ gs_WriteByte(fp, dwb->turret_direction[j]);
1710 //@@ }
1711 //@@
1712 //@@ gs_WriteByte(fp, dwb->wb_anim_mask);
1713 //@@ gs_WriteFloat(fp, dwb->wb_anim_frame);
1714 //@@ gs_WriteVector(fp, dwb->cur_target);
1715 //@@ gs_WriteByte(fp, dwb->upgrade_level);
1716 //@@ gs_WriteInt(fp, dwb->flags);