forked from ForsakenX/forsaken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadsave.c
executable file
·662 lines (541 loc) · 15 KB
/
loadsave.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
// TODO: this code loads pointers !!!!!!!!!!
// apparrently this is somehow possible if you save/load from save binary
/*===================================================================
Include Files...
===================================================================*/
#define SAVEGAME_SLOTS
#include <stdio.h>
#include <stdarg.h>
#include <sys/stat.h>
#include "main.h"
#include "new3d.h"
#include "quat.h"
#include "main.h"
#include "compobjects.h"
#include "bgobjects.h"
#include "object.h"
#include "networking.h"
#include "file.h"
#include "triggers.h"
#include "pickups.h"
#include "enemies.h"
#include "mload.h"
#include "ships.h"
#include "title.h"
#include "2dpolys.h"
#include "polys.h"
#include "screenpolys.h"
#include "primary.h"
#include "secondary.h"
#include "spotfx.h"
#include "pickups.h"
#include "loadsave.h"
#include "rtlight.h"
#include "teleport.h"
#include "extforce.h"
#include "camera.h"
#include "restart.h"
#include "sfx.h"
#include "text.h"
#include "file.h"
#include "util.h"
#include "timer.h"
#include "oct2.h"
/* Stuff that needs to be saved/loaded.....
BackGroundObjects... 1 Dave ( DONE )
Triggers... 2 Olly ( DONE )
Cameras... 3 Dan ( DONE )
RealTimeLights... 4 Olly ( DONE )
XLights 5 Dave ( DONE )
Current Time... 6 Dan ( DONE )
Pickups... 7 Dave ( DONE )
Restart Points... 8 Dave ( DONE )
Start Points... 9 Dave ( DONE )
Enemies... 10 Dan
Trigger Areas... 11 Dan ( DONE )
External Forces... 12 Dan ( DONE )
Teleports... 13 Dan ( DONE )
SpotFx... 14 Dave ( DONE )
Primary Bullets... 15 Dave ( DONE )
Secondary Bullets... 16 Dave ( DONE )
Models ?? 17 Dave ( DONE )
Ships 18 Dan
ScreenPolys 19 Dave ( DONE )
2dPolys 20 Dave ( DONE )
2dTextures 21 Dave ( DONE )
SFX 22 Phil
Temperature.. 23 Dave ( DONE )
TextureAnims 24 Olly
*/
/*===================================================================
Defines
===================================================================*/
#define MAX_SAVEGAME_FILENAME 128
bool SaveSnapShot( int8_t * Filename );
/*===================================================================
Externals ...
===================================================================*/
extern bool Cheated;
extern char biker_name[];
extern float framelag;
extern int CrystalsFound;
extern int16_t Lives;
extern int16_t LevelNum;
extern char MissionTextNames[MAXLEVELS][128];
extern float LevelTimeTaken;
extern int16_t NumInitEnemies;
extern int16_t NumKilledEnemies;
extern LIST LoadSavedGameList;
/*===================================================================
Globals ...
===================================================================*/
char *SaveGameFileName( int slot )
{
static char name[ MAX_SAVEGAME_FILENAME ];
sprintf( name, SAVEGAME_FOLDER "\\save%02d" SAVEGAME_EXTENSION, slot + 1 );
strtoupper( name );
return name;
}
char *SaveGamePicFileName( int slot )
{
static char name[ MAX_SAVEGAME_FILENAME ];
sprintf( name, SAVEGAME_FOLDER "\\save%02d" SAVEGAMEPIC_EXTENSION, slot + 1 );
strtoupper( name );
return name;
}
/*===================================================================
Procedure : PreInGame Load
Input : MENUITEM * MenuItem
Output : nothing
===================================================================*/
int8_t LoadGameFilename[ 256 ];
extern int16_t NumLevels;
bool PreInGameLoad( MENUITEM * MenuItem )
{
FILE * fp;
int16_t i;
int8_t buf[ 256 ];
u_int32_t MagicNumber;
u_int32_t VersionNumber;
#ifdef SAVEGAME_SLOTS
fp = file_open( SaveGameFileName( LoadSavedGameList.selected_item ), "rb" );
#else
sprintf( &LoadGameFilename[ 0 ], "savegame\\%s", &LoadSavedGameList.item[ LoadSavedGameList.selected_item ][ 0 ] );
fp = file_open( &LoadGameFilename[ 0 ], "rb" );
#endif
if( fp != NULL )
{
fread( &MagicNumber, sizeof( u_int32_t ), 1, fp );
fread( &VersionNumber, sizeof( u_int32_t ), 1, fp );
if( ( MagicNumber != MAGIC_NUMBER ) || ( VersionNumber != LOADSAVE_VERSION_NUMBER ) )
Msg("Save file is in an old format.\nYou will most likely crash...\n");
/*
{
fclose( fp );
#ifdef SAVEGAME_SLOTS
Msg( "PreInGameLoad() Incompatible save game file %s", SaveGameFileName( LoadSavedGameList.selected_item ) );
#else
Msg( "PreInGameLoad() Incompatible save game file %s", &LoadGameFilename[ 0 ] );
#endif
return( false );
}
*/
i = 0;
do
{
fread( &buf[i], sizeof(char), 1, fp );
}
while( buf[ i++ ] != 0 );
for (i = 0; i < NumLevels; i++)
{
if( strcasecmp( (char*) &ShortLevelNames[i][0] , (char*) &buf[0] ) == 0 )
{
NewLevelNum = i;
break;
}
}
if( ( NewLevelNum == -1 ) || ( i == 256 ) )
{
fclose( fp );
return false;
}
fclose( fp );
return true;
}
return false;
}
/*===================================================================
Procedure : InGame Load
Input : MENUITEM * MenuItem
Output : nothing
===================================================================*/
extern px_timer_t countdown_timer;
void InGameLoad( MENUITEM * MenuItem )
{
FILE * fp;
int8_t buf[ 256 ];
int8_t biker[ 256 ];
int16_t i;
int16_t Hours, Minutes, Seconds;
int16_t KilledEnemiesNum, InitEnemiesNum;
u_int32_t MagicNumber;
u_int32_t VersionNumber;
#ifdef SAVEGAME_SLOTS
fp = file_open( SaveGameFileName( LoadSavedGameList.selected_item ), "rb" );
#else
fp = file_open( &LoadGameFilename[ 0 ], "rb" );
#endif
if( fp != NULL )
{
fread( &MagicNumber, sizeof( u_int32_t ), 1, fp );
fread( &VersionNumber, sizeof( u_int32_t ), 1, fp );
if( ( MagicNumber != MAGIC_NUMBER ) || ( VersionNumber != LOADSAVE_VERSION_NUMBER ) )
Msg("Save file is in an old format.\nYou will most likely crash...\n");
/*
{
fclose( fp );
#ifdef SAVEGAME_SLOTS
Msg( "PreInGameLoad() Incompatible save game file %s", SaveGameFileName( LoadSavedGameList.selected_item ) );
#else
Msg( "PreInGameLoad() Incompatible save game file %s", &LoadGameFilename[ 0 ] );
#endif
return;
}
*/
i = 0;
do
{
fread( &buf[i], sizeof(char), 1, fp );
}
while( buf[ i++ ] != 0 );
i = 0;
do
{
fread( &biker[i], sizeof(char), 1, fp );
}
while( biker[ i++ ] != 0 );
fread( &Hours, sizeof( Hours ), 1, fp );
fread( &Minutes, sizeof( Minutes ), 1, fp );
fread( &Seconds, sizeof( Seconds ), 1, fp );
fread( &InitEnemiesNum, sizeof( NumInitEnemies ), 1, fp );
fread( &KilledEnemiesNum, sizeof( NumKilledEnemies ), 1, fp );
fread( &Lives, sizeof( Lives ), 1, fp );
DebugPrintf( "Loading Ships\n" );
fp = LoadShips( fp );
if( !fp ) return;
DebugPrintf( "Loading Enemies\n" );
if( !Enemy_Load( fp ) )
return;
DebugPrintf( "Loading LoadSFX\n" );
fp = LoadAllSfx( fp );
if( !fp ) return;
DebugPrintf( "Loading Texture Animations\n" );
fp = LoadTextureAnimations( fp );
if( !fp ) return;
DebugPrintf( "Loading RestartPoints\n" );
fp = LoadStartRestartPoints( fp );
if( !fp ) return;
DebugPrintf( "Loading RemoteCameras\n" );
fp = LoadRemoteCameras( fp );
if( !fp ) return;
DebugPrintf( "Loading ScreePolys\n" );
fp = LoadScreenPolys( fp );
if( !fp ) return;
DebugPrintf( "Loading TriggerAreas\n" );
fp = LoadTriggerAreas( fp );
if( !fp ) return;
DebugPrintf( "Loading ExternalForces\n" );
fp = LoadExternalForces( fp );
if( !fp ) return;
DebugPrintf( "Loading Teleports\n" );
fp = LoadTeleports( fp );
if( !fp ) return;
DebugPrintf( "Loading Realtime Lights\n" );
fp = LoadRealTimeLights( fp );
if( !fp ) return;
DebugPrintf( "Loading XLights\n" );
fp = LoadXLights( fp );
if( !fp ) return;
DebugPrintf( "Loading Triggers\n" );
fp = LoadAllTriggers( fp );
if( !fp ) return;
DebugPrintf( "Loading BGObjects\n" );
fp = LoadBGObjects( fp );
if( !fp ) return;
DebugPrintf( "Loading Pickups\n" );
fp = LoadAllPickups( fp );
if( !fp ) return;
DebugPrintf( "Loading PrimBulls\n" );
fp = LoadPrimBulls( fp );
if( !fp ) return;
DebugPrintf( "Loading SecBulls\n" );
fp = LoadSecBulls( fp );
if( !fp ) return;
DebugPrintf( "Loading Models\n" );
fp = LoadModels( fp );
if( !fp ) return;
DebugPrintf( "Loading Polys\n" );
fp = LoadPolys( fp );
if( !fp ) return;
DebugPrintf( "Loading FmPolys\n" );
fp = LoadFmPolys( fp );
if( !fp ) return;
DebugPrintf( "Loading SpotFX\n" );
fp = LoadAllSpotFX( fp );
if( !fp ) return;
DebugPrintf( "Loading Text\n" );
fp = LoadAllText( fp );
if( !fp ) return;
fread( &Cheated, sizeof( bool ), 1, fp );
DebugPrintf( "Loaded OK\n" );
fclose( fp );
timer_clear( &countdown_timer );
}
}
char *GetMissionName( char *levelname )
{
static char RealLevelName[ 256 ];
FILE *f;
int16_t i;
for (i = 0; i < NumLevels; i++)
{
if( strcasecmp( (char*) &ShortLevelNames[i][0] , levelname ) == 0 )
{
break;
}
}
if ( i >= NumLevels )
return levelname;
f = file_open( MissionTextNames[ i ], "r" );
RealLevelName[ 0 ] = 0;
if( f )
{
i = 0;
do
{
RealLevelName[ i ] = fgetc( f );
i++;
}
while( ( ( RealLevelName[ i - 1 ] != 13 ) && ( RealLevelName[ i - 1 ] != 10 ) ) );
if( i )
{
RealLevelName[ i - 1 ] = 0;
}
fclose( f );
}
return RealLevelName;
}
/*===================================================================
Procedure : InGame Save
Input : MENUITEM * MenuItem
Output : bool True/False
===================================================================*/
bool InGameSave( MENUITEM * MenuItem )
{
FILE * fp;
FILE * f;
int16_t i;
int8_t RealLevelName[ 128 + 1 ];
int8_t Filename[ 256 ];
int8_t SFilename[ 256 ];
int16_t Hours, Minutes, Seconds;
int8_t sep = 0;
u_int32_t MagicNumber = MAGIC_NUMBER;
u_int32_t VersionNumber = LOADSAVE_VERSION_NUMBER;
f = file_open( MissionTextNames[ NewLevelNum ], "r" );
if( f )
{
i = 0;
do
{
RealLevelName[ i ] = fgetc( f );
i++;
}
while( ( ( RealLevelName[ i - 1 ] != 13 ) && ( RealLevelName[ i - 1 ] != 10 ) ) );
if( i )
{
RealLevelName[ i - 1 ] = 0;
}
fclose( f );
Minutes = ( u_int16_t ) ( LevelTimeTaken / 60 );
Seconds = ( u_int16_t ) LevelTimeTaken;
Hours = ( u_int16_t ) ( Minutes / 60 );
Minutes = ( u_int16_t ) ( Minutes % 60 );
#ifdef SAVEGAME_SLOTS
strcpy( Filename, SaveGameFileName( LoadSavedGameList.selected_item ) );
strcpy( SFilename, SaveGamePicFileName( LoadSavedGameList.selected_item ) );
#else
sprintf( &Filename[ 0 ], "savegame\\%s - %hd-%hd-%hd - %hd of %hd Enemies killed.sav", &RealLevelName[ 0 ], Hours, Minutes, Seconds, NumKilledEnemies, NumInitEnemies );
sprintf( &SFilename[ 0 ], "savegame\\%s - %hd-%hd-%hd - %hd of %hd Enemies killed.ppm", &RealLevelName[ 0 ], Hours, Minutes, Seconds, NumKilledEnemies, NumInitEnemies );
#endif
DebugPrintf( "%s\n", &Filename[ 0 ] );
}
else
{
return false;
}
if ( !folder_exists( SAVEGAME_FOLDER ) )
{
return false;
}
if( !SaveSnapShot( &SFilename[ 0 ] ) )
{
return false;
}
fp = file_open( &Filename[ 0 ], "wb" );
if( fp != NULL )
{
fwrite( &MagicNumber, sizeof( u_int32_t ), 1, fp );
fwrite( &VersionNumber, sizeof( u_int32_t ), 1, fp );
i = 0;
do
{
fwrite( &ShortLevelNames[NewLevelNum][i], sizeof(char), 1, fp );
}
while( ShortLevelNames[NewLevelNum][i++] != 0 );
fprintf( fp, "%s", biker_name );
fwrite( &sep, sizeof( sep ), 1, fp );
fwrite( &Hours, sizeof( Hours ), 1, fp );
fwrite( &Minutes, sizeof( Minutes ), 1, fp );
fwrite( &Seconds, sizeof( Seconds ), 1, fp );
fwrite( &NumInitEnemies, sizeof( NumInitEnemies ), 1, fp );
fwrite( &NumKilledEnemies, sizeof( NumKilledEnemies ), 1, fp );
fwrite( &Lives, sizeof( Lives ), 1, fp );
fp = SaveShips( fp );
if( !fp ) return false;
if( !Enemy_Save( fp ) )
return false;
fp = SaveAllSfx( fp );
if( !fp ) return false;
fp = SaveTextureAnimations( fp );
if( !fp ) return false;
fp = SaveStartRestartPoints( fp );
if( !fp ) return false;
fp = SaveRemoteCameras( fp );
if( !fp ) return false;
fp = SaveScreenPolys( fp );
if( !fp ) return false;
fp = SaveTriggerAreas( fp );
if( !fp ) return false;
fp = SaveExternalForces( fp );
if( !fp ) return false;
fp = SaveTeleports( fp );
if( !fp ) return false;
fp = SaveRealTimeLights( fp );
if( !fp ) return false;
fp = SaveXLights( fp );
if( !fp ) return false;
fp = SaveAllTriggers( fp );
if( !fp ) return false;
fp = SaveBGObjects( fp );
if( !fp ) return false;
fp = SaveAllPickups( fp );
if( !fp ) return false;
fp = SavePrimBulls( fp );
if( !fp ) return false;
fp = SaveSecBulls( fp );
if( !fp ) return false;
fp = SaveModels( fp );
if( !fp ) return false;
fp = SavePolys( fp );
if( !fp ) return false;
fp = SaveFmPolys( fp );
if( !fp ) return false;
fp = SaveAllSpotFX( fp );
if( !fp ) return false;
fp = SaveAllText( fp );
if( !fp ) return false;
fwrite( &Cheated, sizeof( bool ), 1, fp );
fclose( fp );
return true;
}
return false;
}
char *SavedGameInfo( int slot )
{
static char info[ 256 ];
FILE * fp;
int8_t buf[ 256 ];
int8_t biker[ 256 ];
int16_t i;
int16_t Hours, Minutes, Seconds;
int16_t KilledEnemiesNum, InitEnemiesNum;
int16_t Lives;
char * LevelName;
u_int32_t MagicNumber;
u_int32_t VersionNumber;
fp = file_open( SaveGameFileName( slot ), "rb" );
if( fp != NULL )
{
fread( &MagicNumber, sizeof( u_int32_t ), 1, fp );
fread( &VersionNumber, sizeof( u_int32_t ), 1, fp );
if( ( MagicNumber != MAGIC_NUMBER ) || ( VersionNumber != LOADSAVE_VERSION_NUMBER ) )
Msg("Save file is in an old format.\nYou will most likely crash...\n");
/*
{
fclose( fp );
sprintf( info, "unused slot %d", slot + 1 );
return info;
}
*/
i = 0;
do
{
fread( &buf[i], sizeof(char), 1, fp );
}
while( buf[ i++ ] != 0 );
i = 0;
do
{
fread( &biker[i], sizeof(char), 1, fp );
}
while( biker[ i++ ] != 0 );
LevelName = GetMissionName( buf );
fread( &Hours, sizeof( Hours ), 1, fp );
fread( &Minutes, sizeof( Minutes ), 1, fp );
fread( &Seconds, sizeof( Seconds ), 1, fp );
fread( &InitEnemiesNum, sizeof( NumInitEnemies ), 1, fp );
fread( &KilledEnemiesNum, sizeof( NumKilledEnemies ), 1, fp );
fread( &Lives, sizeof( Lives ), 1, fp );
if ( strcasecmp( biker, DEFAULT_PLAYER_NAME ) )
sprintf( info, "%s in %s %hd'%02hd %hd/%hd",
biker,
LevelName,
Hours * 60 + Minutes, Seconds,
KilledEnemiesNum, InitEnemiesNum );
else
sprintf( info, "%s %hd'%02hd %hd/%hd",
LevelName,
Hours * 60 + Minutes, Seconds,
KilledEnemiesNum, InitEnemiesNum );
fclose( fp );
}
else
{
sprintf( info, "unused slot %d", slot + 1 );
}
return info;
}
bool SaveGameSlotUsed( int slot )
{
FILE * fp;
u_int32_t MagicNumber;
u_int32_t VersionNumber;
fp = file_open( SaveGameFileName( slot ), "rb" );
if ( fp )
{
fread( &MagicNumber, sizeof( u_int32_t ), 1, fp );
fread( &VersionNumber, sizeof( u_int32_t ), 1, fp );
if( ( MagicNumber != MAGIC_NUMBER ) || ( VersionNumber != LOADSAVE_VERSION_NUMBER ) )
Msg("Save file is in an old format.\nYou will most likely crash...\n");
/*
{
fclose( fp );
return false;
}
*/
fclose( fp );
return true;
}
return false;
}