1 //-------------------------------------------------------------------------
2 /*
3 Copyright (C) 2010-2019 EDuke32 developers and contributors
4 Copyright (C) 2019 Nuke.YKT
5 
6 This file is part of NBlood.
7 
8 NBlood is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License version 2
10 as published by the Free Software Foundation.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 See the GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 */
22 //-------------------------------------------------------------------------
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "compat.h"
27 #include "build.h"
28 #include "common.h"
29 #include "common_game.h"
30 
31 #include "blood.h"
32 #include "config.h"
33 #include "globals.h"
34 #include "resource.h"
35 #include "tile.h"
36 #include "view.h"
37 
qloadvoxel(int32_t nVoxel)38 void qloadvoxel(int32_t nVoxel)
39 {
40     static int nLastVoxel = 0;
41     DICTNODE *hVox = gSysRes.Lookup(nVoxel, "KVX");
42     if (!hVox) {
43         initprintf("Missing voxel #%d (max voxels: %d)", nVoxel, kMaxVoxels);
44         return;
45     }
46 
47     if (!hVox->lockCount)
48         voxoff[nLastVoxel][0] = 0;
49     nLastVoxel = nVoxel;
50     char *pVox = (char*)gSysRes.Lock(hVox);
51     for (int i = 0; i < MAXVOXMIPS; i++)
52     {
53         int nSize = *((int*)pVox);
54 #if B_BIG_ENDIAN == 1
55         nSize = B_LITTLE32(nSize);
56 #endif
57         pVox += 4;
58         voxoff[nVoxel][i] = (intptr_t)pVox;
59         pVox += nSize;
60     }
61 }
62 
CalcPicsiz(int a1,int a2,int a3)63 void CalcPicsiz(int a1, int a2, int a3)
64 {
65     int nP = 0;
66     for (int i = 2; i <= a2; i<<= 1)
67         nP++;
68     for (int i = 2; i <= a3; i<<= 1)
69         nP+=1<<4;
70     picsiz[a1] = nP;
71 }
72 
73 CACHENODE tileNode[kMaxTiles];
74 
75 bool artLoaded = false;
76 int nTileFiles = 0;
77 
78 int tileStart[256];
79 int tileEnd[256];
80 int hTileFile[256];
81 
82 char surfType[kMaxTiles];
83 signed char tileShade[kMaxTiles];
84 short voxelIndex[kMaxTiles];
85 
86 const char *pzBaseFileName = "TILES%03i.ART"; //"TILES%03i.ART";
87 
88 int32_t MAXCACHE1DSIZE = (96*1024*1024);
89 
tileInit(char a1,const char * a2)90 int tileInit(char a1, const char *a2)
91 {
92     UNREFERENCED_PARAMETER(a1);
93     if (artLoaded)
94         return 1;
95     artLoadFiles(a2 ? a2 : pzBaseFileName, MAXCACHE1DSIZE);
96     for (int i = 0; i < kMaxTiles; i++)
97         voxelIndex[i] = 0;
98 
99     int hFile = kopen4loadfrommod("SURFACE.DAT", 0);
100     if (hFile != -1)
101     {
102         kread(hFile, surfType, sizeof(surfType));
103         kclose(hFile);
104     }
105     hFile = kopen4loadfrommod("VOXEL.DAT", 0);
106     if (hFile != -1)
107     {
108         kread(hFile, voxelIndex, sizeof(voxelIndex));
109 #if B_BIG_ENDIAN == 1
110         for (int i = 0; i < kMaxTiles; i++)
111             voxelIndex[i] = B_LITTLE16(voxelIndex[i]);
112 #endif
113         kclose(hFile);
114     }
115     hFile = kopen4loadfrommod("SHADE.DAT", 0);
116     if (hFile != -1)
117     {
118         kread(hFile, tileShade, sizeof(tileShade));
119         kclose(hFile);
120     }
121     for (int i = 0; i < kMaxTiles; i++)
122     {
123         if (voxelIndex[i] >= 0 && voxelIndex[i] < kMaxVoxels)
124             SetBitString((char*)voxreserve, voxelIndex[i]);
125     }
126 
127     artLoaded = 1;
128 
129     #ifdef USE_OPENGL
130     PolymostProcessVoxels_Callback = tileProcessGLVoxels;
131     #endif
132 
133     return 1;
134 }
135 
136 #ifdef USE_OPENGL
tileProcessGLVoxels(void)137 void tileProcessGLVoxels(void)
138 {
139     static bool voxInit = false;
140     if (voxInit)
141         return;
142     voxInit = true;
143     for (int i = 0; i < kMaxVoxels; i++)
144     {
145         DICTNODE *hVox = gSysRes.Lookup(i, "KVX");
146         if (!hVox)
147             continue;
148         char *pVox = (char*)gSysRes.Load(hVox);
149         voxmodels[i] = loadkvxfrombuf(pVox, hVox->size);
150         voxvboalloc(voxmodels[i]);
151     }
152 }
153 #endif
154 
tileLoadTile(int nTile)155 char * tileLoadTile(int nTile)
156 {
157     if (!waloff[nTile]) tileLoad(nTile);
158     return (char*)waloff[nTile];
159 }
160 
tileAllocTile(int nTile,int x,int y,int ox,int oy)161 char * tileAllocTile(int nTile, int x, int y, int ox, int oy)
162 {
163     dassert(nTile >= 0 && nTile < kMaxTiles);
164     char *p = (char*)tileCreate(nTile, x, y);
165     dassert(p != NULL);
166     picanm[nTile].xofs = ClipRange(ox, -127, 127);
167     picanm[nTile].yofs = ClipRange(oy, -127, 127);
168     return (char*)waloff[nTile];
169 }
170 
tilePreloadTile(int nTile)171 void tilePreloadTile(int nTile)
172 {
173     int n = 1;
174     switch (picanm[nTile].extra&7)
175     {
176     case 0:
177         n = 1;
178         break;
179     case 1:
180         n = 5;
181         break;
182     case 2:
183         n = 8;
184         break;
185     case 3:
186         n = 2;
187         break;
188     case 6:
189     case 7:
190         if (voxelIndex[nTile] < 0 || voxelIndex[nTile] >= kMaxVoxels)
191         {
192             voxelIndex[nTile] = -1;
193             picanm[nTile].extra &= ~7;
194         }
195         else
196             qloadvoxel(voxelIndex[nTile]);
197         break;
198     }
199     while(n--)
200     {
201         if (picanm[nTile].sf&PICANM_ANIMTYPE_MASK)
202         {
203             for (int frame = picanm[nTile].num; frame >= 0; frame--)
204             {
205                 if ((picanm[nTile].sf&PICANM_ANIMTYPE_MASK) == PICANM_ANIMTYPE_BACK)
206                     tileLoadTile(nTile-frame);
207                 else
208                     tileLoadTile(nTile+frame);
209             }
210         }
211         else
212             tileLoadTile(nTile);
213         nTile += 1+picanm[nTile].num;
214     }
215 }
216 
217 int nPrecacheCount;
218 char precachehightile[2][(MAXTILES+7)>>3];
219 
tilePrecacheTile(int nTile,int nType)220 void tilePrecacheTile(int nTile, int nType)
221 {
222     int n = 1;
223     switch (picanm[nTile].extra&7)
224     {
225     case 0:
226         n = 1;
227         break;
228     case 1:
229         n = 5;
230         break;
231     case 2:
232         n = 8;
233         break;
234     case 3:
235         n = 2;
236         break;
237     }
238     while(n--)
239     {
240         if (picanm[nTile].sf&PICANM_ANIMTYPE_MASK)
241         {
242             for (int frame = picanm[nTile].num; frame >= 0; frame--)
243             {
244                 int tile;
245                 if ((picanm[nTile].sf&PICANM_ANIMTYPE_MASK) == PICANM_ANIMTYPE_BACK)
246                     tile = nTile-frame;
247                 else
248                     tile = nTile+frame;
249                 if (!TestBitString(gotpic, tile))
250                 {
251                     nPrecacheCount++;
252                     SetBitString(gotpic, tile);
253                 }
254                 SetBitString(precachehightile[nType], tile);
255             }
256         }
257         else
258         {
259             if (!TestBitString(gotpic, nTile))
260             {
261                 nPrecacheCount++;
262                 SetBitString(gotpic, nTile);
263             }
264             SetBitString(precachehightile[nType], nTile);
265         }
266         nTile += 1+picanm[nTile].num;
267     }
268 }
269 
tileGetSurfType(int hit)270 char tileGetSurfType(int hit)
271 {
272     int n = hit & 0x3fff;
273     switch (hit&0xc000)
274     {
275     case 0x4000:
276         return surfType[sector[n].floorpicnum];
277     case 0x6000:
278         return surfType[sector[n].ceilingpicnum];
279     case 0x8000:
280         return surfType[wall[n].picnum];
281     case 0xc000:
282         return surfType[sprite[n].picnum];
283     }
284     return 0;
285 }
286