-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
p_saveg.cpp
1195 lines (1010 loc) · 32 KB
/
p_saveg.cpp
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 p_saveg.cpp Common game-save state management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/
#include "common.h"
#include "p_saveg.h"
#include "dmu_lib.h"
#include "p_actor.h"
#include "p_inventory.h"
#include "p_tick.h" // mapTime
#include "p_saveio.h"
#include "gamestatewriter.h"
#include "gamestatereader.h"
#include "mapstatereader.h"
#include "mapstatewriter.h"
#include <de/String>
#include <de/memory.h>
#include <cstdio>
#include <cstring>
static bool inited = false;
static GameStateReaderFactory gameStateReaderFactory;
static SaveSlots sslots(NUMSAVESLOTS);
SaveSlots *saveSlots = &sslots;
SaveInfo const *curInfo;
int saveToRealPlayerNum[MAXPLAYERS];
#if __JHEXEN__
targetplraddress_t *targetPlayerAddrs;
#endif
#if __JHEXEN__
byte *saveBuffer;
#endif
#if !__JHEXEN__
/**
* Compose the (possibly relative) path to the game-save associated
* with @a sessionId.
*
* @param sessionId Unique game-session identifier.
*
* @return File path to the reachable save directory. If the game-save path
* is unreachable then a zero-length string is returned instead.
*/
static AutoStr *composeGameSavePathForClientSessionId(uint sessionId)
{
AutoStr *path = AutoStr_NewStd();
// Do we have a valid path?
if(!F_MakePath(SV_ClientSavePath()))
{
return path; // return zero-length string.
}
// Compose the full game-save path and filename.
Str_Appendf(path, "%s" CLIENTSAVEGAMENAME "%08X." SAVEGAMEEXTENSION, SV_ClientSavePath(), sessionId);
F_TranslatePath(path, path);
return path;
}
#endif
dd_bool SV_OpenGameSaveFile(Str const *path, dd_bool write)
{
DENG_ASSERT(path != 0);
App_Log(DE2_DEV_RES_MSG, "SV_OpenGameSaveFile: Opening \"%s\"", Str_Text(path));
#if __JHEXEN__
if(!write)
{
bool result = M_ReadFile(Str_Text(path), (char **)&saveBuffer) > 0;
// Set the save pointer.
SV_HxSavePtr()->b = saveBuffer;
return result;
}
else
#endif
{
SV_OpenFile(path, write? "wp" : "rp");
}
return SV_File() != 0;
}
#if __JHEXEN__
dd_bool SV_OpenMapSaveFile(Str const *path)
{
DENG_ASSERT(path != 0);
App_Log(DE2_DEV_RES_MSG, "SV_OpenMapSaveFile: Opening \"%s\"", Str_Text(path));
// Load the file
size_t bufferSize = M_ReadFile(Str_Text(path), (char **)&saveBuffer);
if(!bufferSize) return false;
SV_HxSavePtr()->b = saveBuffer;
SV_HxSetSaveEndPtr(saveBuffer + bufferSize);
return true;
}
dd_bool SV_HxHaveMapStateForSlot(int slot, uint map)
{
DENG_ASSERT(inited);
AutoStr *path = saveSlots->composeSavePathForSlot(slot, (int)map + 1);
if(path && !Str_IsEmpty(path))
{
return SV_ExistingFile(path);
}
return false;
}
#endif
#if __JHEXEN__
void SV_InitTargetPlayers()
{
targetPlayerAddrs = 0;
}
void SV_ClearTargetPlayers()
{
while(targetPlayerAddrs)
{
targetplraddress_t *next = targetPlayerAddrs->next;
M_Free(targetPlayerAddrs);
targetPlayerAddrs = next;
}
}
#endif // __JHEXEN__
void SV_TranslateLegacyMobjFlags(mobj_t *mo, int ver)
{
#if __JDOOM64__
// Nothing to do.
DENG_UNUSED(mo);
DENG_UNUSED(ver);
return;
#endif
#if __JDOOM__ || __JHERETIC__
if(ver < 6)
{
// mobj.flags
# if __JDOOM__
// switched values for MF_BRIGHTSHADOW <> MF_BRIGHTEXPLODE
if((mo->flags & MF_BRIGHTEXPLODE) != (mo->flags & MF_BRIGHTSHADOW))
{
if(mo->flags & MF_BRIGHTEXPLODE) // previously MF_BRIGHTSHADOW
{
mo->flags |= MF_BRIGHTSHADOW;
mo->flags &= ~MF_BRIGHTEXPLODE;
}
else // previously MF_BRIGHTEXPLODE
{
mo->flags |= MF_BRIGHTEXPLODE;
mo->flags &= ~MF_BRIGHTSHADOW;
}
} // else they were both on or off so it doesn't matter.
# endif
// Remove obsoleted flags in earlier save versions.
mo->flags &= ~MF_V6OBSOLETE;
// mobj.flags2
# if __JDOOM__
// jDoom only gained flags2 in ver 6 so all we can do is to
// apply the values as set in the mobjinfo.
// Non-persistent flags might screw things up a lot worse otherwise.
mo->flags2 = mo->info->flags2;
# endif
}
#endif
#if __JDOOM__ || __JHERETIC__
if(ver < 9)
{
mo->spawnSpot.flags &= ~MASK_UNKNOWN_MSF_FLAGS;
// Spawn on the floor by default unless the mobjtype flags override.
mo->spawnSpot.flags |= MSF_Z_FLOOR;
}
#endif
#if __JHEXEN__
if(ver < 5)
#else
if(ver < 7)
#endif
{
// flags3 was introduced in a latter version so all we can do is to
// apply the values as set in the mobjinfo.
// Non-persistent flags might screw things up a lot worse otherwise.
mo->flags3 = mo->info->flags3;
}
}
void playerheader_s::write(Writer *writer)
{
Writer_WriteByte(writer, 2); // version byte
numPowers = NUM_POWER_TYPES;
numKeys = NUM_KEY_TYPES;
numFrags = MAXPLAYERS;
numWeapons = NUM_WEAPON_TYPES;
numAmmoTypes = NUM_AMMO_TYPES;
numPSprites = NUMPSPRITES;
#if __JHERETIC__ || __JHEXEN__ || __JDOOM64__
numInvItemTypes = NUM_INVENTORYITEM_TYPES;
#endif
#if __JHEXEN__
numArmorTypes = NUMARMOR;
#endif
Writer_WriteInt32(writer, numPowers);
Writer_WriteInt32(writer, numKeys);
Writer_WriteInt32(writer, numFrags);
Writer_WriteInt32(writer, numWeapons);
Writer_WriteInt32(writer, numAmmoTypes);
Writer_WriteInt32(writer, numPSprites);
#if __JDOOM64__ || __JHERETIC__ || __JHEXEN__
Writer_WriteInt32(writer, numInvItemTypes);
#endif
#if __JHEXEN__
Writer_WriteInt32(writer, numArmorTypes);
#endif
}
void playerheader_s::read(Reader *reader, int saveVersion)
{
#if __JHEXEN__
if(saveVersion >= 4)
#else
if(saveVersion >= 5)
#endif
{
int ver = Reader_ReadByte(reader);
#if !__JHERETIC__
DENG_UNUSED(ver);
#endif
numPowers = Reader_ReadInt32(reader);
numKeys = Reader_ReadInt32(reader);
numFrags = Reader_ReadInt32(reader);
numWeapons = Reader_ReadInt32(reader);
numAmmoTypes = Reader_ReadInt32(reader);
numPSprites = Reader_ReadInt32(reader);
#if __JHERETIC__
if(ver >= 2)
numInvItemTypes = Reader_ReadInt32(reader);
else
numInvItemTypes = NUM_INVENTORYITEM_TYPES;
#endif
#if __JHEXEN__ || __JDOOM64__
numInvItemTypes = Reader_ReadInt32(reader);
#endif
#if __JHEXEN__
numArmorTypes = Reader_ReadInt32(reader);
#endif
}
else // The old format didn't save the counts.
{
#if __JHEXEN__
numPowers = 9;
numKeys = 11;
numFrags = 8;
numWeapons = 4;
numAmmoTypes = 2;
numPSprites = 2;
numInvItemTypes = 33;
numArmorTypes = 4;
#elif __JDOOM__ || __JDOOM64__
numPowers = 6;
numKeys = 6;
numFrags = 4; // Why was this only 4?
numWeapons = 9;
numAmmoTypes = 4;
numPSprites = 2;
# if __JDOOM64__
numInvItemTypes = 3;
# endif
#elif __JHERETIC__
numPowers = 9;
numKeys = 3;
numFrags = 4; // ?
numWeapons = 8;
numInvItemTypes = 14;
numAmmoTypes = 6;
numPSprites = 2;
#endif
}
}
enum sectorclass_t
{
sc_normal,
sc_ploff, ///< plane offset
#if !__JHEXEN__
sc_xg1,
#endif
NUM_SECTORCLASSES
};
void SV_WriteSector(Sector *sec, MapStateWriter *msw)
{
Writer *writer = msw->writer();
int i, type;
float flooroffx = P_GetFloatp(sec, DMU_FLOOR_MATERIAL_OFFSET_X);
float flooroffy = P_GetFloatp(sec, DMU_FLOOR_MATERIAL_OFFSET_Y);
float ceiloffx = P_GetFloatp(sec, DMU_CEILING_MATERIAL_OFFSET_X);
float ceiloffy = P_GetFloatp(sec, DMU_CEILING_MATERIAL_OFFSET_Y);
byte lightlevel = (byte) (255.f * P_GetFloatp(sec, DMU_LIGHT_LEVEL));
short floorheight = (short) P_GetIntp(sec, DMU_FLOOR_HEIGHT);
short ceilingheight = (short) P_GetIntp(sec, DMU_CEILING_HEIGHT);
short floorFlags = (short) P_GetIntp(sec, DMU_FLOOR_FLAGS);
short ceilingFlags = (short) P_GetIntp(sec, DMU_CEILING_FLAGS);
Material *floorMaterial = (Material *)P_GetPtrp(sec, DMU_FLOOR_MATERIAL);
Material *ceilingMaterial = (Material *)P_GetPtrp(sec, DMU_CEILING_MATERIAL);
xsector_t *xsec = P_ToXSector(sec);
#if !__JHEXEN__
// Determine type.
if(xsec->xg)
type = sc_xg1;
else
#endif
if(!FEQUAL(flooroffx, 0) || !FEQUAL(flooroffy, 0) || !FEQUAL(ceiloffx, 0) || !FEQUAL(ceiloffy, 0))
type = sc_ploff;
else
type = sc_normal;
// Type byte.
Writer_WriteByte(writer, type);
// Version.
// 2: Surface colors.
// 3: Surface flags.
Writer_WriteByte(writer, 3); // write a version byte.
Writer_WriteInt16(writer, floorheight);
Writer_WriteInt16(writer, ceilingheight);
Writer_WriteInt16(writer, msw->serialIdFor(floorMaterial));
Writer_WriteInt16(writer, msw->serialIdFor(ceilingMaterial));
Writer_WriteInt16(writer, floorFlags);
Writer_WriteInt16(writer, ceilingFlags);
#if __JHEXEN__
Writer_WriteInt16(writer, (short) lightlevel);
#else
Writer_WriteByte(writer, lightlevel);
#endif
float rgb[3];
P_GetFloatpv(sec, DMU_COLOR, rgb);
for(i = 0; i < 3; ++i)
Writer_WriteByte(writer, (byte)(255.f * rgb[i]));
P_GetFloatpv(sec, DMU_FLOOR_COLOR, rgb);
for(i = 0; i < 3; ++i)
Writer_WriteByte(writer, (byte)(255.f * rgb[i]));
P_GetFloatpv(sec, DMU_CEILING_COLOR, rgb);
for(i = 0; i < 3; ++i)
Writer_WriteByte(writer, (byte)(255.f * rgb[i]));
Writer_WriteInt16(writer, xsec->special);
Writer_WriteInt16(writer, xsec->tag);
#if __JHEXEN__
Writer_WriteInt16(writer, xsec->seqType);
#endif
if(type == sc_ploff
#if !__JHEXEN__
|| type == sc_xg1
#endif
)
{
Writer_WriteFloat(writer, flooroffx);
Writer_WriteFloat(writer, flooroffy);
Writer_WriteFloat(writer, ceiloffx);
Writer_WriteFloat(writer, ceiloffy);
}
#if !__JHEXEN__
if(xsec->xg) // Extended General?
{
SV_WriteXGSector(sec, writer);
}
#endif
}
void SV_ReadSector(Sector *sec, MapStateReader *msr)
{
xsector_t *xsec = P_ToXSector(sec);
Reader *reader = msr->reader();
int mapVersion = msr->mapVersion();
// A type byte?
int type = 0;
#if __JHEXEN__
if(mapVersion < 4)
type = sc_ploff;
else
#else
if(mapVersion <= 1)
type = sc_normal;
else
#endif
type = Reader_ReadByte(reader);
// A version byte?
int ver = 1;
#if __JHEXEN__
if(mapVersion > 2)
#else
if(mapVersion > 4)
#endif
{
ver = Reader_ReadByte(reader);
}
int fh = Reader_ReadInt16(reader);
int ch = Reader_ReadInt16(reader);
P_SetIntp(sec, DMU_FLOOR_HEIGHT, fh);
P_SetIntp(sec, DMU_CEILING_HEIGHT, ch);
#if __JHEXEN__
// Update the "target heights" of the planes.
P_SetIntp(sec, DMU_FLOOR_TARGET_HEIGHT, fh);
P_SetIntp(sec, DMU_CEILING_TARGET_HEIGHT, ch);
// The move speed is not saved; can cause minor problems.
P_SetIntp(sec, DMU_FLOOR_SPEED, 0);
P_SetIntp(sec, DMU_CEILING_SPEED, 0);
#endif
Material *floorMaterial = 0, *ceilingMaterial = 0;
#if !__JHEXEN__
if(mapVersion == 1)
{
// The flat numbers are absolute lump indices.
Uri *uri = Uri_NewWithPath2("Flats:", RC_NULL);
Uri_SetPath(uri, Str_Text(W_LumpName(Reader_ReadInt16(reader))));
floorMaterial = (Material *)P_ToPtr(DMU_MATERIAL, Materials_ResolveUri(uri));
Uri_SetPath(uri, Str_Text(W_LumpName(Reader_ReadInt16(reader))));
ceilingMaterial = (Material *)P_ToPtr(DMU_MATERIAL, Materials_ResolveUri(uri));
Uri_Delete(uri);
}
else if(mapVersion >= 4)
#endif
{
// The flat numbers are actually archive numbers.
floorMaterial = msr->material(Reader_ReadInt16(reader), 0);
ceilingMaterial = msr->material(Reader_ReadInt16(reader), 0);
}
P_SetPtrp(sec, DMU_FLOOR_MATERIAL, floorMaterial);
P_SetPtrp(sec, DMU_CEILING_MATERIAL, ceilingMaterial);
if(ver >= 3)
{
P_SetIntp(sec, DMU_FLOOR_FLAGS, Reader_ReadInt16(reader));
P_SetIntp(sec, DMU_CEILING_FLAGS, Reader_ReadInt16(reader));
}
byte lightlevel;
#if __JHEXEN__
lightlevel = (byte) Reader_ReadInt16(reader);
#else
// In Ver1 the light level is a short
if(mapVersion == 1)
{
lightlevel = (byte) Reader_ReadInt16(reader);
}
else
{
lightlevel = Reader_ReadByte(reader);
}
#endif
P_SetFloatp(sec, DMU_LIGHT_LEVEL, (float) lightlevel / 255.f);
#if !__JHEXEN__
if(mapVersion > 1)
#endif
{
byte rgb[3];
Reader_Read(reader, rgb, 3);
for(int i = 0; i < 3; ++i)
P_SetFloatp(sec, DMU_COLOR_RED + i, rgb[i] / 255.f);
}
// Ver 2 includes surface colours
if(ver >= 2)
{
byte rgb[3];
Reader_Read(reader, rgb, 3);
for(int i = 0; i < 3; ++i)
P_SetFloatp(sec, DMU_FLOOR_COLOR_RED + i, rgb[i] / 255.f);
Reader_Read(reader, rgb, 3);
for(int i = 0; i < 3; ++i)
P_SetFloatp(sec, DMU_CEILING_COLOR_RED + i, rgb[i] / 255.f);
}
xsec->special = Reader_ReadInt16(reader);
/*xsec->tag =*/ Reader_ReadInt16(reader);
#if __JHEXEN__
xsec->seqType = seqtype_t(Reader_ReadInt16(reader));
#endif
if(type == sc_ploff
#if !__JHEXEN__
|| type == sc_xg1
#endif
)
{
P_SetFloatp(sec, DMU_FLOOR_MATERIAL_OFFSET_X, Reader_ReadFloat(reader));
P_SetFloatp(sec, DMU_FLOOR_MATERIAL_OFFSET_Y, Reader_ReadFloat(reader));
P_SetFloatp(sec, DMU_CEILING_MATERIAL_OFFSET_X, Reader_ReadFloat(reader));
P_SetFloatp(sec, DMU_CEILING_MATERIAL_OFFSET_Y, Reader_ReadFloat(reader));
}
#if !__JHEXEN__
if(type == sc_xg1)
{
SV_ReadXGSector(sec, reader, mapVersion);
}
#endif
#if !__JHEXEN__
if(mapVersion <= 1)
#endif
{
xsec->specialData = 0;
}
// We'll restore the sound targets latter on
xsec->soundTarget = 0;
}
void SV_WriteLine(Line *li, MapStateWriter *msw)
{
xline_t *xli = P_ToXLine(li);
Writer *writer = msw->writer();
#if !__JHEXEN__
Writer_WriteByte(writer, xli->xg? 1 : 0); /// @c 1= XG data will follow.
#else
Writer_WriteByte(writer, 0);
#endif
// Version.
// 2: Per surface texture offsets.
// 2: Surface colors.
// 3: "Mapped by player" values.
// 3: Surface flags.
// 4: Engine-side line flags.
Writer_WriteByte(writer, 4); // Write a version byte
Writer_WriteInt16(writer, P_GetIntp(li, DMU_FLAGS));
Writer_WriteInt16(writer, xli->flags);
for(int i = 0; i < MAXPLAYERS; ++i)
Writer_WriteByte(writer, xli->mapped[i]);
#if __JHEXEN__
Writer_WriteByte(writer, xli->special);
Writer_WriteByte(writer, xli->arg1);
Writer_WriteByte(writer, xli->arg2);
Writer_WriteByte(writer, xli->arg3);
Writer_WriteByte(writer, xli->arg4);
Writer_WriteByte(writer, xli->arg5);
#else
Writer_WriteInt16(writer, xli->special);
Writer_WriteInt16(writer, xli->tag);
#endif
// For each side
float rgba[4];
for(int i = 0; i < 2; ++i)
{
Side *si = (Side *)P_GetPtrp(li, (i? DMU_BACK:DMU_FRONT));
if(!si) continue;
Writer_WriteInt16(writer, P_GetIntp(si, DMU_TOP_MATERIAL_OFFSET_X));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_TOP_MATERIAL_OFFSET_Y));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_MIDDLE_MATERIAL_OFFSET_X));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_MIDDLE_MATERIAL_OFFSET_Y));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_BOTTOM_MATERIAL_OFFSET_X));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_BOTTOM_MATERIAL_OFFSET_Y));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_TOP_FLAGS));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_MIDDLE_FLAGS));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_BOTTOM_FLAGS));
Writer_WriteInt16(writer, msw->serialIdFor((Material *)P_GetPtrp(si, DMU_TOP_MATERIAL)));
Writer_WriteInt16(writer, msw->serialIdFor((Material *)P_GetPtrp(si, DMU_BOTTOM_MATERIAL)));
Writer_WriteInt16(writer, msw->serialIdFor((Material *)P_GetPtrp(si, DMU_MIDDLE_MATERIAL)));
P_GetFloatpv(si, DMU_TOP_COLOR, rgba);
for(int k = 0; k < 3; ++k)
Writer_WriteByte(writer, (byte)(255 * rgba[k]));
P_GetFloatpv(si, DMU_BOTTOM_COLOR, rgba);
for(int k = 0; k < 3; ++k)
Writer_WriteByte(writer, (byte)(255 * rgba[k]));
P_GetFloatpv(si, DMU_MIDDLE_COLOR, rgba);
for(int k = 0; k < 4; ++k)
Writer_WriteByte(writer, (byte)(255 * rgba[k]));
Writer_WriteInt32(writer, P_GetIntp(si, DMU_MIDDLE_BLENDMODE));
Writer_WriteInt16(writer, P_GetIntp(si, DMU_FLAGS));
}
#if !__JHEXEN__
// Extended General?
if(xli->xg)
{
SV_WriteXGLine(li, msw);
}
#endif
}
/**
* Reads all versions of archived lines.
* Including the old Ver1.
*/
void SV_ReadLine(Line *li, MapStateReader *msr)
{
xline_t *xli = P_ToXLine(li);
Reader *reader = msr->reader();
int mapVersion = msr->mapVersion();
bool xgDataFollows = false;
#if __JHEXEN__
if(mapVersion >= 4)
#else
if(mapVersion >= 2)
#endif
{
xgDataFollows = Reader_ReadByte(reader) == 1;
}
#ifdef __JHEXEN__
DENG_UNUSED(xgDataFollows);
#endif
// A version byte?
int ver = 1;
#if __JHEXEN__
if(mapVersion >= 3)
#else
if(mapVersion >= 5)
#endif
{
ver = (int) Reader_ReadByte(reader);
}
if(ver >= 4)
{
P_SetIntp(li, DMU_FLAGS, Reader_ReadInt16(reader));
}
int flags = Reader_ReadInt16(reader);
if(xli->flags & ML_TWOSIDED)
{
flags |= ML_TWOSIDED;
}
if(ver < 4)
{
// Translate old line flags.
int ddLineFlags = 0;
if(flags & 0x0001) // old ML_BLOCKING flag
{
ddLineFlags |= DDLF_BLOCKING;
flags &= ~0x0001;
}
if(flags & 0x0008) // old ML_DONTPEGTOP flag
{
ddLineFlags |= DDLF_DONTPEGTOP;
flags &= ~0x0008;
}
if(flags & 0x0010) // old ML_DONTPEGBOTTOM flag
{
ddLineFlags |= DDLF_DONTPEGBOTTOM;
flags &= ~0x0010;
}
P_SetIntp(li, DMU_FLAGS, ddLineFlags);
}
if(ver < 3)
{
if(flags & ML_MAPPED)
{
int lineIDX = P_ToIndex(li);
// Set line as having been seen by all players..
de::zap(xli->mapped);
for(int i = 0; i < MAXPLAYERS; ++i)
{
P_SetLineAutomapVisibility(i, lineIDX, true);
}
}
}
xli->flags = flags;
if(ver >= 3)
{
for(int i = 0; i < MAXPLAYERS; ++i)
{
xli->mapped[i] = Reader_ReadByte(reader);
}
}
#if __JHEXEN__
xli->special = Reader_ReadByte(reader);
xli->arg1 = Reader_ReadByte(reader);
xli->arg2 = Reader_ReadByte(reader);
xli->arg3 = Reader_ReadByte(reader);
xli->arg4 = Reader_ReadByte(reader);
xli->arg5 = Reader_ReadByte(reader);
#else
xli->special = Reader_ReadInt16(reader);
/*xli->tag =*/ Reader_ReadInt16(reader);
#endif
// For each side
for(int i = 0; i < 2; ++i)
{
Side *si = (Side *)P_GetPtrp(li, (i? DMU_BACK:DMU_FRONT));
if(!si) continue;
// Versions latter than 2 store per surface texture offsets.
if(ver >= 2)
{
float offset[2];
offset[VX] = (float) Reader_ReadInt16(reader);
offset[VY] = (float) Reader_ReadInt16(reader);
P_SetFloatpv(si, DMU_TOP_MATERIAL_OFFSET_XY, offset);
offset[VX] = (float) Reader_ReadInt16(reader);
offset[VY] = (float) Reader_ReadInt16(reader);
P_SetFloatpv(si, DMU_MIDDLE_MATERIAL_OFFSET_XY, offset);
offset[VX] = (float) Reader_ReadInt16(reader);
offset[VY] = (float) Reader_ReadInt16(reader);
P_SetFloatpv(si, DMU_BOTTOM_MATERIAL_OFFSET_XY, offset);
}
else
{
float offset[2];
offset[VX] = (float) Reader_ReadInt16(reader);
offset[VY] = (float) Reader_ReadInt16(reader);
P_SetFloatpv(si, DMU_TOP_MATERIAL_OFFSET_XY, offset);
P_SetFloatpv(si, DMU_MIDDLE_MATERIAL_OFFSET_XY, offset);
P_SetFloatpv(si, DMU_BOTTOM_MATERIAL_OFFSET_XY, offset);
}
if(ver >= 3)
{
P_SetIntp(si, DMU_TOP_FLAGS, Reader_ReadInt16(reader));
P_SetIntp(si, DMU_MIDDLE_FLAGS, Reader_ReadInt16(reader));
P_SetIntp(si, DMU_BOTTOM_FLAGS, Reader_ReadInt16(reader));
}
Material *topMaterial = 0, *bottomMaterial = 0, *middleMaterial = 0;
#if !__JHEXEN__
if(mapVersion >= 4)
#endif
{
topMaterial = msr->material(Reader_ReadInt16(reader), 1);
bottomMaterial = msr->material(Reader_ReadInt16(reader), 1);
middleMaterial = msr->material(Reader_ReadInt16(reader), 1);
}
P_SetPtrp(si, DMU_TOP_MATERIAL, topMaterial);
P_SetPtrp(si, DMU_BOTTOM_MATERIAL, bottomMaterial);
P_SetPtrp(si, DMU_MIDDLE_MATERIAL, middleMaterial);
// Ver2 includes surface colours
if(ver >= 2)
{
float rgba[4];
int flags;
for(int k = 0; k < 3; ++k)
rgba[k] = (float) Reader_ReadByte(reader) / 255.f;
rgba[3] = 1;
P_SetFloatpv(si, DMU_TOP_COLOR, rgba);
for(int k = 0; k < 3; ++k)
rgba[k] = (float) Reader_ReadByte(reader) / 255.f;
rgba[3] = 1;
P_SetFloatpv(si, DMU_BOTTOM_COLOR, rgba);
for(int k = 0; k < 4; ++k)
rgba[k] = (float) Reader_ReadByte(reader) / 255.f;
P_SetFloatpv(si, DMU_MIDDLE_COLOR, rgba);
P_SetIntp(si, DMU_MIDDLE_BLENDMODE, Reader_ReadInt32(reader));
flags = Reader_ReadInt16(reader);
if(mapVersion < 12)
{
if(P_GetIntp(si, DMU_FLAGS) & SDF_SUPPRESS_BACK_SECTOR)
flags |= SDF_SUPPRESS_BACK_SECTOR;
}
P_SetIntp(si, DMU_FLAGS, flags);
}
}
#if !__JHEXEN__
if(xgDataFollows)
{
SV_ReadXGLine(li, msr);
}
#endif
}
void SV_Initialize()
{
SV_InitIO();
// (Re)Initialize the saved game paths, possibly creating them if they do not exist.
SV_ConfigureSavePaths();
if(!inited)
{
// Declare the native game state reader.
gameStateReaderFactory.declareReader(&GameStateReader::recognize, &GameStateReader::make);
}
inited = true;
}
void SV_Shutdown()
{
if(!inited) return;
SV_ShutdownIO();
saveSlots->clearAllSaveInfo();
inited = false;
}
void SV_DeclareGameStateReader(GameStateRecognizeFunc recognizer, GameStateReaderMakeFunc maker)
{
DENG_ASSERT(inited);
gameStateReaderFactory.declareReader(recognizer, maker);
}
bool SV_RecognizeGameState(SaveInfo &info, Str const *path)
{
DENG_ASSERT(inited);
return gameStateReaderFactory.recognize(info, path);
}
static std::auto_ptr<IGameStateReader> gameStateReaderFor(SaveInfo &info, Str const *path)
{
std::auto_ptr<IGameStateReader> p(gameStateReaderFactory.recognizeAndMakeReader(info, path));
if(!p.get())
{
/// @throw Error The saved game state format was not recognized.
throw de::Error("gameStateReaderFor", "Unrecognized savegame format");
}
return p;
}
dd_bool SV_LoadGame(int slot)
{
DENG_ASSERT(inited);
#if __JHEXEN__
int const logicalSlot = BASE_SLOT;
#else
int const logicalSlot = slot;
#endif
if(!saveSlots->isValidSlot(slot))
{
return false;
}
App_Log(DE2_RES_VERBOSE, "Attempting load of save slot #%i...", slot);
AutoStr *path = saveSlots->composeSavePathForSlot(slot);
if(Str_IsEmpty(path))
{
App_Log(DE2_RES_ERROR, "Game not loaded: path \"%s\" is unreachable", SV_SavePath());
return false;
}
#if __JHEXEN__
// Copy all needed save files to the base slot.
/// @todo Why do this BEFORE loading?? (G_NewGame() does not load the serialized map state)
/// @todo Does any caller ever attempt to load the base slot?? (Doesn't seem logical)
if(slot != BASE_SLOT)
{
saveSlots->copySlot(slot, BASE_SLOT);
}
#endif
try
{
SaveInfo &info = saveSlots->saveInfo(logicalSlot);
// Attempt to recognize and load the saved game state.
gameStateReaderFor(info, path)->read(info, path);
// Make note of the last used save slot.
Con_SetInteger2("game-save-last-slot", slot, SVF_WRITE_OVERRIDE);
return true;
}
catch(de::Error const &er)
{
App_Log(DE2_RES_WARNING, "Error loading save slot #%i:\n%s", slot, er.asText().toLatin1().constData());
}
return false;
}
dd_bool SV_SaveGame(int slot, char const *description)
{
DENG_ASSERT(inited);
#if __JHEXEN__
int const logicalSlot = BASE_SLOT;
#else
int const logicalSlot = slot;
#endif
if(!saveSlots->isValidSlot(slot))
{
DENG_ASSERT(!"Invalid slot specified");
return false;
}
if(!description || !description[0])
{
DENG_ASSERT(!"Empty description specified for slot");
return false;
}
AutoStr *path = saveSlots->composeSavePathForSlot(logicalSlot);
if(Str_IsEmpty(path))
{
App_Log(DE2_RES_WARNING, "Cannot save game: path \"%s\" is unreachable", SV_SavePath());
return false;
}
App_Log(DE2_LOG_VERBOSE, "Attempting save game to \"%s\"", Str_Text(path));
SaveInfo *info = SaveInfo::newWithCurrentSessionMetadata(AutoStr_FromTextStd(description));
try
{
GameStateWriter().write(*info, path);
// Swap the save info.
saveSlots->replaceSaveInfo(logicalSlot, info);
#if __JHEXEN__
// Copy base slot to destination slot.
saveSlots->copySlot(logicalSlot, slot);
#endif
// Make note of the last used save slot.
Con_SetInteger2("game-save-last-slot", slot, SVF_WRITE_OVERRIDE);
return true;
}
catch(de::Error const &er)