-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd_level.cpp
429 lines (371 loc) · 13.3 KB
/
d_level.cpp
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
//
// Copyright(C) 2018 James Haley
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// PURPOSE: Level translator - allows output of vanilla Doom format levels
//
#include <vector>
#include "z_zone.h"
#include "z_auto.h"
#include "doomtype.h"
#include "i_system.h"
#include "m_argv.h"
#include "m_binary.h"
#include "m_fixed.h"
#include "m_misc.h"
#include "m_qstr.h"
#include "w_wad.h"
#include "w_iterator.h"
#include "d_wads.h"
#include "d_level.h"
#define DOOM_VERTEX_LEN 4
#define PSX_VERTEX_LEN 8
static std::vector<qstring> g_texturenames;
static std::vector<qstring> g_flatnames;
static void D_BuildTextureNameLookup(const WadDirectory &dir)
{
WadNamespaceIterator wni { dir, lumpinfo_t::ns_textures };
for(wni.begin(); wni.current(); wni.next())
{
g_texturenames.push_back(qstring(wni.current()->name));
}
}
static void D_BuildFlatNameLookup(const WadDirectory &dir)
{
WadNamespaceIterator wni { dir, lumpinfo_t::ns_flats };
for(wni.begin(); wni.current(); wni.next())
{
g_flatnames.push_back(qstring(wni.current()->name));
}
}
static const qstring &D_SafeTextureName(int index)
{
return (index >= 0 && uint(index) < g_texturenames.size()) ? g_texturenames[index] : qstring::emptyString;
}
static const qstring &D_SafeFlatName(int index)
{
return (index >= 0 && uint(index) < g_flatnames.size()) ? g_flatnames[index] : qstring::emptyString;
}
//
// Get output size for VERTEXES lump
//
static inline constexpr size_t D_VERTEXESLen(const size_t inSize)
{
return (inSize / PSX_VERTEX_LEN) * DOOM_VERTEX_LEN;
}
//
// Translate VERTEXES lump
//
static void D_TranslateVERTEXES(byte *inData, size_t inSize, byte *&outData)
{
// input is two 32-bit fixed point coordinates per vertex
// output is two shorts per vertex
while(inSize > 0)
{
const fixed_t x = GetBinaryDWord(&inData);
const fixed_t y = GetBinaryDWord(&inData);
int16_t outX = int16_t(x / FRACUNIT);
int16_t outY = int16_t(y / FRACUNIT);
// round if fractional portion is >= 0.5
if(abs(x & (FRACUNIT - 1)) >= FRACUNIT / 2)
{
if(x < 0)
--outX;
else if(x >= 0)
++outX;
}
if(abs(y & (FRACUNIT - 1)) >= FRACUNIT / 2)
{
if(y < 0)
--outY;
else if(y >= 0)
++outY;
}
PutBinaryWord(outData, outX);
PutBinaryWord(outData, outY);
inSize -= PSX_VERTEX_LEN;
}
}
#define DOOM_SECTOR_SIZE MAPSECTOR_ORIG_SIZEOF
#define PSX_SECTOR_SIZE 28
#define PSX_FINAL_SECTOR_SIZE 16
//
// Get output size for SECTORS lump
//
static inline constexpr size_t D_SECTORSLen(const size_t inSize)
{
return (inSize / PSX_SECTOR_SIZE) * DOOM_SECTOR_SIZE;
}
static inline constexpr size_t D_FinalSECTORSLen(const size_t inSize)
{
return (inSize / PSX_FINAL_SECTOR_SIZE) * DOOM_SECTOR_SIZE;
}
//
// Translate SECTORS lump
//
static void D_TranslateSECTORS(byte *inData, size_t inSize, byte *&outData)
{
const bool isFinalDoom = D_IsFinalDoom();
const size_t numsectors = inSize / (isFinalDoom ? PSX_FINAL_SECTOR_SIZE : PSX_SECTOR_SIZE);
for(size_t i = 0; i < numsectors; i++)
{
edefstructvar(mapsector_t, sector);
sector.floorheight = GetBinaryWord(&inData);
sector.ceilingheight = GetBinaryWord(&inData);
if(isFinalDoom)
{
const int16_t floorpicnum = GetBinaryWord(&inData); // Final Doom: floorpicnum
const int16_t ceilingpicnum = GetBinaryWord(&inData); // Final Doom: ceilingpicnum
strncpy(sector.floorpic, D_SafeFlatName(floorpicnum).constPtr(), 8);
strncpy(sector.ceilingpic, D_SafeFlatName(ceilingpicnum).constPtr(), 8);
}
else
{
GetBinaryString(&inData, sector.floorpic, 8);
GetBinaryString(&inData, sector.ceilingpic, 8);
}
sector.lightlevel = *inData++;
inData++; // discard color byte
sector.special = GetBinaryWord(&inData);
sector.tag = GetBinaryWord(&inData);
inData += 2; // discard flags
PutBinaryWord(outData, sector.floorheight);
PutBinaryWord(outData, sector.ceilingheight);
PutBinaryCharArray<>(outData, sector.floorpic);
PutBinaryCharArray<>(outData, sector.ceilingpic);
PutBinaryWord(outData, sector.lightlevel);
PutBinaryWord(outData, sector.special);
PutBinaryWord(outData, sector.tag);
}
}
#define DOOM_SIDEDEF_SIZE MAPSIDEDEF_ORIG_SIZEOF
#define PSX_FINAL_SIDEDEF_SIZE 12
static inline constexpr size_t D_FinalSIDEDEFSLen(const size_t inSize)
{
return (inSize / PSX_FINAL_SIDEDEF_SIZE) * DOOM_SIDEDEF_SIZE;
}
//
// Translate Final Doom SIDEDEFS lump
//
static void D_TranslateFinalSIDEDEFS(byte *inData, size_t inSize, byte *&outData)
{
const size_t numsides = inSize / PSX_FINAL_SIDEDEF_SIZE;
for(size_t i = 0; i < numsides; i++)
{
edefstructvar(mapsidedef_t, side);
side.textureoffset = GetBinaryWord(&inData);
side.rowoffset = GetBinaryWord(&inData);
int16_t toptexnum = GetBinaryWord(&inData); // Final Doom: top texture index
int16_t bottexnum = GetBinaryWord(&inData); // Final Doom: bottom texture index
int16_t midtexnum = GetBinaryWord(&inData); // Final Doom: middle texture index
strncpy(side.toptexture, D_SafeTextureName(toptexnum).constPtr(), 8);
strncpy(side.bottomtexture, D_SafeTextureName(bottexnum).constPtr(), 8);
strncpy(side.midtexture, D_SafeTextureName(midtexnum).constPtr(), 8);
side.sector = GetBinaryWord(&inData);
PutBinaryWord(outData, side.textureoffset);
PutBinaryWord(outData, side.rowoffset);
PutBinaryCharArray<>(outData, side.toptexture);
PutBinaryCharArray<>(outData, side.bottomtexture);
PutBinaryCharArray<>(outData, side.midtexture);
PutBinaryWord(outData, side.sector);
}
}
enum thingtlflags_e
{
TTLF_FIXTHINGS = 1,
TTLF_USE_EE_DNUMS = 2 // Target Eternity's custom thing types
};
//
// Translate THINGS lump - certain types of transforms may be optionally applied
// to change thing types that are specific to the PlayStation port.
//
static void D_TranslateTHINGS(byte *inData, size_t inSize, byte *&outData, uint tlflags)
{
const size_t numthings = inSize / MAPTHING_ORIG_SIZEOF;
for(size_t i = 0; i < numthings; i++)
{
mapthing_t mt;
mt.x = GetBinaryWord(&inData);
mt.y = GetBinaryWord(&inData);
mt.angle = GetBinaryWord(&inData);
mt.type = GetBinaryWord(&inData);
mt.options = GetBinaryWord(&inData);
// fix up thing types?
if(tlflags & TTLF_FIXTHINGS)
{
psxblendmode_e spectreType = static_cast<psxblendmode_e>((mt.options & MTF_PSX_BLENDMASK) >> MTF_BLENDSHIFT);
switch(mt.type)
{
case DEN_DEMON:
if(mt.options & MTF_PSX_USEBLEND)
{
if(tlflags & TTLF_USE_EE_DNUMS)
{
switch(spectreType)
{
case PBM_TL50:
mt.type = DEN_EE_SPECTRE0;
break;
case PBM_TLADD100:
mt.type = DEN_EE_SPECTRE1;
break;
case PBM_NIGHTMARE:
mt.type = DEN_EE_NMSPECTRE;
break;
case PBM_TLADD25:
mt.type = DEN_EE_SPECTRE3;
break;
}
}
else
mt.type = DEN_SPECTRE;
}
break;
case DEN_CACODEMON:
if(mt.options & MTF_PSX_USEBLEND && tlflags & TTLF_USE_EE_DNUMS)
{
if(spectreType == PBM_TL50)
mt.type = DEN_EE_SPECCACO;
else
printf("Warning: unknown Cacodemon type at (%d, %d); spectre type = %d\n", mt.x, mt.y, spectreType);
}
break;
case DEN_CHAIN:
if(tlflags & TTLF_USE_EE_DNUMS)
mt.type = DEN_EE_CHAIN;
else
mt.type = DEN_HANGINGLEG;
break;
default:
if(mt.options & MTF_PSX_USEBLEND)
printf("Warning: unknown spectral thing type %d at (%d, %d); spectre type = %d\n", mt.type, mt.x, mt.y, spectreType);
break;
}
// remove the special flags since they conflict with ports like Boom and MBF
mt.options &= ~(MTF_PSX_USEBLEND|MTF_PSX_BLENDMASK);
}
PutBinaryWord(outData, mt.x);
PutBinaryWord(outData, mt.y);
PutBinaryWord(outData, mt.angle);
PutBinaryWord(outData, mt.type);
PutBinaryWord(outData, mt.options);
}
}
//
// Ouput a vanilla-format level wad for PSX Doom level input.
// Thing types, line specials, and line flags are not currently accounted for.
// Textures are not translated - TODO: create a separate texture wad to use.
//
void D_TranslateLevelToVanilla(WadDirectory &dir, const qstring &outName)
{
uint tlflags = 0;
if(M_CheckParm("-fixthings") != 0)
tlflags |= TTLF_FIXTHINGS;
if(M_CheckParm("-ee") != 0)
tlflags |= TTLF_USE_EE_DNUMS;
WadNamespaceIterator wni { dir, lumpinfo_t::ns_global };
size_t sizeNeeded = 0;
int numLumps = 0;
const bool isFinalDoom = D_IsFinalDoom();
if(isFinalDoom)
{
D_BuildTextureNameLookup(dir);
D_BuildFlatNameLookup(dir);
}
// add in total of lump sizes
for(wni.begin(); wni.current(); wni.next())
{
lumpinfo_t *curLump = wni.current();
if(!strcasecmp(curLump->name, "SECTORS"))
sizeNeeded += isFinalDoom ? D_FinalSECTORSLen(curLump->size) : D_SECTORSLen(curLump->size);
else if(!strcasecmp(curLump->name, "SIDEDEFS") && isFinalDoom)
sizeNeeded += D_FinalSIDEDEFSLen(curLump->size);
else if(!strcasecmp(curLump->name, "VERTEXES"))
sizeNeeded += D_VERTEXESLen(curLump->size);
else if(!strcasecmp(curLump->name, "LEAFS"))
continue; // don't include LEAFS
else
sizeNeeded += curLump->size;
++numLumps;
}
// setup header and calculate directory size
wadinfo_t header = { {'P','W','A','D'}, numLumps, 12 };
size_t dirSize = 12 + 16 * numLumps;
size_t filepos = dirSize;
// add in directory size
sizeNeeded += dirSize;
if(!sizeNeeded)
I_Error("D_TranslateLevelToVanilla: zero-size wad file\n");
auto buffer = ecalloc(byte *, 1, sizeNeeded);
// write in header
memcpy(buffer, header.identification, 4);
byte *inptr = buffer + 4;
PutBinaryDWord(inptr, header.numlumps);
PutBinaryDWord(inptr, header.infotableofs);
// write in directory
for(wni.begin(); wni.current(); wni.next())
{
lumpinfo_t *lump = wni.current();
if(!strcasecmp(lump->name, "LEAFS"))
continue; // don't include LEAFS
PutBinaryDWord(inptr, int32_t(filepos));
size_t sizeToUse = lump->size;
if(!strcasecmp(lump->name, "SECTORS"))
sizeToUse = isFinalDoom ? D_FinalSECTORSLen(lump->size) : D_SECTORSLen(lump->size);
else if(!strcasecmp(lump->name, "SIDEDEFS") && isFinalDoom)
sizeToUse = D_FinalSIDEDEFSLen(lump->size);
else if(!strcasecmp(lump->name, "VERTEXES"))
sizeToUse = D_VERTEXESLen(lump->size);
PutBinaryDWord(inptr, int32_t(sizeToUse));
memcpy(inptr, lump->name, 8);
inptr += 8;
filepos += sizeToUse;
}
// write in lumps
for(wni.begin(); wni.current(); wni.next())
{
lumpinfo_t *lump = wni.current();
if(lump->size > 0)
{
ZAutoBuffer buf;
dir.cacheLumpAuto(lump->selfindex, buf);
if(!strcasecmp(lump->name, "SECTORS"))
D_TranslateSECTORS(buf.getAs<byte *>(), lump->size, inptr);
else if(!strcasecmp(lump->name, "SIDEDEFS") && isFinalDoom)
D_TranslateFinalSIDEDEFS(buf.getAs<byte *>(), lump->size, inptr);
else if(!strcasecmp(lump->name, "VERTEXES"))
D_TranslateVERTEXES(buf.getAs<byte *>(), lump->size, inptr);
else if(!strcasecmp(lump->name, "THINGS") && tlflags != 0) // only translate things if so instructed
D_TranslateTHINGS(buf.getAs<byte *>(), lump->size, inptr, tlflags);
else if(!strcasecmp(lump->name, "LEAFS"))
continue; // don't include LEAFS
else
{
memcpy(inptr, buf.get(), lump->size);
inptr += lump->size;
}
}
}
// output wad file
qstring wadName = outName;
size_t dotpos;
if((dotpos = wadName.findLastOf('.')) != qstring::npos)
wadName.truncate(dotpos);
wadName.addDefaultExtension("wad");
M_WriteFile(wadName.constPtr(), buffer, sizeNeeded);
efree(buffer);
}
// EOF