-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path3D_MAIN.C
1905 lines (1518 loc) · 42.7 KB
/
3D_MAIN.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
// 3D_MAIN.C
#include "3D_DEF.H"
#pragma hdrstop
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <mem.h>
#include <fcntl.h>
#include <io.h>
#include <dos.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include "jm_io.h"
#include "jm_cio.h"
#include "jm_lzh.h"
#include "jm_error.h"
/*
=============================================================================
BLAKE STONE
(C)opyright 1993, JAM Productions, Inc.
3D engine licensed by ID Software, Inc.
Shareware distribution by Apogee Software, Inc.
=============================================================================
*/
/*
=============================================================================
LOCAL CONSTANTS
=============================================================================
*/
#define SKIP_TITLE_AND_CREDITS (false)
#define FOCALLENGTH (0x5700l) // in global coordinates
#define VIEWGLOBAL 0x10000 // globals visable flush to wall
#define VIEWWIDTH 256 // size of view window
#define VIEWHEIGHT 144
#define MAX_DEST_PATH_LEN 30
/*
=============================================================================
GLOBAL VARIABLES
=============================================================================
*/
extern int pickquick;
void DrawCreditsPage(void);
void unfreed_main(void);
void ShowPromo(void);
char far * far MainStrs[] = {
"q","nowait","l","e",
"version","system",
"dval","tics","mem","powerball","music","d",
"radar",BETA_CODE,
nil
};
short starting_episode,starting_level,starting_difficulty;
char destPath[MAX_DEST_PATH_LEN+1];
char tempPath[MAX_DEST_PATH_LEN+15];
#if BETA_TEST
char far bc_buffer[]=BETA_CODE;
#endif
void InitPlaytemp(void);
char QuitMsg[] = {"Unit: $%02x Error: $%02x"};
#ifdef CEILING_FLOOR_COLORS
unsigned TopColor,BottomColor;
#endif
boolean nospr;
boolean IsA386;
int dirangle[9] = {0,ANGLES/8,2*ANGLES/8,3*ANGLES/8,4*ANGLES/8,5*ANGLES/8,6*ANGLES/8,7*ANGLES/8,ANGLES};
//
// proejection variables
//
fixed focallength;
unsigned screenofs;
int viewwidth;
int viewheight;
int centerx;
int shootdelta; // pixels away from centerx a target can be
fixed scale,maxslope;
long heightnumerator;
int minheightdiv;
boolean startgame,loadedgame;
int mouseadjustment;
char configname[13]="CONFIG.";
short view_xl,view_xh,view_yl,view_yh;
#if IN_DEVELOPMENT
unsigned democount=0,jim=0;
#endif
/*
=============================================================================
LOCAL VARIABLES
=============================================================================
*/
#if 0
unsigned mspeed;
void CalcSpeedRating()
{
short loop;
for (loop=0; loop<10; loop++)
{
ThreeDRefresh();
mspeed += tics;
}
}
#endif
/*
====================
=
= WriteConfig
=
====================
*/
void WriteConfig(void)
{
int file;
MakeDestPath(configname);
file = open(tempPath,O_CREAT | O_BINARY | O_WRONLY,
S_IREAD | S_IWRITE | S_IFREG);
if (file != -1)
{
write(file,Scores,sizeof(HighScore) * MaxScores);
write(file,&SoundMode,sizeof(SoundMode));
write(file,&MusicMode,sizeof(MusicMode));
write(file,&DigiMode,sizeof(DigiMode));
write(file,&mouseenabled,sizeof(mouseenabled));
write(file,&joystickenabled,sizeof(joystickenabled));
write(file,&joypadenabled,sizeof(joypadenabled));
write(file,&joystickprogressive,sizeof(joystickprogressive));
write(file,&joystickport,sizeof(joystickport));
write(file,&dirscan,sizeof(dirscan));
write(file,&buttonscan,sizeof(buttonscan));
write(file,&buttonmouse,sizeof(buttonmouse));
write(file,&buttonjoy,sizeof(buttonjoy));
write(file,&viewsize,sizeof(viewsize));
write(file,&mouseadjustment,sizeof(mouseadjustment));
write(file,&gamestate.flags,sizeof(gamestate.flags));
close(file);
}
}
//===========================================================================
/*
=====================
=
= NewGame
=
= Set up new game to start from the beginning
=
=====================
*/
boolean ShowQuickMsg;
void NewGame (int difficulty,int episode)
{
unsigned oldf=gamestate.flags,loop;
InitPlaytemp();
playstate = ex_stillplaying;
ShowQuickMsg=true;
_fmemset (&gamestuff,0,sizeof(gamestuff));
memset (&gamestate,0,sizeof(gamestate));
memset(&gamestate.barrier_table,0xff,sizeof(gamestate.barrier_table));
memset(&gamestate.old_barrier_table,0xff,sizeof(gamestate.old_barrier_table));
gamestate.flags = oldf & ~(GS_KILL_INF_WARN);
// LoadAccessCodes();
gamestate.difficulty = difficulty;
//
// The following are set to 0 by the memset() to gamestate - Good catch! :JR
//
// gamestate.rzoom
// gamestate.rpower
// gamestate.old_door_bombs
// gamestate.plasma_detonators
//
gamestate.weapons = 1<<wp_autocharge; // |1<<wp_plasma_detonators;
gamestate.weapon = gamestate.chosenweapon = wp_autocharge;
gamestate.old_weapons[0] = gamestate.weapons;
gamestate.old_weapons[1] = gamestate.weapon;
gamestate.old_weapons[2] = gamestate.chosenweapon;
gamestate.health = 100;
gamestate.old_ammo = gamestate.ammo = STARTAMMO;
// gamestate.dollars = START_DOLLARS;
// gamestate.cents = START_CENTS;
gamestate.lives = 3;
gamestate.nextextra = EXTRAPOINTS;
gamestate.episode=episode;
gamestate.flags |= (GS_CLIP_WALLS|GS_ATTACK_INFOAREA); //|GS_DRAW_CEILING|GS_DRAW_FLOOR);
#if IN_DEVELOPMENT || TECH_SUPPORT_VERSION
if (gamestate.flags & GS_STARTLEVEL)
{
gamestate.mapon = starting_level;
gamestate.difficulty = starting_difficulty;
gamestate.episode = starting_episode;
}
else
#endif
gamestate.mapon = 0;
gamestate.key_floor = gamestate.mapon+1;
startgame = true;
for (loop=0; loop<MAPS_WITH_STATS; loop++)
{
gamestuff.old_levelinfo[loop].stats.overall_floor=100;
if (loop)
gamestuff.old_levelinfo[loop].locked=true;
}
// normalshade_div = SHADE_DIV;
// shade_max = SHADE_MAX;
ExtraRadarFlags = InstantWin = InstantQuit = 0;
pickquick = 0;
}
//===========================================================================
//==========================================================================
//
// 'LOAD/SAVE game' and 'LOAD/SAVE level' code
//
//==========================================================================
boolean LevelInPlaytemp(char levelnum);
#define WriteIt(c,p,s) cksize+=WriteInfo(c,(char far *)p,s,handle)
#define ReadIt(d,p,s) ReadInfo(d,(char far *)p,s,handle)
#define LZH_WORK_BUFFER_SIZE 8192
memptr lzh_work_buffer;
long checksum;
//--------------------------------------------------------------------------
// InitPlaytemp()
//--------------------------------------------------------------------------
void InitPlaytemp()
{
int handle;
long size;
MakeDestPath(PLAYTEMP_FILE);
if ((handle=open(tempPath,O_CREAT|O_TRUNC|O_RDWR|O_BINARY,S_IREAD|S_IWRITE))==-1)
MAIN_ERROR(INITPLAYTEMP_OPEN_ERR);
close(handle);
}
//--------------------------------------------------------------------------
// DoChecksum()
//--------------------------------------------------------------------------
long DoChecksum(byte far *source,unsigned size,long checksum)
{
unsigned i;
for (i=0;i<size-1;i++)
checksum += source[i]^source[i+1];
return(checksum);
}
//--------------------------------------------------------------------------
// FindChunk()
//--------------------------------------------------------------------------
long FindChunk(int file, char *chunk)
{
long chunklen;
char fchunk[5]={0,0,0,0,0};
while (1)
{
if (read(file,fchunk,4)!=4) // read chunk id
break;
read(file,&chunklen,4); // read chunk length
if (strstr(fchunk,chunk)) // look for chunk (sub-check!)
return(chunklen); // chunk found!
lseek(file,chunklen,SEEK_CUR); // skip this chunk
}
lseek(file,0,SEEK_END); // make sure we're at the end
return(0);
}
//--------------------------------------------------------------------------
// NextChunk()
//--------------------------------------------------------------------------
long NextChunk(int file)
{
long chunklen;
char fchunk[5]={0,0,0,0,0};
if (read(file,fchunk,4) != 4) // read chunk id
{
lseek(file,0,SEEK_END); // make sure we're at the end
return(0);
}
read(file,&chunklen,4); // read chunk length
return(chunklen);
}
char LS_current=-1,LS_total=-1;
//--------------------------------------------------------------------------
// ReadInfo()
//--------------------------------------------------------------------------
void ReadInfo(boolean decompress,char far *dst, unsigned size, int file)
{
unsigned csize,dsize;
PreloadUpdate(LS_current++,LS_total);
if (decompress)
{
IO_FarRead(file,(char far *)&csize,sizeof(csize));
IO_FarRead(file,lzh_work_buffer,csize);
checksum=DoChecksum(lzh_work_buffer,csize,checksum);
dsize=LZH_Decompress(lzh_work_buffer,dst,size,csize,SRC_MEM|DEST_MEM);
if (dsize != size)
MAIN_ERROR(READINFO_BAD_DECOMP);
}
else
{
IO_FarRead(file,dst,size);
checksum=DoChecksum(dst,size,checksum);
}
}
//--------------------------------------------------------------------------
// WriteInfo()
//--------------------------------------------------------------------------
unsigned WriteInfo(boolean compress, char far *src, unsigned size, int file)
{
unsigned csize;
PreloadUpdate(LS_current++,LS_total);
if (compress)
{
csize=LZH_Compress(src,lzh_work_buffer,size,SRC_MEM|DEST_MEM);
if (csize > LZH_WORK_BUFFER_SIZE)
MAIN_ERROR(WRITEINFO_BIGGER_BUF);
IO_FarWrite (file,(char far *)&csize,sizeof(csize));
IO_FarWrite (file,lzh_work_buffer,csize);
checksum=DoChecksum(lzh_work_buffer,csize,checksum);
csize += sizeof(csize);
}
else
{
IO_FarWrite (file,src,size);
checksum=DoChecksum(src,size,checksum);
csize=size;
}
return(csize);
}
//--------------------------------------------------------------------------
// LoadLevel()
//--------------------------------------------------------------------------
boolean LoadLevel(short levelnum)
{
extern boolean ShowQuickMsg;
extern boolean ForceLoadDefault;
extern unsigned destoff;
boolean oldloaded=loadedgame;
long oldchecksum;
objtype *ob;
statobj_t *statptr;
int handle,picnum;
memptr temp;
unsigned count;
char far *ptr;
char chunk[5]="LVxx";
extern int nsd_table[];
extern int sm_table[];
char mod;
WindowY=181;
gamestuff.level[levelnum].locked=false;
mod = levelnum % 6;
normalshade_div = nsd_table[mod];
shade_max = sm_table[mod];
normalshade=(3*(maxscale>>2))/normalshade_div;
// Open PLAYTEMP file
//
MakeDestPath(PLAYTEMP_FILE);
handle=open(tempPath,O_RDONLY|O_BINARY);
// If level exists in PLAYTEMP file, use it; otherwise, load it from scratch!
//
sprintf(&chunk[2],"%02x",levelnum);
if ((handle==-1) || (!FindChunk(handle,chunk)) || ForceLoadDefault)
{
close(handle);
PreloadUpdate(LS_current+((LS_total-LS_current)>>1),LS_total);
SetupGameLevel();
gamestate.flags |= GS_VIRGIN_LEVEL;
gamestate.turn_around=0;
PreloadUpdate(1,1);
ForceLoadDefault=false;
goto overlay;
}
gamestate.flags &= ~GS_VIRGIN_LEVEL;
// Setup for LZH decompression
//
LZH_Startup();
MM_GetPtr(&lzh_work_buffer,LZH_WORK_BUFFER_SIZE);
// Read all sorts of stuff...
//
checksum = 0;
loadedgame=true;
SetupGameLevel();
loadedgame=oldloaded;
ReadIt(true, tilemap, sizeof(tilemap));
ReadIt(true, actorat, sizeof(actorat));
ReadIt(true, areaconnect, sizeof(areaconnect));
ReadIt(true, areabyplayer, sizeof(areabyplayer));
// Restore 'save game' actors
//
ReadIt(false, &count, sizeof(count));
MM_GetPtr(&temp,count*sizeof(*ob));
ReadIt(true, temp, count*sizeof(*ob));
ptr=temp;
InitActorList (); // start with "player" actor
_fmemcpy(new,ptr,sizeof(*ob)-4); // don't copy over links!
ptr += sizeof(*ob); //
while (--count)
{
GetNewActor();
_fmemcpy(new,ptr,sizeof(*ob)-4); // don't copy over links!
actorat[new->tilex][new->tiley]=new;
#if LOOK_FOR_DEAD_GUYS
if (new->flags & FL_DEADGUY)
DeadGuys[NumDeadGuys++]=new;
#endif
ptr += sizeof(*ob);
}
MM_FreePtr(&temp);
//
// Re-Establish links to barrier switches
//
#pragma warn -pia
ob = objlist;
do
{
switch (ob->obclass)
{
case arc_barrierobj:
case post_barrierobj:
case vspike_barrierobj:
case vpost_barrierobj:
ob->temp2 = ScanBarrierTable(ob->tilex,ob->tiley);
break;
}
} while (ob = ob->next);
#pragma warn +pia
ConnectBarriers();
// Read all sorts of stuff...
//
ReadIt(false, &laststatobj, sizeof(laststatobj));
ReadIt(true, statobjlist, sizeof(statobjlist));
ReadIt(true, doorposition, sizeof(doorposition));
ReadIt(true, doorobjlist, sizeof(doorobjlist));
ReadIt(false, &pwallstate, sizeof(pwallstate));
ReadIt(false, &pwallx, sizeof(pwallx));
ReadIt(false, &pwally, sizeof(pwally));
ReadIt(false, &pwalldir, sizeof(pwalldir));
ReadIt(false, &pwallpos, sizeof(pwallpos));
ReadIt(false, &pwalldist, sizeof(pwalldist));
ReadIt(true, TravelTable, sizeof(TravelTable));
ReadIt(true, &ConHintList, sizeof(ConHintList));
ReadIt(true, eaList, sizeof(eaWallInfo)*MAXEAWALLS);
ReadIt(true, &GoldsternInfo, sizeof(GoldsternInfo));
ReadIt(true, &GoldieList,sizeof(GoldieList));
ReadIt(false, gamestate.barrier_table,sizeof(gamestate.barrier_table));
ReadIt(false, &gamestate.plasma_detonators,sizeof(gamestate.plasma_detonators));
// Read and evaluate checksum
//
PreloadUpdate(LS_current++,LS_total);
IO_FarRead (handle,(void far *)&oldchecksum,sizeof(oldchecksum));
if (oldchecksum != checksum)
{
int old_wx=WindowX,old_wy=WindowY,old_ww=WindowW,old_wh=WindowH,
old_px=px,old_py=py;
WindowX=0; WindowY=16; WindowW=320; WindowH=168;
CacheMessage(BADINFO_TEXT);
WindowX=old_wx; WindowY=old_wy; WindowW=old_ww; WindowH=old_wh;
px=old_px; py=old_py;
IN_ClearKeysDown();
IN_Ack();
gamestate.score = 0;
gamestate.nextextra = EXTRAPOINTS;
gamestate.lives = 1;
gamestate.weapon = gamestate.chosenweapon = wp_autocharge;
gamestate.weapons = 1<<wp_autocharge; // |1<<wp_plasma_detonators;
gamestate.ammo = 8;
}
close(handle);
// Clean-up LZH compression
//
MM_FreePtr(&lzh_work_buffer);
LZH_Shutdown();
NewViewSize(viewsize);
// Check for Strange Door and Actor combos
//
CleanUpDoors_N_Actors();
overlay:;
return(true);
}
//--------------------------------------------------------------------------
// SaveLevel()
//--------------------------------------------------------------------------
boolean SaveLevel(short levelnum)
{
objtype *ob;
int handle;
struct ffblk finfo;
long offset,cksize;
char chunk[5]="LVxx";
unsigned gflags = gamestate.flags;
boolean rt_value=false;
memptr temp;
unsigned count;
char far *ptr;
char oldmapon;
WindowY=181;
// Make sure floor stats are saved!
//
oldmapon=gamestate.mapon;
gamestate.mapon=gamestate.lastmapon;
ShowStats(0,0,ss_justcalc,&gamestuff.level[gamestate.mapon].stats);
gamestate.mapon=oldmapon;
// Yeah! We're no longer a virgin!
//
gamestate.flags &= ~GS_VIRGIN_LEVEL;
// Open PLAYTEMP file
//
MakeDestPath(PLAYTEMP_FILE);
if ((handle=open(tempPath,O_CREAT|O_RDWR|O_BINARY,S_IREAD|S_IWRITE))==-1)
MAIN_ERROR(SAVELEVEL_DISKERR);
// Remove level chunk from file
//
sprintf(&chunk[2],"%02x",levelnum);
DeleteChunk(handle,chunk);
// Setup LZH compression
//
LZH_Startup();
MM_GetPtr(&lzh_work_buffer,LZH_WORK_BUFFER_SIZE);
// Write level chunk id
//
write(handle,chunk,4);
lseek(handle,4,SEEK_CUR); // leave four bytes for chunk size
// Write all sorts of info...
//
checksum = cksize = 0;
WriteIt(true, tilemap, sizeof(tilemap));
WriteIt(true, actorat, sizeof(actorat));
WriteIt(true, areaconnect, sizeof(areaconnect));
WriteIt(true, areabyplayer, sizeof(areabyplayer));
// Write actor list...
//
MM_GetPtr(&temp,sizeof(objlist));
for (ob=player,count=0,ptr=temp; ob; ob=ob->next,count++,ptr+=sizeof(*ob))
_fmemcpy(ptr,ob,sizeof(*ob));
WriteIt(false, &count, sizeof(count));
WriteIt(true, temp, count*sizeof(*ob));
MM_FreePtr(&temp);
// Write all sorts of info...
//
WriteIt(false, &laststatobj, sizeof(laststatobj));
WriteIt(true, statobjlist, sizeof(statobjlist));
WriteIt(true, doorposition, sizeof(doorposition));
WriteIt(true, doorobjlist, sizeof(doorobjlist));
WriteIt(false, &pwallstate, sizeof(pwallstate));
WriteIt(false, &pwallx, sizeof(pwallx));
WriteIt(false, &pwally, sizeof(pwally));
WriteIt(false, &pwalldir, sizeof(pwalldir));
WriteIt(false, &pwallpos, sizeof(pwallpos));
WriteIt(false, &pwalldist, sizeof(pwalldist));
WriteIt(true, TravelTable, sizeof(TravelTable));
WriteIt(true, &ConHintList, sizeof(ConHintList));
WriteIt(true, eaList, sizeof(eaWallInfo)*MAXEAWALLS);
WriteIt(true, &GoldsternInfo, sizeof(GoldsternInfo));
WriteIt(true, &GoldieList,sizeof(GoldieList));
WriteIt(false, gamestate.barrier_table,sizeof(gamestate.barrier_table));
WriteIt(false, &gamestate.plasma_detonators,sizeof(gamestate.plasma_detonators));
// Write checksum and determine size of file
//
WriteIt(false, &checksum, sizeof(checksum));
offset=tell(handle);
// Write chunk size, set file size, and close file
//
lseek(handle,-(cksize+4),SEEK_CUR);
write(handle,&cksize,4);
chsize(handle,offset);
close(handle);
rt_value=true;
// Clean-up LZH compression
//
exit_func:;
MM_FreePtr(&lzh_work_buffer);
LZH_Shutdown();
NewViewSize(viewsize);
gamestate.flags = gflags;
return(rt_value);
}
#pragma warn -pia
//--------------------------------------------------------------------------
// DeleteChunk()
//--------------------------------------------------------------------------
long DeleteChunk(int handle, char *chunk)
{
long filesize,cksize,offset,bmove;
int dhandle;
lseek(handle,0,SEEK_SET);
filesize=lseek(handle,0,SEEK_END);
lseek(handle,0,SEEK_SET);
if (cksize=FindChunk(handle,chunk))
{
offset=lseek(handle,0,SEEK_CUR)-8; // move back to CKID/SIZE
bmove=filesize-(offset+8+cksize); // figure bytes to move
if (bmove) // any data to move?
{
// Move data: FROM --> the start of NEXT chunk through the end of file.
// TO --> the start of THIS chunk.
//
// (ie: erase THIS chunk and re-write it at the end of the file!)
//
lseek(handle,cksize,SEEK_CUR); // seek source to NEXT chunk
MakeDestPath(PLAYTEMP_FILE);
if ((dhandle=open(tempPath,O_CREAT|O_RDWR|O_BINARY,S_IREAD|S_IWRITE))==-1)
MAIN_ERROR(SAVELEVEL_DISKERR);
lseek(dhandle,offset,SEEK_SET); // seek dest to THIS chunk
IO_CopyHandle(handle,dhandle,bmove); // copy "bmove" bytes
close(dhandle);
lseek(handle,offset+bmove,SEEK_SET); // go to end of data moved
}
else
lseek(handle,offset,SEEK_SET);
}
return(cksize);
}
#pragma warn +pia
char far SavegameInfoText[]="\n\r"
"\n\r"
"-------------------------------------\n\r"
" Blake Stone: Aliens Of Gold\n\r"
"Copyright 1993, JAM Productions, Inc.\n\r"
"\n\r"
"SAVEGAME file is from version: "__VERSION__"\n\r"
" Compile Date :"__DATE__" : "__TIME__"\n\r"
"-------------------------------------\n\r"
"\x1a";
//--------------------------------------------------------------------------
// LoadTheGame()
//--------------------------------------------------------------------------
boolean LoadTheGame(int handle)
{
extern int lastmenumusic;
int shandle;
long cksize;
memptr temp=NULL;
boolean rt_value=false;
char InfoSpace[400];
memptr tempspace;
// Setup LZH decompression
//
LZH_Startup();
MM_GetPtr(&lzh_work_buffer,LZH_WORK_BUFFER_SIZE);
// Read in VERSion chunk
//
if (!FindChunk(handle,"VERS"))
goto cleanup;
cksize = sizeof(SavegameInfoText);
read(handle, InfoSpace, cksize);
if (_fmemcmp(InfoSpace, SavegameInfoText, cksize))
{
// Old Version of game
int old_wx=WindowX,old_wy=WindowY,old_ww=WindowW,old_wh=WindowH,
old_px=px,old_py=py;
WindowX=0; WindowY=16; WindowW=320; WindowH=168;
CacheMessage(BADSAVEGAME_TEXT);
SD_PlaySound (NOWAYSND);
WindowX=old_wx; WindowY=old_wy; WindowW=old_ww; WindowH=old_wh;
px=old_px; py=old_py;
IN_ClearKeysDown();
IN_Ack();
VW_FadeOut();
screenfaded = true;
goto cleanup;
}
// Read in HEAD chunk
//
if (!FindChunk(handle,"HEAD"))
goto cleanup;
ReadIt(true, &gamestate, sizeof(gamestate));
ReadIt(true, &gamestuff, sizeof(gamestuff));
// Reinitialize page manager
//
#if DUAL_SWAP_FILES
PM_Shutdown();
PM_Startup ();
PM_UnlockMainMem();
#endif
// Start music for the starting level in this loaded game.
//
FreeMusic();
StartMusic(false);
// Copy all remaining chunks to PLAYTEMP file
//
MakeDestPath(PLAYTEMP_FILE);
if ((shandle=open(tempPath,O_CREAT|O_RDWR|O_TRUNC|O_BINARY,S_IREAD|S_IWRITE))==-1)
goto cleanup;
#pragma warn -pia
while (cksize=NextChunk(handle))
{
cksize += 8; // include chunk ID and LENGTH
lseek(handle,-8,SEEK_CUR); // seek to start of chunk
MM_GetPtr(&temp,cksize); // alloc temp buffer
IO_FarRead(handle,temp,cksize); // read chunk from SAVEGAME file
IO_FarWrite(shandle,temp,cksize); // write chunk to PLAYTEMP file
MM_FreePtr(&temp); // free temp buffer
}
#pragma warn +pia
close(shandle);
rt_value=true;
// Clean-up LZH decompression
//
cleanup:;
MM_FreePtr(&lzh_work_buffer);
LZH_Shutdown();
NewViewSize(viewsize);
// Load current level
//
if (rt_value)
{
LoadLevel(0xff);
ShowQuickMsg=false;
}
return(rt_value);
}
//--------------------------------------------------------------------------
// SaveTheGame()
//--------------------------------------------------------------------------
boolean SaveTheGame(int handle, char far *description)
{
struct ffblk finfo;
unsigned long cksize,offset;
int shandle;
memptr temp;
char nbuff[GAME_DESCRIPTION_LEN+1];
boolean rt_value=false,exists;
//
// Save PLAYTEMP becuase we'll want to restore it to the way it was
// before the save.
//
// IO_CopyFile(PLAYTEMP_FILE,OLD_PLAYTEMP_FILE);
//
// Save current level -- saves it into PLAYTEMP.
//
SaveLevel(0xff);
// Setup LZH compression
//
LZH_Startup();
MM_GetPtr(&lzh_work_buffer,LZH_WORK_BUFFER_SIZE);
// Write VERSion chunk
//
cksize=sizeof(SavegameInfoText);
write(handle,"VERS",4);
write(handle,&cksize,4);
IO_FarWrite(handle,SavegameInfoText,cksize);
// Write DESC chunk
//
_fmemcpy(nbuff,description,sizeof(nbuff));
cksize=strlen(nbuff)+1;
write(handle,"DESC",4);
write(handle,&cksize,4);
write(handle,nbuff,cksize);
// Write HEAD chunk
//
cksize=0;
write(handle,"HEAD",4);
lseek(handle,4,SEEK_CUR); // leave four bytes for chunk size
WriteIt(true, &gamestate, sizeof(gamestate));
WriteIt(true, &gamestuff, sizeof(gamestuff));
lseek(handle,-(cksize+4),SEEK_CUR);
write(handle,&cksize,4);
lseek(handle,cksize,SEEK_CUR);
// Append PLAYTEMP file to savegame file
//
MakeDestPath(PLAYTEMP_FILE);
if (findfirst(tempPath,&finfo,0))
goto cleanup;
if ((shandle=open(tempPath,O_RDONLY|O_BINARY))==-1)
goto cleanup;
IO_CopyHandle(shandle,handle,-1);
close(shandle);
rt_value=true;
// Clean-up LZH compression
//
cleanup:;
MM_FreePtr(&lzh_work_buffer);
LZH_Shutdown();
NewViewSize(viewsize);
//
// Return PLAYTEMP to original state!
//
// remove(PLAYTEMP_FILE);
// rename(OLD_PLAYTEMP_FILE,PLAYTEMP_FILE);
//
return(rt_value);
}
//--------------------------------------------------------------------------
// LevelInPlaytemp()
//--------------------------------------------------------------------------
boolean LevelInPlaytemp(char levelnum)
{
int handle;
char chunk[5]="LVxx";
boolean rt_value;
// Open PLAYTEMP file
//
MakeDestPath(PLAYTEMP_FILE);
handle=open(tempPath,O_RDONLY|O_BINARY);