3 // this has been customized for WOLF
\r
6 =============================================================================
\r
8 Id Software Caching Manager
\r
9 ---------------------------
\r
11 Must be started BEFORE the memory manager, because it needs to get the headers
\r
12 loaded into the data segment
\r
14 =============================================================================
\r
17 #include "ID_HEADS.H"
\r
23 #define THREEBYTEGRSTARTS
\r
26 =============================================================================
\r
30 =============================================================================
\r
35 unsigned bit0,bit1; // 0-255 is a character, > is a pointer to a node
\r
42 long headeroffsets[100];
\r
48 =============================================================================
\r
52 =============================================================================
\r
58 unsigned _seg *mapsegs[MAPPLANES];
\r
59 maptype _seg *mapheaderseg[NUMMAPS];
\r
60 byte _seg *audiosegs[NUMSNDCHUNKS];
\r
61 void _seg *grsegs[NUMCHUNKS];
\r
63 byte far grneeded[NUMCHUNKS];
\r
64 byte ca_levelbit,ca_levelnum;
\r
66 int profilehandle,debughandle;
\r
68 char audioname[13]="AUDIO.";
\r
71 =============================================================================
\r
75 =============================================================================
\r
78 extern long far CGAhead;
\r
79 extern long far EGAhead;
\r
80 extern byte CGAdict;
\r
81 extern byte EGAdict;
\r
82 extern byte far maphead;
\r
83 extern byte mapdict;
\r
84 extern byte far audiohead;
\r
85 extern byte audiodict;
\r
88 char extension[5], // Need a string, not constant to change cache files
\r
89 gheadname[10]=GREXT"HEAD.",
\r
90 gfilename[10]=GREXT"GRAPH.",
\r
91 gdictname[10]=GREXT"DICT.",
\r
92 mheadname[10]="MAPHEAD.",
\r
93 mfilename[10]="MAPTEMP.",
\r
94 aheadname[10]="AUDIOHED.",
\r
95 afilename[10]="AUDIOT.";
\r
97 void CA_CannotOpen(char *string);
\r
99 long _seg *grstarts; // array of offsets in egagraph, -1 for sparse
\r
100 long _seg *audiostarts; // array of offsets in audio / audiot
\r
102 #ifdef GRHEADERLINKED
\r
103 huffnode *grhuffman;
\r
105 huffnode grhuffman[255];
\r
108 #ifdef AUDIOHEADERLINKED
\r
109 huffnode *audiohuffman;
\r
111 huffnode audiohuffman[255];
\r
115 int grhandle; // handle to EGAGRAPH
\r
116 int maphandle; // handle to MAPTEMP / GAMEMAPS
\r
117 int audiohandle; // handle to AUDIOT / AUDIO
\r
119 long chunkcomplen,chunkexplen;
\r
121 SDMode oldsoundmode;
\r
125 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest,
\r
129 #ifdef THREEBYTEGRSTARTS
\r
130 #define FILEPOSSIZE 3
\r
131 //#define GRFILEPOS(c) (*(long far *)(((byte far *)grstarts)+(c)*3)&0xffffff)
\r
132 long GRFILEPOS(int c)
\r
139 value = *(long far *)(((byte far *)grstarts)+offset);
\r
141 value &= 0x00ffffffl;
\r
143 if (value == 0xffffffl)
\r
149 #define FILEPOSSIZE 4
\r
150 #define GRFILEPOS(c) (grstarts[c])
\r
154 =============================================================================
\r
158 =============================================================================
\r
162 ============================
\r
164 = CA_OpenDebug / CA_CloseDebug
\r
166 = Opens a binary file with the handle "debughandle"
\r
168 ============================
\r
171 void CA_OpenDebug (void)
\r
173 unlink ("DEBUG.TXT");
\r
174 debughandle = open("DEBUG.TXT", O_CREAT | O_WRONLY | O_TEXT);
\r
177 void CA_CloseDebug (void)
\r
179 close (debughandle);
\r
185 ============================
\r
187 = CAL_GetGrChunkLength
\r
189 = Gets the length of an explicit length chunk (not tiles)
\r
190 = The file pointer is positioned so the compressed data can be read in next.
\r
192 ============================
\r
195 void CAL_GetGrChunkLength (int chunk)
\r
197 lseek(grhandle,GRFILEPOS(chunk),SEEK_SET);
\r
198 read(grhandle,&chunkexplen,sizeof(chunkexplen));
\r
199 chunkcomplen = GRFILEPOS(chunk+1)-GRFILEPOS(chunk)-4;
\r
204 ==========================
\r
208 = Read from a file to a far pointer
\r
210 ==========================
\r
213 boolean CA_FarRead (int handle, byte far *dest, long length)
\r
215 if (length>0xffffl)
\r
216 Quit ("CA_FarRead doesn't support 64K reads yet!");
\r
219 asm mov bx,[handle]
\r
220 asm mov cx,[WORD PTR length]
\r
221 asm mov dx,[WORD PTR dest]
\r
222 asm mov ds,[WORD PTR dest+2]
\r
223 asm mov ah,0x3f // READ w/handle
\r
230 asm cmp ax,[WORD PTR length]
\r
232 errno = EINVFMT; // user manager knows this is bad read
\r
240 ==========================
\r
244 = Write from a file to a far pointer
\r
246 ==========================
\r
249 boolean CA_FarWrite (int handle, byte far *source, long length)
\r
251 if (length>0xffffl)
\r
252 Quit ("CA_FarWrite doesn't support 64K reads yet!");
\r
255 asm mov bx,[handle]
\r
256 asm mov cx,[WORD PTR length]
\r
257 asm mov dx,[WORD PTR source]
\r
258 asm mov ds,[WORD PTR source+2]
\r
259 asm mov ah,0x40 // WRITE w/handle
\r
266 asm cmp ax,[WORD PTR length]
\r
268 errno = ENOMEM; // user manager knows this is bad write
\r
277 ==========================
\r
281 = Reads a file into an allready allocated buffer
\r
283 ==========================
\r
286 boolean CA_ReadFile (char *filename, memptr *ptr)
\r
291 if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
294 size = filelength (handle);
\r
295 if (!CA_FarRead (handle,*ptr,size))
\r
306 ==========================
\r
310 = Writes a file from a memory buffer
\r
312 ==========================
\r
315 boolean CA_WriteFile (char *filename, void far *ptr, long length)
\r
320 handle = open(filename,O_CREAT | O_BINARY | O_WRONLY,
\r
321 S_IREAD | S_IWRITE | S_IFREG);
\r
326 if (!CA_FarWrite (handle,ptr,length))
\r
338 ==========================
\r
342 = Allocate space for and load a file
\r
344 ==========================
\r
347 boolean CA_LoadFile (char *filename, memptr *ptr)
\r
352 if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
355 size = filelength (handle);
\r
356 MM_GetPtr (ptr,size);
\r
357 if (!CA_FarRead (handle,*ptr,size))
\r
367 ============================================================================
\r
369 COMPRESSION routines, see JHUFF.C for more
\r
371 ============================================================================
\r
379 = CAL_OptimizeNodes
\r
381 = Goes through a huffman table and changes the 256-511 node numbers to the
\r
382 = actular address of the node. Must be called before CAL_HuffExpand
\r
387 void CAL_OptimizeNodes (huffnode *table)
\r
394 for (i=0;i<255;i++)
\r
396 if (node->bit0 >= 256)
\r
397 node->bit0 = (unsigned)(table+(node->bit0-256));
\r
398 if (node->bit1 >= 256)
\r
399 node->bit1 = (unsigned)(table+(node->bit1-256));
\r
407 ======================
\r
411 = Length is the length of the EXPANDED data
\r
412 = If screenhack, the data is decompressed in four planes directly
\r
415 ======================
\r
418 void CAL_HuffExpand (byte huge *source, byte huge *dest,
\r
419 long length,huffnode *hufftable, boolean screenhack)
\r
421 // unsigned bit,byte,node,code;
\r
422 unsigned sourceseg,sourceoff,destseg,destoff,endoff;
\r
425 // huffnode *nodeon;
\r
427 headptr = hufftable+254; // head node is allways node 254
\r
429 source++; // normalize
\r
437 asm mov dx,SC_INDEX
\r
438 asm mov ax,SC_MAPMASK + 256
\r
443 sourceseg = FP_SEG(source);
\r
444 sourceoff = FP_OFF(source);
\r
445 destseg = FP_SEG(dest);
\r
446 destoff = FP_OFF(dest);
\r
447 endoff = destoff+length;
\r
452 // ss:bx node pointer
\r
455 if (length <0xfff0)
\r
458 //--------------------------
\r
459 // expand less than 64k of data
\r
460 //--------------------------
\r
462 asm mov bx,[headptr]
\r
464 asm mov si,[sourceoff]
\r
465 asm mov di,[destoff]
\r
466 asm mov es,[destseg]
\r
467 asm mov ds,[sourceseg]
\r
468 asm mov ax,[endoff]
\r
470 asm mov ch,[si] // load first byte
\r
475 asm test ch,cl // bit set?
\r
477 asm mov dx,[ss:bx] // take bit0 path from node
\r
478 asm shl cl,1 // advance to next bit position
\r
479 asm jc newbyteshort
\r
480 asm jnc sourceupshort
\r
483 asm mov dx,[ss:bx+2] // take bit1 path
\r
484 asm shl cl,1 // advance to next bit position
\r
485 asm jnc sourceupshort
\r
488 asm mov ch,[si] // load next byte
\r
490 asm mov cl,1 // back to first bit
\r
493 asm or dh,dh // if dx<256 its a byte, else move node
\r
494 asm jz storebyteshort
\r
495 asm mov bx,dx // next node = (huffnode *)code
\r
496 asm jmp expandshort
\r
500 asm inc di // write a decopmpressed byte out
\r
501 asm mov bx,[headptr] // back to the head node for next bit
\r
503 asm cmp di,ax // done?
\r
504 asm jne expandshort
\r
507 // perform screenhack if needed
\r
509 asm test [screenhack],1
\r
511 asm shl [mapmask],1
\r
512 asm mov ah,[mapmask]
\r
514 asm je notscreen // all four planes done
\r
515 asm mov dx,SC_INDEX
\r
516 asm mov al,SC_MAPMASK
\r
518 asm mov di,[destoff]
\r
519 asm mov ax,[endoff]
\r
520 asm jmp expandshort
\r
527 //--------------------------
\r
528 // expand more than 64k of data
\r
529 //--------------------------
\r
533 asm mov bx,[headptr]
\r
536 asm mov si,[sourceoff]
\r
537 asm mov di,[destoff]
\r
538 asm mov es,[destseg]
\r
539 asm mov ds,[sourceseg]
\r
541 asm lodsb // load first byte
\r
544 asm test al,cl // bit set?
\r
546 asm mov dx,[ss:bx] // take bit0 path from node
\r
549 asm mov dx,[ss:bx+2] // take bit1 path
\r
552 asm shl cl,1 // advance to next bit position
\r
555 asm cmp si,0x10 // normalize ds:si
\r
562 asm mov cl,1 // back to first bit
\r
565 asm or dh,dh // if dx<256 its a byte, else move node
\r
567 asm mov bx,dx // next node = (huffnode *)code
\r
572 asm inc di // write a decopmpressed byte out
\r
573 asm mov bx,[headptr] // back to the head node for next bit
\r
575 asm cmp di,0x10 // normalize es:di
\r
583 asm sub [WORD PTR ss:length],1
\r
585 asm dec [WORD PTR ss:length+2]
\r
586 asm jns expand // when length = ffff ffff, done
\r
597 ======================
\r
599 = CAL_CarmackExpand
\r
601 = Length is the length of the EXPANDED data
\r
603 ======================
\r
606 #define NEARTAG 0xa7
\r
607 #define FARTAG 0xa8
\r
609 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest, unsigned length)
\r
611 unsigned ch,chhigh,count,offset;
\r
612 unsigned far *copyptr, far *inptr, far *outptr;
\r
623 if (chhigh == NEARTAG)
\r
627 { // have to insert a word containing the tag byte
\r
628 ch |= *((unsigned char far *)inptr)++;
\r
634 offset = *((unsigned char far *)inptr)++;
\r
635 copyptr = outptr - offset;
\r
638 *outptr++ = *copyptr++;
\r
641 else if (chhigh == FARTAG)
\r
645 { // have to insert a word containing the tag byte
\r
646 ch |= *((unsigned char far *)inptr)++;
\r
653 copyptr = dest + offset;
\r
656 *outptr++ = *copyptr++;
\r
670 ======================
\r
674 ======================
\r
677 long CA_RLEWCompress (unsigned huge *source, long length, unsigned huge *dest,
\r
681 unsigned value,count,i;
\r
682 unsigned huge *start,huge *end;
\r
686 end = source + (length+1)/2;
\r
695 while (*source == value && source<end)
\r
700 if (count>3 || value == rlewtag)
\r
703 // send a tag / count / value string
\r
712 // send word without compressing
\r
714 for (i=1;i<=count;i++)
\r
718 } while (source<end);
\r
720 complength = 2*(dest-start);
\r
726 ======================
\r
729 = length is EXPANDED length
\r
731 ======================
\r
734 void CA_RLEWexpand (unsigned huge *source, unsigned huge *dest,long length,
\r
737 // unsigned value,count,i;
\r
738 unsigned huge *end;
\r
739 unsigned sourceseg,sourceoff,destseg,destoff,endseg,endoff;
\r
749 if (value != rlewtag)
\r
757 // compressed string
\r
761 for (i=1;i<=count;i++)
\r
764 } while (dest<end);
\r
767 end = dest + (length)/2;
\r
768 sourceseg = FP_SEG(source);
\r
769 sourceoff = FP_OFF(source);
\r
770 destseg = FP_SEG(dest);
\r
771 destoff = FP_OFF(dest);
\r
772 endseg = FP_SEG(end);
\r
773 endoff = FP_OFF(end);
\r
777 // ax = source value
\r
779 // cx = repeat counts
\r
782 // NOTE: A repeat count that produces 0xfff0 bytes can blow this!
\r
786 asm mov si,sourceoff
\r
789 asm mov ds,sourceseg
\r
800 asm mov cx,ax // repeat count
\r
801 asm lodsw // repeat value
\r
806 asm cmp si,0x10 // normalize ds:si
\r
818 asm cmp di,0x10 // normalize es:di
\r
831 asm cmp di,ss:endoff
\r
834 asm cmp ax,ss:endseg
\r
845 =============================================================================
\r
847 CACHE MANAGER ROUTINES
\r
849 =============================================================================
\r
854 ======================
\r
858 ======================
\r
861 void CAL_SetupGrFile (void)
\r
867 #ifdef GRHEADERLINKED
\r
869 grhuffman = (huffnode *)&EGAdict;
\r
870 grstarts = (long _seg *)FP_SEG(&EGAhead);
\r
872 CAL_OptimizeNodes (grhuffman);
\r
877 // load ???dict.ext (huffman dictionary for graphics files)
\r
880 strcpy(fname,gdictname);
\r
881 strcat(fname,extension);
\r
883 if ((handle = open(fname,
\r
884 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
885 CA_CannotOpen(fname);
\r
887 read(handle, &grhuffman, sizeof(grhuffman));
\r
889 CAL_OptimizeNodes (grhuffman);
\r
891 // load the data offsets from ???head.ext
\r
893 MM_GetPtr (&(memptr)grstarts,(NUMCHUNKS+1)*FILEPOSSIZE);
\r
895 strcpy(fname,gheadname);
\r
896 strcat(fname,extension);
\r
898 if ((handle = open(fname,
\r
899 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
900 CA_CannotOpen(fname);
\r
902 CA_FarRead(handle, (memptr)grstarts, (NUMCHUNKS+1)*FILEPOSSIZE);
\r
910 // Open the graphics file, leaving it open until the game is finished
\r
912 strcpy(fname,gfilename);
\r
913 strcat(fname,extension);
\r
915 grhandle = open(fname, O_RDONLY | O_BINARY);
\r
916 if (grhandle == -1)
\r
917 CA_CannotOpen(fname);
\r
921 // load the pic and sprite headers into the arrays in the data segment
\r
923 MM_GetPtr(&(memptr)pictable,NUMPICS*sizeof(pictabletype));
\r
924 CAL_GetGrChunkLength(STRUCTPIC); // position file pointer
\r
925 MM_GetPtr(&compseg,chunkcomplen);
\r
926 CA_FarRead (grhandle,compseg,chunkcomplen);
\r
927 CAL_HuffExpand (compseg, (byte huge *)pictable,NUMPICS*sizeof(pictabletype),grhuffman,false);
\r
928 MM_FreePtr(&compseg);
\r
931 //==========================================================================
\r
935 ======================
\r
939 ======================
\r
942 void CAL_SetupMapFile (void)
\r
950 // load maphead.ext (offsets and tileinfo for map file)
\r
952 #ifndef MAPHEADERLINKED
\r
953 strcpy(fname,mheadname);
\r
954 strcat(fname,extension);
\r
956 if ((handle = open(fname,
\r
957 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
958 CA_CannotOpen(fname);
\r
960 length = filelength(handle);
\r
961 MM_GetPtr (&(memptr)tinf,length);
\r
962 CA_FarRead(handle, tinf, length);
\r
966 tinf = (byte _seg *)FP_SEG(&maphead);
\r
971 // open the data file
\r
974 strcpy(fname,"GAMEMAPS.");
\r
975 strcat(fname,extension);
\r
977 if ((maphandle = open(fname,
\r
978 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
979 CA_CannotOpen(fname);
\r
981 strcpy(fname,mfilename);
\r
982 strcat(fname,extension);
\r
984 if ((maphandle = open(fname,
\r
985 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
986 CA_CannotOpen(fname);
\r
990 // load all map header
\r
992 for (i=0;i<NUMMAPS;i++)
\r
994 pos = ((mapfiletype _seg *)tinf)->headeroffsets[i];
\r
995 if (pos<0) // $FFFFFFFF start is a sparse map
\r
998 MM_GetPtr(&(memptr)mapheaderseg[i],sizeof(maptype));
\r
999 MM_SetLock(&(memptr)mapheaderseg[i],true);
\r
1000 lseek(maphandle,pos,SEEK_SET);
\r
1001 CA_FarRead (maphandle,(memptr)mapheaderseg[i],sizeof(maptype));
\r
1005 // allocate space for 3 64*64 planes
\r
1007 for (i=0;i<MAPPLANES;i++)
\r
1009 MM_GetPtr (&(memptr)mapsegs[i],64*64*2);
\r
1010 MM_SetLock (&(memptr)mapsegs[i],true);
\r
1015 //==========================================================================
\r
1019 ======================
\r
1021 = CAL_SetupAudioFile
\r
1023 ======================
\r
1026 void CAL_SetupAudioFile (void)
\r
1033 // load maphead.ext (offsets and tileinfo for map file)
\r
1035 #ifndef AUDIOHEADERLINKED
\r
1036 strcpy(fname,aheadname);
\r
1037 strcat(fname,extension);
\r
1039 if ((handle = open(fname,
\r
1040 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1041 CA_CannotOpen(fname);
\r
1043 length = filelength(handle);
\r
1044 MM_GetPtr (&(memptr)audiostarts,length);
\r
1045 CA_FarRead(handle, (byte far *)audiostarts, length);
\r
1048 audiohuffman = (huffnode *)&audiodict;
\r
1049 CAL_OptimizeNodes (audiohuffman);
\r
1050 audiostarts = (long _seg *)FP_SEG(&audiohead);
\r
1054 // open the data file
\r
1056 #ifndef AUDIOHEADERLINKED
\r
1057 strcpy(fname,afilename);
\r
1058 strcat(fname,extension);
\r
1060 if ((audiohandle = open(fname,
\r
1061 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1062 CA_CannotOpen(fname);
\r
1064 if ((audiohandle = open("AUDIO."EXTENSION,
\r
1065 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1066 Quit ("Can't open AUDIO."EXTENSION"!");
\r
1070 //==========================================================================
\r
1074 ======================
\r
1078 = Open all files and load in headers
\r
1080 ======================
\r
1083 void CA_Startup (void)
\r
1086 unlink ("PROFILE.TXT");
\r
1087 profilehandle = open("PROFILE.TXT", O_CREAT | O_WRONLY | O_TEXT);
\r
1090 CAL_SetupMapFile ();
\r
1091 CAL_SetupGrFile ();
\r
1092 CAL_SetupAudioFile ();
\r
1100 //==========================================================================
\r
1104 ======================
\r
1108 = Closes all files
\r
1110 ======================
\r
1113 void CA_Shutdown (void)
\r
1116 close (profilehandle);
\r
1119 close (maphandle);
\r
1121 close (audiohandle);
\r
1124 //===========================================================================
\r
1127 ======================
\r
1129 = CA_CacheAudioChunk
\r
1131 ======================
\r
1134 void CA_CacheAudioChunk (int chunk)
\r
1136 long pos,compressed;
\r
1137 #ifdef AUDIOHEADERLINKED
\r
1139 memptr bigbufferseg;
\r
1143 if (audiosegs[chunk])
\r
1145 MM_SetPurge (&(memptr)audiosegs[chunk],0);
\r
1146 return; // allready in memory
\r
1150 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1151 // a larger buffer
\r
1153 pos = audiostarts[chunk];
\r
1154 compressed = audiostarts[chunk+1]-pos;
\r
1156 lseek(audiohandle,pos,SEEK_SET);
\r
1158 #ifndef AUDIOHEADERLINKED
\r
1160 MM_GetPtr (&(memptr)audiosegs[chunk],compressed);
\r
1164 CA_FarRead(audiohandle,audiosegs[chunk],compressed);
\r
1168 if (compressed<=BUFFERSIZE)
\r
1170 CA_FarRead(audiohandle,bufferseg,compressed);
\r
1171 source = bufferseg;
\r
1175 MM_GetPtr(&bigbufferseg,compressed);
\r
1178 MM_SetLock (&bigbufferseg,true);
\r
1179 CA_FarRead(audiohandle,bigbufferseg,compressed);
\r
1180 source = bigbufferseg;
\r
1183 expanded = *(long far *)source;
\r
1184 source += 4; // skip over length
\r
1185 MM_GetPtr (&(memptr)audiosegs[chunk],expanded);
\r
1188 CAL_HuffExpand (source,audiosegs[chunk],expanded,audiohuffman,false);
\r
1191 if (compressed>BUFFERSIZE)
\r
1192 MM_FreePtr(&bigbufferseg);
\r
1196 //===========================================================================
\r
1199 ======================
\r
1201 = CA_LoadAllSounds
\r
1203 = Purges all sounds, then loads all new ones (mode switch)
\r
1205 ======================
\r
1208 void CA_LoadAllSounds (void)
\r
1212 switch (oldsoundmode)
\r
1217 start = STARTPCSOUNDS;
\r
1220 start = STARTADLIBSOUNDS;
\r
1224 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1225 if (audiosegs[start])
\r
1226 MM_SetPurge (&(memptr)audiosegs[start],3); // make purgable
\r
1230 switch (SoundMode)
\r
1235 start = STARTPCSOUNDS;
\r
1238 start = STARTADLIBSOUNDS;
\r
1242 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1243 CA_CacheAudioChunk (start);
\r
1245 oldsoundmode = SoundMode;
\r
1248 //===========================================================================
\r
1252 ======================
\r
1254 = CAL_ExpandGrChunk
\r
1256 = Does whatever is needed with a pointer to a compressed chunk
\r
1258 ======================
\r
1261 void CAL_ExpandGrChunk (int chunk, byte far *source)
\r
1266 if (chunk >= STARTTILE8 && chunk < STARTEXTERNS)
\r
1269 // expanded sizes of tile8/16/32 are implicit
\r
1273 #define MASKBLOCK 128
\r
1275 if (chunk<STARTTILE8M) // tile 8s are all in one chunk!
\r
1276 expanded = BLOCK*NUMTILE8;
\r
1277 else if (chunk<STARTTILE16)
\r
1278 expanded = MASKBLOCK*NUMTILE8M;
\r
1279 else if (chunk<STARTTILE16M) // all other tiles are one/chunk
\r
1280 expanded = BLOCK*4;
\r
1281 else if (chunk<STARTTILE32)
\r
1282 expanded = MASKBLOCK*4;
\r
1283 else if (chunk<STARTTILE32M)
\r
1284 expanded = BLOCK*16;
\r
1286 expanded = MASKBLOCK*16;
\r
1291 // everything else has an explicit size longword
\r
1293 expanded = *(long far *)source;
\r
1294 source += 4; // skip over length
\r
1298 // allocate final space, decompress it, and free bigbuffer
\r
1299 // Sprites need to have shifts made and various other junk
\r
1301 MM_GetPtr (&grsegs[chunk],expanded);
\r
1304 CAL_HuffExpand (source,grsegs[chunk],expanded,grhuffman,false);
\r
1309 ======================
\r
1313 = Makes sure a given chunk is in memory, loadiing it if needed
\r
1315 ======================
\r
1318 void CA_CacheGrChunk (int chunk)
\r
1320 long pos,compressed;
\r
1321 memptr bigbufferseg;
\r
1325 grneeded[chunk] |= ca_levelbit; // make sure it doesn't get removed
\r
1326 if (grsegs[chunk])
\r
1328 MM_SetPurge (&grsegs[chunk],0);
\r
1329 return; // allready in memory
\r
1333 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1334 // a larger buffer
\r
1336 pos = GRFILEPOS(chunk);
\r
1337 if (pos<0) // $FFFFFFFF start is a sparse tile
\r
1341 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1344 compressed = GRFILEPOS(next)-pos;
\r
1346 lseek(grhandle,pos,SEEK_SET);
\r
1348 if (compressed<=BUFFERSIZE)
\r
1350 CA_FarRead(grhandle,bufferseg,compressed);
\r
1351 source = bufferseg;
\r
1355 MM_GetPtr(&bigbufferseg,compressed);
\r
1356 MM_SetLock (&bigbufferseg,true);
\r
1357 CA_FarRead(grhandle,bigbufferseg,compressed);
\r
1358 source = bigbufferseg;
\r
1361 CAL_ExpandGrChunk (chunk,source);
\r
1363 if (compressed>BUFFERSIZE)
\r
1364 MM_FreePtr(&bigbufferseg);
\r
1369 //==========================================================================
\r
1372 ======================
\r
1376 = Decompresses a chunk from disk straight onto the screen
\r
1378 ======================
\r
1381 void CA_CacheScreen (int chunk)
\r
1383 long pos,compressed,expanded;
\r
1384 memptr bigbufferseg;
\r
1389 // load the chunk into a buffer
\r
1391 pos = GRFILEPOS(chunk);
\r
1393 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1395 compressed = GRFILEPOS(next)-pos;
\r
1397 lseek(grhandle,pos,SEEK_SET);
\r
1399 MM_GetPtr(&bigbufferseg,compressed);
\r
1400 MM_SetLock (&bigbufferseg,true);
\r
1401 CA_FarRead(grhandle,bigbufferseg,compressed);
\r
1402 source = bigbufferseg;
\r
1404 expanded = *(long far *)source;
\r
1405 source += 4; // skip over length
\r
1408 // allocate final space, decompress it, and free bigbuffer
\r
1409 // Sprites need to have shifts made and various other junk
\r
1411 CAL_HuffExpand (source,MK_FP(SCREENSEG,bufferofs),expanded,grhuffman,true);
\r
1412 VW_MarkUpdateBlock (0,0,319,199);
\r
1413 MM_FreePtr(&bigbufferseg);
\r
1416 //==========================================================================
\r
1419 ======================
\r
1423 = WOLF: This is specialized for a 64*64 map size
\r
1425 ======================
\r
1428 void CA_CacheMap (int mapnum)
\r
1430 long pos,compressed;
\r
1432 memptr *dest,bigbufferseg;
\r
1434 unsigned far *source;
\r
1436 memptr buffer2seg;
\r
1443 // load the planes into the allready allocated buffers
\r
1447 for (plane = 0; plane<MAPPLANES; plane++)
\r
1449 pos = mapheaderseg[mapnum]->planestart[plane];
\r
1450 compressed = mapheaderseg[mapnum]->planelength[plane];
\r
1452 dest = &(memptr)mapsegs[plane];
\r
1454 lseek(maphandle,pos,SEEK_SET);
\r
1455 if (compressed<=BUFFERSIZE)
\r
1456 source = bufferseg;
\r
1459 MM_GetPtr(&bigbufferseg,compressed);
\r
1460 MM_SetLock (&bigbufferseg,true);
\r
1461 source = bigbufferseg;
\r
1464 CA_FarRead(maphandle,(byte far *)source,compressed);
\r
1467 // unhuffman, then unRLEW
\r
1468 // The huffman'd chunk has a two byte expanded length first
\r
1469 // The resulting RLEW chunk also does, even though it's not really
\r
1472 expanded = *source;
\r
1474 MM_GetPtr (&buffer2seg,expanded);
\r
1475 CAL_CarmackExpand (source, (unsigned far *)buffer2seg,expanded);
\r
1476 CA_RLEWexpand (((unsigned far *)buffer2seg)+1,*dest,size,
\r
1477 ((mapfiletype _seg *)tinf)->RLEWtag);
\r
1478 MM_FreePtr (&buffer2seg);
\r
1482 // unRLEW, skipping expanded length
\r
1484 CA_RLEWexpand (source+1, *dest,size,
\r
1485 ((mapfiletype _seg *)tinf)->RLEWtag);
\r
1488 if (compressed>BUFFERSIZE)
\r
1489 MM_FreePtr(&bigbufferseg);
\r
1493 //===========================================================================
\r
1496 ======================
\r
1500 = Goes up a bit level in the needed lists and clears it out.
\r
1501 = Everything is made purgable
\r
1503 ======================
\r
1506 void CA_UpLevel (void)
\r
1510 if (ca_levelnum==7)
\r
1511 Quit ("CA_UpLevel: Up past level 7!");
\r
1513 for (i=0;i<NUMCHUNKS;i++)
\r
1515 MM_SetPurge (&(memptr)grsegs[i],3);
\r
1520 //===========================================================================
\r
1523 ======================
\r
1527 = Goes down a bit level in the needed lists and recaches
\r
1528 = everything from the lower level
\r
1530 ======================
\r
1533 void CA_DownLevel (void)
\r
1536 Quit ("CA_DownLevel: Down past level 0!");
\r
1542 //===========================================================================
\r
1545 ======================
\r
1549 = Clears out all the marks at the current level
\r
1551 ======================
\r
1554 void CA_ClearMarks (void)
\r
1558 for (i=0;i<NUMCHUNKS;i++)
\r
1559 grneeded[i]&=~ca_levelbit;
\r
1563 //===========================================================================
\r
1566 ======================
\r
1568 = CA_ClearAllMarks
\r
1570 = Clears out all the marks on all the levels
\r
1572 ======================
\r
1575 void CA_ClearAllMarks (void)
\r
1577 _fmemset (grneeded,0,sizeof(grneeded));
\r
1583 //===========================================================================
\r
1587 ======================
\r
1591 ======================
\r
1595 void CA_SetGrPurge (void)
\r
1604 for (i=0;i<NUMCHUNKS;i++)
\r
1606 MM_SetPurge (&(memptr)grsegs[i],3);
\r
1612 ======================
\r
1616 = Make everything possible purgable
\r
1618 ======================
\r
1621 void CA_SetAllPurge (void)
\r
1629 for (i=0;i<NUMSNDCHUNKS;i++)
\r
1631 MM_SetPurge (&(memptr)audiosegs[i],3);
\r
1640 //===========================================================================
\r
1643 ======================
\r
1647 ======================
\r
1649 #define MAXEMPTYREAD 1024
\r
1651 void CA_CacheMarks (void)
\r
1653 int i,next,numcache;
\r
1654 long pos,endpos,nextpos,nextendpos,compressed;
\r
1655 long bufferstart,bufferend; // file position of general buffer
\r
1657 memptr bigbufferseg;
\r
1661 // go through and make everything not needed purgable
\r
1663 for (i=0;i<NUMCHUNKS;i++)
\r
1664 if (grneeded[i]&ca_levelbit)
\r
1666 if (grsegs[i]) // its allready in memory, make
\r
1667 MM_SetPurge(&grsegs[i],0); // sure it stays there!
\r
1673 if (grsegs[i]) // not needed, so make it purgeable
\r
1674 MM_SetPurge(&grsegs[i],3);
\r
1677 if (!numcache) // nothing to cache!
\r
1682 // go through and load in anything still needed
\r
1684 bufferstart = bufferend = 0; // nothing good in buffer now
\r
1686 for (i=0;i<NUMCHUNKS;i++)
\r
1687 if ( (grneeded[i]&ca_levelbit) && !grsegs[i])
\r
1689 pos = GRFILEPOS(i);
\r
1694 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1697 compressed = GRFILEPOS(next)-pos;
\r
1698 endpos = pos+compressed;
\r
1700 if (compressed<=BUFFERSIZE)
\r
1702 if (bufferstart<=pos
\r
1703 && bufferend>= endpos)
\r
1705 // data is allready in buffer
\r
1706 source = (byte _seg *)bufferseg+(pos-bufferstart);
\r
1710 // load buffer with a new block from disk
\r
1711 // try to get as many of the needed blocks in as possible
\r
1712 while ( next < NUMCHUNKS )
\r
1714 while (next < NUMCHUNKS &&
\r
1715 !(grneeded[next]&ca_levelbit && !grsegs[next]))
\r
1717 if (next == NUMCHUNKS)
\r
1720 nextpos = GRFILEPOS(next);
\r
1721 while (GRFILEPOS(++next) == -1) // skip past any sparse tiles
\r
1723 nextendpos = GRFILEPOS(next);
\r
1724 if (nextpos - endpos <= MAXEMPTYREAD
\r
1725 && nextendpos-pos <= BUFFERSIZE)
\r
1726 endpos = nextendpos;
\r
1728 next = NUMCHUNKS; // read pos to posend
\r
1731 lseek(grhandle,pos,SEEK_SET);
\r
1732 CA_FarRead(grhandle,bufferseg,endpos-pos);
\r
1733 bufferstart = pos;
\r
1734 bufferend = endpos;
\r
1735 source = bufferseg;
\r
1740 // big chunk, allocate temporary buffer
\r
1741 MM_GetPtr(&bigbufferseg,compressed);
\r
1744 MM_SetLock (&bigbufferseg,true);
\r
1745 lseek(grhandle,pos,SEEK_SET);
\r
1746 CA_FarRead(grhandle,bigbufferseg,compressed);
\r
1747 source = bigbufferseg;
\r
1750 CAL_ExpandGrChunk (i,source);
\r
1754 if (compressed>BUFFERSIZE)
\r
1755 MM_FreePtr(&bigbufferseg);
\r
1760 void CA_CannotOpen(char *string)
\r
1764 strcpy(str,"Can't open ");
\r
1765 strcat(str,string);
\r
1766 strcat(str,"!\n");
\r