-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
p_saveg.c
5465 lines (4650 loc) · 139 KB
/
p_saveg.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file
*\section License
* License: GPL + jHeretic/jHexen Exception
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2008 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2008 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* In addition, as a special exception, we, the authors of deng
* give permission to link the code of our release of deng with
* the libjhexen and/or the libjheretic libraries (or with modified
* versions of it that use the same license as the libjhexen or
* libjheretic libraries), and distribute the linked executables.
* You must obey the GNU General Public License in all respects for
* all of the code used other than “libjhexen or libjheretic”. If
* you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your version.
*/
/**
* p_saveg.c: Save Game I/O
*
* Compiles for all supported games.
*
* \bug Not 64bit clean: In function 'SV_ReadPlayer': cast from pointer to integer of different size
* \bug Not 64bit clean: In function 'SV_WriteMobj': cast from pointer to integer of different size
* \bug Not 64bit clean: In function 'RestoreMobj': cast from pointer to integer of different size
* \bug Not 64bit clean: In function 'SV_ReadMobj': cast to pointer from integer of different size
* \bug Not 64bit clean: In function 'P_UnArchiveThinkers': cast from pointer to integer of different size
* \bug Not 64bit clean: In function 'P_UnArchiveBrain': cast to pointer from integer of different size, cast from pointer to integer of different size
* \bug Not 64bit clean: In function 'P_UnArchiveSoundTargets': cast to pointer from integer of different size, cast from pointer to integer of different size
*/
// HEADER FILES ------------------------------------------------------------
#include <lzss.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#if __WOLFTC__
# include "wolftc.h"
#elif __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#endif
#include "p_saveg.h"
#include "f_infine.h"
#include "d_net.h"
#include "p_svtexarc.h"
#include "dmu_lib.h"
#include "p_map.h"
#include "p_mapsetup.h"
#include "p_player.h"
#include "p_inventory.h"
#include "am_map.h"
#include "p_tick.h"
#include "p_actor.h"
#include "p_ceiling.h"
#include "p_door.h"
#include "p_floor.h"
#include "p_plat.h"
#include "hu_msg.h"
// MACROS ------------------------------------------------------------------
#if __WOLFTC__
# define MY_SAVE_MAGIC 0x1A8AFF08
# define MY_CLIENT_SAVE_MAGIC 0x2A8AFF08
# define MY_SAVE_VERSION 6
# define SAVESTRINGSIZE 24
# define CONSISTENCY 0x2c
# define SAVEGAMENAME "WolfSav"
# define CLIENTSAVEGAMENAME "WolfCl"
# define SAVEGAMEEXTENSION "wsg"
#elif __JDOOM__
# define MY_SAVE_MAGIC 0x1DEAD666
# define MY_CLIENT_SAVE_MAGIC 0x2DEAD666
# define MY_SAVE_VERSION 6
# define SAVESTRINGSIZE 24
# define CONSISTENCY 0x2c
# define SAVEGAMENAME "DoomSav"
# define CLIENTSAVEGAMENAME "DoomCl"
# define SAVEGAMEEXTENSION "dsg"
#elif __JDOOM64__
# define MY_SAVE_MAGIC 0x1D6420F4
# define MY_CLIENT_SAVE_MAGIC 0x2D6420F4
# define MY_SAVE_VERSION 6
# define SAVESTRINGSIZE 24
# define CONSISTENCY 0x2c
# define SAVEGAMENAME "D64Sav"
# define CLIENTSAVEGAMENAME "D64Cl"
# define SAVEGAMEEXTENSION "6sg"
#elif __JHERETIC__
# define MY_SAVE_MAGIC 0x7D9A12C5
# define MY_CLIENT_SAVE_MAGIC 0x1062AF43
# define MY_SAVE_VERSION 6
# define SAVESTRINGSIZE 24
# define CONSISTENCY 0x9d
# define SAVEGAMENAME "HticSav"
# define CLIENTSAVEGAMENAME "HticCl"
# define SAVEGAMEEXTENSION "hsg"
#elif __JHEXEN__
# define HXS_VERSION_TEXT "HXS Ver " // Do not change me!
# define HXS_VERSION_TEXT_LENGTH 16
# define MY_SAVE_VERSION 6
# define SAVESTRINGSIZE 24
# define SAVEGAMENAME "Hex"
# define CLIENTSAVEGAMENAME "HexenCl"
# define SAVEGAMEEXTENSION "hxs"
# define MOBJ_XX_PLAYER -2
# define MAX_MAPS 99
# define BASE_SLOT 6
# define REBORN_SLOT 7
# define REBORN_DESCRIPTION "TEMP GAME"
#endif
#if !__JHEXEN__
# define PRE_VER5_END_SPECIALS 7
#endif
#define FF_FULLBRIGHT 0x8000 // used to be flag in thing->frame
#define FF_FRAMEMASK 0x7fff
// TYPES -------------------------------------------------------------------
#if !__JHEXEN__
typedef struct saveheader_s {
int magic;
int version;
int gameMode;
char description[SAVESTRINGSIZE];
byte skill;
byte episode;
byte map;
byte deathmatch;
byte noMonsters;
byte respawnMonsters;
int levelTime;
byte players[MAXPLAYERS];
unsigned int gameID;
} saveheader_t;
#endif
typedef struct playerheader_s {
int numPowers;
int numKeys;
int numFrags;
int numWeapons;
int numAmmoTypes;
int numPSprites;
#if __JHERETIC__ || __JHEXEN__
int numInvSlots;
#endif
#if __JHEXEN__
int numArmorTypes;
#endif
#if __JDOOM64__
int numArtifacts;
#endif
} playerheader_t;
typedef enum gamearchivesegment_e {
ASEG_GAME_HEADER = 101, //jhexen only
ASEG_MAP_HEADER, //jhexen only
ASEG_WORLD,
ASEG_POLYOBJS, //jhexen only
ASEG_MOBJS, //jhexen < ver 4 only
ASEG_THINKERS,
ASEG_SCRIPTS, //jhexen only
ASEG_PLAYERS,
ASEG_SOUNDS, //jhexen only
ASEG_MISC, //jhexen only
ASEG_END,
ASEG_MATERIAL_ARCHIVE,
ASEG_MAP_HEADER2, //jhexen only
ASEG_PLAYER_HEADER
} gamearchivesegment_t;
#if __JHEXEN__
static union saveptr_u {
byte *b;
short *w;
int *l;
float *f;
} saveptr;
typedef struct targetplraddress_s {
void **address;
struct targetplraddress_s *next;
} targetplraddress_t;
#endif
// Thinker Save flags
#define TSF_SERVERONLY 0x01 // Only saved by servers.
#define TSF_SPECIAL 0x02 // Its a special (eg T_MoveCeiling)
typedef struct thinkerinfo_s {
thinkerclass_t thinkclass;
think_t function;
int flags;
void (*Write) ();
int (*Read) ();
size_t size;
} thinkerinfo_t;
typedef enum sectorclass_e {
sc_normal,
sc_ploff, // plane offset
#if !__JHEXEN__
sc_xg1
#endif
} sectorclass_t;
typedef enum lineclass_e {
lc_normal,
#if !__JHEXEN__
lc_xg1
#endif
} lineclass_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void SV_WriteMobj(mobj_t *mobj);
static int SV_ReadMobj(thinker_t *th);
static void SV_WriteCeiling(ceiling_t* ceiling);
static int SV_ReadCeiling(ceiling_t* ceiling);
static void SV_WriteDoor(door_t* door);
static int SV_ReadDoor(door_t* door);
static void SV_WriteFloor(floor_t* floor);
static int SV_ReadFloor(floor_t* floor);
static void SV_WritePlat(plat_t* plat);
static int SV_ReadPlat(plat_t* plat);
#if __JHEXEN__
static void SV_WriteLight(light_t *light);
static int SV_ReadLight(light_t *light);
static void SV_WritePhase(phase_t *phase);
static int SV_ReadPhase(phase_t *phase);
static void SV_WriteScript(acs_t *script);
static int SV_ReadScript(acs_t *script);
static void SV_WriteDoorPoly(polydoor_t *polydoor);
static int SV_ReadDoorPoly(polydoor_t *polydoor);
static void SV_WriteMovePoly(polyevent_t *movepoly);
static int SV_ReadMovePoly(polyevent_t *movepoly);
static void SV_WriteRotatePoly(polyevent_t *rotatepoly);
static int SV_ReadRotatePoly(polyevent_t *rotatepoly);
static void SV_WritePillar(pillar_t *pillar);
static int SV_ReadPillar(pillar_t *pillar);
static void SV_WriteFloorWaggle(waggle_t *floorwaggle);
static int SV_ReadFloorWaggle(waggle_t *floorwaggle);
#else
static void SV_WriteFlash(lightflash_t* flash);
static int SV_ReadFlash(lightflash_t* flash);
static void SV_WriteStrobe(strobe_t* strobe);
static int SV_ReadStrobe(strobe_t* strobe);
static void SV_WriteGlow(glow_t* glow);
static int SV_ReadGlow(glow_t* glow);
# if __JDOOM__ || __JDOOM64__
static void SV_WriteFlicker(fireflicker_t* flicker);
static int SV_ReadFlicker(fireflicker_t* flicker);
# endif
# if __JDOOM64__
static void SV_WriteBlink(lightblink_t* flicker);
static int SV_ReadBlink(lightblink_t* flicker);
# endif
#endif
#if __JHEXEN__
static void OpenStreamOut(char *fileName);
static void CloseStreamOut(void);
static void ClearSaveSlot(int slot);
static void CopySaveSlot(int sourceSlot, int destSlot);
static void CopyFile(char *sourceName, char *destName);
static boolean ExistingFile(char *name);
static void SV_HxLoadMap(void);
#else
static void SV_DMLoadMap(void);
#endif
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
#if __JHEXEN__
extern int ACScriptCount;
extern byte *ActionCodeBase;
extern acsinfo_t *ACSInfo;
#endif
// PUBLIC DATA DEFINITIONS -------------------------------------------------
LZFILE *savefile;
#if __JHEXEN__
char savePath[256]; /* = "hexndata\\"; */
char clientSavePath[256]; /* = "hexndata\\client\\"; */
#else
char savePath[128]; /* = "savegame\\"; */
char clientSavePath[128]; /* = "savegame\\client\\"; */
#endif
// PRIVATE DATA DEFINITIONS ------------------------------------------------
#if __JHEXEN__
static int saveVersion;
#else
static saveheader_t hdr;
#endif
static playerheader_t playerHeader;
static boolean playerHeaderOK = false;
static mobj_t **thing_archive = NULL;
static uint thing_archiveSize = 0;
static int saveToRealPlayerNum[MAXPLAYERS];
#if __JHEXEN__
static targetplraddress_t *targetPlayerAddrs = NULL;
static byte *saveBuffer;
static boolean savingPlayers;
#else
static int numSoundTargets = 0;
#endif
static byte *junkbuffer; // Old save data is read into here.
static thinkerinfo_t thinkerInfo[] = {
{
TC_MOBJ,
P_MobjThinker,
TSF_SERVERONLY,
SV_WriteMobj,
SV_ReadMobj,
sizeof(mobj_t)
},
#if !__JHEXEN__
{
TC_XGMOVER,
XS_PlaneMover,
0,
SV_WriteXGPlaneMover,
SV_ReadXGPlaneMover,
sizeof(xgplanemover_t)
},
#endif
{
TC_CEILING,
T_MoveCeiling,
TSF_SPECIAL,
SV_WriteCeiling,
SV_ReadCeiling,
sizeof(ceiling_t)
},
{
TC_DOOR,
T_Door,
TSF_SPECIAL,
SV_WriteDoor,
SV_ReadDoor,
sizeof(door_t)
},
{
TC_FLOOR,
T_MoveFloor,
TSF_SPECIAL,
SV_WriteFloor,
SV_ReadFloor,
sizeof(floor_t)
},
{
TC_PLAT,
T_PlatRaise,
TSF_SPECIAL,
SV_WritePlat,
SV_ReadPlat,
sizeof(plat_t)
},
#if __JHEXEN__
{
TC_INTERPRET_ACS,
T_InterpretACS,
0,
SV_WriteScript,
SV_ReadScript,
sizeof(acs_t)
},
{
TC_FLOOR_WAGGLE,
T_FloorWaggle,
0,
SV_WriteFloorWaggle,
SV_ReadFloorWaggle,
sizeof(waggle_t)
},
{
TC_LIGHT,
T_Light,
0,
SV_WriteLight,
SV_ReadLight,
sizeof(light_t)
},
{
TC_PHASE,
T_Phase,
0,
SV_WritePhase,
SV_ReadPhase,
sizeof(phase_t)
},
{
TC_BUILD_PILLAR,
T_BuildPillar,
0,
SV_WritePillar,
SV_ReadPillar,
sizeof(pillar_t)
},
{
TC_ROTATE_POLY,
T_RotatePoly,
0,
SV_WriteRotatePoly,
SV_ReadRotatePoly,
sizeof(polyevent_t)
},
{
TC_MOVE_POLY,
T_MovePoly,
0,
SV_WriteMovePoly,
SV_ReadMovePoly,
sizeof(polyevent_t)
},
{
TC_POLY_DOOR,
T_PolyDoor,
0,
SV_WriteDoorPoly,
SV_ReadDoorPoly,
sizeof(polydoor_t)
},
#else
{
TC_FLASH,
T_LightFlash,
TSF_SPECIAL,
SV_WriteFlash,
SV_ReadFlash,
sizeof(lightflash_t)
},
{
TC_STROBE,
T_StrobeFlash,
TSF_SPECIAL,
SV_WriteStrobe,
SV_ReadStrobe,
sizeof(strobe_t)
},
{
TC_GLOW,
T_Glow,
TSF_SPECIAL,
SV_WriteGlow,
SV_ReadGlow,
sizeof(glow_t)
},
# if __JDOOM__ || __JDOOM64__
{
TC_FLICKER,
T_FireFlicker,
TSF_SPECIAL,
SV_WriteFlicker,
SV_ReadFlicker,
sizeof(fireflicker_t)
},
# endif
# if __JDOOM64__
{
TC_BLINK,
T_LightBlink,
TSF_SPECIAL,
SV_WriteBlink,
SV_ReadBlink,
sizeof(lightblink_t)
},
# endif
#endif
// Terminator
{ TC_NULL, NULL, 0, NULL, NULL, 0 }
};
// CODE --------------------------------------------------------------------
/**
* Exit with a fatal error if the value at the current location in the save
* file does not match that associated with the specified segment type.
*
* @param segType Value by which to check for alignment.
*/
static void AssertSegment(int segType)
{
#if __JHEXEN__
if(SV_ReadLong() != segType)
{
Con_Error("Corrupt save game: Segment [%d] failed alignment check",
segType);
}
#endif
}
static void SV_BeginSegment(int segType)
{
#if __JHEXEN__
SV_WriteLong(segType);
#endif
}
/**
* @return Ptr to the thinkerinfo for the given thinker.
*/
static thinkerinfo_t* infoForThinker(thinker_t* th)
{
thinkerinfo_t* thInfo = thinkerInfo;
if(!th)
return NULL;
while(thInfo->thinkclass != TC_NULL)
{
if(thInfo->function == th->function)
return thInfo;
thInfo++;
}
return NULL;
}
static boolean removeThinker(thinker_t* th, void* context)
{
if(th->function == P_MobjThinker)
P_MobjRemove((mobj_t *) th, true);
else
Z_Free(th);
return true; // Continue iteration.
}
typedef struct {
uint count;
boolean savePlayers;
} countmobjsparams_t;
static boolean countMobjs(thinker_t* th, void* context)
{
countmobjsparams_t* params = (countmobjsparams_t*) context;
mobj_t* mo = (mobj_t*) th;
if(!(mo->player && !params->savePlayers))
params->count++;
return true; // Continue iteration.
}
/**
* Must be called before saving or loading any data.
*/
static uint SV_InitThingArchive(boolean load, boolean savePlayers)
{
countmobjsparams_t params;
params.count = 0;
params.savePlayers = savePlayers;
if(load)
{
#if !__JHEXEN__
if(hdr.version < 5)
params.count = 1024; // Limit in previous versions.
else
#endif
params.count = SV_ReadLong();
}
else
{
// Count the number of mobjs we'll be writing.
P_IterateThinkers(P_MobjThinker, countMobjs, ¶ms);
}
thing_archive = calloc(params.count, sizeof(mobj_t*));
return thing_archiveSize = params.count;
}
/**
* Used by the read code when mobjs are read.
*/
static void SV_SetArchiveThing(mobj_t *mo, int num)
{
#if __JHEXEN__
if(saveVersion >= 4)
#endif
num -= 1;
if(num < 0)
return;
if(!thing_archive)
Con_Error("SV_SetArchiveThing: Thing archive uninitialized.");
thing_archive[num] = mo;
}
/**
* Free the thing archive. Called when load is complete.
*/
static void SV_FreeThingArchive(void)
{
if(thing_archive)
{
free(thing_archive);
thing_archive = NULL;
thing_archiveSize = 0;
}
}
/**
* Called by the write code to get archive numbers.
* If the mobj is already archived, the existing number is returned.
* Number zero is not used.
*/
#if __JHEXEN__
int SV_ThingArchiveNum(mobj_t *mo)
#else
unsigned short SV_ThingArchiveNum(mobj_t *mo)
#endif
{
uint i, first_empty = 0;
boolean found;
// We only archive valid mobj thinkers.
if(mo == NULL || ((thinker_t *) mo)->function != P_MobjThinker)
return 0;
#if __JHEXEN__
if(mo->player && !savingPlayers)
return MOBJ_XX_PLAYER;
#endif
if(!thing_archive)
Con_Error("SV_ThingArchiveNum: Thing archive uninitialized.");
found = false;
for(i = 0; i < thing_archiveSize; ++i)
{
if(!thing_archive[i] && !found)
{
first_empty = i;
found = true;
continue;
}
if(thing_archive[i] == mo)
return i + 1;
}
if(!found)
{
Con_Error("SV_ThingArchiveNum: Thing archive exhausted!\n");
return 0; // No number available!
}
// OK, place it in an empty pos.
thing_archive[first_empty] = mo;
return first_empty + 1;
}
#if __JHEXEN__
static void SV_FreeTargetPlayerList(void)
{
targetplraddress_t *p = targetPlayerAddrs, *np;
while(p != NULL)
{
np = p->next;
free(p);
p = np;
}
targetPlayerAddrs = NULL;
}
#endif
/**
* Called by the read code to resolve mobj ptrs from archived thing ids
* after all thinkers have been read and spawned into the map.
*/
mobj_t *SV_GetArchiveThing(int thingid, void *address)
{
#if __JHEXEN__
if(thingid == MOBJ_XX_PLAYER)
{
targetplraddress_t *tpa = malloc(sizeof(targetplraddress_t));
tpa->address = address;
tpa->next = targetPlayerAddrs;
targetPlayerAddrs = tpa;
return NULL;
}
#endif
if(!thing_archive)
Con_Error("SV_GetArchiveThing: Thing archive uninitialized.");
// Check that the thing archive id is valid.
#if __JHEXEN__
if(saveVersion < 4)
{ // Old format is base 0.
if(thingid == -1)
return NULL; // A NULL reference.
if(thingid < 0 || (uint) thingid > thing_archiveSize - 1)
return NULL;
}
else
#endif
{ // New format is base 1.
if(thingid == 0)
return NULL; // A NULL reference.
if(thingid < 1 || (uint) thingid > thing_archiveSize)
{
Con_Message("SV_GetArchiveThing: Invalid NUM %i??\n", thingid);
return NULL;
}
thingid -= 1;
}
return thing_archive[thingid];
}
static playerheader_t *GetPlayerHeader(void)
{
#if _DEBUG
if(!playerHeaderOK)
Con_Error("GetPlayerHeader: Attempted to read before init!");
#endif
return &playerHeader;
}
unsigned int SV_GameID(void)
{
return Sys_GetRealTime() + (levelTime << 24);
}
void SV_Write(void *data, int len)
{
lzWrite(data, len, savefile);
}
void SV_WriteByte(byte val)
{
lzPutC(val, savefile);
}
#if __JHEXEN__
void SV_WriteShort(unsigned short val)
#else
void SV_WriteShort(short val)
#endif
{
lzPutW(val, savefile);
}
#if __JHEXEN__
void SV_WriteLong(unsigned int val)
#else
void SV_WriteLong(long val)
#endif
{
lzPutL(val, savefile);
}
void SV_WriteFloat(float val)
{
#if __JHEXEN__
lzPutL(*(int *) &val, savefile);
#else
long temp = 0;
memcpy(&temp, &val, 4);
lzPutL(temp, savefile);
#endif
}
void SV_Read(void *data, int len)
{
#if __JHEXEN__
memcpy(data, saveptr.b, len);
saveptr.b += len;
#else
lzRead(data, len, savefile);
#endif
}
byte SV_ReadByte(void)
{
#if __JHEXEN__
return (*saveptr.b++);
#else
return lzGetC(savefile);
#endif
}
short SV_ReadShort(void)
{
#if __JHEXEN__
return (SHORT(*saveptr.w++));
#else
return lzGetW(savefile);
#endif
}
long SV_ReadLong(void)
{
#if __JHEXEN__
return (LONG(*saveptr.l++));
#else
return lzGetL(savefile);
#endif
}
float SV_ReadFloat(void)
{
#if __JHEXEN__
return (FLOAT(*saveptr.f++));
#else
long val = lzGetL(savefile);
float returnValue = 0;
memcpy(&returnValue, &val, 4);
return returnValue;
#endif
}
#if __JHEXEN__
static void OpenStreamOut(char *fileName)
{
savefile = lzOpen(fileName, "wp");
}
static void CloseStreamOut(void)
{
if(savefile)
{
lzClose(savefile);
}
}
static boolean ExistingFile(char *name)
{
FILE *fp;
if((fp = fopen(name, "rb")) != NULL)
{
fclose(fp);
return true;
}
else
{
return false;
}
}
/**
* Deletes all save game files associated with a slot number.
*/
static void ClearSaveSlot(int slot)
{
int i;
char fileName[100];
for(i = 0; i < MAX_MAPS; ++i)
{
sprintf(fileName, "%shex%d%02d.hxs", savePath, slot, i);
M_TranslatePath(fileName, fileName);
remove(fileName);
}
sprintf(fileName, "%shex%d.hxs", savePath, slot);
M_TranslatePath(fileName, fileName);
remove(fileName);
}
static void CopyFile(char *sourceName, char *destName)
{
size_t length;
byte *buffer;
LZFILE *outf;
length = M_ReadFile(sourceName, &buffer);
outf = lzOpen(destName, "wp");
if(outf)
{
lzWrite(buffer, length, outf);
lzClose(outf);
}
Z_Free(buffer);
}
/**
* Copies all the save game files from one slot to another.
*/
static void CopySaveSlot(int sourceSlot, int destSlot)
{
int i;
char sourceName[100];
char destName[100];
for(i = 0; i < MAX_MAPS; ++i)
{
sprintf(sourceName, "%shex%d%02d.hxs", savePath, sourceSlot, i);
M_TranslatePath(sourceName, sourceName);
if(ExistingFile(sourceName))
{
sprintf(destName, "%shex%d%02d.hxs", savePath, destSlot, i);
M_TranslatePath(destName, destName);
CopyFile(sourceName, destName);
}
}
sprintf(sourceName, "%shex%d.hxs", savePath, sourceSlot);
M_TranslatePath(sourceName, sourceName);
if(ExistingFile(sourceName))
{
sprintf(destName, "%shex%d.hxs", savePath, destSlot);
M_TranslatePath(destName, destName);
CopyFile(sourceName, destName);
}
}
/**
* Copies the base slot to the reborn slot.
*/
void SV_HxUpdateRebornSlot(void)
{
ClearSaveSlot(REBORN_SLOT);
CopySaveSlot(BASE_SLOT, REBORN_SLOT);
}
void SV_HxClearRebornSlot(void)
{
ClearSaveSlot(REBORN_SLOT);
}
int SV_HxGetRebornSlot(void)
{
return (REBORN_SLOT);
}
/**
* @return @c true, if the reborn slot is available.
*/
boolean SV_HxRebornSlotAvailable(void)
{
char fileName[100];
sprintf(fileName, "%shex%d.hxs", savePath, REBORN_SLOT);
M_TranslatePath(fileName, fileName);
return ExistingFile(fileName);
}
void SV_HxInitBaseSlot(void)