-
Notifications
You must be signed in to change notification settings - Fork 0
/
ID_CA.C
1768 lines (1432 loc) · 32.7 KB
/
ID_CA.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
// ID_CA.C
// this has been customized for WOLF
/*
=============================================================================
Id Software Caching Manager
---------------------------
Must be started BEFORE the memory manager, because it needs to get the headers
loaded into the data segment
=============================================================================
*/
#include "ID_HEADS.H"
#pragma hdrstop
#pragma warn -pro
#pragma warn -use
#define THREEBYTEGRSTARTS
/*
=============================================================================
LOCAL CONSTANTS
=============================================================================
*/
typedef struct
{
unsigned bit0,bit1; // 0-255 is a character, > is a pointer to a node
} huffnode;
typedef struct
{
unsigned RLEWtag;
long headeroffsets[100];
byte tileinfo[];
} mapfiletype;
/*
=============================================================================
GLOBAL VARIABLES
=============================================================================
*/
byte _seg *tinf;
int mapon;
unsigned _seg *mapsegs[MAPPLANES];
maptype _seg *mapheaderseg[NUMMAPS];
byte _seg *audiosegs[NUMSNDCHUNKS];
void _seg *grsegs[NUMCHUNKS];
byte far grneeded[NUMCHUNKS];
byte ca_levelbit,ca_levelnum;
int profilehandle,debughandle;
char audioname[13]="AUDIO.";
/*
=============================================================================
LOCAL VARIABLES
=============================================================================
*/
extern long far CGAhead;
extern long far EGAhead;
extern byte CGAdict;
extern byte EGAdict;
extern byte far maphead;
extern byte mapdict;
extern byte far audiohead;
extern byte audiodict;
char extension[5], // Need a string, not constant to change cache files
gheadname[10]=GREXT"HEAD.",
gfilename[10]=GREXT"GRAPH.",
gdictname[10]=GREXT"DICT.",
mheadname[10]="MAPHEAD.",
mfilename[10]="MAPTEMP.",
aheadname[10]="AUDIOHED.",
afilename[10]="AUDIOT.";
void CA_CannotOpen(char *string);
long _seg *grstarts; // array of offsets in egagraph, -1 for sparse
long _seg *audiostarts; // array of offsets in audio / audiot
#ifdef GRHEADERLINKED
huffnode *grhuffman;
#else
huffnode grhuffman[255];
#endif
#ifdef AUDIOHEADERLINKED
huffnode *audiohuffman;
#else
huffnode audiohuffman[255];
#endif
int grhandle; // handle to EGAGRAPH
int maphandle; // handle to MAPTEMP / GAMEMAPS
int audiohandle; // handle to AUDIOT / AUDIO
long chunkcomplen,chunkexplen;
SDMode oldsoundmode;
void CAL_CarmackExpand (unsigned far *source, unsigned far *dest,
unsigned length);
#ifdef THREEBYTEGRSTARTS
#define FILEPOSSIZE 3
//#define GRFILEPOS(c) (*(long far *)(((byte far *)grstarts)+(c)*3)&0xffffff)
long GRFILEPOS(int c)
{
long value;
int offset;
offset = c*3;
value = *(long far *)(((byte far *)grstarts)+offset);
value &= 0x00ffffffl;
if (value == 0xffffffl)
value = -1;
return value;
};
#else
#define FILEPOSSIZE 4
#define GRFILEPOS(c) (grstarts[c])
#endif
/*
=============================================================================
LOW LEVEL ROUTINES
=============================================================================
*/
/*
============================
=
= CA_OpenDebug / CA_CloseDebug
=
= Opens a binary file with the handle "debughandle"
=
============================
*/
void CA_OpenDebug (void)
{
unlink ("DEBUG.TXT");
debughandle = open("DEBUG.TXT", O_CREAT | O_WRONLY | O_TEXT);
}
void CA_CloseDebug (void)
{
close (debughandle);
}
/*
============================
=
= CAL_GetGrChunkLength
=
= Gets the length of an explicit length chunk (not tiles)
= The file pointer is positioned so the compressed data can be read in next.
=
============================
*/
void CAL_GetGrChunkLength (int chunk)
{
lseek(grhandle,GRFILEPOS(chunk),SEEK_SET);
read(grhandle,&chunkexplen,sizeof(chunkexplen));
chunkcomplen = GRFILEPOS(chunk+1)-GRFILEPOS(chunk)-4;
}
/*
==========================
=
= CA_FarRead
=
= Read from a file to a far pointer
=
==========================
*/
boolean CA_FarRead (int handle, byte far *dest, long length)
{
if (length>0xffffl)
Quit ("CA_FarRead doesn't support 64K reads yet!");
asm push ds
asm mov bx,[handle]
asm mov cx,[WORD PTR length]
asm mov dx,[WORD PTR dest]
asm mov ds,[WORD PTR dest+2]
asm mov ah,0x3f // READ w/handle
asm int 21h
asm pop ds
asm jnc good
errno = _AX;
return false;
good:
asm cmp ax,[WORD PTR length]
asm je done
errno = EINVFMT; // user manager knows this is bad read
return false;
done:
return true;
}
/*
==========================
=
= CA_SegWrite
=
= Write from a file to a far pointer
=
==========================
*/
boolean CA_FarWrite (int handle, byte far *source, long length)
{
if (length>0xffffl)
Quit ("CA_FarWrite doesn't support 64K reads yet!");
asm push ds
asm mov bx,[handle]
asm mov cx,[WORD PTR length]
asm mov dx,[WORD PTR source]
asm mov ds,[WORD PTR source+2]
asm mov ah,0x40 // WRITE w/handle
asm int 21h
asm pop ds
asm jnc good
errno = _AX;
return false;
good:
asm cmp ax,[WORD PTR length]
asm je done
errno = ENOMEM; // user manager knows this is bad write
return false;
done:
return true;
}
/*
==========================
=
= CA_ReadFile
=
= Reads a file into an allready allocated buffer
=
==========================
*/
boolean CA_ReadFile (char *filename, memptr *ptr)
{
int handle;
long size;
if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
return false;
size = filelength (handle);
if (!CA_FarRead (handle,*ptr,size))
{
close (handle);
return false;
}
close (handle);
return true;
}
/*
==========================
=
= CA_WriteFile
=
= Writes a file from a memory buffer
=
==========================
*/
boolean CA_WriteFile (char *filename, void far *ptr, long length)
{
int handle;
long size;
handle = open(filename,O_CREAT | O_BINARY | O_WRONLY,
S_IREAD | S_IWRITE | S_IFREG);
if (handle == -1)
return false;
if (!CA_FarWrite (handle,ptr,length))
{
close (handle);
return false;
}
close (handle);
return true;
}
/*
==========================
=
= CA_LoadFile
=
= Allocate space for and load a file
=
==========================
*/
boolean CA_LoadFile (char *filename, memptr *ptr)
{
int handle;
long size;
if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
return false;
size = filelength (handle);
MM_GetPtr (ptr,size);
if (!CA_FarRead (handle,*ptr,size))
{
close (handle);
return false;
}
close (handle);
return true;
}
/*
============================================================================
COMPRESSION routines, see JHUFF.C for more
============================================================================
*/
/*
===============
=
= CAL_OptimizeNodes
=
= Goes through a huffman table and changes the 256-511 node numbers to the
= actular address of the node. Must be called before CAL_HuffExpand
=
===============
*/
void CAL_OptimizeNodes (huffnode *table)
{
huffnode *node;
int i;
node = table;
for (i=0;i<255;i++)
{
if (node->bit0 >= 256)
node->bit0 = (unsigned)(table+(node->bit0-256));
if (node->bit1 >= 256)
node->bit1 = (unsigned)(table+(node->bit1-256));
node++;
}
}
/*
======================
=
= CAL_HuffExpand
=
= Length is the length of the EXPANDED data
= If screenhack, the data is decompressed in four planes directly
= to the screen
=
======================
*/
void CAL_HuffExpand (byte huge *source, byte huge *dest,
long length,huffnode *hufftable, boolean screenhack)
{
// unsigned bit,byte,node,code;
unsigned sourceseg,sourceoff,destseg,destoff,endoff;
huffnode *headptr;
byte mapmask;
// huffnode *nodeon;
headptr = hufftable+254; // head node is allways node 254
source++; // normalize
source--;
dest++;
dest--;
if (screenhack)
{
mapmask = 1;
asm mov dx,SC_INDEX
asm mov ax,SC_MAPMASK + 256
asm out dx,ax
length >>= 2;
}
sourceseg = FP_SEG(source);
sourceoff = FP_OFF(source);
destseg = FP_SEG(dest);
destoff = FP_OFF(dest);
endoff = destoff+length;
//
// ds:si source
// es:di dest
// ss:bx node pointer
//
if (length <0xfff0)
{
//--------------------------
// expand less than 64k of data
//--------------------------
asm mov bx,[headptr]
asm mov si,[sourceoff]
asm mov di,[destoff]
asm mov es,[destseg]
asm mov ds,[sourceseg]
asm mov ax,[endoff]
asm mov ch,[si] // load first byte
asm inc si
asm mov cl,1
expandshort:
asm test ch,cl // bit set?
asm jnz bit1short
asm mov dx,[ss:bx] // take bit0 path from node
asm shl cl,1 // advance to next bit position
asm jc newbyteshort
asm jnc sourceupshort
bit1short:
asm mov dx,[ss:bx+2] // take bit1 path
asm shl cl,1 // advance to next bit position
asm jnc sourceupshort
newbyteshort:
asm mov ch,[si] // load next byte
asm inc si
asm mov cl,1 // back to first bit
sourceupshort:
asm or dh,dh // if dx<256 its a byte, else move node
asm jz storebyteshort
asm mov bx,dx // next node = (huffnode *)code
asm jmp expandshort
storebyteshort:
asm mov [es:di],dl
asm inc di // write a decopmpressed byte out
asm mov bx,[headptr] // back to the head node for next bit
asm cmp di,ax // done?
asm jne expandshort
//
// perform screenhack if needed
//
asm test [screenhack],1
asm jz notscreen
asm shl [mapmask],1
asm mov ah,[mapmask]
asm cmp ah,16
asm je notscreen // all four planes done
asm mov dx,SC_INDEX
asm mov al,SC_MAPMASK
asm out dx,ax
asm mov di,[destoff]
asm mov ax,[endoff]
asm jmp expandshort
notscreen:;
}
else
{
//--------------------------
// expand more than 64k of data
//--------------------------
length--;
asm mov bx,[headptr]
asm mov cl,1
asm mov si,[sourceoff]
asm mov di,[destoff]
asm mov es,[destseg]
asm mov ds,[sourceseg]
asm lodsb // load first byte
expand:
asm test al,cl // bit set?
asm jnz bit1
asm mov dx,[ss:bx] // take bit0 path from node
asm jmp gotcode
bit1:
asm mov dx,[ss:bx+2] // take bit1 path
gotcode:
asm shl cl,1 // advance to next bit position
asm jnc sourceup
asm lodsb
asm cmp si,0x10 // normalize ds:si
asm jb sinorm
asm mov cx,ds
asm inc cx
asm mov ds,cx
asm xor si,si
sinorm:
asm mov cl,1 // back to first bit
sourceup:
asm or dh,dh // if dx<256 its a byte, else move node
asm jz storebyte
asm mov bx,dx // next node = (huffnode *)code
asm jmp expand
storebyte:
asm mov [es:di],dl
asm inc di // write a decopmpressed byte out
asm mov bx,[headptr] // back to the head node for next bit
asm cmp di,0x10 // normalize es:di
asm jb dinorm
asm mov dx,es
asm inc dx
asm mov es,dx
asm xor di,di
dinorm:
asm sub [WORD PTR ss:length],1
asm jnc expand
asm dec [WORD PTR ss:length+2]
asm jns expand // when length = ffff ffff, done
}
asm mov ax,ss
asm mov ds,ax
}
/*
======================
=
= CAL_CarmackExpand
=
= Length is the length of the EXPANDED data
=
======================
*/
#define NEARTAG 0xa7
#define FARTAG 0xa8
void CAL_CarmackExpand (unsigned far *source, unsigned far *dest, unsigned length)
{
unsigned ch,chhigh,count,offset;
unsigned far *copyptr, far *inptr, far *outptr;
length/=2;
inptr = source;
outptr = dest;
while (length)
{
ch = *inptr++;
chhigh = ch>>8;
if (chhigh == NEARTAG)
{
count = ch&0xff;
if (!count)
{ // have to insert a word containing the tag byte
ch |= *((unsigned char far *)inptr)++;
*outptr++ = ch;
length--;
}
else
{
offset = *((unsigned char far *)inptr)++;
copyptr = outptr - offset;
length -= count;
while (count--)
*outptr++ = *copyptr++;
}
}
else if (chhigh == FARTAG)
{
count = ch&0xff;
if (!count)
{ // have to insert a word containing the tag byte
ch |= *((unsigned char far *)inptr)++;
*outptr++ = ch;
length --;
}
else
{
offset = *inptr++;
copyptr = dest + offset;
length -= count;
while (count--)
*outptr++ = *copyptr++;
}
}
else
{
*outptr++ = ch;
length --;
}
}
}
/*
======================
=
= CA_RLEWcompress
=
======================
*/
long CA_RLEWCompress (unsigned huge *source, long length, unsigned huge *dest,
unsigned rlewtag)
{
long complength;
unsigned value,count,i;
unsigned huge *start,huge *end;
start = dest;
end = source + (length+1)/2;
//
// compress it
//
do
{
count = 1;
value = *source++;
while (*source == value && source<end)
{
count++;
source++;
}
if (count>3 || value == rlewtag)
{
//
// send a tag / count / value string
//
*dest++ = rlewtag;
*dest++ = count;
*dest++ = value;
}
else
{
//
// send word without compressing
//
for (i=1;i<=count;i++)
*dest++ = value;
}
} while (source<end);
complength = 2*(dest-start);
return complength;
}
/*
======================
=
= CA_RLEWexpand
= length is EXPANDED length
=
======================
*/
void CA_RLEWexpand (unsigned huge *source, unsigned huge *dest,long length,
unsigned rlewtag)
{
// unsigned value,count,i;
unsigned huge *end;
unsigned sourceseg,sourceoff,destseg,destoff,endseg,endoff;
//
// expand it
//
#if 0
do
{
value = *source++;
if (value != rlewtag)
//
// uncompressed
//
*dest++=value;
else
{
//
// compressed string
//
count = *source++;
value = *source++;
for (i=1;i<=count;i++)
*dest++ = value;
}
} while (dest<end);
#endif
end = dest + (length)/2;
sourceseg = FP_SEG(source);
sourceoff = FP_OFF(source);
destseg = FP_SEG(dest);
destoff = FP_OFF(dest);
endseg = FP_SEG(end);
endoff = FP_OFF(end);
//
// ax = source value
// bx = tag value
// cx = repeat counts
// dx = scratch
//
// NOTE: A repeat count that produces 0xfff0 bytes can blow this!
//
asm mov bx,rlewtag
asm mov si,sourceoff
asm mov di,destoff
asm mov es,destseg
asm mov ds,sourceseg
expand:
asm lodsw
asm cmp ax,bx
asm je repeat
asm stosw
asm jmp next
repeat:
asm lodsw
asm mov cx,ax // repeat count
asm lodsw // repeat value
asm rep stosw
next:
asm cmp si,0x10 // normalize ds:si
asm jb sinorm
asm mov ax,si
asm shr ax,1
asm shr ax,1
asm shr ax,1
asm shr ax,1
asm mov dx,ds
asm add dx,ax
asm mov ds,dx
asm and si,0xf
sinorm:
asm cmp di,0x10 // normalize es:di
asm jb dinorm
asm mov ax,di
asm shr ax,1
asm shr ax,1
asm shr ax,1
asm shr ax,1
asm mov dx,es
asm add dx,ax
asm mov es,dx
asm and di,0xf
dinorm:
asm cmp di,ss:endoff
asm jne expand
asm mov ax,es
asm cmp ax,ss:endseg
asm jb expand
asm mov ax,ss
asm mov ds,ax
}
/*
=============================================================================
CACHE MANAGER ROUTINES
=============================================================================
*/
/*
======================
=
= CAL_SetupGrFile
=
======================
*/
void CAL_SetupGrFile (void)
{
char fname[13];
int handle;
memptr compseg;
#ifdef GRHEADERLINKED
grhuffman = (huffnode *)&EGAdict;
grstarts = (long _seg *)FP_SEG(&EGAhead);
CAL_OptimizeNodes (grhuffman);
#else
//
// load ???dict.ext (huffman dictionary for graphics files)
//
strcpy(fname,gdictname);
strcat(fname,extension);
if ((handle = open(fname,
O_RDONLY | O_BINARY, S_IREAD)) == -1)
CA_CannotOpen(fname);
read(handle, &grhuffman, sizeof(grhuffman));
close(handle);
CAL_OptimizeNodes (grhuffman);
//
// load the data offsets from ???head.ext
//
MM_GetPtr (&(memptr)grstarts,(NUMCHUNKS+1)*FILEPOSSIZE);
strcpy(fname,gheadname);
strcat(fname,extension);
if ((handle = open(fname,
O_RDONLY | O_BINARY, S_IREAD)) == -1)
CA_CannotOpen(fname);
CA_FarRead(handle, (memptr)grstarts, (NUMCHUNKS+1)*FILEPOSSIZE);
close(handle);
#endif
//
// Open the graphics file, leaving it open until the game is finished
//
strcpy(fname,gfilename);
strcat(fname,extension);
grhandle = open(fname, O_RDONLY | O_BINARY);
if (grhandle == -1)
CA_CannotOpen(fname);
//
// load the pic and sprite headers into the arrays in the data segment
//
MM_GetPtr(&(memptr)pictable,NUMPICS*sizeof(pictabletype));
CAL_GetGrChunkLength(STRUCTPIC); // position file pointer
MM_GetPtr(&compseg,chunkcomplen);
CA_FarRead (grhandle,compseg,chunkcomplen);
CAL_HuffExpand (compseg, (byte huge *)pictable,NUMPICS*sizeof(pictabletype),grhuffman,false);
MM_FreePtr(&compseg);
}
//==========================================================================
/*
======================
=
= CAL_SetupMapFile
=
======================
*/
void CAL_SetupMapFile (void)
{
int i;
int handle;
long length,pos;
char fname[13];
//
// load maphead.ext (offsets and tileinfo for map file)
//
#ifndef MAPHEADERLINKED
strcpy(fname,mheadname);
strcat(fname,extension);
if ((handle = open(fname,
O_RDONLY | O_BINARY, S_IREAD)) == -1)
CA_CannotOpen(fname);
length = filelength(handle);
MM_GetPtr (&(memptr)tinf,length);
CA_FarRead(handle, tinf, length);
close(handle);
#else
tinf = (byte _seg *)FP_SEG(&maphead);
#endif
//
// open the data file
//
#ifdef CARMACIZED
strcpy(fname,"GAMEMAPS.");
strcat(fname,extension);
if ((maphandle = open(fname,
O_RDONLY | O_BINARY, S_IREAD)) == -1)
CA_CannotOpen(fname);
#else
strcpy(fname,mfilename);
strcat(fname,extension);
if ((maphandle = open(fname,
O_RDONLY | O_BINARY, S_IREAD)) == -1)
CA_CannotOpen(fname);
#endif
//
// load all map header
//
for (i=0;i<NUMMAPS;i++)
{
pos = ((mapfiletype _seg *)tinf)->headeroffsets[i];
if (pos<0) // $FFFFFFFF start is a sparse map
continue;
MM_GetPtr(&(memptr)mapheaderseg[i],sizeof(maptype));
MM_SetLock(&(memptr)mapheaderseg[i],true);
lseek(maphandle,pos,SEEK_SET);