1 /* Catacomb Apocalypse Source Code
\r
2 * Copyright (C) 1993-2014 Flat Rock Software
\r
4 * This program is free software; you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation; either version 2 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License along
\r
15 * with this program; if not, write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
22 =============================================================================
\r
24 Id Software Caching Manager
\r
25 ---------------------------
\r
27 Must be started BEFORE the memory manager, because it needs to get the headers
\r
28 loaded into the data segment
\r
30 =============================================================================
\r
33 #include "src/lib/16_ca.h"
\r
39 #define THREEBYTEGRSTARTS
\r
40 //https://github.com/open-watcom/open-watcom-v2/issues/279#issuecomment-244587566 for _seg
\r
41 //http://www.shikadi.net/moddingwiki/GameMaps_Format for info on the code
\r
43 =============================================================================
\r
47 =============================================================================
\r
52 word bit0,bit1; // 0-255 is a character, > is a pointer to a node
\r
59 long headeroffsets[100];
\r
65 =============================================================================
\r
69 =============================================================================
\r
75 unsigned _seg *mapsegs[3];
\r
76 maptype _seg *mapheaderseg[NUMMAPS];
\r
77 byte _seg *audiosegs[NUMSNDCHUNKS];
\r
78 void _seg *grsegs[NUMCHUNKS];
\r
80 byte far grneeded[NUMCHUNKS];*/
\r
82 void (*drawcachebox) (char *title, unsigned numcache);
\r
83 void (*updatecachebox) (void);
\r
84 void (*finishcachebox) (void);
\r
87 =============================================================================
\r
91 =============================================================================
\r
94 /*extern long far CGAhead;
\r
95 extern long far EGAhead;
\r
96 extern byte CGAdict;
\r
97 extern byte EGAdict;
\r
98 extern byte far maphead;
\r
99 extern byte mapdict;
\r
100 extern byte far audiohead;
\r
101 extern byte audiodict;*/
\r
103 void CA_CannotOpen(char *string, global_game_variables_t *gvar);
\r
105 /*long _seg *grstarts; // array of offsets in egagraph, -1 for sparse
\r
106 long _seg *audiostarts; // array of offsets in audio / audiot
\r
108 #ifdef GRHEADERLINKED
\r
109 huffnode *gvar->ca.grhuffman;
\r
111 huffnode gvar->ca.grhuffman[255];
\r
114 #ifdef AUDIOHEADERLINKED
\r
115 huffnode *audiohuffman;
\r
117 huffnode audiohuffman[255];
\r
121 int grhandle; // handle to EGAGRAPH
\r
122 int maphandle; // handle to MAPTEMP / GAMEMAPS
\r
123 int audiohandle; // handle to AUDIOT / AUDIO
\r
125 long chunkcomplen,chunkexplen;
\r
127 SDMode oldsoundmode;
\r
131 void CAL_DialogDraw (char *title,unsigned numcache);
\r
132 void CAL_DialogUpdate (void);
\r
133 void CAL_DialogFinish (void);*/
\r
134 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest,
\r
138 #ifdef THREEBYTEGRSTARTS
\r
139 #define FILEPOSSIZE 3
\r
140 //#define GRFILEPOS(c) (*(long far *)(((byte far *)grstarts)+(c)*3)&0xffffff)
\r
141 CASVT GRFILEPOS(int c, global_game_variables_t *gvar)
\r
148 value = *(CASVT far *)(((byte far *)gvar->ca.grstarts)+offset);
\r
150 value &= 0x00ffffffl;
\r
152 if (value == 0xffffffl)
\r
158 #define FILEPOSSIZE 4
\r
159 //#define GRFILEPOS(c) (gvar->ca.grstarts[c])
\r
160 CASVT GRFILEPOS(int c, global_game_variables_t *gvar)
\r
162 return gvar->ca.grstarts[c];
\r
166 #define EXTENSION "hb1"
\r
169 =============================================================================
\r
173 =============================================================================
\r
177 ============================
\r
179 = CA_OpenDebug / CA_CloseDebug
\r
181 = Opens a binary file with the handle "debughandle"
\r
183 ============================
\r
185 void CA_OpenDebug(global_game_variables_t *gvar)
\r
187 #ifdef __BORLANDC__
\r
188 unlink("debug.16b");
\r
189 gvar->handle.debughandle = open("debug.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
192 unlink("debug.16w");
\r
193 gvar->handle.debughandle = open("debug.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
197 void CA_CloseDebug(global_game_variables_t *gvar)
\r
199 close(gvar->handle.debughandle);
\r
205 ============================
\r
207 = CAL_GetGrChunkLength
\r
209 = Gets the length of an explicit length chunk (not tiles)
\r
210 = The file pointer is positioned so the compressed data can be read in next.
\r
212 ============================
\r
215 void CAL_GetGrChunkLength (int chunk,global_game_variables_t *gvar)
\r
217 lseek(gvar->ca.file.grhandle,GRFILEPOS(chunk,gvar),SEEK_SET);
\r
218 read(gvar->ca.file.grhandle,&gvar->ca.chunkexplen,sizeof(gvar->ca.chunkexplen));
\r
219 gvar->ca.chunkcomplen = GRFILEPOS(chunk+1,gvar)-GRFILEPOS(chunk,gvar)-4;
\r
224 ==========================
\r
228 = Read from a file to a far pointer
\r
230 ==========================
\r
233 boolean CA_FarRead(int handle, byte far *dest, dword length, global_game_variables_t *gvar)
\r
236 if (length>0xfffflu)
\r
237 Quit (gvar, "CA_FarRead doesn't support 64K reads yet!");//TODO: EXPAND!!!
\r
242 mov cx,[WORD PTR length]
\r
243 mov dx,[WORD PTR dest]
\r
244 mov ds,[WORD PTR dest+2]
\r
245 mov ah,0x3f // READ w/handle
\r
252 #ifdef __BORLANDC__
\r
256 #ifdef __BORLANDC__
\r
259 cmp ax,[WORD PTR length]
\r
261 // errno = EINVFMT; // user manager knows this is bad read
\r
264 #ifdef __BORLANDC__
\r
268 #ifdef __BORLANDC__
\r
272 #ifdef __BORLANDC__
\r
284 ==========================
\r
288 = Write from a file to a far pointer
\r
290 ==========================
\r
293 boolean CA_FarWrite(int handle, byte far *source, dword length, global_game_variables_t *gvar)
\r
296 if (length>0xfffflu)
\r
297 Quit (gvar, "CA_FarWrite doesn't support 64K reads yet!");//TODO: EXPAND!!!
\r
302 mov cx,[WORD PTR length]
\r
303 mov dx,[WORD PTR source]
\r
304 mov ds,[WORD PTR source+2]
\r
305 mov ah,0x40 // WRITE w/handle
\r
312 #ifdef __BORLANDC__
\r
316 #ifdef __BORLANDC__
\r
319 cmp ax,[WORD PTR length]
\r
321 // errno = ENOMEM; // user manager knows this is bad write
\r
324 #ifdef __BORLANDC__
\r
328 #ifdef __BORLANDC__
\r
332 #ifdef __BORLANDC__
\r
344 ==========================
\r
348 = Reads a file into an allready allocated buffer
\r
350 ==========================
\r
353 boolean CA_ReadFile(char *filename, memptr *ptr, global_game_variables_t *gvar)
\r
359 if((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
362 size = filelength(handle);
\r
363 #ifdef __DEBUG_CA__
\r
365 printf("===============================================================================\n");
\r
366 printf(" CA_ReadFile\n");
\r
367 printf("===============================================================================\n");
\r
369 printf(" ptr=%Fp\n", ptr);
\r
370 printf(" *ptr=%Fp\n", *ptr);
\r
371 printf(" &ptr=%Fp\n", &ptr);
\r
374 if(!CA_FarRead(handle,*ptr,size, gvar))
\r
385 ==========================
\r
389 = Writes a file from a memory buffer
\r
391 ==========================
\r
394 boolean CA_WriteFile (char *filename, void far *ptr, long length, global_game_variables_t *gvar)
\r
400 handle = open(filename,O_CREAT | O_BINARY | O_WRONLY,
\r
401 S_IREAD | S_IWRITE | S_IFREG);
\r
406 if (!CA_FarWrite (handle,ptr,length, gvar))
\r
418 ==========================
\r
422 = Allocate space for and load a file
\r
424 ==========================
\r
427 boolean CA_LoadFile(char *filename, memptr *ptr, global_game_variables_t *gvar)
\r
433 if((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
436 size = filelength(handle);
\r
437 #ifdef __DEBUG_CA__
\r
439 printf("===============================================================================\n");
\r
440 printf(" CA_LoadFile\n");
\r
441 printf("===============================================================================\n");
\r
443 printf(" ptr=%Fp\n", ptr);
\r
444 printf(" *ptr=%Fp\n", *ptr);
\r
445 printf(" &ptr=%Fp\n", &ptr);
\r
448 MM_GetPtr(ptr,size, gvar);
\r
449 #ifdef __DEBUG_CA__
\r
452 printf("---------------------------------------\n");
\r
453 printf(" ptr=%Fp\n", ptr);
\r
454 printf(" *ptr=%Fp\n", *ptr);
\r
455 printf(" &ptr=%Fp\n", &ptr);
\r
456 printf("-------------------------------------------------------------------------------\n");
\r
459 if(!CA_FarRead(handle,*ptr,size, gvar))
\r
469 ============================================================================
\r
471 COMPRESSION routines, see JHUFF.C for more
\r
473 ============================================================================
\r
481 = CAL_OptimizeNodes
\r
483 = Goes through a huffman table and changes the 256-511 node numbers to the
\r
484 = actular address of the node. Must be called before CAL_HuffExpand
\r
489 void CAL_OptimizeNodes(huffnode *table)
\r
496 for (i=0;i<255;i++)
\r
498 if (node->bit0 >= 256)
\r
499 node->bit0 = (unsigned)(table+(node->bit0-256));
\r
500 if (node->bit1 >= 256)
\r
501 node->bit1 = (unsigned)(table+(node->bit1-256));
\r
509 ======================
\r
513 = Length is the length of the EXPANDED data
\r
515 ======================
\r
518 void CAL_HuffExpand (byte far *source, byte far *dest,
\r
519 long length,huffnode *hufftable)
\r
521 // unsigned bit,byte,node,code;
\r
522 unsigned sourceseg,sourceoff,destseg,destoff,endoff;
\r
524 // huffnode *nodeon;
\r
526 headptr = hufftable+254; // head node is allways node 254
\r
528 source++; // normalize
\r
533 sourceseg = FP_SEG(source);
\r
534 sourceoff = FP_OFF(source);
\r
535 destseg = FP_SEG(dest);
\r
536 destoff = FP_OFF(dest);
\r
537 endoff = destoff+length;
\r
542 // ss:bx node pointer
\r
545 if (length <0xfff0)
\r
548 //--------------------------
\r
549 // expand less than 64k of data
\r
550 //--------------------------
\r
553 mov bx,[word ptr headptr]
\r
561 mov ch,[si] // load first byte
\r
564 #ifdef __BORLANDC__
\r
568 #ifdef __BORLANDC__
\r
571 test ch,cl // bit set?
\r
573 mov dx,[ss:bx] // take bit0 path from node
\r
574 shl cl,1 // advance to next bit position
\r
577 #ifdef __BORLANDC__
\r
581 #ifdef __BORLANDC__
\r
584 mov dx,[ss:bx+2] // take bit1 path
\r
585 shl cl,1 // advance to next bit position
\r
587 #ifdef __BORLANDC__
\r
591 #ifdef __BORLANDC__
\r
594 mov ch,[si] // load next byte
\r
596 mov cl,1 // back to first bit
\r
597 #ifdef __BORLANDC__
\r
601 #ifdef __BORLANDC__
\r
604 or dh,dh // if dx<256 its a byte, else move node
\r
606 mov bx,dx // next node = (huffnode *)code
\r
608 #ifdef __BORLANDC__
\r
612 #ifdef __BORLANDC__
\r
616 inc di // write a decopmpressed byte out
\r
617 mov bx,[word ptr headptr] // back to the head node for next bit
\r
626 //--------------------------
\r
627 // expand more than 64k of data
\r
628 //--------------------------
\r
633 mov bx,[word ptr headptr]
\r
641 lodsb // load first byte
\r
642 #ifdef __BORLANDC__
\r
646 #ifdef __BORLANDC__
\r
649 test al,cl // bit set?
\r
651 mov dx,[ss:bx] // take bit0 path from node
\r
653 #ifdef __BORLANDC__
\r
657 #ifdef __BORLANDC__
\r
660 mov dx,[ss:bx+2] // take bit1 path
\r
661 #ifdef __BORLANDC__
\r
665 #ifdef __BORLANDC__
\r
668 shl cl,1 // advance to next bit position
\r
671 cmp si,0x10 // normalize ds:si
\r
677 #ifdef __BORLANDC__
\r
681 #ifdef __BORLANDC__
\r
684 mov cl,1 // back to first bit
\r
685 #ifdef __BORLANDC__
\r
689 #ifdef __BORLANDC__
\r
692 or dh,dh // if dx<256 its a byte, else move node
\r
694 mov bx,dx // next node = (huffnode *)code
\r
696 #ifdef __BORLANDC__
\r
700 #ifdef __BORLANDC__
\r
704 inc di // write a decopmpressed byte out
\r
705 mov bx,[word ptr headptr] // back to the head node for next bit
\r
707 cmp di,0x10 // normalize es:di
\r
713 #ifdef __BORLANDC__
\r
717 #ifdef __BORLANDC__
\r
720 sub [WORD PTR ss:length],1
\r
722 dec [WORD PTR ss:length+2]
\r
723 jns expand // when length = ffff ffff, done
\r
736 ======================
\r
738 = CAL_CarmackExpand
\r
740 = Length is the length of the EXPANDED data
\r
742 ======================
\r
745 #define NEARTAG 0xa7
\r
746 #define FARTAG 0xa8
\r
748 void CAL_CarmackExpand (unsigned far *source, unsigned far *dest, unsigned length)
\r
750 unsigned ch,chhigh,count,offset;
\r
751 unsigned far *copyptr, far *inptr, far *outptr;
\r
762 if (chhigh == NEARTAG)
\r
766 { // have to insert a word containing the tag byte
\r
767 ch |= *(/*(unsigned char far *)*/inptr)++;
\r
773 offset = *(/*(unsigned char far *)*/inptr)++;
\r
774 copyptr = outptr - offset;
\r
777 *outptr++ = *copyptr++;
\r
780 else if (chhigh == FARTAG)
\r
784 { // have to insert a word containing the tag byte
\r
785 ch |= *(/*(unsigned char far *)*/inptr)++;
\r
792 copyptr = dest + offset;
\r
795 *outptr++ = *copyptr++;
\r
808 ======================
\r
812 ======================
\r
815 long CA_RLEWCompress (unsigned far *source, long length, unsigned far *dest,
\r
819 unsigned value,count,i;
\r
820 unsigned far *start,far *end;
\r
824 end = source + (length+1)/2;
\r
833 while (*source == value && source<end)
\r
838 if (count>3 || value == rlewtag)
\r
841 // send a tag / count / value string
\r
850 // send word without compressing
\r
852 for (i=1;i<=count;i++)
\r
856 } while (source<end);
\r
858 complength = 2*(dest-start);
\r
864 ======================
\r
867 = length is EXPANDED length
\r
869 ======================
\r
872 void CA_RLEWexpand (unsigned far *source, unsigned far *dest,long length,
\r
875 // unsigned value,count,i;
\r
877 unsigned sourceseg,sourceoff,destseg,destoff,endseg,endoff;
\r
887 if (value != rlewtag)
\r
895 // compressed string
\r
899 for (i=1;i<=count;i++)
\r
902 } while (dest<end);
\r
905 end = dest + (length)/2;
\r
906 sourceseg = FP_SEG(source);
\r
907 sourceoff = FP_OFF(source);
\r
908 destseg = FP_SEG(dest);
\r
909 destoff = FP_OFF(dest);
\r
910 endseg = FP_SEG(end);
\r
911 endoff = FP_OFF(end);
\r
915 // ax = source value
\r
917 // cx = repeat counts
\r
920 // NOTE: A repeat count that produces 0xfff0 bytes can blow this!
\r
928 #ifdef __BORLANDC__
\r
932 #ifdef __BORLANDC__
\r
940 #ifdef __BORLANDC__
\r
944 #ifdef __BORLANDC__
\r
948 mov cx,ax // repeat count
\r
949 lodsw // repeat value
\r
951 #ifdef __BORLANDC__
\r
955 #ifdef __BORLANDC__
\r
958 cmp si,0x10 // normalize ds:si
\r
969 #ifdef __BORLANDC__
\r
973 #ifdef __BORLANDC__
\r
976 cmp di,0x10 // normalize es:di
\r
987 #ifdef __BORLANDC__
\r
991 #ifdef __BORLANDC__
\r
1007 =============================================================================
\r
1009 CACHE MANAGER ROUTINES
\r
1011 =============================================================================
\r
1015 ======================
\r
1019 ======================
\r
1022 void CAL_SetupGrFile (global_game_variables_t *gvar)
\r
1030 #ifdef GRHEADERLINKED
\r
1032 gvar->ca.grhuffman = (huffnode *)&VGAdict;
\r
1033 grstarts = (long _seg *)FP_SEG(&VGAhead);
\r
1035 CAL_OptimizeNodes (gvar->ca.grhuffman);
\r
1040 // load ???dict.ext (huffman dictionary for graphics files)
\r
1043 strcpy(fname,GDICTNAME);
\r
1044 strcat(fname,EXTENSION);
\r
1046 if ((handle = open(fname,
\r
1047 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1048 CA_CannotOpen(fname,gvar);
\r
1050 read(handle, &gvar->ca.grhuffman, sizeof(gvar->ca.grhuffman));
\r
1052 CAL_OptimizeNodes (gvar->ca.grhuffman);
\r
1054 // load the data offsets from ???head.ext
\r
1056 MM_GetPtr (MEMPTRCONV gvar->ca.grstarts,(NUMCHUNKS+1)*FILEPOSSIZE, gvar);
\r
1058 strcpy(fname,GHEADNAME);
\r
1059 strcat(fname,EXTENSION);
\r
1061 if ((handle = open(fname,
\r
1062 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1063 CA_CannotOpen(fname,gvar);
\r
1065 CA_FarRead(handle, (memptr)gvar->ca.grstarts, (NUMCHUNKS+1)*FILEPOSSIZE, gvar);
\r
1073 // Open the graphics file, leaving it open until the game is finished
\r
1075 strcpy(fname,GFILENAME);
\r
1076 strcat(fname,EXTENSION);
\r
1078 gvar->ca.file.grhandle = open(fname, O_RDONLY | O_BINARY);
\r
1079 if (gvar->ca.file.grhandle == -1)
\r
1080 CA_CannotOpen(fname,gvar);
\r
1084 // load the pic and sprite headers into the arrays in the data segment
\r
1087 MM_GetPtr(MEMPTRCONV gvar->video.pictable,NUMPICS*sizeof(pictabletype),gvar);
\r
1088 CAL_GetGrChunkLength(STRUCTPIC,gvar); // position file pointer
\r
1089 printf("CAL_SetupGrFile:\n");
\r
1090 printf(" gvar->ca.chunkcomplen size is %lu\n", gvar->ca.chunkcomplen);
\r
1091 MM_GetPtr(MEMPTRANDPERCONV compseg,gvar->ca.chunkcomplen,gvar); IN_Ack(gvar);
\r
1092 CA_FarRead (gvar->ca.file.grhandle,compseg,gvar->ca.chunkcomplen,gvar);
\r
1093 CAL_HuffExpand (compseg, (byte far *)gvar->video.pictable,NUMPICS*sizeof(pictabletype),gvar->ca.grhuffman);
\r
1094 MM_FreePtr(MEMPTRANDPERCONV compseg,gvar);
\r
1099 MM_GetPtr(MEMPTRCONV picmtable,NUMPICM*sizeof(pictabletype));
\r
1100 CAL_GetGrChunkLength(STRUCTPICM); // position file pointer
\r
1101 MM_GetPtr(&compseg,gvar->ca.chunkcomplen);
\r
1102 CA_FarRead (gvar->ca.file.grhandle,compseg,gvar->ca.chunkcomplen);
\r
1103 CAL_HuffExpand (compseg, (byte far *)picmtable,NUMPICS*sizeof(pictabletype),gvar->ca.grhuffman);
\r
1104 MM_FreePtr(&compseg);
\r
1107 //#if NUMSPRITES>0
\r
1108 MM_GetPtr(MEMPTRCONV spritetable,NUMSPRITES*sizeof(spritetabletype));
\r
1109 CAL_GetGrChunkLength(STRUCTSPRITE); // position file pointer
\r
1110 MM_GetPtr(&compseg,gvar->ca.chunkcomplen);
\r
1111 CA_FarRead (gvar->ca.file.grhandle,compseg,gvar->ca.chunkcomplen);
\r
1112 CAL_HuffExpand (compseg, (byte far *)spritetable,NUMSPRITES*sizeof(spritetabletype),gvar->ca.grhuffman);
\r
1113 MM_FreePtr(&compseg);
\r
1118 //==========================================================================
\r
1122 ======================
\r
1124 = CAL_SetupMapFile
\r
1126 ======================
\r
1129 void CAL_SetupMapFile (global_game_variables_t *gvar)
\r
1131 #ifndef MAPHEADERLINKED
\r
1137 // load maphead.ext (offsets and tileinfo for map file)
\r
1139 #ifndef MAPHEADERLINKED
\r
1140 if ((handle = open("maphead.mph",
\r
1141 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1142 Quit (gvar, "Can't open maphead.mph");
\r
1143 length = filelength(handle);
\r
1144 MM_GetPtr (MEMPTRCONV gvar->ca.tinf,length,gvar);
\r
1145 CA_FarRead(handle, gvar->ca.tinf, length,gvar);
\r
1149 gvar->ca.tinf = (byte _seg *)FP_SEG(&maphead);
\r
1154 // open the data file
\r
1156 //TODO: multiple files
\r
1157 if ((gvar->ca.file.maphandle = open("data/test.map",
\r
1158 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1159 Quit (gvar, "Can't open data/test.map!");
\r
1160 /*#ifdef MAPHEADERLINKED
\r
1161 if ((maphandle = open("GAMEMAPS.16"ENSION,
\r
1162 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1163 Quit (gvar, "Can't open GAMEMAPS.16"ENSION"!");
\r
1165 if ((maphandle = open("MAPTEMP.16"ENSION,
\r
1166 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1167 Quit (gvar, "Can't open MAPTEMP.16"ENSION"!");
\r
1171 //==========================================================================
\r
1175 ======================
\r
1177 = CAL_SetupAudioFile
\r
1179 ======================
\r
1182 /*void CAL_SetupAudioFile (void)
\r
1188 // load maphead.ext (offsets and tileinfo for map file)
\r
1190 #ifndef AUDIOHEADERLINKED
\r
1191 if ((handle = open("AUDIOHED.16",
\r
1192 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1193 Quit (gvar, "Can't open AUDIOHED.16""!");
\r
1194 length = filelength(handle);
\r
1195 MM_GetPtr (MEMPTRCONV audiostarts,length);
\r
1196 CA_FarRead(handle, (byte far *)audiostarts, length);
\r
1199 audiohuffman = (huffnode *)&audiodict;
\r
1200 CAL_OptimizeNodes (audiohuffman);
\r
1201 audiostarts = (long _seg *)FP_SEG(&audiohead);
\r
1205 // open the data file
\r
1207 #ifndef AUDIOHEADERLINKED
\r
1208 if ((audiohandle = open("AUDIOT.16",
\r
1209 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1210 Quit (gvar, "Can't open AUDIOT.16""!");
\r
1212 if ((audiohandle = open("AUDIO.16",
\r
1213 O_RDONLY | O_BINARY, S_IREAD)) == -1)
\r
1214 Quit (gvar, "Can't open AUDIO.16""!");
\r
1218 //==========================================================================
\r
1222 ======================
\r
1226 = Open all files and load in headers
\r
1228 ======================
\r
1231 void CA_Startup(global_game_variables_t *gvar)
\r
1234 #ifdef __BORLANDC__
\r
1235 unlink("profile.16b");
\r
1236 gvar->handle.profilehandle = open("profile.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
1238 #ifdef __WATCOMC__
\r
1239 unlink("profile.16w");
\r
1240 gvar->handle.profilehandle = open("profile.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
1244 #ifdef SHOWMEMINFO
\r
1245 #ifdef __BORLANDC__
\r
1246 unlink("meminfo.16b");
\r
1247 gvar->handle.showmemhandle = open("meminfo.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
1249 #ifdef __WATCOMC__
\r
1250 unlink("meminfo.16w");
\r
1251 gvar->handle.showmemhandle = open("meminfo.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
1257 CAL_SetupMapFile (gvar);
\r
1259 #ifndef NOGRAPHICS
\r
1260 CAL_SetupGrFile (gvar);
\r
1263 CAL_SetupMapFile (gvar);
\r
1266 gvar->ca.mapon = -1;
\r
1267 gvar->ca.ca_levelbit = 1;
\r
1268 gvar->ca.ca_levelnum = 0;
\r
1270 /* drawcachebox = CAL_DialogDraw;
\r
1271 updatecachebox = CAL_DialogUpdate;
\r
1272 finishcachebox = CAL_DialogFinish;*/
\r
1275 //==========================================================================
\r
1279 ======================
\r
1283 = Closes all files
\r
1285 ======================
\r
1288 void CA_Shutdown(global_game_variables_t *gvar)
\r
1291 close(gvar->handle.profilehandle);
\r
1293 #ifdef SHOWMEMINFO
\r
1294 close(gvar->handle.showmemhandle);
\r
1297 close(gvar->ca.file.maphandle);
\r
1298 close(gvar->ca.file.grhandle);
\r
1299 close(gvar->ca.file.audiohandle);
\r
1302 //===========================================================================
\r
1305 ======================
\r
1307 = CA_CacheAudioChunk
\r
1309 ======================
\r
1312 void CA_CacheAudioChunk (int chunk)
\r
1314 long pos,compressed;
\r
1315 #ifdef AUDIOHEADERLINKED
\r
1317 memptr bigbufferseg;
\r
1321 if (audiosegs[chunk])
\r
1323 MM_SetPurge (MEMPTRCONV audiosegs[chunk],0);
\r
1324 return; // allready in memory
\r
1327 // MDM begin - (GAMERS EDGE)
\r
1329 if (!FindFile("AUDIO.16",NULL,2))
\r
1330 Quit (gvar, "CA_CacheAudioChunk(): Can't find audio files.");
\r
1335 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1336 // a larger buffer
\r
1338 pos = audiostarts[chunk];
\r
1339 compressed = audiostarts[chunk+1]-pos;
\r
1341 lseek(audiohandle,pos,SEEK_SET);
\r
1343 #ifndef AUDIOHEADERLINKED
\r
1345 MM_GetPtr (MEMPTRCONV audiosegs[chunk],compressed);
\r
1349 CA_FarRead(audiohandle,audiosegs[chunk],compressed);
\r
1353 if (compressed<=BUFFERSIZE)
\r
1355 CA_FarRead(audiohandle,bufferseg,compressed);
\r
1356 source = bufferseg;
\r
1360 MM_GetPtr(&bigbufferseg,compressed);
\r
1363 MM_SetLock (&bigbufferseg,true);
\r
1364 CA_FarRead(audiohandle,bigbufferseg,compressed);
\r
1365 source = bigbufferseg;
\r
1368 expanded = *(long far *)source;
\r
1369 source += 4; // skip over length
\r
1370 MM_GetPtr (MEMPTRCONV audiosegs[chunk],expanded);
\r
1373 CAL_HuffExpand (source,audiosegs[chunk],expanded,audiohuffman);
\r
1376 if (compressed>BUFFERSIZE)
\r
1377 MM_FreePtr(&bigbufferseg);
\r
1381 //===========================================================================
\r
1384 ======================
\r
1386 = CA_LoadAllSounds
\r
1388 = Purges all sounds, then loads all new ones (mode switch)
\r
1390 ======================
\r
1393 void CA_LoadAllSounds (void)
\r
1397 switch (oldsoundmode)
\r
1402 start = STARTPCSOUNDS;
\r
1405 start = STARTADLIBSOUNDS;
\r
1409 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1410 if (audiosegs[start])
\r
1411 MM_SetPurge (MEMPTRCONV audiosegs[start],3); // make purgable
\r
1415 switch (SoundMode)
\r
1420 start = STARTPCSOUNDS;
\r
1423 start = STARTADLIBSOUNDS;
\r
1427 for (i=0;i<NUMSOUNDS;i++,start++)
\r
1428 CA_CacheAudioChunk (start);
\r
1430 oldsoundmode = SoundMode;
\r
1433 //===========================================================================
\r
1435 //????#if GRMODE == EGAGR
\r
1438 ======================
\r
1442 = Make a shifted (one byte wider) copy of a sprite into another area
\r
1444 ======================
\r
1447 unsigned static sheight,swidth;
\r
1448 boolean static dothemask;
\r
1450 void CAL_ShiftSprite (unsigned segment,unsigned source,unsigned dest,
\r
1451 unsigned width, unsigned height, unsigned pixshift, boolean domask, global_game_variables_t *gvar)
\r
1454 sheight = height; // because we are going to reassign bp
\r
1456 dothemask = domask;
\r
1460 mov ds,ax // source and dest are in same segment, and all local
\r
1467 mov bp,WORD PTR [shifttabletable+bp] // bp holds pointer to shift table
\r
1468 // mov bp,WORD PTR [gvar->video.shifttabletable+bp] // bp holds pointer to shift table
\r
1470 cmp [ss:dothemask],0
\r
1474 // table shift the mask
\r
1476 mov dx,[ss:sheight]
\r
1477 #ifdef __BORLANDC__
\r
1481 #ifdef __BORLANDC__
\r
1484 mov BYTE PTR [di],255 // 0xff first byte
\r
1485 mov cx,ss:[swidth]
\r
1486 #ifdef __BORLANDC__
\r
1490 #ifdef __BORLANDC__
\r
1493 mov al,[bx] // source
\r
1495 inc bx // next source byte
\r
1499 mov ax,[bp+si] // table shift into two bytes
\r
1501 and [di],al // and with first byte
\r
1503 mov [di],ah // replace next byte
\r
1507 inc di // the last shifted byte has 1s in it
\r
1510 #ifdef __BORLANDC__
\r
1514 #ifdef __BORLANDC__
\r
1518 // table shift the data
\r
1520 mov dx,ss:[sheight]
\r
1522 shl dx,1 // four planes of data
\r
1523 #ifdef __BORLANDC__
\r
1527 #ifdef __BORLANDC__
\r
1530 mov BYTE PTR [di],0 // 0 first byte
\r
1531 mov cx,ss:[swidth]
\r
1532 #ifdef __BORLANDC__
\r
1536 #ifdef __BORLANDC__
\r
1539 mov al,[bx] // source
\r
1540 inc bx // next source byte
\r
1544 mov ax,[bp+si] // table shift into two bytes
\r
1545 or [di],al // or with first byte
\r
1547 mov [di],ah // replace next byte
\r
1551 inc di // the last shifted byte has 0s in it
\r
1559 mov ax,ss // restore data segment
\r
1567 //===========================================================================
\r
1570 ======================
\r
1574 = Generate shifts and set up sprite structure for a given sprite
\r
1576 ======================
\r
1579 void CAL_CacheSprite (int chunk, byte far *compressed, global_game_variables_t *gvar)
\r
1582 unsigned shiftstarts[5];
\r
1583 unsigned smallplane,bigplane,expanded;
\r
1584 spritetabletype far *spr;
\r
1585 spritetype _seg *dest;
\r
1590 // CGA has no pel panning, so shifts are never needed
\r
1592 spr = &gvar->video.spritetable[chunk-STARTSPRITES];
\r
1593 smallplane = spr->width*spr->height;
\r
1594 MM_GetPtr (&grsegs[chunk],smallplane*2+MAXSHIFTS*6);
\r
1597 dest = (spritetype _seg *)grsegs[chunk];
\r
1598 dest->sourceoffset[0] = MAXSHIFTS*6; // start data after 3 unsigned tables
\r
1599 dest->planesize[0] = smallplane;
\r
1600 dest->width[0] = spr->width;
\r
1603 // expand the unshifted shape
\r
1605 CAL_HuffExpand (compressed, &dest->data[0],smallplane*2,gvar->ca.grhuffman);
\r
1610 //#if GRMODE == EGAGR
\r
1613 // calculate sizes
\r
1615 spr = &gvar->video.spritetable[chunk-STARTSPRITES];
\r
1616 smallplane = spr->width*spr->height;
\r
1617 bigplane = (spr->width+1)*spr->height;
\r
1619 shiftstarts[0] = MAXSHIFTS*6; // start data after 3 unsigned tables
\r
1620 shiftstarts[1] = shiftstarts[0] + smallplane*5; // 5 planes in a sprite
\r
1621 shiftstarts[2] = shiftstarts[1] + bigplane*5;
\r
1622 shiftstarts[3] = shiftstarts[2] + bigplane*5;
\r
1623 shiftstarts[4] = shiftstarts[3] + bigplane*5; // nothing ever put here
\r
1625 expanded = shiftstarts[spr->shifts];
\r
1626 MM_GetPtr (MEMPTRCONV gvar->ca.grsegs[chunk],expanded, gvar);
\r
1627 if (gvar->mm.mmerror)
\r
1629 dest = (spritetype _seg *)gvar->ca.grsegs[chunk];
\r
1632 // expand the unshifted shape
\r
1634 CAL_HuffExpand (compressed, &dest->data[0],smallplane*5,gvar->ca.grhuffman);
\r
1637 // make the shifts!
\r
1639 switch (spr->shifts)
\r
1644 dest->sourceoffset[i] = shiftstarts[0];
\r
1645 dest->planesize[i] = smallplane;
\r
1646 dest->width[i] = spr->width;
\r
1653 dest->sourceoffset[i] = shiftstarts[0];
\r
1654 dest->planesize[i] = smallplane;
\r
1655 dest->width[i] = spr->width;
\r
1659 dest->sourceoffset[i] = shiftstarts[1];
\r
1660 dest->planesize[i] = bigplane;
\r
1661 dest->width[i] = spr->width+1;
\r
1663 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1664 dest->sourceoffset[2],spr->width,spr->height,4,true,gvar);
\r
1668 dest->sourceoffset[0] = shiftstarts[0];
\r
1669 dest->planesize[0] = smallplane;
\r
1670 dest->width[0] = spr->width;
\r
1672 dest->sourceoffset[1] = shiftstarts[1];
\r
1673 dest->planesize[1] = bigplane;
\r
1674 dest->width[1] = spr->width+1;
\r
1675 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1676 dest->sourceoffset[1],spr->width,spr->height,2,true,gvar);
\r
1678 dest->sourceoffset[2] = shiftstarts[2];
\r
1679 dest->planesize[2] = bigplane;
\r
1680 dest->width[2] = spr->width+1;
\r
1681 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1682 dest->sourceoffset[2],spr->width,spr->height,4,true,gvar);
\r
1684 dest->sourceoffset[3] = shiftstarts[3];
\r
1685 dest->planesize[3] = bigplane;
\r
1686 dest->width[3] = spr->width+1;
\r
1687 CAL_ShiftSprite ((unsigned)gvar->ca.grsegs[chunk],dest->sourceoffset[0],
\r
1688 dest->sourceoffset[3],spr->width,spr->height,6,true,gvar);
\r
1693 Quit (gvar, "CAL_CacheSprite: Bad shifts number!");
\r
1699 //===========================================================================
\r
1703 ======================
\r
1705 = CAL_ExpandGrChunk
\r
1707 = Does whatever is needed with a pointer to a compressed chunk
\r
1709 ======================
\r
1712 void CAL_ExpandGrChunk (int chunk, byte far *source)
\r
1717 if (chunk >= STARTTILE8 && chunk < STARTEXTERNS)
\r
1720 // expanded sizes of tile8/16/32 are implicit
\r
1723 #if GRMODE == EGAGR
\r
1725 #define MASKBLOCK 40
\r
1728 #if GRMODE == CGAGR
\r
1730 #define MASKBLOCK 32
\r
1733 if (chunk<STARTTILE8M) // tile 8s are all in one chunk!
\r
1734 expanded = BLOCK*NUMTILE8;
\r
1735 else if (chunk<STARTTILE16)
\r
1736 expanded = MASKBLOCK*NUMTILE8M;
\r
1737 else if (chunk<STARTTILE16M) // all other tiles are one/chunk
\r
1738 expanded = BLOCK*4;
\r
1739 else if (chunk<STARTTILE32)
\r
1740 expanded = MASKBLOCK*4;
\r
1741 else if (chunk<STARTTILE32M)
\r
1742 expanded = BLOCK*16;
\r
1744 expanded = MASKBLOCK*16;
\r
1749 // everything else has an explicit size longword
\r
1751 expanded = *(long far *)source;
\r
1752 source += 4; // skip over length
\r
1756 // allocate final space, decompress it, and free bigbuffer
\r
1757 // Sprites need to have shifts made and various other junk
\r
1759 if (chunk>=STARTSPRITES && chunk< STARTTILE8)
\r
1760 CAL_CacheSprite(chunk,source);
\r
1763 MM_GetPtr (&grsegs[chunk],expanded);
\r
1766 CAL_HuffExpand (source,grsegs[chunk],expanded,gvar->ca.grhuffman);
\r
1772 ======================
\r
1776 = Gets a chunk off disk, optimizing reads to general buffer
\r
1778 ======================
\r
1781 void CAL_ReadGrChunk (int chunk)
\r
1783 long pos,compressed;
\r
1784 memptr bigbufferseg;
\r
1789 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1790 // a larger buffer
\r
1792 pos = GRFILEPOS(chunk,gvar);
\r
1793 if (pos<0) // $FFFFFFFF start is a sparse tile
\r
1797 while (GRFILEPOS(next,gvar) == -1) // skip past any sparse tiles
\r
1800 compressed = GRFILEPOS(next,gvar)-pos;
\r
1802 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
1804 if (compressed<=BUFFERSIZE)
\r
1806 CA_FarRead(gvar->ca.file.grhandle,bufferseg,compressed);
\r
1807 source = bufferseg;
\r
1811 MM_GetPtr(&bigbufferseg,compressed);
\r
1814 MM_SetLock (&bigbufferseg,true);
\r
1815 CA_FarRead(gvar->ca.file.grhandle,bigbufferseg,compressed);
\r
1816 source = bigbufferseg;
\r
1819 CAL_ExpandGrChunk (chunk,source);
\r
1821 if (compressed>BUFFERSIZE)
\r
1822 MM_FreePtr(&bigbufferseg);
\r
1826 ======================
\r
1830 = Makes sure a given chunk is in memory, loadiing it if needed
\r
1832 ======================
\r
1835 void CA_CacheGrChunk (int chunk)
\r
1837 long pos,compressed;
\r
1838 memptr bigbufferseg;
\r
1842 gvar->ca.grneeded[chunk] |= ca_levelbit; // make sure it doesn't get removed
\r
1843 if (grsegs[chunk])
\r
1845 MM_SetPurge (&grsegs[chunk],0);
\r
1846 return; // allready in memory
\r
1849 // MDM begin - (GAMERS EDGE)
\r
1851 if (!FindFile("EGAGRAPH.16",NULL,2))
\r
1852 Quit (gvar, "CA_CacheGrChunk(): Can't find graphics files.");
\r
1857 // load the chunk into a buffer, either the miscbuffer if it fits, or allocate
\r
1858 // a larger buffer
\r
1860 pos = GRFILEPOS(chunk,gvar);
\r
1861 if (pos<0) // $FFFFFFFF start is a sparse tile
\r
1865 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
1868 compressed = GRFILEPOS(next,gvar)-pos;
\r
1870 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
1872 if (compressed<=BUFFERSIZE)
\r
1874 CA_FarRead(gvar->ca.file.grhandle,bufferseg,compressed);
\r
1875 source = bufferseg;
\r
1879 MM_GetPtr(&bigbufferseg,compressed);
\r
1880 MM_SetLock (&bigbufferseg,true);
\r
1881 CA_FarRead(gvar->ca.file.grhandle,bigbufferseg,compressed);
\r
1882 source = bigbufferseg;
\r
1885 CAL_ExpandGrChunk (chunk,source);
\r
1887 if (compressed>BUFFERSIZE)
\r
1888 MM_FreePtr(&bigbufferseg);
\r
1893 //==========================================================================
\r
1896 ======================
\r
1900 ======================
\r
1903 void CA_CacheMap (global_game_variables_t *gvar)
\r
1905 long pos,compressed;
\r
1907 memptr *dest,bigbufferseg;
\r
1909 unsigned far *source;
\r
1910 #ifdef MAPHEADERLINKED
\r
1911 memptr buffer2seg;
\r
1917 // free up memory from last map
\r
1919 if (gvar->ca.mapon>-1 && gvar->ca.mapheaderseg[gvar->ca.mapon])
\r
1920 MM_SetPurge ((MEMPTRCONV gvar->ca.mapheaderseg[(gvar->ca.mapon)]), 3, gvar);
\r
1921 for (plane=0;plane<MAPPLANES;plane++)
\r
1922 if (gvar->ca.mapsegs[plane])
\r
1923 MM_FreePtr (MEMPTRCONV gvar->ca.mapsegs[plane], gvar);
\r
1925 gvar->ca.mapon = gvar->ca.mapnum;
\r
1929 // load map header
\r
1930 // The header will be cached if it is still around
\r
1932 if (!gvar->ca.mapheaderseg[gvar->ca.mapnum])
\r
1934 pos = ((mapfiletype _seg *)gvar->ca.tinf)->headeroffsets[gvar->ca.mapnum];
\r
1935 if (pos<0) // $FFFFFFFF start is a sparse map
\r
1936 Quit (gvar, "CA_CacheMap: Tried to load a non existent map!");
\r
1938 MM_GetPtr(MEMPTRCONV gvar->ca.mapheaderseg[gvar->ca.mapnum],sizeof(maptype), gvar);
\r
1939 lseek(gvar->ca.file.maphandle,pos,SEEK_SET);
\r
1940 #ifdef MAPHEADERLINKED
\r
1941 //#if BUFFERSIZE < sizeof(maptype)
\r
1942 //The general buffer size is too small!
\r
1945 // load in, then unhuffman to the destination
\r
1947 CA_FarRead (gvar->ca.file.maphandle,gvar->mm.bufferseg,((mapfiletype _seg *)gvar->ca.tinf)->headersize[gvar->ca.mapnum], gvar);
\r
1948 CAL_HuffExpand ((byte huge *)bufferseg,
\r
1949 (byte huge *)gvar->ca.mapheaderseg[gvar->ca.mapnum],sizeof(maptype),maphuffman, gvar);
\r
1951 CA_FarRead (gvar->ca.file.maphandle,(memptr)gvar->ca.mapheaderseg[gvar->ca.mapnum],sizeof(maptype), gvar);
\r
1955 MM_SetPurge (MEMPTRCONV gvar->ca.mapheaderseg[gvar->ca.mapnum], 0, gvar);
\r
1958 // load the planes in
\r
1959 // If a plane's pointer still exists it will be overwritten (levels are
\r
1960 // allways reloaded, never cached)
\r
1963 size = gvar->ca.mapheaderseg[gvar->ca.mapnum]->width * gvar->ca.mapheaderseg[gvar->ca.mapnum]->height * 2;
\r
1965 for (plane = 0; plane<MAPPLANES; plane++)
\r
1967 pos = gvar->ca.mapheaderseg[gvar->ca.mapnum]->planestart[plane];
\r
1968 compressed = gvar->ca.mapheaderseg[gvar->ca.mapnum]->planelength[plane];
\r
1971 continue; // the plane is not used in this game
\r
1973 dest = MEMPTRCONV gvar->ca.mapsegs[plane];
\r
1974 MM_GetPtr(dest,size, gvar);
\r
1976 lseek(gvar->ca.file.maphandle,pos,SEEK_SET);
\r
1977 if (compressed<=BUFFERSIZE)
\r
1978 source = gvar->mm.bufferseg;
\r
1981 MM_GetPtr(&bigbufferseg,compressed, gvar);
\r
1982 MM_SetLock (&bigbufferseg,true, gvar);
\r
1983 source = bigbufferseg;
\r
1986 CA_FarRead(gvar->ca.file.maphandle,(byte far *)source,compressed, gvar);
\r
1987 #ifdef MAPHEADERLINKED
\r
1989 // unhuffman, then unRLEW
\r
1990 // The huffman'd chunk has a two byte expanded length first
\r
1991 // The resulting RLEW chunk also does, even though it's not really
\r
1994 expanded = *source;
\r
1996 MM_GetPtr (&buffer2seg,expanded, gvar);
\r
1997 CAL_CarmackExpand (source, (unsigned far *)buffer2seg,expanded);
\r
1998 CA_RLEWexpand (((unsigned far *)buffer2seg)+1,*dest,size,
\r
1999 ((mapfiletype _seg *)tinf)->RLEWtag);
\r
2000 MM_FreePtr (&buffer2seg);
\r
2004 // unRLEW, skipping expanded length
\r
2006 CA_RLEWexpand (source+1, *dest,size,
\r
2007 ((mapfiletype _seg *)gvar->ca.tinf)->RLEWtag);
\r
2010 if (compressed>BUFFERSIZE)
\r
2011 MM_FreePtr(&bigbufferseg, gvar);
\r
2015 //===========================================================================
\r
2018 ======================
\r
2022 = Goes up a bit level in the needed lists and clears it out.
\r
2023 = Everything is made purgable
\r
2025 ======================
\r
2028 void CA_UpLevel (global_game_variables_t *gvar)
\r
2030 if (gvar->ca.ca_levelnum==7)
\r
2031 printf("CA_UpLevel: Up past level 7!");
\r
2033 gvar->ca.ca_levelbit<<=1;
\r
2034 gvar->ca.ca_levelnum++;
\r
2037 //===========================================================================
\r
2040 ======================
\r
2044 = Goes down a bit level in the needed lists and recaches
\r
2045 = everything from the lower level
\r
2047 ======================
\r
2050 void CA_DownLevel (global_game_variables_t *gvar)
\r
2052 if (!gvar->ca.ca_levelnum)
\r
2053 printf("CA_DownLevel: Down past level 0!");
\r
2054 gvar->ca.ca_levelbit>>=1;
\r
2055 gvar->ca.ca_levelnum--;
\r
2056 ////++++++++++++++++++++++++++++++++++++++++++CA_CacheMarks(NULL);
\r
2059 //===========================================================================
\r
2062 ======================
\r
2066 = Clears out all the marks at the current level
\r
2068 ======================
\r
2071 void CA_ClearMarks (global_game_variables_t *gvar)
\r
2075 for (i=0;i<NUMCHUNKS;i++)
\r
2076 gvar->ca.grneeded[i]&=~gvar->ca.ca_levelbit;
\r
2079 //===========================================================================
\r
2082 ======================
\r
2084 = CA_ClearAllMarks
\r
2086 = Clears out all the marks on all the levels
\r
2088 ======================
\r
2091 void CA_ClearAllMarks (global_game_variables_t *gvar)
\r
2093 _fmemset (gvar->ca.grneeded,0,sizeof(gvar->ca.grneeded));
\r
2094 gvar->ca.ca_levelbit = 1;
\r
2095 gvar->ca.ca_levelnum = 0;
\r
2098 //===========================================================================
\r
2101 ======================
\r
2105 ======================
\r
2108 void CA_SetGrPurge (global_game_variables_t *gvar)
\r
2115 CA_ClearMarks (gvar);
\r
2117 for (i=0;i<NUMCHUNKS;i++)
\r
2118 if (gvar->ca.grsegs[i])
\r
2119 MM_SetPurge (gvar->ca.grsegs[i],3, gvar);
\r
2124 ======================
\r
2128 = Make everything possible purgable
\r
2130 ======================
\r
2133 void CA_SetAllPurge (global_game_variables_t *gvar)
\r
2137 CA_ClearMarks (gvar);
\r
2140 // free cursor sprite and background save
\r
2142 //____VW_FreeCursor ();
\r
2145 // free map headers and map planes
\r
2147 for (i=0;i<NUMMAPS;i++)
\r
2148 if (gvar->ca.mapheaderseg[i])
\r
2149 MM_SetPurge (MEMPTRCONV gvar->ca.mapheaderseg[i],3, gvar);
\r
2152 if (gvar->ca.mapsegs[i])
\r
2153 MM_FreePtr (MEMPTRCONV gvar->ca.mapsegs[i], gvar);
\r
2158 for (i=0;i<NUMSNDCHUNKS;i++)
\r
2159 if (gvar->ca.audiosegs[i])
\r
2160 MM_SetPurge (MEMPTRCONV gvar->ca.audiosegs[i],3, gvar);
\r
2165 CA_SetGrPurge (gvar);
\r
2169 //===========================================================================
\r
2173 ======================
\r
2177 ======================
\r
2180 #define NUMBARS (17l*8)
\r
2183 unsigned thx,thy,lastx;
\r
2184 long barx,barstep;
\r
2186 void CAL_DialogDraw (char *title,unsigned numcache)
\r
2188 unsigned homex,homey,x;
\r
2190 barstep = (NUMBARS<<16)/numcache;
\r
2193 // draw dialog window (masked tiles 12 - 20 are window borders)
\r
2195 US_CenterWindow (20,8);
\r
2199 US_CPrint ("Loading");
\r
2200 fontcolor = F_SECONDCOLOR;
\r
2201 US_CPrint (title);
\r
2202 fontcolor = F_BLACK;
\r
2205 // draw thermometer bar
\r
2209 VWB_DrawTile8(thx,thy,0); // CAT3D numbers
\r
2210 VWB_DrawTile8(thx,thy+8,3);
\r
2211 VWB_DrawTile8(thx,thy+16,6);
\r
2212 VWB_DrawTile8(thx+17*8,thy,2);
\r
2213 VWB_DrawTile8(thx+17*8,thy+8,5);
\r
2214 VWB_DrawTile8(thx+17*8,thy+16,8);
\r
2215 for (x=thx+8;x<thx+17*8;x+=8)
\r
2217 VWB_DrawTile8(x,thy,1);
\r
2218 VWB_DrawTile8(x,thy+8,4);
\r
2219 VWB_DrawTile8(x,thy+16,7);
\r
2222 thx += 4; // first line location
\r
2224 barx = (long)thx<<16;
\r
2227 VW_UpdateScreen();
\r
2232 ======================
\r
2234 = CAL_DialogUpdate
\r
2236 ======================
\r
2239 void CAL_DialogUpdate (void)
\r
2245 if (xh - lastx > BARSTEP)
\r
2247 for (x=lastx;x<=xh;x++)
\r
2248 #if GRMODE == EGAGR
\r
2249 VWB_Vlin (thy,thy+13,x,14);
\r
2251 #if GRMODE == CGAGR
\r
2252 VWB_Vlin (thy,thy+13,x,SECONDCOLOR);
\r
2255 VW_UpdateScreen();
\r
2260 ======================
\r
2262 = CAL_DialogFinish
\r
2264 ======================
\r
2267 void CAL_DialogFinish (void)
\r
2271 xh = thx + NUMBARS;
\r
2272 for (x=lastx;x<=xh;x++)
\r
2273 #if GRMODE == EGAGR
\r
2274 VWB_Vlin (thy,thy+13,x,14);
\r
2276 #if GRMODE == CGAGR
\r
2277 VWB_Vlin (thy,thy+13,x,SECONDCOLOR);
\r
2279 VW_UpdateScreen();
\r
2283 //===========================================================================
\r
2286 ======================
\r
2290 ======================
\r
2292 #define MAXEMPTYREAD 1024
\r
2294 void CAL_CacheMarks (char *title, global_game_variables_t *gvar)
\r
2297 int i,next,numcache;
\r
2298 long pos,endpos,nextpos,nextendpos,compressed;
\r
2299 long bufferstart,bufferend; // file position of general buffer
\r
2301 memptr bigbufferseg;
\r
2303 dialog = (title!=NULL);
\r
2307 // go through and make everything not needed purgable
\r
2309 for (i=0;i<NUMCHUNKS;i++)
\r
2310 if (gvar->ca.grneeded[i]&(gvar->ca.ca_levelbit))//if (grneeded[i]&ca_levelbit)
\r
2312 if (gvar->ca.grsegs[i]) // its allready in memory, make
\r
2313 MM_SetPurge(gvar->ca.grsegs[i],0,gvar); // sure it stays there!
\r
2319 if (gvar->ca.grsegs[i]) // not needed, so make it purgeable
\r
2320 MM_SetPurge(gvar->ca.grsegs[i],3,gvar);
\r
2323 if (!numcache) // nothing to cache!
\r
2326 // MDM begin - (GAMERS EDGE)
\r
2328 //???? if (!FindFile("EGAGRAPH.16",NULL,2))
\r
2329 //???? Quit (gvar, "CA_CacheMarks(): Can't find graphics files.");
\r
2336 write(profilehandle,title,strlen(title));
\r
2337 write(profilehandle,"\n",1);
\r
2340 drawcachebox(title,numcache);
\r
2344 // go through and load in anything still needed
\r
2346 bufferstart = bufferend = 0; // nothing good in buffer now
\r
2348 for (i=0;i<NUMCHUNKS;i++)
\r
2349 if ( (gvar->ca.grneeded[i]&(gvar->ca.ca_levelbit)) && !gvar->ca.grsegs[i])
\r
2352 // update thermometer
\r
2354 if (dialog && updatecachebox)
\r
2355 updatecachebox ();
\r
2357 pos = GRFILEPOS(i,gvar);
\r
2362 while (GRFILEPOS(next) == -1) // skip past any sparse tiles
\r
2365 compressed = GRFILEPOS(next,gvar)-pos;
\r
2366 endpos = pos+compressed;
\r
2368 if (compressed<=BUFFERSIZE)
\r
2370 if (bufferstart<=pos
\r
2371 && bufferend>= endpos)
\r
2373 // data is allready in buffer
\r
2374 source = (byte _seg *)bufferseg+(pos-bufferstart);
\r
2378 // load buffer with a new block from disk
\r
2379 // try to get as many of the needed blocks in as possible
\r
2380 while ( next < NUMCHUNKS )
\r
2382 while (next < NUMCHUNKS &&
\r
2383 !(gvar->video.grneeded[next]&ca_levelbit && !grsegs[next]))
\r
2385 if (next == NUMCHUNKS)
\r
2388 nextpos = GRFILEPOS(next,gvar);
\r
2389 while (GRFILEPOS(++next,gvar) == -1) // skip past any sparse tiles
\r
2391 nextendpos = GRFILEPOS(next,gvar);
\r
2392 if (nextpos - endpos <= MAXEMPTYREAD
\r
2393 && nextendpos-pos <= BUFFERSIZE)
\r
2394 endpos = nextendpos;
\r
2396 next = NUMCHUNKS; // read pos to posend
\r
2399 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
2400 CA_FarRead(gvar->ca.file.grhandle,(gvar->mm.bufferseg),endpos-pos,gvar);
\r
2401 bufferstart = pos;
\r
2402 bufferend = endpos;
\r
2403 source = bufferseg;
\r
2408 // big chunk, allocate temporary buffer
\r
2409 MM_GetPtr(&bigbufferseg,compressed,gvar);
\r
2412 MM_SetLock (&bigbufferseg,true);
\r
2413 lseek(gvar->ca.file.grhandle,pos,SEEK_SET);
\r
2414 CA_FarRead(gvar->ca.file.grhandle,bigbufferseg,compressed,gvar);
\r
2415 source = bigbufferseg;
\r
2418 CAL_ExpandGrChunk (i,source);
\r
2422 if (compressed>BUFFERSIZE)
\r
2423 MM_FreePtr(&bigbufferseg);
\r
2428 // finish up any thermometer remnants
\r
2430 if (dialog && finishcachebox)
\r
2434 void CA_CannotOpen(char *string, global_game_variables_t *gvar)
\r
2438 strcpy(str,"Can't open ");
\r
2439 strcat(str,string);
\r
2440 strcat(str,"!\n");
\r