-
Notifications
You must be signed in to change notification settings - Fork 5
/
p_setup.c
616 lines (487 loc) · 15.6 KB
/
p_setup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* Emacs style mode select -*- C++ -*-
*-----------------------------------------------------------------------------
*
*
* PrBoom: a Doom port merged with LxDoom and LSDLDoom
* based on BOOM, a modified and improved DOOM engine
* Copyright (C) 1999 by
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
* Copyright (C) 1999-2000 by
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
* Copyright 2005, 2006 by
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
* Copyright 2023, 2024 by
* Frenkel Smeijers
*
* 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.
*
* DESCRIPTION:
* Do all the WAD I/O, get map description,
* set up initial state and misc. LUTs.
*
*-----------------------------------------------------------------------------*/
#include <math.h>
#include <stdint.h>
#include "d_player.h"
#include "g_game.h"
#include "w_wad.h"
#include "r_main.h"
#include "r_things.h"
#include "p_maputl.h"
#include "p_map.h"
#include "p_setup.h"
#include "p_spec.h"
#include "p_tick.h"
#include "p_enemy.h"
#include "s_sound.h"
#include "i_system.h"
#include "v_video.h"
#include "globdata.h"
//
// MAP related Lookup tables.
// Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
//
const seg_t __far* _g_segs;
int16_t _g_numsectors;
sector_t __far* _g_sectors;
static int16_t numsubsectors;
subsector_t __far* _g_subsectors;
int16_t _g_numlines;
line_t __far* _g_lines;
static int16_t numsides;
side_t __far* _g_sides;
// BLOCKMAP
// Created from axis aligned bounding box
// of the map, a rectangular array of
// blocks of size ...
// Used to speed up collision detection
// by spatial subdivision in 2D.
//
// Blockmap size.
int16_t _g_bmapwidth, _g_bmapheight; // size in mapblocks
// killough 3/1/98: remove blockmap limit internally:
const int16_t __far* _g_blockmap;
// offsets in blockmap are from here
const int16_t __far* _g_blockmaplump;
fixed_t _g_bmaporgx, _g_bmaporgy; // origin of block map
mobj_t __far*__far* _g_blocklinks; // for thing chains
//
// REJECT
// For fast sight rejection.
// Speeds up enemy AI by skipping detailed
// LineOf Sight calculation.
// Without the special effect, this could
// be used as a PVS lookup as well.
//
const byte __far* _g_rejectmatrix;
mobj_t __far* _g_thingPool;
int16_t _g_thingPoolSize;
// Lump order in a map WAD: each map needs a couple of lumps
// to provide a complete scene geometry description.
enum {
ML_LABEL, // A separator, name, ExMx or MAPxx
ML_THINGS, // Monsters, items..
ML_LINEDEFS, // LineDefs, from editing
ML_SIDEDEFS, // SideDefs, from editing
ML_SEGS, // LineSegs, from LineDefs split by BSP
ML_SSECTORS, // SubSectors, list of LineSegs
ML_NODES, // BSP nodes
ML_SECTORS, // Sectors, from editing
ML_REJECT, // LUT, sector-sector visibility
ML_BLOCKMAP // LUT, motion clipping, walls/grid element
};
//
// P_LoadSegs
//
static void P_LoadSegs (int16_t lump)
{
_g_segs = (const seg_t __far*)W_GetLumpByNumAutoFree(lump);
}
//
// P_LoadSubsectors
//
// SubSector, as generated by BSP.
typedef struct {
int8_t numsegs;
} mapsubsector_t;
typedef char assertMapsubsectorSize[sizeof(mapsubsector_t) == 1 ? 1 : -1];
static void P_LoadSubsectors (int16_t lump)
{
const mapsubsector_t __far* data;
int16_t i;
uint16_t firstseg; // Index of first one; segs are stored sequentially.
numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
_g_subsectors = Z_CallocLevel(numsubsectors * sizeof(subsector_t));
data = W_GetLumpByNum(lump);
firstseg = 0;
for (i=0; i<numsubsectors; i++)
{
_g_subsectors[i].numlines = data[i].numsegs;
_g_subsectors[i].firstline = firstseg;
firstseg += data[i].numsegs;
}
Z_Free(data);
}
//
// P_LoadSectors
//
// Sector definition, from editing.
#if defined FLAT_SPAN
typedef struct {
int16_t floorheight;
int16_t ceilingheight;
int16_t floorpic;
int16_t ceilingpic;
uint8_t lightlevel;
int8_t special;
int16_t tag;
} mapsector_t;
typedef char assertMapsectorSize[sizeof(mapsector_t) == 12 ? 1 : -1];
#define R_FlatNumForFarName(p) (p)
#else
typedef struct {
int16_t floorheight;
int16_t ceilingheight;
char floorpic[8];
char ceilingpic[8];
uint8_t lightlevel;
int8_t special;
int16_t tag;
} mapsector_t;
typedef char assertMapsectorSize[sizeof(mapsector_t) == 24 ? 1 : -1];
static int16_t R_FlatNumForFarName(const char __far* far_name)
{
uint64_t nameint = *(uint64_t __far*)far_name;
char* near_name = (char*)&nameint;
return R_FlatNumForName(near_name);
}
#endif
static void P_LoadSectors (int16_t lump)
{
const mapsector_t __far* data;
int16_t i;
_g_numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
_g_sectors = Z_CallocLevel(_g_numsectors * sizeof(sector_t));
data = W_GetLumpByNum(lump);
for (i=0; i<_g_numsectors; i++)
{
sector_t __far* ss = _g_sectors + i;
const mapsector_t __far* ms = data + i;
ss->floorheight = ((int32_t)SHORT(ms->floorheight))<<FRACBITS;
ss->ceilingheight = ((int32_t)SHORT(ms->ceilingheight))<<FRACBITS;
ss->floorpic = R_FlatNumForFarName(ms->floorpic);
ss->ceilingpic = R_FlatNumForFarName(ms->ceilingpic);
ss->lightlevel = ms->lightlevel;
ss->special = ms->special;
ss->oldspecial = ms->special;
ss->tag = SHORT(ms->tag);
ss->thinglist = NULL;
ss->touching_thinglist = NULL;
}
Z_Free(data);
}
//
// P_LoadNodes
//
static void P_LoadNodes (int16_t lump)
{
numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
nodes = W_GetLumpByNumAutoFree (lump);
}
/*
* P_LoadThings
*
*/
static void P_LoadThings(int16_t lump)
{
_g_thingPoolSize = W_LumpLength(lump) / sizeof(mapthing_t);
_g_thingPool = Z_CallocLevel(_g_thingPoolSize * sizeof(mobj_t));
for (int16_t i = 0; i < _g_thingPoolSize; i++)
_g_thingPool[i].type = MT_NOTHING;
}
static void P_LoadThings2(int16_t lump)
{
const mapthing_t __far* data = W_GetLumpByNum(lump);
for (int16_t i = 0; i < _g_thingPoolSize; i++)
{
const mapthing_t __far* mt = &data[i];
// Do spawn all other stuff.
P_SpawnMapThing(mt);
}
Z_Free(data);
}
//
// P_LoadLineDefs
// Also counts secret lines for intermissions.
// ^^^
// ??? killough ???
// Does this mean secrets used to be linedef-based, rather than sector-based?
//
typedef PACKEDATTR_PRE struct
{
vertex16_t v1;
vertex16_t v2; // Vertices, from v1 to v2.
int16_t frontsidenum; // Visual appearance: SideDefs.
int16_t backsidenum;
uint8_t flags; // Animation related.
int8_t const_special;
int8_t tag;
} PACKEDATTR_POST packed_line_t;
typedef char assertLineSize[sizeof(packed_line_t) == 15 ? 1 : -1];
static void P_LoadLineDefs (int16_t lump)
{
_g_numlines = W_LumpLength(lump) / sizeof(packed_line_t);
_g_lines = Z_MallocLevel(_g_numlines * sizeof(line_t), NULL);
const packed_line_t __far* lines = W_GetLumpByNum(lump);
for (int16_t i = 0; i < _g_numlines; i++)
{
_g_lines[i].v1 = lines[i].v1;
_g_lines[i].v2 = lines[i].v2;
_g_lines[i].dx = lines[i].v2.x - lines[i].v1.x; // for side checking.
_g_lines[i].dy = lines[i].v2.y - lines[i].v1.y; // for side checking.
_g_lines[i].sidenum[0] = lines[i].frontsidenum;
_g_lines[i].sidenum[1] = lines[i].backsidenum;
_g_lines[i].bbox[BOXTOP] = lines[i].v1.y < lines[i].v2.y ? lines[i].v2.y : lines[i].v1.y; // Line bounding box.
_g_lines[i].bbox[BOXBOTTOM] = lines[i].v1.y < lines[i].v2.y ? lines[i].v1.y : lines[i].v2.y; // Line bounding box.
_g_lines[i].bbox[BOXLEFT] = lines[i].v1.x < lines[i].v2.x ? lines[i].v1.x : lines[i].v2.x; // Line bounding box.
_g_lines[i].bbox[BOXRIGHT] = lines[i].v1.x < lines[i].v2.x ? lines[i].v2.x : lines[i].v1.x; // Line bounding box.
_g_lines[i].tag = lines[i].tag;
_g_lines[i].flags = lines[i].flags;
// To aid move clipping.
if (_g_lines[i].dx == 0)
_g_lines[i].slopetype = ST_VERTICAL;
else if (_g_lines[i].dy == 0)
_g_lines[i].slopetype = ST_HORIZONTAL;
else if (((fixed_t)_g_lines[i].dy << FRACBITS) / _g_lines[i].dx > 0)
_g_lines[i].slopetype = ST_POSITIVE;
else
_g_lines[i].slopetype = ST_NEGATIVE;
_g_lines[i].validcount = 0;
_g_lines[i].r_validcount = 0;
_g_lines[i].r_flags = 0;
_g_lines[i].special = lines[i].const_special;
}
Z_Free(lines);
}
// A SideDef, defining the visual appearance of a wall,
// by setting textures and offsets.
typedef PACKEDATTR_PRE struct {
int16_t textureoffset;
uint8_t rowoffset;
int8_t toptexture;
int8_t bottomtexture;
int8_t midtexture;
uint8_t sector; // Front sector, towards viewer.
} PACKEDATTR_POST mapsidedef_t;
typedef char assertMapsidedefSize[sizeof(mapsidedef_t) == 7 ? 1 : -1];
//
// P_LoadSideDefs
//
static void P_LoadSideDefs (int16_t lump)
{
numsides = W_LumpLength(lump) / sizeof(mapsidedef_t);
_g_sides = Z_CallocLevel(numsides * sizeof(side_t));
const mapsidedef_t __far* data = W_GetLumpByNum(lump);
for (int16_t i = 0; i < numsides; i++)
{
const mapsidedef_t __far* msd = data + i;
side_t __far* sd = _g_sides + i;
sd->textureoffset = msd->textureoffset;
sd->rowoffset = msd->rowoffset;
sd->sector = &_g_sectors[msd->sector];
sd->midtexture = msd->midtexture;
sd->toptexture = msd->toptexture;
sd->bottomtexture = msd->bottomtexture;
P_LoadTexture(sd->midtexture);
P_LoadTexture(sd->toptexture);
P_LoadTexture(sd->bottomtexture);
}
Z_Free(data);
}
//
// P_LoadBlockMap
//
// killough 3/1/98: substantially modified to work
// towards removing blockmap limit (a wad limitation)
//
// killough 3/30/98: Rewritten to remove blockmap limit,
// though current algorithm is brute-force and unoptimal.
//
static void P_LoadBlockMap (int16_t lump)
{
_g_blockmaplump = W_GetLumpByNumAutoFree(lump);
_g_bmaporgx = ((int32_t)_g_blockmaplump[0])<<FRACBITS;
_g_bmaporgy = ((int32_t)_g_blockmaplump[1])<<FRACBITS;
_g_bmapwidth = _g_blockmaplump[2];
_g_bmapheight = _g_blockmaplump[3];
// clear out mobj chains - CPhipps - use calloc
_g_blocklinks = Z_CallocLevel(_g_bmapwidth * _g_bmapheight * sizeof(*_g_blocklinks));
_g_blockmap = _g_blockmaplump+4;
}
//
// P_LoadReject - load the reject table
//
static void P_LoadReject(int16_t lump)
{
_g_rejectmatrix = W_GetLumpByNumAutoFree(lump);
}
//
// P_GroupLines
// Builds sector line lists and subsector sector numbers.
// Finds block bounding boxes for sectors.
//
// killough 5/3/98: reformatted, cleaned up
// cph 18/8/99: rewritten to avoid O(numlines * numsectors) section
// It makes things more complicated, but saves seconds on big levels
// figgi 09/18/00 -- adapted for gl-nodes
// cph - convenient sub-function
static void P_AddLineToSector(const line_t __far* li, sector_t __far* sector)
{
sector->lines[sector->linecount++] = li;
}
static void M_ClearBox (fixed_t *box)
{
box[BOXTOP] = box[BOXRIGHT] = INT32_MIN;
box[BOXBOTTOM] = box[BOXLEFT] = INT32_MAX;
}
static void M_AddToBox(fixed_t* box,fixed_t x,fixed_t y)
{
if (x<box[BOXLEFT])
box[BOXLEFT] = x;
else if (x>box[BOXRIGHT])
box[BOXRIGHT] = x;
if (y<box[BOXBOTTOM])
box[BOXBOTTOM] = y;
else if (y>box[BOXTOP])
box[BOXTOP] = y;
}
static void P_GroupLines (void)
{
const line_t __far* li;
sector_t __far* sector;
int16_t i,j, total = _g_numlines;
// figgi
for (i=0 ; i<numsubsectors ; i++)
{
const seg_t __far* seg = &_g_segs[_g_subsectors[i].firstline];
_g_subsectors[i].sector = NULL;
for(j=0; j<_g_subsectors[i].numlines; j++)
{
if(seg->sidenum != NO_INDEX)
{
_g_subsectors[i].sector = _g_sides[seg->sidenum].sector;
break;
}
seg++;
}
}
// count number of lines in each sector
for (i=0,li=_g_lines; i<_g_numlines; i++, li++)
{
LN_FRONTSECTOR(li)->linecount++;
if (LN_BACKSECTOR(li) && LN_BACKSECTOR(li) != LN_FRONTSECTOR(li))
{
LN_BACKSECTOR(li)->linecount++;
total++;
}
}
{ // allocate line tables for each sector
const line_t __far*__far*linebuffer = Z_MallocLevel(total*sizeof(line_t __far*), NULL);
for (i=0, sector = _g_sectors; i<_g_numsectors; i++, sector++)
{
sector->lines = linebuffer;
linebuffer += sector->linecount;
sector->linecount = 0;
}
}
// Enter those lines
for (i=0,li=_g_lines; i<_g_numlines; i++, li++)
{
P_AddLineToSector(li, LN_FRONTSECTOR(li));
if (LN_BACKSECTOR(li) && LN_BACKSECTOR(li) != LN_FRONTSECTOR(li))
P_AddLineToSector(li, LN_BACKSECTOR(li));
}
for (i=0, sector = _g_sectors; i<_g_numsectors; i++, sector++)
{
fixed_t bbox[4];
M_ClearBox(bbox);
for(int16_t l = 0; l < sector->linecount; l++)
{
M_AddToBox (bbox, (fixed_t)sector->lines[l]->v1.x<<FRACBITS, (fixed_t)sector->lines[l]->v1.y<<FRACBITS);
M_AddToBox (bbox, (fixed_t)sector->lines[l]->v2.x<<FRACBITS, (fixed_t)sector->lines[l]->v2.y<<FRACBITS);
}
sector->soundorg.x = bbox[BOXRIGHT]/2+bbox[BOXLEFT]/2;
sector->soundorg.y = bbox[BOXTOP]/2+bbox[BOXBOTTOM]/2;
}
}
static void P_FreeLevelData()
{
#if !defined FLAT_SPAN
R_ResetPlanes();
#endif
Z_FreeTags();
}
//
// P_SetupLevel
//
void P_SetupLevel(int16_t map)
{
int_fast8_t i;
char lumpname[9];
int16_t lumpnum;
_g_totallive = _g_totalkills = _g_totalitems = _g_totalsecret = 0;
_g_wminfo.partime = 180;
for (i=0; i<MAXPLAYERS; i++)
_g_player.killcount = _g_player.secretcount = _g_player.itemcount = 0;
// Initial height of PointOfView will be set by player think.
_g_player.viewz = 1;
// Make sure all sounds are stopped before Z_FreeTags.
S_Start();
P_FreeLevelData();
P_InitThinkers();
_g_leveltime = 0;
_g_totallive = 0;
// find map name
sprintf(lumpname, "E1M%d", map); // killough 1/24/98: simplify
lumpnum = W_GetNumForName(lumpname);
P_LoadThings (lumpnum + ML_THINGS);
P_LoadLineDefs (lumpnum + ML_LINEDEFS);
P_LoadSegs (lumpnum + ML_SEGS);
P_LoadBlockMap (lumpnum + ML_BLOCKMAP);
P_LoadNodes (lumpnum + ML_NODES);
P_LoadSectors (lumpnum + ML_SECTORS);
P_LoadSideDefs (lumpnum + ML_SIDEDEFS);
P_LoadReject (lumpnum + ML_REJECT);
P_LoadSubsectors(lumpnum + ML_SSECTORS);
P_GroupLines();
// Note: you don't need to clear player queue slots
// a much simpler fix is in g_game.c
for (i = 0; i < MAXPLAYERS; i++)
_g_player.mo = NULL;
P_LoadThings2(lumpnum + ML_THINGS);
// set up world state
P_SpawnSpecials();
P_MapEnd();
}
//
// P_Init
//
void P_Init (void)
{
P_InitSwitchList();
P_InitPicAnims();
R_InitSprites();
}