-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsv_save.c
694 lines (618 loc) · 16.5 KB
/
sv_save.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//**************************************************************************
//**
//** sv_save.c
//**
//**************************************************************************
// HEADER FILES ------------------------------------------------------------
#include "h2stdinc.h"
#include "doomdef.h"
#include "p_local.h"
// MACROS ------------------------------------------------------------------
#define SVG_RAM 0
#define SVG_FILE 1
#define SAVEGAMESIZE 0x30000
#define SAVE_GAME_TERMINATOR 0x1d
// TYPES -------------------------------------------------------------------
typedef enum
{
tc_end,
tc_mobj
} thinkerclass_t;
enum
{
tc_ceiling,
tc_door,
tc_floor,
tc_plat,
tc_flash,
tc_strobe,
tc_glow,
tc_endspecials
} specials_e;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void ArchivePlayers(void);
static void UnarchivePlayers(void);
static void ArchiveWorld(void);
static void UnarchiveWorld(void);
static void ArchiveThinkers(void);
static void UnarchiveThinkers(void);
static void ArchiveSpecials(void);
static void UnarchiveSpecials(void);
static void OpenStreamOut(const char *fileName);
static void CloseStreamOut(const char *fileName);
static void StreamOutBuffer(const void *buffer, int size);
static void StreamOutByte(byte val);
static void StreamOutWord(unsigned short val);
//static void StreamOutLong(unsigned int val);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static FILE *SavingFP;
static int SaveGameType;
static void *SaveBuffer;
static byte *SavePtr;
// CODE --------------------------------------------------------------------
//==========================================================================
//
// SV_SaveGame
//
//==========================================================================
void SV_SaveGame(int slot, const char *description)
{
int i;
char fileName[MAX_OSPATH];
char verString[SAVEVERSIONSIZE];
snprintf(fileName, sizeof(fileName), "%s%s%d.hsg",
basePath, SAVEGAMENAME, slot);
OpenStreamOut(fileName);
StreamOutBuffer(description, SAVESTRINGSIZE);
memset(verString, 0, sizeof(verString));
sprintf(verString, "version %i", VERSION);
StreamOutBuffer(verString, SAVEVERSIONSIZE);
StreamOutByte(gameskill);
StreamOutByte(gameepisode);
StreamOutByte(gamemap);
for (i = 0; i < MAXPLAYERS; i++)
{
StreamOutByte(playeringame[i]);
}
StreamOutByte(leveltime>>16);
StreamOutByte(leveltime>>8);
StreamOutByte(leveltime);
ArchivePlayers();
ArchiveWorld();
ArchiveThinkers();
ArchiveSpecials();
CloseStreamOut(fileName);
}
//==========================================================================
//
// SV_LoadGame
//
//==========================================================================
void SV_LoadGame(int slot)
{
int i;
int a, b, c;
char fileName[MAX_OSPATH];
char vcheck[SAVEVERSIONSIZE];
snprintf(fileName, sizeof(fileName), "%s%s%d.hsg",
basePath, SAVEGAMENAME, slot);
M_ReadFile(fileName, &SaveBuffer);
SavePtr = (byte *)SaveBuffer + SAVESTRINGSIZE; // Skip the description field
memset(vcheck, 0, sizeof(vcheck));
sprintf(vcheck, "version %i", VERSION);
if (strcmp((char *)SavePtr, vcheck) != 0)
{ // Bad version
return;
}
SavePtr += SAVEVERSIONSIZE;
gameskill = *SavePtr++;
gameepisode = *SavePtr++;
gamemap = *SavePtr++;
for (i = 0; i < MAXPLAYERS; i++)
{
playeringame[i] = *SavePtr++;
}
// Load a base level
G_InitNew(gameskill, gameepisode, gamemap);
// Create leveltime
a = *SavePtr++;
b = *SavePtr++;
c = *SavePtr++;
leveltime = (a<<16) + (b<<8) + c;
// De-archive all the modifications
UnarchivePlayers();
UnarchiveWorld();
UnarchiveThinkers();
UnarchiveSpecials();
if (*SavePtr != SAVE_GAME_TERMINATOR)
{ // Missing savegame termination marker
I_Error("Bad savegame");
}
Z_Free(SaveBuffer);
}
//==========================================================================
//
// ArchivePlayers
//
//==========================================================================
static void ArchivePlayers(void)
{
int i, j;
player_t dest;
for (i = 0; i < MAXPLAYERS; i++)
{
if (!playeringame[i])
{
continue;
}
memcpy(&dest, &players[i], sizeof(player_t));
for (j = 0; j < NUMPSPRITES; j++)
{
if (dest.psprites[j].state)
{
dest.psprites[j].state =
(state_t *)(dest.psprites[j].state - states);
}
}
StreamOutBuffer(&dest, sizeof(player_t));
}
}
//==========================================================================
//
// UnarchivePlayers
//
//==========================================================================
static void UnarchivePlayers(void)
{
int i, j;
for (i = 0; i < MAXPLAYERS; i++)
{
if (!playeringame[i])
continue;
memcpy(&players[i], SavePtr, sizeof(player_t));
SavePtr += sizeof(player_t);
players[i].mo = NULL; // will be set when unarc thinker
players[i].message = NULL;
players[i].attacker = NULL;
for (j = 0; j < NUMPSPRITES; j++)
{
if (players[i]. psprites[j].state)
players[i].psprites[j].state = &states[(int)players[i].psprites[j].state];
}
}
}
//==========================================================================
//
// ArchiveWorld
//
//==========================================================================
static void ArchiveWorld(void)
{
int i, j;
sector_t *sec;
line_t *li;
side_t *si;
// Sectors
for (i = 0, sec = sectors; i < numsectors; i++, sec++)
{
StreamOutWord(sec->floorheight>>FRACBITS);
StreamOutWord(sec->ceilingheight>>FRACBITS);
StreamOutWord(sec->floorpic);
StreamOutWord(sec->ceilingpic);
StreamOutWord(sec->lightlevel);
StreamOutWord(sec->special); // needed?
StreamOutWord(sec->tag); // needed?
}
// Lines
for (i = 0, li = lines; i < numlines; i++, li++)
{
StreamOutWord(li->flags);
StreamOutWord(li->special);
StreamOutWord(li->tag);
for (j = 0; j < 2; j++)
{
if (li->sidenum[j] == -1)
{
continue;
}
si = &sides[li->sidenum[j]];
StreamOutWord(si->textureoffset>>FRACBITS);
StreamOutWord(si->rowoffset>>FRACBITS);
StreamOutWord(si->toptexture);
StreamOutWord(si->bottomtexture);
StreamOutWord(si->midtexture);
}
}
}
//==========================================================================
//
// UnarchiveWorld
//
//==========================================================================
static void UnarchiveWorld(void)
{
int i, j;
sector_t *sec;
line_t *li;
side_t *si;
short *get;
get = (short *)SavePtr;
//
// do sectors
//
for (i = 0, sec = sectors; i < numsectors; i++, sec++)
{
sec->floorheight = *get++ << FRACBITS;
sec->ceilingheight = *get++ << FRACBITS;
sec->floorpic = *get++;
sec->ceilingpic = *get++;
sec->lightlevel = *get++;
sec->special = *get++; // needed?
sec->tag = *get++; // needed?
sec->specialdata = 0;
sec->soundtarget = 0;
}
//
// do lines
//
for (i = 0, li = lines; i < numlines; i++, li++)
{
li->flags = *get++;
li->special = *get++;
li->tag = *get++;
for (j = 0; j < 2; j++)
{
if (li->sidenum[j] == -1)
continue;
si = &sides[li->sidenum[j]];
si->textureoffset = *get++ << FRACBITS;
si->rowoffset = *get++ << FRACBITS;
si->toptexture = *get++;
si->bottomtexture = *get++;
si->midtexture = *get++;
}
}
SavePtr = (byte *)get;
}
//==========================================================================
//
// ArchiveThinkers
//
//==========================================================================
static void ArchiveThinkers(void)
{
thinker_t *th;
mobj_t mobj;
for (th = thinkercap.next; th != &thinkercap; th = th->next)
{
if (th->function == P_MobjThinker)
{
StreamOutByte(tc_mobj);
memcpy(&mobj, th, sizeof(mobj_t));
mobj.state = (state_t *)(mobj.state - states);
if (mobj.player)
{
mobj.player = (player_t *)((mobj.player - players) + 1);
}
StreamOutBuffer(&mobj, sizeof(mobj_t));
continue;
}
//I_Error("P_ArchiveThinkers: Unknown thinker function");
}
// Add a terminating marker
StreamOutByte(tc_end);
}
//==========================================================================
//
// UnarchiveThinkers
//
//==========================================================================
static void UnarchiveThinkers(void)
{
byte tclass;
thinker_t *currentthinker, *next;
mobj_t *mobj;
//
// remove all the current thinkers
//
currentthinker = thinkercap.next;
while (currentthinker != &thinkercap)
{
next = currentthinker->next;
if (currentthinker->function == P_MobjThinker)
P_RemoveMobj ((mobj_t *)currentthinker);
else
Z_Free (currentthinker);
currentthinker = next;
}
P_InitThinkers ();
// read in saved thinkers
while (1)
{
tclass = *SavePtr++;
switch (tclass)
{
case tc_end:
return; // end of list
case tc_mobj:
mobj = (mobj_t *) Z_Malloc (sizeof(*mobj), PU_LEVEL, NULL);
memcpy (mobj, SavePtr, sizeof(*mobj));
SavePtr += sizeof(*mobj);
mobj->state = &states[(int)mobj->state];
mobj->target = NULL;
if (mobj->player)
{
mobj->player = &players[(int)mobj->player - 1];
mobj->player->mo = mobj;
}
P_SetThingPosition (mobj);
mobj->info = &mobjinfo[mobj->type];
mobj->floorz = mobj->subsector->sector->floorheight;
mobj->ceilingz = mobj->subsector->sector->ceilingheight;
mobj->thinker.function = P_MobjThinker;
P_AddThinker (&mobj->thinker);
break;
default:
I_Error("Unknown tclass %i in savegame", tclass);
}
}
}
//==========================================================================
//
// ArchiveSpecials
//
//==========================================================================
static void ArchiveSpecials(void)
{
/*
T_MoveCeiling, (ceiling_t: sector_t * swizzle), - active list
T_VerticalDoor, (vldoor_t: sector_t * swizzle),
T_MoveFloor, (floormove_t: sector_t * swizzle),
T_LightFlash, (lightflash_t: sector_t * swizzle),
T_StrobeFlash, (strobe_t: sector_t *),
T_Glow, (glow_t: sector_t *),
T_PlatRaise, (plat_t: sector_t *), - active list
*/
thinker_t *th;
ceiling_t ceiling;
vldoor_t door;
floormove_t floor;
plat_t plat;
lightflash_t flash;
strobe_t strobe;
glow_t glow;
for (th = thinkercap.next; th != &thinkercap; th = th->next)
{
if (th->function == T_MoveCeiling)
{
StreamOutByte(tc_ceiling);
memcpy(&ceiling, th, sizeof(ceiling_t));
ceiling.sector = (sector_t *)(ceiling.sector - sectors);
StreamOutBuffer(&ceiling, sizeof(ceiling_t));
continue;
}
if (th->function == T_VerticalDoor)
{
StreamOutByte(tc_door);
memcpy(&door, th, sizeof(vldoor_t));
door.sector = (sector_t *)(door.sector - sectors);
StreamOutBuffer(&door, sizeof(vldoor_t));
continue;
}
if (th->function == T_MoveFloor)
{
StreamOutByte(tc_floor);
memcpy(&floor, th, sizeof(floormove_t));
floor.sector = (sector_t *)(floor.sector - sectors);
StreamOutBuffer(&floor, sizeof(floormove_t));
continue;
}
if (th->function == T_PlatRaise)
{
StreamOutByte(tc_plat);
memcpy(&plat, th, sizeof(plat_t));
plat.sector = (sector_t *)(plat.sector - sectors);
StreamOutBuffer(&plat, sizeof(plat_t));
continue;
}
if (th->function == T_LightFlash)
{
StreamOutByte(tc_flash);
memcpy(&flash, th, sizeof(lightflash_t));
flash.sector = (sector_t *)(flash.sector - sectors);
StreamOutBuffer(&flash, sizeof(lightflash_t));
continue;
}
if (th->function == T_StrobeFlash)
{
StreamOutByte(tc_strobe);
memcpy(&strobe, th, sizeof(strobe_t));
strobe.sector = (sector_t *)(strobe.sector - sectors);
StreamOutBuffer(&strobe, sizeof(strobe_t));
continue;
}
if (th->function == T_Glow)
{
StreamOutByte(tc_glow);
memcpy(&glow, th, sizeof(glow_t));
glow.sector = (sector_t *)(glow.sector - sectors);
StreamOutBuffer(&glow, sizeof(glow_t));
continue;
}
}
// Add a terminating marker
StreamOutByte(tc_endspecials);
}
//==========================================================================
//
// UnarchiveSpecials
//
//==========================================================================
static void UnarchiveSpecials(void)
{
byte tclass;
ceiling_t *ceiling;
vldoor_t *door;
floormove_t *floor;
plat_t *plat;
lightflash_t *flash;
strobe_t *strobe;
glow_t *glow;
// read in saved thinkers
while (1)
{
tclass = *SavePtr++;
switch (tclass)
{
case tc_endspecials:
return; // end of list
case tc_ceiling:
ceiling = (ceiling_t *) Z_Malloc (sizeof(*ceiling), PU_LEVEL, NULL);
memcpy (ceiling, SavePtr, sizeof(*ceiling));
SavePtr += sizeof(*ceiling);
ceiling->sector = §ors[(int)ceiling->sector];
ceiling->sector->specialdata = T_MoveCeiling;
if (ceiling->thinker.function)
ceiling->thinker.function = T_MoveCeiling;
P_AddThinker (&ceiling->thinker);
P_AddActiveCeiling(ceiling);
break;
case tc_door:
door = (vldoor_t *) Z_Malloc (sizeof(*door), PU_LEVEL, NULL);
memcpy (door, SavePtr, sizeof(*door));
SavePtr += sizeof(*door);
door->sector = §ors[(int)door->sector];
door->sector->specialdata = door;
door->thinker.function = T_VerticalDoor;
P_AddThinker (&door->thinker);
break;
case tc_floor:
floor = (floormove_t *) Z_Malloc (sizeof(*floor), PU_LEVEL, NULL);
memcpy (floor, SavePtr, sizeof(*floor));
SavePtr += sizeof(*floor);
floor->sector = §ors[(int)floor->sector];
floor->sector->specialdata = T_MoveFloor;
floor->thinker.function = T_MoveFloor;
P_AddThinker (&floor->thinker);
break;
case tc_plat:
plat = (plat_t *) Z_Malloc (sizeof(*plat), PU_LEVEL, NULL);
memcpy (plat, SavePtr, sizeof(*plat));
SavePtr += sizeof(*plat);
plat->sector = §ors[(int)plat->sector];
plat->sector->specialdata = T_PlatRaise;
if (plat->thinker.function)
plat->thinker.function = T_PlatRaise;
P_AddThinker (&plat->thinker);
P_AddActivePlat(plat);
break;
case tc_flash:
flash = (lightflash_t *) Z_Malloc (sizeof(*flash), PU_LEVEL, NULL);
memcpy (flash, SavePtr, sizeof(*flash));
SavePtr += sizeof(*flash);
flash->sector = §ors[(int)flash->sector];
flash->thinker.function = T_LightFlash;
P_AddThinker (&flash->thinker);
break;
case tc_strobe:
strobe = (strobe_t *) Z_Malloc (sizeof(*strobe), PU_LEVEL, NULL);
memcpy (strobe, SavePtr, sizeof(*strobe));
SavePtr += sizeof(*strobe);
strobe->sector = §ors[(int)strobe->sector];
strobe->thinker.function = T_StrobeFlash;
P_AddThinker (&strobe->thinker);
break;
case tc_glow:
glow = (glow_t *) Z_Malloc (sizeof(*glow), PU_LEVEL, NULL);
memcpy (glow, SavePtr, sizeof(*glow));
SavePtr += sizeof(*glow);
glow->sector = §ors[(int)glow->sector];
glow->thinker.function = T_Glow;
P_AddThinker (&glow->thinker);
break;
default:
I_Error("P_UnarchiveSpecials:Unknown tclass %i "
"in savegame", tclass);
}
}
}
//==========================================================================
//
// OpenStreamOut
//
//==========================================================================
static void OpenStreamOut(const char *fileName)
{
MallocFailureOk = true;
SaveBuffer = Z_Malloc(SAVEGAMESIZE, PU_STATIC, NULL);
SavePtr = (byte *)SaveBuffer;
MallocFailureOk = false;
if (SaveBuffer == NULL)
{ // Not enough memory - use file save method
SaveGameType = SVG_FILE;
SavingFP = fopen(fileName, "wb");
}
else
{
SaveGameType = SVG_RAM;
}
}
//==========================================================================
//
// CloseStreamOut
//
//==========================================================================
static void CloseStreamOut(const char *fileName)
{
int length;
StreamOutByte(SAVE_GAME_TERMINATOR);
if (SaveGameType == SVG_RAM)
{
length = SavePtr - (byte *)SaveBuffer;
if (length > SAVEGAMESIZE)
{
I_Error("Savegame buffer overrun");
}
M_WriteFile(fileName, SaveBuffer, length);
Z_Free(SaveBuffer);
}
else
{ // SVG_FILE
fclose(SavingFP);
}
}
//==========================================================================
//
// SV_Write
//
//==========================================================================
static void StreamOutBuffer(const void *buffer, int size)
{
if (SaveGameType == SVG_RAM)
{
memcpy(SavePtr, buffer, size);
SavePtr += size;
}
else
{ // SVG_FILE
fwrite(buffer, size, 1, SavingFP);
}
}
static void StreamOutByte(byte val)
{
StreamOutBuffer(&val, sizeof(byte));
}
static void StreamOutWord(unsigned short val)
{
StreamOutBuffer(&val, sizeof(unsigned short));
}
/*
static void StreamOutLong(unsigned int val)
{
StreamOutBuffer(&val, sizeof(int));
}
*/