-
Notifications
You must be signed in to change notification settings - Fork 42
/
sv_save.c
2484 lines (1996 loc) · 65.4 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
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
/*
sv_save.c - save\restore implementation
Copyright (C) 2008 Uncle Mike
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 3 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.
*/
#include "common.h"
#include "server.h"
#include "library.h"
#include "const.h"
#include "render_api.h" // decallist_t
#include "sound.h" // S_GetDynamicSounds
/*
==============================================================================
SAVE FILE
half-life implementation of saverestore system
==============================================================================
*/
#define SAVEFILE_HEADER (('V'<<24)+('L'<<16)+('A'<<8)+'V') // little-endian "VALV"
#define SAVEGAME_HEADER (('V'<<24)+('A'<<16)+('S'<<8)+'J') // little-endian "JSAV"
#define SAVEGAME_VERSION 0x0065 // Version 0.65
#define CLIENT_SAVEGAME_VERSION 0x0068 // Version 0.68
#define SAVE_AGED_COUNT 1
#define SAVENAME_LENGTH 128 // matches with MAX_OSPATH
#define LUMP_DECALS_OFFSET 0
#define LUMP_STATIC_OFFSET 1
#define LUMP_SOUNDS_OFFSET 2
#define LUMP_MUSIC_OFFSET 3
#define NUM_CLIENT_OFFSETS 4
void (__cdecl *pfnSaveGameComment)( char *buffer, int max_length ) = NULL;
typedef struct
{
int nBytesSymbols;
int nSymbols;
int nBytesDataHeaders;
int nBytesData;
} SaveFileSectionsInfo_t;
typedef struct
{
char *pSymbols;
char *pDataHeaders;
char *pData;
} SaveFileSections_t;
typedef struct
{
int offsets[NUM_CLIENT_OFFSETS];
} ClientSections_t;
typedef struct
{
char mapName[32];
char comment[80];
int mapCount;
} GAME_HEADER;
typedef struct
{
int skillLevel;
int entityCount;
int connectionCount;
int lightStyleCount;
float time;
char mapName[32];
char skyName[32];
int skyColor_r;
int skyColor_g;
int skyColor_b;
float skyVec_x;
float skyVec_y;
float skyVec_z;
int viewentity; // Xash3D added
int serverflags; // converted to float and back
float wateralpha;
float skyDir_x; // rotating sky support
float skyDir_y;
float skyDir_z;
float skyAngle;
float skySpeed;
} SAVE_HEADER;
typedef struct
{
int index;
char style[256];
float time;
} SAVE_LIGHTSTYLE;
static TYPEDESCRIPTION gGameHeader[] =
{
DEFINE_ARRAY( GAME_HEADER, mapName, FIELD_CHARACTER, 32 ),
DEFINE_ARRAY( GAME_HEADER, comment, FIELD_CHARACTER, 80 ),
DEFINE_FIELD( GAME_HEADER, mapCount, FIELD_INTEGER ),
};
static TYPEDESCRIPTION gSaveHeader[] =
{
DEFINE_FIELD( SAVE_HEADER, skillLevel, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, entityCount, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, connectionCount, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, lightStyleCount, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, time, FIELD_TIME ),
DEFINE_ARRAY( SAVE_HEADER, mapName, FIELD_CHARACTER, 32 ),
DEFINE_ARRAY( SAVE_HEADER, skyName, FIELD_CHARACTER, 32 ),
DEFINE_FIELD( SAVE_HEADER, skyColor_r, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, skyColor_g, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, skyColor_b, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, skyVec_x, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skyVec_y, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skyVec_z, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, viewentity, FIELD_SHORT ),
DEFINE_FIELD( SAVE_HEADER, serverflags, FIELD_INTEGER ),
DEFINE_FIELD( SAVE_HEADER, wateralpha, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skyDir_x, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skyDir_y, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skyDir_z, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skyAngle, FIELD_FLOAT ),
DEFINE_FIELD( SAVE_HEADER, skySpeed, FIELD_FLOAT ),
};
static TYPEDESCRIPTION gAdjacency[] =
{
DEFINE_ARRAY( LEVELLIST, mapName, FIELD_CHARACTER, 32 ),
DEFINE_ARRAY( LEVELLIST, landmarkName, FIELD_CHARACTER, 32 ),
DEFINE_FIELD( LEVELLIST, pentLandmark, FIELD_EDICT ),
DEFINE_FIELD( LEVELLIST, vecLandmarkOrigin, FIELD_VECTOR ),
};
static TYPEDESCRIPTION gLightStyle[] =
{
DEFINE_FIELD( SAVE_LIGHTSTYLE, index, FIELD_INTEGER ),
DEFINE_ARRAY( SAVE_LIGHTSTYLE, style, FIELD_CHARACTER, 256 ),
DEFINE_FIELD( SAVE_LIGHTSTYLE, time, FIELD_FLOAT ),
};
static TYPEDESCRIPTION gEntityTable[] =
{
DEFINE_FIELD( ENTITYTABLE, id, FIELD_INTEGER ),
DEFINE_FIELD( ENTITYTABLE, location, FIELD_INTEGER ),
DEFINE_FIELD( ENTITYTABLE, size, FIELD_INTEGER ),
DEFINE_FIELD( ENTITYTABLE, flags, FIELD_INTEGER ),
DEFINE_FIELD( ENTITYTABLE, classname, FIELD_STRING ),
};
static TYPEDESCRIPTION gEntvarsDescription[] =
{
DEFINE_ENTITY_FIELD( classname, FIELD_STRING ),
DEFINE_ENTITY_GLOBAL_FIELD( globalname, FIELD_STRING ),
};
int SumBytes( SaveFileSectionsInfo_t *section )
{
return ( section->nBytesSymbols + section->nBytesDataHeaders + section->nBytesData );
}
void SV_InitSaveRestore( void )
{
pfnSaveGameComment = Com_GetProcAddress( svgame.hInstance, "SV_SaveGameComment" );
}
/*
----------------------------------------------------------
SaveRestore helpers
assume pSaveData is valid
----------------------------------------------------------
*/
void SaveRestore_Init( SAVERESTOREDATA *pSaveData, void *pNewBase, int nBytes )
{
pSaveData->pCurrentData = pSaveData->pBaseData = (char *)pNewBase;
pSaveData->bufferSize = nBytes;
pSaveData->size = 0;
}
void SaveRestore_MoveCurPos( SAVERESTOREDATA *pSaveData, int nBytes )
{
pSaveData->pCurrentData += nBytes;
pSaveData->size += nBytes;
}
void SaveRestore_Rebase( SAVERESTOREDATA *pSaveData )
{
pSaveData->pBaseData = pSaveData->pCurrentData;
pSaveData->bufferSize -= pSaveData->size;
pSaveData->size = 0;
}
void SaveRestore_Rewind( SAVERESTOREDATA *pSaveData, int nBytes )
{
if( pSaveData->size < nBytes )
nBytes = pSaveData->size;
SaveRestore_MoveCurPos( pSaveData, -nBytes );
}
char *SaveRestore_GetBuffer( SAVERESTOREDATA *pSaveData )
{
return pSaveData->pBaseData;
}
int SaveRestore_BytesAvailable( SAVERESTOREDATA *pSaveData )
{
return (pSaveData->bufferSize - pSaveData->size);
}
int SaveRestore_SizeBuffer( SAVERESTOREDATA *pSaveData )
{
return pSaveData->bufferSize;
}
qboolean SaveRestore_Write( SAVERESTOREDATA *pSaveData, const void *pData, int nBytes )
{
if( nBytes > SaveRestore_BytesAvailable( pSaveData ))
{
pSaveData->size = pSaveData->bufferSize;
return false;
}
Q_memcpy( pSaveData->pCurrentData, pData, nBytes );
SaveRestore_MoveCurPos( pSaveData, nBytes );
return true;
}
qboolean SaveRestore_Read( SAVERESTOREDATA *pSaveData, void *pOutput, int nBytes )
{
if( !SaveRestore_BytesAvailable( pSaveData ))
return false;
if( nBytes > SaveRestore_BytesAvailable( pSaveData ))
{
pSaveData->size = pSaveData->bufferSize;
return false;
}
if( pOutput ) Q_memcpy( pOutput, pSaveData->pCurrentData, nBytes );
SaveRestore_MoveCurPos( pSaveData, nBytes );
return true;
}
int SaveRestore_GetCurPos( SAVERESTOREDATA *pSaveData )
{
return pSaveData->size;
}
char *SaveRestore_AccessCurPos( SAVERESTOREDATA *pSaveData )
{
return pSaveData->pCurrentData;
}
qboolean SaveRestore_Seek( SAVERESTOREDATA *pSaveData, int absPosition )
{
if( absPosition < 0 || absPosition >= pSaveData->bufferSize )
return false;
pSaveData->size = absPosition;
pSaveData->pCurrentData = pSaveData->pBaseData + pSaveData->size;
return true;
}
void SaveRestore_InitEntityTable( SAVERESTOREDATA *pSaveData, ENTITYTABLE *pNewTable, int entityCount )
{
ENTITYTABLE *pTable;
int i;
ASSERT( pSaveData->pTable == NULL );
pSaveData->tableCount = entityCount;
pSaveData->pTable = pNewTable;
// setup entitytable
for( i = 0; i < entityCount; i++ )
{
pTable = &pSaveData->pTable[i];
pTable->pent = EDICT_NUM( i );
}
}
ENTITYTABLE *SaveRestore_DetachEntityTable( SAVERESTOREDATA *pSaveData )
{
ENTITYTABLE *pReturn = pSaveData->pTable;
pSaveData->pTable = NULL;
pSaveData->tableCount = 0;
return pReturn;
}
void SaveRestore_InitSymbolTable( SAVERESTOREDATA *pSaveData, char **pNewTokens, int sizeTable )
{
ASSERT( pSaveData->pTokens == NULL );
pSaveData->tokenCount = sizeTable;
pSaveData->pTokens = pNewTokens;
}
char **SaveRestore_DetachSymbolTable( SAVERESTOREDATA *pSaveData )
{
char **pResult = pSaveData->pTokens;
pSaveData->tokenCount = 0;
pSaveData->pTokens = NULL;
return pResult;
}
qboolean SaveRestore_DefineSymbol( SAVERESTOREDATA *pSaveData, const char *pszToken, int token )
{
if( pSaveData->pTokens[token] == NULL )
{
pSaveData->pTokens[token] = (char *)pszToken;
return true;
}
ASSERT( 0 );
return false;
}
const char *SaveRestore_StringFromSymbol( SAVERESTOREDATA *pSaveData, int token )
{
if( token >= 0 && token < pSaveData->tokenCount )
return pSaveData->pTokens[token];
return "<<bad string>>";
}
void SV_BuildSaveComment( char *text, int maxlength )
{
if( pfnSaveGameComment != NULL )
{
// get save comment from gamedll
pfnSaveGameComment( text, maxlength );
}
else
{
const char *pName;
edict_t *pWorld = EDICT_NUM( 0 );
float time = sv.time;
if( pWorld && pWorld->v.message )
{
// trying to extract message from the world
pName = STRING( pWorld->v.message );
}
else
{
// or use mapname
pName = STRING( svgame.globals->mapname );
}
Q_snprintf( text, maxlength, "%-64.64s %02d:%02d", pName, (int)(time / 60.0f ), (int)fmod( time, 60.0f ));
}
}
int SV_MapCount( const char *pPath )
{
search_t *t;
int count = 0;
t = FS_Search( pPath, true, true ); // lookup only in gamedir
if( !t ) return count; // empty
count = t->numfilenames;
Mem_Free( t );
return count;
}
int EntryInTable( SAVERESTOREDATA *pSaveData, const char *pMapName, int index )
{
int i;
index++;
for( i = index; i < pSaveData->connectionCount; i++ )
{
if ( !Q_strcmp( pSaveData->levelList[i].mapName, pMapName ))
return i;
}
return -1;
}
void LandmarkOrigin( SAVERESTOREDATA *pSaveData, vec3_t output, const char *pLandmarkName )
{
int i;
for( i = 0; i < pSaveData->connectionCount; i++ )
{
if( !Q_strcmp( pSaveData->levelList[i].landmarkName, pLandmarkName ))
{
VectorCopy( pSaveData->levelList[i].vecLandmarkOrigin, output );
return;
}
}
VectorClear( output );
}
int EntityInSolid( edict_t *ent )
{
edict_t *pParent = ent->v.aiment;
vec3_t point;
// if you're attached to a client, always go through
if( SV_IsValidEdict( pParent ))
{
if( pParent->v.flags & FL_CLIENT )
return 0;
}
VectorAverage( ent->v.absmin, ent->v.absmax, point );
return (SV_PointContents( point ) == CONTENTS_SOLID);
}
void ReapplyDecal( SAVERESTOREDATA *pSaveData, decallist_t *entry, qboolean adjacent )
{
int flags = entry->flags;
int decalIndex, entityIndex = 0;
int modelIndex = 0;
edict_t *pEdict;
// never transition permanent decals
if( adjacent && ( flags & FDECAL_PERMANENT ))
return;
if( entry->entityIndex < 0 || entry->entityIndex > pSaveData->tableCount )
return;
// restore entity and model index
pEdict = pSaveData->pTable[entry->entityIndex].pent;
if( SV_IsValidEdict( pEdict ) )
{
modelIndex = pEdict->v.modelindex;
entityIndex = NUM_FOR_EDICT( pEdict );
}
if( SV_RestoreCustomDecal( entry, pEdict, adjacent ))
return; // decal was sucessfully restored at the game-side
// NOTE: at this point all decal indexes is valid
decalIndex = pfnDecalIndex( entry->name );
if( flags & FDECAL_STUDIO )
{
// NOTE: studio decal trace start saved into impactPlaneNormal
SV_CreateStudioDecal( &sv.signon, entry->position, entry->impactPlaneNormal, decalIndex, entityIndex, modelIndex, flags, &entry->studio_state );
}
else if( adjacent && entityIndex != 0 && !SV_IsValidEdict( pEdict ))
{
vec3_t testspot, testend;
trace_t tr;
// these entities might not exist over transitions,
// so we'll use the saved plane and do a traceline instead
flags |= FDECAL_DONTSAVE;
MsgDev( D_ERROR, "couldn't restore entity index %i, do trace for decal\n", entityIndex );
VectorCopy( entry->position, testspot );
VectorMA( testspot, 5.0f, entry->impactPlaneNormal, testspot );
VectorCopy( entry->position, testend );
VectorMA( testend, -5.0f, entry->impactPlaneNormal, testend );
tr = SV_Move( testspot, vec3_origin, vec3_origin, testend, MOVE_NOMONSTERS, NULL );
// NOTE: this code may does wrong result on moving brushes e.g. func_tracktrain
if( tr.fraction != 1.0f && !tr.allsolid )
{
// check impact plane normal
float dot = DotProduct( entry->impactPlaneNormal, tr.plane.normal );
if( dot >= 0.95f )
{
entityIndex = pfnIndexOfEdict( tr.ent );
if( entityIndex > 0 ) modelIndex = tr.ent->v.modelindex;
SV_CreateDecal( &sv.signon, tr.endpos, decalIndex, entityIndex, modelIndex, flags, entry->scale );
}
}
}
else
{
// global entity is exist on new level so we can apply decal in local space
// NOTE: this case also used for transition world decals
SV_CreateDecal( &sv.signon, entry->position, decalIndex, entityIndex, modelIndex, flags, entry->scale );
}
}
void RestoreSound( soundlist_t *entry )
{
int soundIndex;
int flags = 0;
edict_t *ent;
// this can happens if serialized map contain 4096 static decals...
if(( BF_GetNumBytesWritten( &sv.signon ) + 20 ) >= BF_GetMaxBytes( &sv.signon ))
return;
if( entry->name[0] == '!' && Q_isdigit( entry->name + 1 ))
{
flags |= SND_SENTENCE;
soundIndex = Q_atoi( entry->name + 1 );
}
else if( entry->name[0] == '#' && Q_isdigit( entry->name + 1 ))
{
flags |= SND_SENTENCE;
soundIndex = Q_atoi( entry->name + 1 ) + 1536;
}
else
{
// precache_sound can be used twice: cache sounds when loading
// and return sound index when server is active
soundIndex = SV_SoundIndex( entry->name );
}
ent = EDICT_NUM( entry->entnum );
if( entry->attenuation < 0.0f || entry->attenuation > 4.0f )
{
MsgDev( D_ERROR, "SV_RestoreSound: attenuation %g must be in range 0-4\n", entry->attenuation );
return;
}
if( entry->channel > 7 )
{
MsgDev( D_ERROR, "SV_RestoreSound: channel must be in range 0-7\n" );
return;
}
if( !SV_IsValidEdict( ent ))
{
MsgDev( D_ERROR, "SV_RestoreSound: edict == NULL\n" );
return;
}
if( entry->volume != VOL_NORM ) flags |= SND_VOLUME;
if( entry->attenuation != ATTN_NONE ) flags |= SND_ATTENUATION;
if( entry->pitch != PITCH_NORM ) flags |= SND_PITCH;
if( !entry->looping ) flags |= SND_STOP_LOOPING; // just in case
if( soundIndex > 255 ) flags |= SND_LARGE_INDEX;
BF_WriteByte( &sv.signon, svc_restoresound );
BF_WriteWord( &sv.signon, flags );
if( flags & SND_LARGE_INDEX )
BF_WriteWord( &sv.signon, soundIndex );
else BF_WriteByte( &sv.signon, soundIndex );
BF_WriteByte( &sv.signon, entry->channel );
if( flags & SND_VOLUME ) BF_WriteByte( &sv.signon, entry->volume * 255 );
if( flags & SND_ATTENUATION ) BF_WriteByte( &sv.signon, entry->attenuation * 64 );
if( flags & SND_PITCH ) BF_WriteByte( &sv.signon, entry->pitch );
BF_WriteWord( &sv.signon, entry->entnum );
BF_WriteVec3Coord( &sv.signon, entry->origin );
BF_WriteByte( &sv.signon, entry->wordIndex );
// send two doubles as raw-data
BF_WriteBytes( &sv.signon, &entry->samplePos, sizeof( entry->samplePos ));
BF_WriteBytes( &sv.signon, &entry->forcedEnd, sizeof( entry->forcedEnd ));
}
void SV_ClearSaveDir( void )
{
search_t *t;
int i;
// just delete all HL? files
t = FS_Search( "save/*.hl?", true, true ); // lookup only in gamedir
if( !t ) return; // already empty
for( i = 0; i < t->numfilenames; i++ )
FS_Delete( t->filenames[i] );
Mem_Free( t );
}
int SV_IsValidSave( void )
{
if( sv.background )
return 0;
// don't parse autosave/transition save/restores during playback!
if( CL_IsPlaybackDemo( ))
return 0;
if( !svs.initialized || sv.state != ss_active )
{
Msg( "Not playing a local game.\n" );
return 0;
}
if( svgame.physFuncs.SV_AllowSaveGame != NULL )
{
if( !svgame.physFuncs.SV_AllowSaveGame( ))
{
Msg( "Savegame is not allowed.\n" );
return 0;
}
}
if( !Host_IsDedicated() )
{
// Enable save/load in xashds
if( !CL_Active( ))
{
Msg( "Can't save if not active.\n" );
return 0;
}
if( CL_IsIntermission( ))
{
Msg( "Can't save during intermission.\n" );
return 0;
}
if( sv_maxclients->integer != 1 )
{
Msg( "Can't save multiplayer games.\n" );
return 0;
}
}
if( svs.clients && svs.clients[0].state == cs_spawned )
{
edict_t *pl = svs.clients[0].edict;
if( !pl )
{
Msg( "Can't savegame without a player!\n" );
return 0;
}
if( pl->v.deadflag != false || pl->v.health <= 0.0f )
{
Msg( "Can't savegame with a dead player.\n" );
return 0;
}
// Passed all checks, it's ok to save
return 1;
}
Msg( "Can't savegame without a client!\n" );
return 0;
}
void SV_AgeSaveList( const char *pName, int count )
{
string newName, oldName, newImage, oldImage;
// delete last quick/autosave (e.g. quick05.sav)
Q_snprintf( newName, sizeof( newName ), "save/%s%02d.sav", pName, count );
Q_snprintf( newImage, sizeof( newImage ), "save/%s%02d.bmp", pName, count );
// only delete from game directory, basedir is read-only
FS_Delete( newName );
FS_Delete( newImage );
GL_FreeImage( newImage );
while( count > 0 )
{
if( count == 1 )
{
// quick.sav
Q_snprintf( oldName, sizeof( oldName ), "save/%s.sav", pName );
Q_snprintf( oldImage, sizeof( oldImage ), "save/%s.bmp", pName );
}
else
{
// quick04.sav, etc.
Q_snprintf( oldName, sizeof( oldName ), "save/%s%02d.sav", pName, count - 1 );
Q_snprintf( oldImage, sizeof( oldImage ), "save/%s%02d.bmp", pName, count - 1 );
}
Q_snprintf( newName, sizeof( newName ), "save/%s%02d.sav", pName, count );
Q_snprintf( newImage, sizeof( newImage ), "save/%s%02d.bmp", pName, count );
GL_FreeImage( oldImage );
// scroll the name list down (rename quick04.sav to quick05.sav)
FS_Rename( oldName, newName );
FS_Rename( oldImage, newImage );
count--;
}
}
void SV_DirectoryCopy( const char *pPath, file_t *pFile )
{
search_t *t;
file_t *pCopy;
int i, fileSize;
char szName[SAVENAME_LENGTH];
t = FS_Search( pPath, true, true );
if( !t ) return;
for( i = 0; i < t->numfilenames; i++ )
{
Q_memset( szName, 0, SAVENAME_LENGTH );
fileSize = FS_FileSize( t->filenames[i], true );
pCopy = FS_Open( t->filenames[i], "rb", true );
// filename can only be as long as a map name + extension
Q_strncpy( szName, FS_FileWithoutPath( t->filenames[i] ), SAVENAME_LENGTH );
FS_Write( pFile, szName, SAVENAME_LENGTH );
FS_Write( pFile, &fileSize, sizeof( int ));
FS_FileCopy( pFile, pCopy, fileSize );
FS_Close( pCopy );
}
Mem_Free( t );
}
void SV_DirectoryExtract( file_t *pFile, int fileCount )
{
char szName[SAVENAME_LENGTH], fileName[SAVENAME_LENGTH];
int i, fileSize;
file_t *pCopy;
for( i = 0; i < fileCount; i++ )
{
// filename can only be as long as a map name + extension
FS_Read( pFile, fileName, SAVENAME_LENGTH );
FS_Read( pFile, &fileSize, sizeof( int ));
Q_snprintf( szName, sizeof( szName ), "save/%s", fileName );
pCopy = FS_Open( szName, "wb", true );
FS_FileCopy( pCopy, pFile, fileSize );
FS_Close( pCopy );
}
}
void SV_SaveFinish( SAVERESTOREDATA *pSaveData )
{
char **pTokens;
ENTITYTABLE *pEntityTable;
pTokens = SaveRestore_DetachSymbolTable( pSaveData );
if( pTokens ) Mem_Free( pTokens );
pEntityTable = SaveRestore_DetachEntityTable( pSaveData );
if( pEntityTable ) Mem_Free( pEntityTable );
if( pSaveData ) Mem_Free( pSaveData );
svgame.globals->pSaveData = NULL;
}
SAVERESTOREDATA *SV_SaveInit( int size )
{
SAVERESTOREDATA *pSaveData;
const int nTokens = 0xfff; // Assume a maximum of 4K-1 symbol table entries(each of some length)
int numents;
if( size <= 0 ) size = 0x200000; // Reserve 2Mb for now
numents = svgame.numEntities;
pSaveData = Mem_Alloc( host.mempool, sizeof(SAVERESTOREDATA) + ( sizeof(ENTITYTABLE) * numents ) + size );
SaveRestore_Init( pSaveData, (char *)(pSaveData + 1), size ); // skip the save structure
SaveRestore_InitSymbolTable( pSaveData, (char **)Mem_Alloc( host.mempool, nTokens * sizeof( char* )), nTokens );
pSaveData->time = svgame.globals->time; // Use DLL time
VectorClear( pSaveData->vecLandmarkOffset );
pSaveData->fUseLandmark = false;
pSaveData->connectionCount = 0;
// shared with dlls
svgame.globals->pSaveData = pSaveData;
return pSaveData;
}
void SV_SaveGameStateGlobals( SAVERESTOREDATA *pSaveData )
{
sv_client_t *cl;
SAVE_HEADER header;
SAVE_LIGHTSTYLE light;
int i;
Q_memset( &header, 0, sizeof( SAVE_HEADER ) );
Q_memset( &light, 0, sizeof( SAVE_LIGHTSTYLE ) );
// write global data
header.skillLevel = Cvar_VariableValue( "skill" ); // This is created from an int even though it's a float
header.connectionCount = pSaveData->connectionCount;
header.time = svgame.globals->time;
if( sv_skyname->string[0] )
Q_strncpy( header.skyName, sv_skyname->string, sizeof( header.skyName ));
else Q_strncpy( header.skyName, "", sizeof( header.skyName ));
Q_strncpy( header.mapName, sv.name, sizeof( header.mapName ));
header.lightStyleCount = 0;
header.entityCount = svgame.numEntities;
for( i = 0; i < MAX_LIGHTSTYLES; i++ )
{
if( sv.lightstyles[i].pattern[0] )
header.lightStyleCount++;
}
// sky variables
header.skyColor_r = Cvar_VariableValue( "sv_skycolor_r" );
header.skyColor_g = Cvar_VariableValue( "sv_skycolor_g" );
header.skyColor_b = Cvar_VariableValue( "sv_skycolor_b" );
header.skyVec_x = Cvar_VariableValue( "sv_skyvec_x" );
header.skyVec_y = Cvar_VariableValue( "sv_skyvec_y" );
header.skyVec_z = Cvar_VariableValue( "sv_skyvec_z" );
header.skyDir_x = Cvar_VariableValue( "sv_skydir_x" );
header.skyDir_y = Cvar_VariableValue( "sv_skydir_y" );
header.skyDir_z = Cvar_VariableValue( "sv_skydir_z" );
header.skyAngle = Cvar_VariableValue( "sv_skyangle" );
header.skySpeed = Cvar_VariableValue( "sv_skyspeed" );
// save viewentity to allow camera works after save\restore
if(( cl = SV_ClientFromEdict( EDICT_NUM( 1 ), true )) != NULL )
{
if( cl->pViewEntity )
header.viewentity = NUM_FOR_EDICT( cl->pViewEntity );
else header.viewentity = 1;
}
else header.viewentity = 1;
header.serverflags = (int)svgame.globals->serverflags;
header.wateralpha = Cvar_VariableValue( "sv_wateralpha" );
pSaveData->time = 0.0f; // prohibits rebase of header.time (why not just save time as a field_float and ditch this hack?)
svgame.dllFuncs.pfnSaveWriteFields( pSaveData, "Save Header", &header, gSaveHeader, ARRAYSIZE( gSaveHeader ));
pSaveData->time = header.time;
// write entity table
for( i = 0; i < pSaveData->tableCount; i++ )
svgame.dllFuncs.pfnSaveWriteFields( pSaveData, "ETABLE", pSaveData->pTable + i, gEntityTable, ARRAYSIZE( gEntityTable ));
// write adjacency list
for( i = 0; i < pSaveData->connectionCount; i++ )
svgame.dllFuncs.pfnSaveWriteFields( pSaveData, "ADJACENCY", pSaveData->levelList + i, gAdjacency, ARRAYSIZE( gAdjacency ));
// write the lightstyles
for( i = 0; i < MAX_LIGHTSTYLES; i++ )
{
if( sv.lightstyles[i].pattern[0] )
{
light.index = i;
light.time = sv.lightstyles[i].time;
Q_strncpy( light.style, sv.lightstyles[i].pattern, sizeof( light.style ));
svgame.dllFuncs.pfnSaveWriteFields( pSaveData, "LIGHTSTYLE", &light, gLightStyle, ARRAYSIZE( gLightStyle ));
}
}
}
SAVERESTOREDATA *SV_LoadSaveData( const char *level )
{
string name;
file_t *pFile;
SaveFileSectionsInfo_t sectionsInfo;
SAVERESTOREDATA *pSaveData;
char *pszTokenList;
int i, id, size, version;
Q_snprintf( name, sizeof( name ), "save/%s.hl1", level );
MsgDev( D_INFO, "Loading game from %s...\n", name );
pFile = FS_Open( name, "rb", true );
if( !pFile )
{
MsgDev( D_INFO, "ERROR: couldn't open.\n" );
return NULL;
}
// Read the header
FS_Read( pFile, &id, sizeof( int ));
FS_Read( pFile, &version, sizeof( int ));
// is this a valid save?
if( id != SAVEFILE_HEADER || version != SAVEGAME_VERSION )
{
FS_Close( pFile );
return NULL;
}
// Read the sections info and the data
FS_Read( pFile, §ionsInfo, sizeof( sectionsInfo ));
pSaveData = Mem_Alloc( host.mempool, sizeof(SAVERESTOREDATA) + SumBytes( §ionsInfo ));
Q_strncpy( pSaveData->szCurrentMapName, level, sizeof( pSaveData->szCurrentMapName ));
FS_Read( pFile, (char *)(pSaveData + 1), SumBytes( §ionsInfo ));
FS_Close( pFile );
// Parse the symbol table
pszTokenList = (char *)(pSaveData + 1); // Skip past the CSaveRestoreData structure
if( sectionsInfo.nBytesSymbols > 0 )
{
SaveRestore_InitSymbolTable( pSaveData, (char **)Mem_Alloc( host.mempool, sectionsInfo.nSymbols * sizeof( char* )), sectionsInfo.nSymbols );
// make sure the token strings pointed to by the pToken hashtable.
for( i = 0; i < sectionsInfo.nSymbols; i++ )
{
if( *pszTokenList )
{
ASSERT( SaveRestore_DefineSymbol( pSaveData, pszTokenList, i ));
}
// find next token (after next null)
while( *pszTokenList++ );
}
}
else
{
SaveRestore_InitSymbolTable( pSaveData, NULL, 0 );
}
ASSERT( pszTokenList - (char *)(pSaveData + 1) == sectionsInfo.nBytesSymbols );
// set up the restore basis
size = SumBytes( §ionsInfo ) - sectionsInfo.nBytesSymbols;
// the point pszTokenList was incremented to the end of the tokens
SaveRestore_Init( pSaveData, (char *)(pszTokenList), size );
pSaveData->connectionCount = 0;
pSaveData->fUseLandmark = true;
pSaveData->time = 0.0f;
VectorClear( pSaveData->vecLandmarkOffset );
// shared with dlls
svgame.globals->pSaveData = pSaveData;
return pSaveData;
}
void SV_ReadEntityTable( SAVERESTOREDATA *pSaveData )
{
ENTITYTABLE *pEntityTable;
int i;
pEntityTable = (ENTITYTABLE *)Mem_Alloc( host.mempool, sizeof( ENTITYTABLE ) * pSaveData->tableCount );
SaveRestore_InitEntityTable( pSaveData, pEntityTable, pSaveData->tableCount );
for( i = 0; i < pSaveData->tableCount; i++ )
svgame.dllFuncs.pfnSaveReadFields( pSaveData, "ETABLE", pSaveData->pTable + i, gEntityTable, ARRAYSIZE( gEntityTable ));
}
void SV_ParseSaveTables( SAVERESTOREDATA *pSaveData, SAVE_HEADER *pHeader, int setupLightstyles )
{
SAVE_LIGHTSTYLE light;
int i;
// process SAVE_HEADER
svgame.dllFuncs.pfnSaveReadFields( pSaveData, "Save Header", pHeader, gSaveHeader, ARRAYSIZE( gSaveHeader ));
pSaveData->connectionCount = pHeader->connectionCount;
pSaveData->time = pHeader->time;
pSaveData->fUseLandmark = true;
VectorClear( pSaveData->vecLandmarkOffset );
pSaveData->tableCount = pHeader->entityCount;
SV_ReadEntityTable( pSaveData );
// read adjacency list
for( i = 0; i < pSaveData->connectionCount; i++ )
{
LEVELLIST *pList = &pSaveData->levelList[i];
svgame.dllFuncs.pfnSaveReadFields( pSaveData, "ADJACENCY", pList, gAdjacency, ARRAYSIZE( gAdjacency ));
}
if( setupLightstyles )
Q_memset( sv.lightstyles, 0, sizeof( sv.lightstyles ));
for( i = 0; i < pHeader->lightStyleCount; i++ )
{
svgame.dllFuncs.pfnSaveReadFields( pSaveData, "LIGHTSTYLE", &light, gLightStyle, ARRAYSIZE( gLightStyle ));
if( setupLightstyles ) SV_SetLightStyle( light.index, light.style, light.time );
}
}