Debian Codegraph @

prboom2/src/p_setup.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * Do all the WAD I/O, get map description,
31 * set up initial state and misc. LUTs.
32 *
33 *-----------------------------------------------------------------------------*/
34
35 #include <math.h>
36 #include <zlib.h>
37
38 #include "doomstat.h"
39 #include "doomtype.h"
40 #include "m_bbox.h"
41 #include "g_game.h"
42 #include "w_wad.h"
43 #include "r_main.h"
44 #include "r_things.h"
45 #include "p_maputl.h"
46 #include "p_map.h"
47 #include "p_setup.h"
48 #include "p_spec.h"
49 #include "p_tick.h"
50 #include "p_enemy.h"
51 #include "s_sound.h"
52 #include "s_advsound.h"
53 #include "lprintf.h" //jff 10/6/98 for debug outputs
54 #include "v_video.h"
55 #include "smooth.h"
56 #include "r_fps.h"
57 #include "r_plane.h"
58 #include "g_overflow.h"
59 #include "am_map.h"
60 #include "e6y.h"//e6y
61
62 #include "dsda.h"
63 #include "dsda/args.h"
64 #include "dsda/compatibility.h"
65 #include "dsda/destructible.h"
66 #include "dsda/id_list.h"
67 #include "dsda/line_special.h"
68 #include "dsda/map_format.h"
69 #include "dsda/mapinfo.h"
70 #include "dsda/preferences.h"
71 #include "dsda/scroll.h"
72 #include "dsda/settings.h"
73 #include "dsda/skip.h"
74 #include "dsda/tranmap.h"
75 #include "dsda/udmf.h"
76 #include "dsda/utility.h"
77
78 #include "hexen/p_acs.h"
79 #include "hexen/p_anim.h"
80 #include "hexen/po_man.h"
81 #include "hexen/sn_sonix.h"
82
83 #include "config.h"
84
85 //
86 // MAP related Lookup tables.
87 // Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
88 //
89
90 int numvertexes;
91 vertex_t *vertexes;
92
93 int numsegs;
94 seg_t *segs;
95
96 int numsectors;
97 sector_t *sectors;
98
99 int numsubsectors;
100 subsector_t *subsectors;
101
102 int numnodes;
103 node_t *nodes;
104
105 int numlines;
106 line_t *lines;
107
108 int numsides;
109 side_t *sides;
110
111 int *sslines_indexes;
112 ssline_t *sslines;
113
114 byte *map_subsectors;
115
116 typedef enum {
117 UNKNOWN_NODES = -1,
118 DEFAULT_BSP_NODES,
119 DEEP_BSP_V4_NODES,
120 ZDOOM_XNOD_NODES,
121 ZDOOM_ZNOD_NODES,
122 ZDOOM_XGLN_NODES,
123 ZDOOM_ZGLN_NODES,
124 ZDOOM_XGL2_NODES,
125 ZDOOM_ZGL2_NODES,
126 ZDOOM_XGL3_NODES,
127 ZDOOM_ZGL3_NODES,
128 } nodes_version_t;
129
130 int firstglvertex = 0;
131 static nodes_version_t nodesVersion = DEFAULT_BSP_NODES;
132 dboolean use_gl_nodes = false;
133 dboolean has_behavior;
134 dboolean udmf_map;
135
136 // figgi 08/21/00 -- glSegs
137 typedef struct
138 {
139 unsigned short v1; // start vertex (16 bit)
140 unsigned short v2; // end vertex (16 bit)
141 unsigned short linedef; // linedef, or -1 for minisegs
142 short side; // side on linedef: 0 for right, 1 for left
143 unsigned short partner; // corresponding partner seg, or -1 on one-sided walls
144 } glseg_t;
145
146 ////////////////////////////////////////////////////////////////////////////////////////////
147
148
149 // BLOCKMAP
150 // Created from axis aligned bounding box
151 // of the map, a rectangular array of
152 // blocks of size ...
153 // Used to speed up collision detection
154 // by spatial subdivision in 2D.
155 //
156 // Blockmap size.
157
158 int bmapwidth, bmapheight; // size in mapblocks
159
160 // killough 3/1/98: remove blockmap limit internally:
161 int *blockmap; // was short -- killough
162
163 // offsets in blockmap are from here
164 int *blockmaplump; // was short -- killough
165
166 fixed_t bmaporgx, bmaporgy; // origin of block map
167
168 mobj_t **blocklinks; // for thing chains
169 int blocklinks_count;
170
171 // MAES: extensions to support 512x512 blockmaps.
172 // They represent the maximum negative number which represents
173 // a positive offset, otherwise they are left at -257, which
174 // never triggers a check.
175 // If a blockmap index is ever LE than either, then
176 // its actual value is to be interpreted as 0x01FF&x.
177 // Full 512x512 blockmaps get this value set to -1.
178 // A 511x511 blockmap would still have a valid negative number
179 // e.g. -1..510, so they would be set to -2
180 // Non-extreme maps remain unaffected.
181 int blockmapxneg = -257;
182 int blockmapyneg = -257;
183
184 dboolean skipblstart; // MaxW: Skip initial blocklist short
185
186 //
187 // REJECT
188 // For fast sight rejection.
189 // Speeds up enemy AI by skipping detailed
190 // LineOf Sight calculation.
191 // Without the special effect, this could
192 // be used as a PVS lookup as well.
193 //
194
195 const byte *rejectmatrix; // cph - const*
196
197 // Maintain single and multi player starting spots.
198
199 // 1/11/98 killough: Remove limit on deathmatch starts
200 mapthing_t *deathmatchstarts; // killough
201 size_t num_deathmatchstarts; // killough
202
203 mapthing_t *deathmatch_p;
204 mapthing_t playerstarts[MAX_PLAYER_STARTS][MAX_MAXPLAYERS];
205
206 static int current_episode = -1;
207 static int current_map = -1;
208 static nodes_version_t current_nodesVersion = UNKNOWN_NODES;
209 static int samelevel = false;
210 static int inconsistent_nodes;
211
212 typedef struct
213 {
214 int label;
215 int things;
216 int linedefs;
217 int sidedefs;
218 int vertexes;
219 int segs;
220 int ssectors;
221 int nodes;
222 int sectors;
223 int reject;
224 int blockmap;
225 int behavior;
226
227 int znodes;
228 } level_components_t;
229
230 static level_components_t level_components;
231
232 // e6y: Smart malloc
233 // Used by P_SetupLevel() for smart data loading
234 // Do nothing if level is the same
235 static void *malloc_IfSameLevel(void* p, size_t size)
236 {
237 if (!samelevel || !p)
238 {
239 return Z_Malloc(size);
240 }
241 return p;
242 }
243
244 // e6y: Smart calloc
245 // Used by P_SetupLevel() for smart data loading
246 // Clear the memory without allocation if level is the same
247 static void *calloc_IfSameLevel(void* p, size_t n1, size_t n2)
248 {
249 if (!samelevel)
250 {
251 return Z_Calloc(n1, n2);
252 }
253 else
254 {
255 memset(p, 0, n1 * n2);
256 return p;
257 }
258 }
259
260 //
261 // CheckForIdentifier
262 // Checks a lump for a magic string to identify its type (e.g. extended nodes)
263 //
264
265 static dboolean CheckForIdentifier(int lumpnum, const char *id, size_t length)
266 {
267 dboolean result = false;
268
269 if (W_SafeLumpLength(lumpnum) >= length)
270 {
271 const char *data = W_LumpByNum(lumpnum);
272
273 if (!memcmp(data, id, length))
274 result = true;
275 }
276
277 return result;
278 }
279
280 //
281 // P_GetNodesVersion
282 //
283
284 static void P_GetNodesVersion(void)
285 {
286 nodesVersion = DEFAULT_BSP_NODES;
287 use_gl_nodes = false;
288
289 if (nodesVersion == DEFAULT_BSP_NODES)
290 {
291 // https://zdoom.org/wiki/Node
292 if (CheckForIdentifier(level_components.ssectors, "ZGL", 3) ||
293 CheckForIdentifier(level_components.ssectors, "XGL", 3))
294 level_components.znodes = level_components.ssectors;
295
296 if (dsda_Flag(dsda_arg_force_old_zdoom_nodes) && demoplayback)
297 level_components.znodes = LUMP_NOT_FOUND;
298
299 if (CheckForIdentifier(level_components.znodes, "XGLN", 4))
300 {
301 nodesVersion = ZDOOM_XGLN_NODES;
302 lprintf(LO_DEBUG,"P_GetNodesVersion: using XGLN zdoom nodes\n");
303 }
304 else if (CheckForIdentifier(level_components.znodes, "ZGLN", 4))
305 {
306 nodesVersion = ZDOOM_ZGLN_NODES;
307 lprintf(LO_DEBUG,"P_GetNodesVersion: using ZGLN zdoom nodes\n");
308 }
309 else if (CheckForIdentifier(level_components.znodes, "ZGL2", 4))
310 {
311 nodesVersion = ZDOOM_ZGL2_NODES;
312 lprintf(LO_DEBUG,"P_GetNodesVersion: using ZGL2 zdoom nodes\n");
313 }
314 else if (CheckForIdentifier(level_components.znodes, "XGL2", 4))
315 {
316 nodesVersion = ZDOOM_XGL2_NODES;
317 lprintf(LO_DEBUG,"P_GetNodesVersion: using XGL2 zdoom nodes\n");
318 }
319 else if (CheckForIdentifier(level_components.znodes, "ZGL3", 4))
320 {
321 nodesVersion = ZDOOM_ZGL3_NODES;
322 lprintf(LO_DEBUG,"P_GetNodesVersion: using ZGL3 zdoom nodes\n");
323 }
324 else if (CheckForIdentifier(level_components.znodes, "XGL3", 4))
325 {
326 nodesVersion = ZDOOM_XGL3_NODES;
327 lprintf(LO_DEBUG,"P_GetNodesVersion: using XGL3 zdoom nodes\n");
328 }
329 else if (CheckForIdentifier(level_components.nodes, "XNOD", 4))
330 {
331 nodesVersion = ZDOOM_XNOD_NODES;
332 lprintf(LO_DEBUG,"P_GetNodesVersion: using XNOD zdoom nodes\n");
333 }
334 else if (CheckForIdentifier(level_components.nodes, "ZNOD", 4))
335 {
336 nodesVersion = ZDOOM_ZNOD_NODES;
337 lprintf(LO_DEBUG,"P_GetNodesVersion: using ZNOD zdoom nodes\n");
338 }
339 else if (CheckForIdentifier(level_components.nodes, "xNd4\0\0\0\0", 8))
340 { // http://www.sbsoftware.com/files/DeePBSPV4specs.txt
341 nodesVersion = DEEP_BSP_V4_NODES;
342 lprintf(LO_DEBUG,"P_GetNodesVersion: using v4 DeePBSP nodes\n");
343 }
344 else
345 {
346 lprintf(LO_DEBUG,"P_GetNodesVersion: using normal BSP nodes\n");
347 }
348 }
349 }
350
351 //
352 // P_LoadVertexes
353 //
354 // killough 5/3/98: reformatted, cleaned up
355 //
356
357 /*******************************************
358 * Name : P_LoadVertexes *
359 * modified : 09/18/00, adapted for PrBoom *
360 * author : figgi *
361 * what : support for gl nodes *
362 *******************************************/
363
364 // figgi -- FIXME: Automap showes wrong zoom boundaries when starting game
365 // when P_LoadVertexes is used with classic BSP nodes.
366
367 static void P_LoadVertexes(int lump)
368 {
369 int i;
370 const mapvertex_t* ml;
371
372 // Determine number of lumps:
373 // total lump length / vertex record length.
374 numvertexes = W_LumpLength(lump) / sizeof(mapvertex_t);
375 firstglvertex = numvertexes;
376
377 // Allocate zone memory for buffer.
378 vertexes = calloc_IfSameLevel(vertexes, numvertexes, sizeof(vertex_t));
379
380 // Load data into cache.
381 // cph 2006/07/29 - cast to mapvertex_t here, making the loop below much neater
382 ml = (const mapvertex_t*) W_LumpByNum(lump);
383
384 // Copy and convert vertex coordinates,
385 // internal representation as fixed.
386 for (i=0; i < firstglvertex; i++)
387 {
388 vertexes[i].x = LittleShort(ml->x)<<FRACBITS;
389 vertexes[i].y = LittleShort(ml->y)<<FRACBITS;
390 ml++;
391 }
392 }
393
394 static void P_LoadUDMFVertexes(int lump)
395 {
396 int i;
397
398 numvertexes = udmf.num_vertices;
399 vertexes = calloc_IfSameLevel(vertexes, numvertexes, sizeof(vertex_t));
400
401 for (i = 0; i < numvertexes; ++i)
402 {
403 vertexes[i].x = dsda_StringToFixed(udmf.vertices[i].x);
404 vertexes[i].y = dsda_StringToFixed(udmf.vertices[i].y);
405 }
406 }
407
408 /*******************************************
409 * created : 08/13/00 *
410 * modified : 09/18/00, adapted for PrBoom *
411 * author : figgi *
412 * what : basic functions needed for *
413 * computing gl nodes *
414 *******************************************/
415
416 static int checkGLVertex(int num)
417 {
418 if (num & 0x8000)
419 num = (num&0x7FFF)+firstglvertex;
420 return num;
421 }
422
423 static float GetTexelDistance(int dx, int dy)
424 {
425 float fx = (float)(dx)/FRACUNIT, fy = (float)(dy)/FRACUNIT;
426 return (float)((int)(0.5f + (float)sqrt(fx*fx + fy*fy)));
427 }
428
429 static int GetOffset(vertex_t *v1, vertex_t *v2)
430 {
431 float a, b;
432 int r;
433 a = (float)(v1->x - v2->x) / (float)FRACUNIT;
434 b = (float)(v1->y - v2->y) / (float)FRACUNIT;
435 r = (int)(sqrt(a*a+b*b) * (float)FRACUNIT);
436 return r;
437 }
438
439
440
441 //
442 // P_LoadSegs
443 //
444 // killough 5/3/98: reformatted, cleaned up
445
446 static void P_LoadSegs (int lump)
447 {
448 int i;
449 const mapseg_t *data; // cph - const
450
451 numsegs = W_LumpLength(lump) / sizeof(mapseg_t);
452 segs = calloc_IfSameLevel(segs, numsegs, sizeof(seg_t));
453 data = (const mapseg_t *)W_LumpByNum(lump); // cph - wad lump handling updated
454
455 if ((!data) || (!numsegs))
456 I_Error("P_LoadSegs: no segs in level");
457
458 for (i=0; i<numsegs; i++)
459 {
460 seg_t *li = segs+i;
461 const mapseg_t *ml = data + i;
462 unsigned short v1, v2;
463
464 int side, linedef;
465 line_t *ldef;
466
467 v1 = (unsigned short)LittleShort(ml->v1);
468 v2 = (unsigned short)LittleShort(ml->v2);
469
470 // e6y
471 // moved down for additional checks to avoid overflow
472 // if wrong vertexe's indexes are in SEGS lump
473 // see below for more detailed information
474 //li->v1 = &vertexes[v1];
475 //li->v2 = &vertexes[v2];
476
477 // e6y: moved down, see below
478 //li->length = GetDistance(li->v2->x - li->v1->x, li->v2->y - li->v1->y);
479
480 li->angle = (LittleShort(ml->angle))<<16;
481 li->offset =(LittleShort(ml->offset))<<16;
482 linedef = (unsigned short)LittleShort(ml->linedef);
483
484 //e6y: check for wrong indexes
485 if ((unsigned)linedef >= (unsigned)numlines)
486 {
487 I_Error("P_LoadSegs: seg %d references a non-existent linedef %d",
488 i, (unsigned)linedef);
489 }
490
491 ldef = &lines[linedef];
492 li->linedef = ldef;
493 side = LittleShort(ml->side);
494
495 //e6y: fix wrong side index
496 if (side != 0 && side != 1)
497 {
498 lprintf(LO_DEBUG, "P_LoadSegs: seg %d contains wrong side index %d. Replaced with 1.\n", i, side);
499 side = 1;
500 }
501
502 //e6y: check for wrong indexes
503 if ((unsigned)ldef->sidenum[side] >= (unsigned)numsides)
504 {
505 I_Error("P_LoadSegs: linedef %d for seg %d references a non-existent sidedef %d",
506 linedef, i, (unsigned)ldef->sidenum[side]);
507 }
508
509 li->sidedef = &sides[ldef->sidenum[side]];
510
511 /* cph 2006/09/30 - our frontsector can be the second side of the
512 * linedef, so must check for NO_INDEX in case we are incorrectly
513 * referencing the back of a 1S line */
514 if (ldef->sidenum[side] != NO_INDEX)
515 li->frontsector = sides[ldef->sidenum[side]].sector;
516 else {
517 li->frontsector = 0;
518 lprintf(LO_DEBUG, "P_LoadSegs: front of seg %i has no sidedef\n", i);
519 }
520
521 if (ldef->flags & ML_TWOSIDED)
522 {
523 int sidenum = ldef->sidenum[side ^ 1];
524
525 if (sidenum == NO_INDEX)
526 {
527 // this is wrong
528 li->backsector = GetSectorAtNullAddress();
529 }
530 else
531 {
532 li->backsector = sides[sidenum].sector;
533 }
534 }
535 else
536 {
537 li->backsector = 0;
538 }
539
540 // e6y
541 // check and fix wrong references to non-existent vertexes
542 // see e1m9 @ NIVELES.WAD
543 // http://www.doomworld.com/idgames/index.php?id=12647
544 if (v1 >= numvertexes || v2 >= numvertexes)
545 {
546 char str[200] =
547 "P_LoadSegs: compatibility loss - seg %d references a non-existent vertex %d\n";
548
549 if (demorecording)
550 {
551 I_Error(strcat(str, "Demo recording on levels with invalid nodes is not allowed"),
552 i, (v1 >= numvertexes ? v1 : v2));
553 }
554
555 if (v1 >= numvertexes)
556 lprintf(LO_WARN, str, i, v1);
557 if (v2 >= numvertexes)
558 lprintf(LO_WARN, str, i, v2);
559
560 if (li->sidedef == &sides[li->linedef->sidenum[0]])
561 {
562 li->v1 = lines[ml->linedef].v1;
563 li->v2 = lines[ml->linedef].v2;
564 }
565 else
566 {
567 li->v1 = lines[ml->linedef].v2;
568 li->v2 = lines[ml->linedef].v1;
569 }
570 }
571 else
572 {
573 li->v1 = &vertexes[v1];
574 li->v2 = &vertexes[v2];
575 }
576
577 // Recalculate seg offsets that are sometimes incorrect
578 // with certain nodebuilders. Fixes among others, line 20365
579 // of DV.wad, map 5
580 li->offset = GetOffset(li->v1, (ml->side ? ldef->v2 : ldef->v1));
581 }
582 }
583
584 static void P_LoadSegs_V4(int lump)
585 {
586 int i;
587 const mapseg_v4_t *data;
588
589 numsegs = W_LumpLength(lump) / sizeof(mapseg_v4_t);
590 segs = calloc_IfSameLevel(segs, numsegs, sizeof(seg_t));
591 data = (const mapseg_v4_t *)W_LumpByNum(lump);
592
593 if ((!data) || (!numsegs))
594 I_Error("P_LoadSegs_V4: no segs in level");
595
596 for (i = 0; i < numsegs; i++)
597 {
598 seg_t *li = segs+i;
599 const mapseg_v4_t *ml = data + i;
600 int v1, v2;
601
602 int side, linedef;
603 line_t *ldef;
604
605 // MB 2020-04-22: Fix endianess for DeePBSP V4 extended nodes
606 v1 = LittleLong(ml->v1);
607 v2 = LittleLong(ml->v2);
608
609 li->angle = (LittleShort(ml->angle))<<16;
610 li->offset =(LittleShort(ml->offset))<<16;
611 linedef = (unsigned short)LittleShort(ml->linedef);
612
613 //e6y: check for wrong indexes
614 if ((unsigned)linedef >= (unsigned)numlines)
615 {
616 I_Error("P_LoadSegs_V4: seg %d references a non-existent linedef %d",
617 i, (unsigned)linedef);
618 }
619
620 ldef = &lines[linedef];
621 li->linedef = ldef;
622 side = LittleShort(ml->side);
623
624 //e6y: fix wrong side index
625 if (side != 0 && side != 1)
626 {
627 lprintf(LO_DEBUG, "P_LoadSegs_V4: seg %d contains wrong side index %d. Replaced with 1.\n", i, side);
628 side = 1;
629 }
630
631 //e6y: check for wrong indexes
632 if ((unsigned)ldef->sidenum[side] >= (unsigned)numsides)
633 {
634 I_Error("P_LoadSegs_V4: linedef %d for seg %d references a non-existent sidedef %d",
635 linedef, i, (unsigned)ldef->sidenum[side]);
636 }
637
638 li->sidedef = &sides[ldef->sidenum[side]];
639
640 /* cph 2006/09/30 - our frontsector can be the second side of the
641 * linedef, so must check for NO_INDEX in case we are incorrectly
642 * referencing the back of a 1S line */
643 if (ldef->sidenum[side] != NO_INDEX)
644 {
645 li->frontsector = sides[ldef->sidenum[side]].sector;
646 }
647 else
648 {
649 li->frontsector = 0;
650 lprintf(LO_DEBUG, "P_LoadSegs_V4: front of seg %i has no sidedef\n", i);
651 }
652
653 if (ldef->flags & ML_TWOSIDED && ldef->sidenum[side^1]!=NO_INDEX)
654 li->backsector = sides[ldef->sidenum[side^1]].sector;
655 else
656 li->backsector = 0;
657
658 // e6y
659 // check and fix wrong references to non-existent vertexes
660 // see e1m9 @ NIVELES.WAD
661 // http://www.doomworld.com/idgames/index.php?id=12647
662 if (v1 >= numvertexes || v2 >= numvertexes)
663 {
664 char str[200] =
665 "P_LoadSegs_V4: compatibility loss - seg %d references a non-existent vertex %d\n";
666
667 if (demorecording)
668 {
669 I_Error(strcat(str, "Demo recording on levels with invalid nodes is not allowed"),
670 i, (v1 >= numvertexes ? v1 : v2));
671 }
672
673 if (v1 >= numvertexes)
674 lprintf(LO_WARN, str, i, v1);
675 if (v2 >= numvertexes)
676 lprintf(LO_WARN, str, i, v2);
677
678 if (li->sidedef == &sides[li->linedef->sidenum[0]])
679 {
680 li->v1 = lines[ml->linedef].v1;
681 li->v2 = lines[ml->linedef].v2;
682 }
683 else
684 {
685 li->v1 = lines[ml->linedef].v2;
686 li->v2 = lines[ml->linedef].v1;
687 }
688 }
689 else
690 {
691 li->v1 = &vertexes[v1];
692 li->v2 = &vertexes[v2];
693 }
694
695 // Recalculate seg offsets that are sometimes incorrect
696 // with certain nodebuilders. Fixes among others, line 20365
697 // of DV.wad, map 5
698 li->offset = GetOffset(li->v1, (ml->side ? ldef->v2 : ldef->v1));
699 }
700 }
701
702 //
703 // P_LoadSubsectors
704 //
705 // killough 5/3/98: reformatted, cleaned up
706
707 static void P_LoadSubsectors (int lump)
708 {
709 /* cph 2006/07/29 - make data a const mapsubsector_t *, so the loop below is simpler & gives no constness warnings */
710 const mapsubsector_t *data;
711 int i;
712
713 numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
714 subsectors = calloc_IfSameLevel(subsectors, numsubsectors, sizeof(subsector_t));
715 data = (const mapsubsector_t *)W_LumpByNum(lump);
716
717 if ((!data) || (!numsubsectors))
718 I_Error("P_LoadSubsectors: no subsectors in level");
719
720 for (i=0; i<numsubsectors; i++)
721 {
722 // e6y: support for extended nodes
723 subsectors[i].numlines = (unsigned short)LittleShort(data[i].numsegs );
724 subsectors[i].firstline = (unsigned short)LittleShort(data[i].firstseg);
725 }
726 }
727
728 static void P_LoadSubsectors_V4(int lump)
729 {
730 /* cph 2006/07/29 - make data a const mapsubsector_t *, so the loop below is simpler & gives no constness warnings */
731 const mapsubsector_v4_t *data;
732 int i;
733
734 numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_v4_t);
735 subsectors = calloc_IfSameLevel(subsectors, numsubsectors, sizeof(subsector_t));
736 data = (const mapsubsector_v4_t *)W_LumpByNum(lump);
737
738 if ((!data) || (!numsubsectors))
739 I_Error("P_LoadSubsectors_V4: no subsectors in level");
740
741 for (i = 0; i < numsubsectors; i++)
742 {
743 // MB 2020-04-22: Fix endianess for DeePBSP V4 extended nodes
744 subsectors[i].numlines = (unsigned short)LittleShort(data[i].numsegs);
745 subsectors[i].firstline = LittleLong(data[i].firstseg);
746 }
747 }
748
749 //
750 // P_LoadSectors
751 //
752 // killough 5/3/98: reformatted, cleaned up
753
754 static void P_InitializeSectorDefaults(sector_t *ss)
755 {
756 ss->thinglist = NULL;
757 ss->touching_thinglist = NULL; // phares 3/14/98
758 ss->nextsec = -1; //jff 2/26/98 add fields to support locking out
759 ss->prevsec = -1; // stair retriggering until build completes
760 // killough 3/7/98:
761 ss->floor_xoffs = 0;
762 ss->floor_yoffs = 0; // floor and ceiling flats offsets
763 ss->floor_rotation = 0;
764 ss->floor_xscale = FRACUNIT;
765 ss->floor_yscale = FRACUNIT;
766 ss->ceiling_xoffs = 0;
767 ss->ceiling_yoffs = 0;
768 ss->ceiling_rotation = 0;
769 ss->ceiling_xscale = FRACUNIT;
770 ss->ceiling_yscale = FRACUNIT;
771 ss->heightsec = -1; // sector used to get floor and ceiling height
772 ss->floorlightsec = -1; // sector used to get floor lighting
773 // killough 3/7/98: end changes
774
775 // killough 4/11/98 sector used to get ceiling lighting:
776 ss->ceilinglightsec = -1;
777
778 // killough 4/4/98: colormaps:
779 ss->bottommap = ss->midmap = ss->topmap = ss->colormap = 0;
780
781 ss->floorsky = 0;
782 ss->ceilingsky = 0;
783
784 // [kb] For R_WiggleFix
785 ss->cachedheight = 0;
786 ss->scaleindex = 0;
787
788 // hexen
789 ss->seqType = SEQTYPE_STONE; // default seqType
790
791 // killough 8/28/98: initialize all sectors to normal friction
792 ss->friction = ORIG_FRICTION;
793 ss->movefactor = ORIG_FRICTION_FACTOR;
794
795 // zdoom
796 ss->gravity = FRACUNIT;
797 }
798
799 static void P_LoadSectors (int lump)
800 {
801 const byte *data; // cph - const*
802 int i;
803
804 numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
805 sectors = calloc_IfSameLevel(sectors, numsectors, sizeof(sector_t));
806 data = W_LumpByNum (lump); // cph - wad lump handling updated
807
808 dsda_ResetSectorIDList(numsectors);
809
810 for (i=0; i<numsectors; i++)
811 {
812 sector_t *ss = sectors + i;
813 const mapsector_t *ms = (const mapsector_t *) data + i;
814
815 P_InitializeSectorDefaults(ss);
816
817 ss->iSectorID=i; // proff 04/05/2000: needed for OpenGL
818 ss->floorheight = LittleShort(ms->floorheight)<<FRACBITS;
819 ss->ceilingheight = LittleShort(ms->ceilingheight)<<FRACBITS;
820 ss->floorpic = R_FlatNumForName(ms->floorpic);
821 ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
822 ss->lightlevel = LittleShort(ms->lightlevel);
823 ss->special = LittleShort(ms->special);
824 ss->tag = LittleShort(ms->tag);
825
826 dsda_AddSectorID(ss->tag, i);
827 }
828 }
829
830 static void P_LoadUDMFSectors(int lump)
831 {
832 int i;
833
834 numsectors = udmf.num_sectors;
835 sectors = calloc_IfSameLevel(sectors, numsectors, sizeof(sector_t));
836
837 dsda_ResetSectorIDList(numsectors);
838
839 for (i = 0; i < numsectors; ++i)
840 {
841 sector_t *ss = &sectors[i];
842 const udmf_sector_t *ms = &udmf.sectors[i];
843
844 P_InitializeSectorDefaults(ss);
845
846 ss->iSectorID = i;
847 ss->floorheight = dsda_IntToFixed(ms->heightfloor);
848 ss->ceilingheight = dsda_IntToFixed(ms->heightceiling);
849 ss->floorpic = R_FlatNumForName(ms->texturefloor);
850 ss->ceilingpic = R_FlatNumForName(ms->textureceiling);
851 ss->lightlevel = ms->lightlevel;
852 ss->lightlevel_floor = ms->lightfloor;
853 ss->lightlevel_ceiling = ms->lightceiling;
854 ss->special = ms->special;
855 ss->tag = ms->id;
856 ss->floor_xoffs = dsda_FloatToFixed(ms->xpanningfloor);
857 ss->floor_yoffs = dsda_FloatToFixed(ms->ypanningfloor);
858 ss->floor_rotation = dsda_DegreesToAngle(ms->rotationfloor);
859 ss->floor_xscale = dsda_FloatToFixed(ms->xscalefloor);
860 ss->floor_yscale = dsda_FloatToFixed(ms->yscalefloor);
861 ss->ceiling_xoffs = dsda_FloatToFixed(ms->xpanningceiling);
862 ss->ceiling_yoffs = dsda_FloatToFixed(ms->ypanningceiling);
863 ss->ceiling_rotation = dsda_DegreesToAngle(ms->rotationceiling);
864 ss->ceiling_xscale = dsda_FloatToFixed(ms->xscaleceiling);
865 ss->ceiling_yscale = dsda_FloatToFixed(ms->yscaleceiling);
866 ss->gravity = dsda_StringToFixed(ms->gravity);
867
868 if (ms->frictionfactor)
869 {
870 P_ResolveFrictionFactor(dsda_StringToFixed(ms->frictionfactor), ss);
871 }
872
873 if (ms->movefactor)
874 {
875 ss->movefactor = dsda_StringToFixed(ms->movefactor);
876 ss->flags |= SECF_FRICTION;
877 }
878
879 ss->damage.amount = ms->damageamount;
880 ss->damage.leakrate = ms->leakiness;
881 ss->damage.interval = ms->damageinterval;
882
883 if (ms->colormap)
884 {
885 ss->colormap = R_ColormapNumForName(ms->colormap);
886 if (ss->colormap < 0)
887 {
888 lprintf(LO_WARN, "Unknown colormap %s in sector %d.\n", ms->colormap, i);
889 ss->colormap = 0;
890 }
891 }
892
893 if (ms->skyfloor)
894 {
895 ss->floorsky = R_TextureNumForName(ms->skyfloor) | PL_SKYFLAT_SECTOR;
896 }
897
898 if (ms->skyceiling)
899 {
900 ss->ceilingsky = R_TextureNumForName(ms->skyceiling) | PL_SKYFLAT_SECTOR;
901 }
902
903 if ((ms->xscrollfloor || ms->yscrollfloor) && ms->scrollfloormode)
904 dsda_AddZDoomFloorScroller(dsda_FloatToFixed(ms->xscrollfloor),
905 dsda_FloatToFixed(ms->yscrollfloor), i, ms->scrollfloormode);
906
907 if ((ms->xscrollceiling || ms->yscrollceiling) && ms->scrollceilingmode)
908 dsda_AddZDoomCeilingScroller(dsda_FloatToFixed(ms->xscrollceiling),
909 dsda_FloatToFixed(ms->yscrollceiling), i, ms->scrollceilingmode);
910
911 if ((ms->xthrust || ms->ythrust) && ms->thrustgroup && ms->thrustlocation)
912 dsda_AddThruster(dsda_StringToFixed(ms->xthrust), dsda_StringToFixed(ms->ythrust),
913 i, ms->thrustgroup + (ms->thrustlocation << THRUST_LOCATION_SHIFT));
914
915 if (ms->flags & UDMF_SECF_DAMAGEHAZARD)
916 ss->flags |= SECF_HAZARD;
917
918 if (ms->flags & UDMF_SECF_DAMAGETERRAINEFFECT)
919 ss->flags |= SECF_DMGTERRAINFX;
920
921 if (ms->flags & UDMF_SECF_NOATTACK)
922 ss->flags |= SECF_NOATTACK;
923
924 if (ms->flags & UDMF_SECF_SILENT)
925 ss->flags |= SECF_SILENT;
926
927 if (ms->flags & UDMF_SECF_LIGHTFLOORABSOLUTE)
928 ss->flags |= SECF_LIGHTFLOORABSOLUTE;
929
930 if (ms->flags & UDMF_SECF_LIGHTCEILINGABSOLUTE)
931 ss->flags |= SECF_LIGHTCEILINGABSOLUTE;
932
933 if (ms->flags & UDMF_SECF_HIDDEN)
934 ss->flags |= SECF_HIDDEN;
935
936 if (ss->tag > 0)
937 dsda_AddSectorID(ss->tag, i);
938
939 if (ms->moreids)
940 {
941 char **more_ids;
942
943 more_ids = dsda_SplitString(ms->moreids, " ");
944
945 if (more_ids)
946 {
947 int j, id;
948
949 for (j = 0; more_ids[j]; ++j)
950 if (sscanf(more_ids[j], "%d", &id) == 1 && id > 0)
951 dsda_AddSectorID(id, i);
952
953 Z_Free(more_ids);
954 }
955 }
956 }
957 }
958
959 //
960 // P_LoadNodes
961 //
962 // killough 5/3/98: reformatted, cleaned up
963
964 static void P_LoadNodes (int lump)
965 {
966 const byte *data; // cph - const*
967 int i;
968
969 numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
970 nodes = malloc_IfSameLevel(nodes, numnodes * sizeof(node_t));
971 data = W_LumpByNum (lump); // cph - wad lump handling updated
972
973 if ((!data) || (!numnodes))
974 {
975 // allow trivial maps
976 if (numsubsectors == 1)
977 lprintf(LO_INFO,
978 "P_LoadNodes: trivial map (no nodes, one subsector)\n");
979 else
980 I_Error("P_LoadNodes: no nodes in level");
981 }
982
983 for (i=0; i<numnodes; i++)
984 {
985 node_t *no = nodes + i;
986 const mapnode_t *mn = (const mapnode_t *) data + i;
987 int j;
988
989 no->x = LittleShort(mn->x)<<FRACBITS;
990 no->y = LittleShort(mn->y)<<FRACBITS;
991 no->dx = LittleShort(mn->dx)<<FRACBITS;
992 no->dy = LittleShort(mn->dy)<<FRACBITS;
993
994 for (j=0 ; j<2 ; j++)
995 {
996 int k;
997 // e6y: support for extended nodes
998 no->children[j] = (unsigned short)LittleShort(mn->children[j]);
999
1000 // e6y: support for extended nodes
1001 if (no->children[j] == 0xFFFF)
1002 {
1003 no->children[j] = -1;
1004 }
1005 else if (no->children[j] & 0x8000)
1006 {
1007 // Convert to extended type
1008 no->children[j] &= ~0x8000;
1009
1010 // haleyjd 11/06/10: check for invalid subsector reference
1011 if(no->children[j] >= numsubsectors)
1012 {
1013 lprintf(LO_ERROR, "P_LoadNodes: BSP tree references invalid subsector %d.\n", no->children[j]);
1014 no->children[j] = 0;
1015 }
1016
1017 no->children[j] |= NF_SUBSECTOR;
1018 }
1019
1020 for (k=0 ; k<4 ; k++)
1021 no->bbox[j][k] = LittleShort(mn->bbox[j][k])<<FRACBITS;
1022 }
1023 }
1024 }
1025
1026 static void P_LoadNodes_V4(int lump)
1027 {
1028 const byte *data; // cph - const*
1029 int i;
1030
1031 numnodes = (W_LumpLength (lump) - 8) / sizeof(mapnode_v4_t);
1032 nodes = malloc_IfSameLevel(nodes, numnodes * sizeof(node_t));
1033 data = W_LumpByNum (lump); // cph - wad lump handling updated
1034
1035 // skip header
1036 data = data + 8;
1037
1038 if ((!data) || (!numnodes))
1039 {
1040 // allow trivial maps
1041 if (numsubsectors == 1)
1042 lprintf(LO_INFO, "P_LoadNodes_V4: trivial map (no nodes, one subsector)\n");
1043 else
1044 I_Error("P_LoadNodes_V4: no nodes in level");
1045 }
1046
1047 for (i = 0; i < numnodes; i++)
1048 {
1049 node_t *no = nodes + i;
1050 const mapnode_v4_t *mn = (const mapnode_v4_t *) data + i;
1051 int j;
1052
1053 no->x = LittleShort(mn->x)<<FRACBITS;
1054 no->y = LittleShort(mn->y)<<FRACBITS;
1055 no->dx = LittleShort(mn->dx)<<FRACBITS;
1056 no->dy = LittleShort(mn->dy)<<FRACBITS;
1057
1058 for (j=0 ; j<2 ; j++)
1059 {
1060 int k;
1061 // MB 2020-04-22: Fix endianess for DeePBSP V4 extended nodes
1062 no->children[j] = LittleLong(mn->children[j]);
1063
1064 for (k=0 ; k<4 ; k++)
1065 no->bbox[j][k] = LittleShort(mn->bbox[j][k])<<FRACBITS;
1066 }
1067 }
1068 }
1069
1070 static void CheckZNodesOverflow(int *size, int count)
1071 {
1072 (*size) -= count;
1073
1074 if ((*size) < 0)
1075 {
1076 I_Error("P_LoadZNodes: incorrect nodes");
1077 }
1078 }
1079
1080 static byte *P_DecompressData(const byte **data, int *len)
1081 {
1082 byte *output;
1083 int outlen, err;
1084 z_stream *zstream;
1085 union
1086 {
1087 const byte* cd;
1088 byte* d;
1089 } u = { *data };
1090
1091 // first estimate for compression rate:
1092 // output buffer size == 2.5 * input size
1093 outlen = 2.5 * *len;
1094 output = Z_Malloc(outlen);
1095
1096 // initialize stream state for decompression
1097 zstream = Z_Malloc(sizeof(*zstream));
1098 memset(zstream, 0, sizeof(*zstream));
1099
1100 zstream->next_in = u.d;
1101 zstream->avail_in = *len;
1102 zstream->next_out = output;
1103 zstream->avail_out = outlen;
1104
1105 if (inflateInit(zstream) != Z_OK)
1106 I_Error("P_DecompressData: Error during decompression initialization!");
1107
1108 // resize if output buffer runs full
1109 while ((err = inflate(zstream, Z_SYNC_FLUSH)) == Z_OK)
1110 {
1111 int outlen_old = outlen;
1112 outlen = 2 * outlen_old;
1113 output = Z_Realloc(output, outlen);
1114 zstream->next_out = output + outlen_old;
1115 zstream->avail_out = outlen - outlen_old;
1116 }
1117
1118 if (err != Z_STREAM_END)
1119 I_Error("P_DecompressData: Error during decompression!");
1120
1121 *data = output;
1122 *len = zstream->total_out;
1123
1124 if (inflateEnd(zstream) != Z_OK)
1125 I_Error("P_DecompressData: Error during decompression shut-down!");
1126
1127 Z_Free(zstream);
1128
1129 return output;
1130 }
1131
1132 // MB 2020-03-01: Fix endianess for 32-bit ZDoom nodes
1133 static void P_LoadZSegs (const byte *data)
1134 {
1135 int i;
1136
1137 for (i = 0; i < numsegs; i++)
1138 {
1139 line_t *ldef;
1140 unsigned int v1, v2;
1141 unsigned int linedef;
1142 unsigned char side;
1143 seg_t *li = segs+i;
1144 const mapseg_znod_t *ml = (const mapseg_znod_t *) data + i;
1145
1146 v1 = LittleLong(ml->v1);
1147 v2 = LittleLong(ml->v2);
1148
1149 linedef = (unsigned short)LittleShort(ml->linedef);
1150
1151 //e6y: check for wrong indexes
1152 if ((unsigned int)linedef >= (unsigned int)numlines)
1153 {
1154 I_Error("P_LoadZSegs: seg %d references a non-existent linedef %d",
1155 i, (unsigned)linedef);
1156 }
1157
1158 ldef = &lines[linedef];
1159 li->linedef = ldef;
1160 side = ml->side;
1161
1162 //e6y: fix wrong side index
1163 if (side != 0 && side != 1)
1164 {
1165 lprintf(LO_DEBUG, "P_LoadZSegs: seg %d contains wrong side index %d. Replaced with 1.\n", i, side);
1166 side = 1;
1167 }
1168
1169 //e6y: check for wrong indexes
1170 if ((unsigned)ldef->sidenum[side] >= (unsigned)numsides)
1171 {
1172 I_Error("P_LoadZSegs: linedef %d for seg %d references a non-existent sidedef %d",
1173 linedef, i, (unsigned)ldef->sidenum[side]);
1174 }
1175
1176 li->sidedef = &sides[ldef->sidenum[side]];
1177
1178 /* cph 2006/09/30 - our frontsector can be the second side of the
1179 * linedef, so must check for NO_INDEX in case we are incorrectly
1180 * referencing the back of a 1S line */
1181 if (ldef->sidenum[side] != NO_INDEX)
1182 {
1183 li->frontsector = sides[ldef->sidenum[side]].sector;
1184 }
1185 else
1186 {
1187 li->frontsector = 0;
1188 lprintf(LO_DEBUG, "P_LoadZSegs: front of seg %i has no sidedef\n", i);
1189 }
1190
1191 if ((ldef->flags & ML_TWOSIDED) && (ldef->sidenum[side^1] != NO_INDEX))
1192 li->backsector = sides[ldef->sidenum[side^1]].sector;
1193 else
1194 li->backsector = 0;
1195
1196 li->v1 = &vertexes[v1];
1197 li->v2 = &vertexes[v2];
1198
1199 li->offset = GetOffset(li->v1, (side ? ldef->v2 : ldef->v1));
1200 li->angle = R_PointToAngle2(li->v1->x, li->v1->y, li->v2->x, li->v2->y);
1201 //li->angle = (int)((float)atan2(li->v2->y - li->v1->y,li->v2->x - li->v1->x) * (ANG180 / M_PI));
1202 }
1203 }
1204
1205 static void P_LoadGLZSegs(const byte *data, int type)
1206 {
1207 int i, j;
1208 const mapseg_znod_t *ml = (const mapseg_znod_t *) data;
1209 const mapseg_znod2_t *ml2 = (const mapseg_znod2_t *) data;
1210
1211 for (i = 0; i < numsubsectors; ++i)
1212 {
1213 for (j = 0; j < subsectors[i].numlines; ++j)
1214 {
1215 unsigned int v1;
1216 // unsigned int partner;
1217 unsigned int line;
1218 unsigned char side;
1219 seg_t *seg;
1220
1221 if (type < 2)
1222 {
1223 v1 = LittleLong(ml->v1);
1224 // partner = LittleLong(ml->v2);
1225 line = (unsigned short) LittleShort(ml->linedef);
1226 side = ml->side;
1227
1228 if (line == 0xffff)
1229 line = 0xffffffff;
1230
1231 ml++;
1232 }
1233 else
1234 {
1235 v1 = LittleLong(ml2->v1);
1236 // partner = LittleLong(ml2->v2);
1237 line = (unsigned int) LittleLong(ml2->linedef);
1238 side = ml2->side;
1239
1240 ml2++;
1241 }
1242
1243 seg = &segs[subsectors[i].firstline + j];
1244
1245 seg->v1 = &vertexes[v1];
1246 if (j == 0)
1247 {
1248 seg[subsectors[i].numlines - 1].v2 = seg->v1;
1249 }
1250 else
1251 {
1252 seg[-1].v2 = seg->v1;
1253 }
1254
1255 if (line != 0xffffffff)
1256 {
1257 line_t *ldef;
1258
1259 if ((unsigned int) line >= (unsigned int) numlines)
1260 {
1261 I_Error("P_LoadGLZSegs: seg %d, %d references a non-existent linedef %d",
1262 i, j, (unsigned int) line);
1263 }
1264
1265 ldef = &lines[line];
1266 seg->linedef = ldef;
1267
1268 if (side != 0 && side != 1)
1269 {
1270 I_Error("P_LoadGLZSegs: seg %d, %d references a non-existent side %d",
1271 i, j, (unsigned int) side);
1272 }
1273
1274 if ((unsigned)ldef->sidenum[side] >= (unsigned)numsides)
1275 {
1276 I_Error("P_LoadGLZSegs: linedef %d for seg %d, %d references a non-existent sidedef %d",
1277 line, i, j, (unsigned)ldef->sidenum[side]);
1278 }
1279
1280 seg->sidedef = &sides[ldef->sidenum[side]];
1281
1282 /* cph 2006/09/30 - our frontsector can be the second side of the
1283 * linedef, so must check for NO_INDEX in case we are incorrectly
1284 * referencing the back of a 1S line */
1285 if (ldef->sidenum[side] != NO_INDEX)
1286 {
1287 seg->frontsector = sides[ldef->sidenum[side]].sector;
1288 }
1289 else
1290 {
1291 seg->frontsector = 0;
1292 lprintf(LO_DEBUG, "P_LoadGLZSegs: front of seg %d, %d has no sidedef\n", i, j);
1293 }
1294
1295 if ((ldef->flags & ML_TWOSIDED) && (ldef->sidenum[side^1] != NO_INDEX))
1296 seg->backsector = sides[ldef->sidenum[side^1]].sector;
1297 else
1298 seg->backsector = 0;
1299
1300 seg->offset = GetOffset(seg->v1, (side ? ldef->v2 : ldef->v1));
1301 }
1302 else
1303 {
1304 seg->angle = 0;
1305 seg->offset = 0;
1306 seg->linedef = NULL;
1307 seg->sidedef = NULL;
1308 seg->frontsector = segs[subsectors[i].firstline].frontsector;
1309 seg->backsector = seg->frontsector;
1310 }
1311 }
1312
1313 // Need all vertices to be defined before setting angles
1314 for (j = 0; j < subsectors[i].numlines; ++j)
1315 {
1316 seg_t *seg;
1317
1318 seg = &segs[subsectors[i].firstline + j];
1319
1320 if (seg->linedef)
1321 seg->angle = R_PointToAngle2(seg->v1->x, seg->v1->y, seg->v2->x, seg->v2->y);
1322 }
1323 }
1324 }
1325
1326 // MB 2020-03-01: Fix endianess for 32-bit ZDoom nodes
1327 // https://zdoom.org/wiki/Node#ZDoom_extended_nodes
1328 static void P_LoadZNodes(int lump, int glnodes)
1329 {
1330 const byte *data;
1331 size_t node_size;
1332 unsigned int i;
1333 int len;
1334
1335 unsigned int orgVerts, newVerts;
1336 unsigned int numSubs, currSeg;
1337 unsigned int numSegs;
1338 unsigned int numNodes;
1339 vertex_t *newvertarray = NULL;
1340 byte *output = NULL;
1341
1342 data = W_LumpByNum(lump);
1343 len = W_LumpLength(lump);
1344
1345 // skip header
1346 CheckZNodesOverflow(&len, 4);
1347 data += 4;
1348
1349 if (nodesVersion == ZDOOM_ZNOD_NODES ||
1350 nodesVersion == ZDOOM_ZGLN_NODES ||
1351 nodesVersion == ZDOOM_ZGL2_NODES ||
1352 nodesVersion == ZDOOM_ZGL3_NODES)
1353 {
1354 output = P_DecompressData(&data, &len);
1355 }
1356
1357 // Read extra vertices added during node building
1358 CheckZNodesOverflow(&len, sizeof(orgVerts));
1359 orgVerts = LittleLong(*((const unsigned int*)data));
1360 data += sizeof(orgVerts);
1361
1362 CheckZNodesOverflow(&len, sizeof(newVerts));
1363 newVerts = LittleLong(*((const unsigned int*)data));
1364 data += sizeof(newVerts);
1365
1366 if (!samelevel || glnodes)
1367 {
1368 if (orgVerts + newVerts == (unsigned int)numvertexes)
1369 {
1370 newvertarray = vertexes;
1371 }
1372 else
1373 {
1374 newvertarray = Z_Calloc(orgVerts + newVerts, sizeof(vertex_t));
1375 memcpy (newvertarray, vertexes, orgVerts * sizeof(vertex_t));
1376 }
1377
1378 CheckZNodesOverflow(&len, newVerts * (sizeof(newvertarray[0].x) + sizeof(newvertarray[0].y)));
1379 for (i = 0; i < newVerts; i++)
1380 {
1381 newvertarray[i + orgVerts].x = LittleLong(*((const unsigned int*)data));
1382 data += sizeof(newvertarray[0].x);
1383
1384 newvertarray[i + orgVerts].y = LittleLong(*((const unsigned int*)data));
1385 data += sizeof(newvertarray[0].y);
1386 }
1387
1388 if (vertexes != newvertarray)
1389 {
1390 for (i = 0; i < (unsigned int)numlines; i++)
1391 {
1392 lines[i].v1 = lines[i].v1 - vertexes + newvertarray;
1393 lines[i].v2 = lines[i].v2 - vertexes + newvertarray;
1394 }
1395
1396 Z_Free(vertexes);
1397 vertexes = newvertarray;
1398
1399 if (orgVerts + newVerts < numvertexes)
1400 {
1401 lprintf(LO_WARN, "Warning: inconsistent nodes detected\n");
1402 inconsistent_nodes = true;
1403 }
1404
1405 numvertexes = orgVerts + newVerts;
1406 }
1407 }
1408 else
1409 {
1410 int size = newVerts * (sizeof(newvertarray[0].x) + sizeof(newvertarray[0].y));
1411 CheckZNodesOverflow(&len, size);
1412 data += size;
1413
1414 // P_LoadVertexes reset numvertexes, need to increase it again
1415 numvertexes = orgVerts + newVerts;
1416 }
1417
1418 // Read the subsectors
1419 CheckZNodesOverflow(&len, sizeof(numSubs));
1420 numSubs = LittleLong(*((const unsigned int*)data));
1421 data += sizeof(numSubs);
1422
1423 numsubsectors = numSubs;
1424 if (numsubsectors <= 0)
1425 I_Error("P_LoadZNodes: no subsectors in level");
1426 subsectors = calloc_IfSameLevel(subsectors, numsubsectors, sizeof(subsector_t));
1427
1428 CheckZNodesOverflow(&len, numSubs * sizeof(mapsubsector_znod_t));
1429 // MB 2020-03-01
1430 // First segment number of each subsector is not stored
1431 // First subsector starts at segment 0
1432 // Subsequent subsectors starts with the next unused segment number (currSeg)
1433 for (i = currSeg = 0; i < numSubs; i++)
1434 {
1435 const mapsubsector_znod_t *mseg = (const mapsubsector_znod_t *) data + i;
1436
1437 subsectors[i].firstline = currSeg;
1438 subsectors[i].numlines = LittleLong(mseg->numsegs);
1439 currSeg += LittleLong(mseg->numsegs);
1440 }
1441 data += numSubs * sizeof(mapsubsector_znod_t);
1442
1443 // Read the segs
1444 CheckZNodesOverflow(&len, sizeof(numSegs));
1445 numSegs = LittleLong(*((const unsigned int*)data));
1446 data += sizeof(numSegs);
1447
1448 // The number of segs stored should match the number of
1449 // segs used by subsectors.
1450 if (numSegs != currSeg)
1451 {
1452 I_Error("P_LoadZNodes: Incorrect number of segs in nodes.");
1453 }
1454
1455 numsegs = numSegs;
1456 segs = calloc_IfSameLevel(segs, numsegs, sizeof(seg_t));
1457
1458 if (glnodes == 0)
1459 {
1460 CheckZNodesOverflow(&len, numsegs * sizeof(mapseg_znod_t));
1461 P_LoadZSegs(data);
1462 data += numsegs * sizeof(mapseg_znod_t);
1463 }
1464 else
1465 {
1466 size_t seg_size;
1467 use_gl_nodes = true;
1468
1469 seg_size = (glnodes < 2 ? sizeof(mapseg_znod_t) : sizeof(mapseg_znod2_t));
1470
1471 CheckZNodesOverflow(&len, numsegs * seg_size);
1472 P_LoadGLZSegs(data, glnodes);
1473 data += numsegs * seg_size;
1474 }
1475
1476 // Read nodes
1477 CheckZNodesOverflow(&len, sizeof(numNodes));
1478 numNodes = LittleLong(*((const unsigned int*)data));
1479 data += sizeof(numNodes);
1480
1481 numnodes = numNodes;
1482 nodes = calloc_IfSameLevel(nodes, numNodes, sizeof(node_t));
1483
1484 node_size = (glnodes < 3 ? sizeof(mapnode_znod_t) : sizeof(mapnode_znod2_t));
1485 CheckZNodesOverflow(&len, numNodes * node_size);
1486 for (i = 0; i < numNodes; i++)
1487 {
1488 int j, k;
1489 node_t *no = nodes + i;
1490
1491 if (glnodes < 3)
1492 {
1493 const mapnode_znod_t *mn = (const mapnode_znod_t *) data + i;
1494
1495 no->x = LittleShort(mn->x)<<FRACBITS;
1496 no->y = LittleShort(mn->y)<<FRACBITS;
1497 no->dx = LittleShort(mn->dx)<<FRACBITS;
1498 no->dy = LittleShort(mn->dy)<<FRACBITS;
1499
1500 for (j = 0; j < 2; j++)
1501 {
1502 no->children[j] = LittleLong(mn->children[j]);
1503
1504 for (k = 0; k < 4; k++)
1505 no->bbox[j][k] = LittleShort(mn->bbox[j][k])<<FRACBITS;
1506 }
1507 }
1508 else
1509 {
1510 const mapnode_znod2_t *mn2 = (const mapnode_znod2_t *) data + i;
1511
1512 no->x = LittleLong(mn2->x);
1513 no->y = LittleLong(mn2->y);
1514 no->dx = LittleLong(mn2->dx);
1515 no->dy = LittleLong(mn2->dy);
1516
1517 for (j = 0; j < 2; j++)
1518 {
1519 no->children[j] = LittleLong(mn2->children[j]);
1520
1521 for (k = 0; k < 4; k++)
1522 no->bbox[j][k] = LittleShort(mn2->bbox[j][k])<<FRACBITS;
1523 }
1524 }
1525 }
1526
1527 if (output)
1528 Z_Free(output);
1529 }
1530
1531 static int no_overlapped_sprites;
1532 #define GETXY(mobj) ((mobj)->x + ((mobj)->y >> 16))
1533 static int C_DECL dicmp_sprite_by_pos(const void *a, const void *b)
1534 {
1535 const mobj_t *m1 = (*((const mobj_t * const *)a));
1536 const mobj_t *m2 = (*((const mobj_t * const *)b));
1537
1538 int res = GETXY(m2) - GETXY(m1);
1539 no_overlapped_sprites = no_overlapped_sprites && res;
1540 return res;
1541 }
1542
1543 /*
1544 * P_LoadThings
1545 *
1546 * killough 5/3/98: reformatted, cleaned up
1547 * cph 2001/07/07 - don't write into the lump cache, especially non-idepotent
1548 * changes like byte order reversals. Take a copy to edit.
1549 */
1550
1551 static void P_PostProcessThings(int mobjcount, mobj_t **mobjlist)
1552 {
1553 int i;
1554
1555 if (map_format.thing_id)
1556 {
1557 map_format.build_mobj_thing_id_list();
1558 }
1559
1560 if (hexen)
1561 {
1562 P_InitCreatureCorpseQueue(false); // false = do NOT scan for corpses
1563 }
1564
1565 if (V_IsOpenGLMode())
1566 {
1567 no_overlapped_sprites = true;
1568 qsort(mobjlist, mobjcount, sizeof(mobjlist[0]), dicmp_sprite_by_pos);
1569 if (!no_overlapped_sprites)
1570 {
1571 i = 1;
1572 while (i < mobjcount)
1573 {
1574 mobj_t *m1 = mobjlist[i - 1];
1575 mobj_t *m2 = mobjlist[i - 0];
1576
1577 if (GETXY(m1) == GETXY(m2))
1578 {
1579 mobj_t *mo = (m1->index < m2->index ? m1 : m2);
1580 i++;
1581 while (i < mobjcount && GETXY(mobjlist[i]) == GETXY(m1))
1582 {
1583 if (mobjlist[i]->index < mo->index)
1584 {
1585 mo = mobjlist[i];
1586 }
1587 i++;
1588 }
1589
1590 // 'nearest'
1591 mo->flags |= MF_FOREGROUND;
1592 }
1593 i++;
1594 }
1595 }
1596 }
1597
1598 Z_Free(mobjlist);
1599 }
1600
1601 static void P_PostProcessMapThing(mapthing_t *mt, int i, int *mobjcount, mobj_t **mobjlist)
1602 {
1603 mobj_t *mobj;
1604
1605 if (!P_IsDoomnumAllowed(mt->type))
1606 return;
1607
1608 // Although all resources of the Wolf SS have been removed
1609 // off the BFG Edition, there is still one left in MAP33.
1610 // Replace with a Former Human instead.
1611 if (bfgedition && allow_incompatibility && mt->type == 84)
1612 mt->type = 3004;
1613
1614 // Do spawn all other stuff.
1615 mobj = P_SpawnMapThing(mt, i);
1616 if (mobj && mobj->info->speed == 0) {
1617 mobjlist[*mobjcount] = mobj;
1618 *mobjcount += 1;
1619 }
1620 }
1621
1622 static void P_LoadThings(int lump)
1623 {
1624 int i, numthings;
1625 int mobjcount;
1626 mobj_t **mobjlist;
1627 const byte *data;
1628 const hexen_mapthing_t *hexen_data;
1629 const doom_mapthing_t *doom_data;
1630
1631 numthings = W_LumpLength (lump) / map_format.mapthing_size;
1632 data = W_LumpByNum(lump);
1633 hexen_data = (const hexen_mapthing_t*) data;
1634 doom_data = (const doom_mapthing_t*) data;
1635 mobjcount = 0;
1636 mobjlist = Z_Malloc(numthings * sizeof(mobjlist[0]));
1637
1638 if (!data || !numthings)
1639 I_Error("P_LoadThings: no things in level");
1640
1641 for (i = 0; i < numthings; i++)
1642 {
1643 mapthing_t mt;
1644
1645 if (map_format.hexen)
1646 {
1647 const hexen_mapthing_t *hmt = &hexen_data[i];
1648
1649 mt.tid = LittleShort(hmt->tid);
1650 mt.x = LittleShort(hmt->x) << FRACBITS;
1651 mt.y = LittleShort(hmt->y) << FRACBITS;
1652 mt.height = LittleShort(hmt->height) << FRACBITS;
1653 mt.angle = LittleShort(hmt->angle);
1654 mt.type = LittleShort(hmt->type);
1655 mt.options = LittleShort(hmt->options);
1656 mt.special = hmt->special;
1657 mt.special_args[0] = hmt->arg1;
1658 mt.special_args[1] = hmt->arg2;
1659 mt.special_args[2] = hmt->arg3;
1660 mt.special_args[3] = hmt->arg4;
1661 mt.special_args[4] = hmt->arg5;
1662 mt.gravity = FRACUNIT;
1663 mt.health = FRACUNIT;
1664 mt.alpha = 1.f;
1665 }
1666 else
1667 {
1668 const doom_mapthing_t *dmt = &doom_data[i];
1669
1670 mt.tid = 0;
1671 mt.x = LittleShort(dmt->x) << FRACBITS;
1672 mt.y = LittleShort(dmt->y) << FRACBITS;
1673 mt.height = 0;
1674 mt.angle = LittleShort(dmt->angle);
1675 mt.type = LittleShort(dmt->type);
1676 mt.options = LittleShort(dmt->options);
1677 mt.special = 0;
1678 mt.special_args[0] = 0;
1679 mt.special_args[1] = 0;
1680 mt.special_args[2] = 0;
1681 mt.special_args[3] = 0;
1682 mt.special_args[4] = 0;
1683 mt.gravity = FRACUNIT;
1684 mt.health = FRACUNIT;
1685 mt.alpha = 1.f;
1686 }
1687
1688 if (mt.options & MTF_EASY)
1689 mt.options |= MTF_SKILL1 | MTF_SKILL2;
1690
1691 if (mt.options & MTF_NORMAL)
1692 mt.options |= MTF_SKILL3;
1693
1694 if (mt.options & MTF_HARD)
1695 mt.options |= MTF_SKILL4 | MTF_SKILL5;
1696
1697 P_PostProcessMapThing(&mt, i, &mobjcount, mobjlist);
1698 }
1699
1700 P_PostProcessThings(mobjcount, mobjlist);
1701 }
1702
1703 static void P_LoadUDMFThings(int lump)
1704 {
1705 int i, numthings;
1706 int mobjcount;
1707 mobj_t **mobjlist;
1708
1709 numthings = udmf.num_things;
1710 mobjcount = 0;
1711 mobjlist = Z_Malloc(numthings * sizeof(mobjlist[0]));
1712
1713 for (i = 0; i < numthings; i++)
1714 {
1715 mapthing_t mt;
1716 const udmf_thing_t *dmt = &udmf.things[i];
1717
1718 mt.tid = dmt->id;
1719 mt.x = dsda_StringToFixed(dmt->x);
1720 mt.y = dsda_StringToFixed(dmt->y);
1721 mt.height = dsda_StringToFixed(dmt->height);
1722 mt.angle = dmt->angle;
1723 mt.type = dmt->type;
1724 mt.options = 0;
1725 mt.special = dmt->special;
1726 mt.special_args[0] = dmt->arg0;
1727 mt.special_args[1] = dmt->arg1;
1728 mt.special_args[2] = dmt->arg2;
1729 mt.special_args[3] = dmt->arg3;
1730 mt.special_args[4] = dmt->arg4;
1731 mt.gravity = dsda_StringToFixed(dmt->gravity);
1732 mt.health = dsda_StringToFixed(dmt->health);
1733 mt.alpha = dmt->alpha;
1734
1735 if (mt.special == zl_sector_set_colormap || mt.special == zl_map_set_colormap)
1736 {
1737 if (dmt->arg0str)
1738 mt.special_args[0] = R_ColormapNumForName(dmt->arg0str);
1739 else
1740 mt.special_args[0] = -1;
1741
1742 if (mt.special_args[0] < 0)
1743 {
1744 lprintf(LO_WARN, "Unknown colormap in thing %d action.\n", i);
1745 mt.special = 0;
1746 }
1747 }
1748
1749 if (mt.special == zl_music_change_song)
1750 {
1751 if (dmt->arg0str)
1752 mt.special_args[0] = W_CheckNumForName(dmt->arg0str);
1753 else
1754 mt.special_args[0] = LUMP_NOT_FOUND;
1755
1756 if (mt.special_args[0] == LUMP_NOT_FOUND)
1757 {
1758 lprintf(LO_WARN, "Unknown song lump in thing %d action.\n", i);
1759 mt.special = 0;
1760 }
1761 }
1762
1763 if (dmt->flags & UDMF_TF_SKILL1)
1764 mt.options |= MTF_SKILL1;
1765
1766 if (dmt->flags & UDMF_TF_SKILL2)
1767 mt.options |= MTF_SKILL2;
1768
1769 if (dmt->flags & UDMF_TF_SKILL3)
1770 mt.options |= MTF_SKILL3;
1771
1772 if (dmt->flags & UDMF_TF_SKILL4)
1773 mt.options |= MTF_SKILL4;
1774
1775 if (dmt->flags & UDMF_TF_SKILL5)
1776 mt.options |= MTF_SKILL5;
1777
1778 if (dmt->flags & UDMF_TF_AMBUSH)
1779 mt.options |= MTF_AMBUSH;
1780
1781 if (dmt->flags & UDMF_TF_SINGLE)
1782 mt.options |= MTF_GSINGLE;
1783
1784 if (dmt->flags & UDMF_TF_DM)
1785 mt.options |= MTF_GDEATHMATCH;
1786
1787 if (dmt->flags & UDMF_TF_COOP)
1788 mt.options |= MTF_GCOOP;
1789
1790 if (dmt->flags & UDMF_TF_FRIEND)
1791 mt.options |= MTF_FRIENDLY;
1792
1793 if (dmt->flags & UDMF_TF_DORMANT)
1794 mt.options |= MTF_DORMANT;
1795
1796 if (dmt->flags & UDMF_TF_CLASS1)
1797 mt.options |= MTF_FIGHTER;
1798
1799 if (dmt->flags & UDMF_TF_CLASS2)
1800 mt.options |= MTF_CLERIC;
1801
1802 if (dmt->flags & UDMF_TF_CLASS3)
1803 mt.options |= MTF_MAGE;
1804
1805 if (dmt->flags & UDMF_TF_TRANSLUCENT)
1806 mt.options |= MTF_TRANSLUCENT;
1807
1808 if (dmt->flags & UDMF_TF_INVISIBLE)
1809 mt.options |= MTF_INVISIBLE;
1810
1811 if (dmt->flags & UDMF_TF_COUNTSECRET)
1812 mt.options |= MTF_COUNTSECRET;
1813
1814 P_PostProcessMapThing(&mt, i, &mobjcount, mobjlist);
1815 }
1816
1817 P_PostProcessThings(mobjcount, mobjlist);
1818 }
1819
1820 //
1821 // P_LoadLineDefs
1822 // Also counts secret lines for intermissions.
1823 // ^^^
1824 // ??? killough ???
1825 // Does this mean secrets used to be linedef-based, rather than sector-based?
1826 //
1827 // killough 4/4/98: split into two functions, to allow sidedef overloading
1828 //
1829 // killough 5/3/98: reformatted, cleaned up
1830
1831 void P_TranslateZDoomLineFlags(unsigned int *flags, line_activation_t *spac)
1832 {
1833 unsigned int result;
1834 static unsigned int spac_lookup[8] = {
1835 SPAC_CROSS,
1836 SPAC_USE,
1837 SPAC_MCROSS,
1838 SPAC_IMPACT,
1839 SPAC_PUSH,
1840 SPAC_PCROSS,
1841 SPAC_USE,
1842 SPAC_IMPACT | SPAC_PCROSS
1843 };
1844
1845 result = *flags & 0x1ff;
1846
1847 // from zdoom-in-hexen to dsda-doom
1848
1849 *spac = spac_lookup[GET_SPAC_INDEX(*flags)];
1850
1851 if (GET_SPAC_INDEX(*flags) == 6)
1852 result |= ML_PASSUSE;
1853
1854 if (*flags & HML_REPEATSPECIAL)
1855 result |= ML_REPEATSPECIAL;
1856
1857 if (*flags & ZML_BLOCKPLAYERS)
1858 result |= ML_BLOCKPLAYERS;
1859
1860 if (*flags & ZML_MONSTERSCANACTIVATE)
1861 result |= ML_MONSTERSCANACTIVATE;
1862
1863 if (*flags & ZML_BLOCKEVERYTHING)
1864 result |= ML_BLOCKING | ML_BLOCKEVERYTHING;
1865
1866 *flags = result;
1867 }
1868
1869 void P_TranslateHexenLineFlags(unsigned int *flags, line_activation_t *spac)
1870 {
1871 unsigned int result;
1872 static unsigned int spac_lookup[8] = {
1873 SPAC_CROSS,
1874 SPAC_USE,
1875 SPAC_MCROSS,
1876 SPAC_IMPACT,
1877 SPAC_PUSH,
1878 SPAC_PCROSS,
1879 SPAC_NONE,
1880 SPAC_NONE
1881 };
1882
1883 result = *flags & 0x1ff;
1884
1885 // from hexen to dsda-doom
1886
1887 *spac = spac_lookup[GET_SPAC_INDEX(*flags)];
1888
1889 if (*flags & HML_REPEATSPECIAL)
1890 result |= ML_REPEATSPECIAL;
1891
1892 *flags = result;
1893 }
1894
1895 void P_TranslateCompatibleLineFlags(unsigned int *flags, line_activation_t *spac)
1896 {
1897 int filter;
1898
1899 if (mbf21)
1900 filter = (*flags & ML_RESERVED && comp[comp_reservedlineflag]) ? ML_VANILLA : ML_MBF21;
1901 else
1902 filter = ML_BOOM;
1903
1904 *flags = *flags & filter;
1905 *spac = SPAC_NONE;
1906 }
1907
1908 static void P_SetLineID(line_t *ld)
1909 {
1910 if (!map_format.zdoom) return;
1911
1912 switch (ld->special)
1913 {
1914 case zl_line_set_identification:
1915 ld->tag = (unsigned short) 256 * ld->special_args[4] + ld->special_args[0];
1916 ld->special = 0;
1917 break;
1918 case zl_translucent_line:
1919 ld->tag = ld->special_args[0];
1920 break;
1921 case zl_teleport_line:
1922 case zl_scroll_texture_model:
1923 ld->tag = ld->special_args[0];
1924 break;
1925 case zl_polyobj_start_line:
1926 ld->tag = ld->special_args[3];
1927 break;
1928 case zl_polyobj_explicit_line:
1929 ld->tag = ld->special_args[4];
1930 break;
1931 }
1932 }
1933
1934 static void P_CalculateLineDefProperties(line_t *ld)
1935 {
1936 vertex_t *v1, *v2;
1937
1938 v1 = ld->v1;
1939 v2 = ld->v2;
1940
1941 ld->dx = v2->x - v1->x;
1942 ld->dy = v2->y - v1->y;
1943
1944 // e6y
1945 // Rounding the wall length to the nearest integer
1946 // when determining length instead of always rounding down
1947 // There is no more glitches on seams between identical textures.
1948 ld->texel_length = GetTexelDistance(ld->dx, ld->dy);
1949
1950 ld->slopetype = !ld->dx ? ST_VERTICAL :
1951 !ld->dy ? ST_HORIZONTAL :
1952 FixedDiv(ld->dy, ld->dx) > 0 ? ST_POSITIVE : ST_NEGATIVE;
1953
1954 if (v1->x < v2->x)
1955 {
1956 ld->bbox[BOXLEFT] = v1->x;
1957 ld->bbox[BOXRIGHT] = v2->x;
1958 }
1959 else
1960 {
1961 ld->bbox[BOXLEFT] = v2->x;
1962 ld->bbox[BOXRIGHT] = v1->x;
1963 }
1964 if (v1->y < v2->y)
1965 {
1966 ld->bbox[BOXBOTTOM] = v1->y;
1967 ld->bbox[BOXTOP] = v2->y;
1968 }
1969 else
1970 {
1971 ld->bbox[BOXBOTTOM] = v2->y;
1972 ld->bbox[BOXTOP] = v1->y;
1973 }
1974
1975 /* calculate sound origin of line to be its midpoint */
1976 //e6y: fix sound origin for large levels
1977 // no need for comp_sound test, these are only used when comp_sound = 0
1978 ld->soundorg.x = ld->bbox[BOXLEFT] / 2 + ld->bbox[BOXRIGHT] / 2;
1979 ld->soundorg.y = ld->bbox[BOXTOP] / 2 + ld->bbox[BOXBOTTOM] / 2;
1980
1981 {
1982 /* cph 2006/09/30 - fix sidedef errors right away.
1983 * cph 2002/07/20 - these errors are fatal if not fixed, so apply them
1984 * in compatibility mode - a desync is better than a crash! */
1985 int j;
1986
1987 for (j = 0; j < 2; j++)
1988 {
1989 if (ld->sidenum[j] != NO_INDEX && ld->sidenum[j] >= numsides)
1990 {
1991 ld->sidenum[j] = NO_INDEX;
1992 lprintf(LO_DEBUG, "P_LoadLineDefs: linedef %d"
1993 " has out-of-range sidedef number\n", ld->iLineID);
1994 }
1995 }
1996
1997 // killough 11/98: fix common wad errors (missing sidedefs):
1998
1999 if (ld->sidenum[0] == NO_INDEX)
2000 {
2001 ld->sidenum[0] = 0; // Substitute dummy sidedef for missing right side
2002 }
2003
2004 if ((ld->sidenum[1] == NO_INDEX) && (ld->flags & ML_TWOSIDED))
2005 {
2006 // e6y
2007 // ML_TWOSIDED flag shouldn't be cleared for compatibility purposes
2008 // see CLNJ-506.LMP at https://dsdarchive.com/wads/challenj
2009 MissedBackSideOverrun(ld);
2010 if (!demo_compatibility || !EMULATE(OVERFLOW_MISSEDBACKSIDE))
2011 {
2012 ld->flags &= ~ML_TWOSIDED; // Clear 2s flag for missing left side
2013 }
2014
2015 // cph - print a warning about the bug
2016 lprintf(LO_DEBUG, "P_LoadLineDefs: linedef %d"
2017 " has two-sided flag set, but no second sidedef\n", ld->iLineID);
2018 }
2019 }
2020
2021 // killough 4/4/98: support special sidedef interpretation below
2022 if (ld->sidenum[0] != NO_INDEX && ld->special)
2023 sides[*ld->sidenum].special = ld->special;
2024 }
2025
2026 static void P_LoadLineDefs (int lump)
2027 {
2028 const byte *data; // cph - const*
2029 int i;
2030
2031 numlines = W_LumpLength (lump) / map_format.maplinedef_size;
2032 lines = calloc_IfSameLevel(lines, numlines, sizeof(line_t));
2033 data = W_LumpByNum (lump); // cph - wad lump handling updated
2034
2035 dsda_ResetLineIDList(numlines);
2036
2037 for (i=0; i<numlines; i++)
2038 {
2039 line_t *ld = lines+i;
2040
2041 ld->iLineID=i; // proff 04/05/2000: needed for OpenGL
2042 ld->alpha = 1.f;
2043
2044 if (map_format.hexen)
2045 {
2046 const hexen_maplinedef_t *mld = (const hexen_maplinedef_t *) data + i;
2047
2048 ld->flags = (unsigned short)LittleShort(mld->flags);
2049 ld->special = mld->special; // just a byte in hexen
2050 ld->tag = 0;
2051 ld->special_args[0] = mld->arg1;
2052 ld->special_args[1] = mld->arg2;
2053 ld->special_args[2] = mld->arg3;
2054 ld->special_args[3] = mld->arg4;
2055 ld->special_args[4] = mld->arg5;
2056 ld->v1 = &vertexes[(unsigned short)LittleShort(mld->v1)];
2057 ld->v2 = &vertexes[(unsigned short)LittleShort(mld->v2)];
2058 ld->sidenum[0] = LittleShort(mld->sidenum[0]);
2059 ld->sidenum[1] = LittleShort(mld->sidenum[1]);
2060 P_SetLineID(ld);
2061 }
2062 else
2063 {
2064 const doom_maplinedef_t *mld = (const doom_maplinedef_t *) data + i;
2065
2066 ld->flags = (unsigned short)LittleShort(mld->flags);
2067 ld->special = LittleShort(mld->special);
2068 ld->tag = LittleShort(mld->tag);
2069 ld->special_args[0] = 0;
2070 ld->special_args[1] = 0;
2071 ld->special_args[2] = 0;
2072 ld->special_args[3] = 0;
2073 ld->special_args[4] = 0;
2074 ld->v1 = &vertexes[(unsigned short)LittleShort(mld->v1)];
2075 ld->v2 = &vertexes[(unsigned short)LittleShort(mld->v2)];
2076 ld->sidenum[0] = LittleShort(mld->sidenum[0]);
2077 ld->sidenum[1] = LittleShort(mld->sidenum[1]);
2078 }
2079
2080 map_format.translate_line_flags(&ld->flags, &ld->activation);
2081
2082 P_CalculateLineDefProperties(ld);
2083
2084 dsda_AddLineID(ld->tag, i);
2085 }
2086 }
2087
2088 static void P_LoadUDMFLineDefs(int lump)
2089 {
2090 int i;
2091
2092 numlines = udmf.num_lines;
2093 lines = calloc_IfSameLevel(lines, numlines, sizeof(line_t));
2094
2095 dsda_ResetLineIDList(numlines);
2096
2097 for (i = 0; i < numlines; ++i)
2098 {
2099 line_t *ld = &lines[i];
2100 const udmf_line_t *mld = &udmf.lines[i];
2101
2102 ld->iLineID=i; // proff 04/05/2000: needed for OpenGL
2103
2104 ld->flags = (mld->flags & ML_BOOM);
2105 ld->special = mld->special;
2106 ld->tag = (mld->id >= 0 ? mld->id : 0);
2107 ld->special_args[0] = mld->arg0;
2108 ld->special_args[1] = mld->arg1;
2109 ld->special_args[2] = mld->arg2;
2110 ld->special_args[3] = mld->arg3;
2111 ld->special_args[4] = mld->arg4;
2112 ld->v1 = &vertexes[mld->v1];
2113 ld->v2 = &vertexes[mld->v2];
2114 ld->sidenum[0] = mld->sidefront;
2115 ld->sidenum[1] = mld->sideback;
2116 ld->alpha = mld->alpha;
2117 ld->locknumber = mld->locknumber;
2118 ld->automap_style = mld->automapstyle;
2119 ld->health = mld->health;
2120 ld->healthgroup = mld->healthgroup;
2121
2122 if (ld->special == zl_sector_set_colormap || ld->special == zl_map_set_colormap)
2123 {
2124 if (mld->arg0str)
2125 ld->special_args[0] = R_ColormapNumForName(mld->arg0str);
2126 else
2127 ld->special_args[0] = -1;
2128
2129 if (ld->special_args[0] < 0)
2130 {
2131 lprintf(LO_WARN, "Unknown colormap in line %d action.\n", i);
2132 ld->special = 0;
2133 }
2134 }
2135
2136 if (ld->special == zl_music_change_song)
2137 {
2138 if (mld->arg0str)
2139 ld->special_args[0] = W_CheckNumForName(mld->arg0str);
2140 else
2141 ld->special_args[0] = LUMP_NOT_FOUND;
2142
2143 if (ld->special_args[0] == LUMP_NOT_FOUND)
2144 {
2145 lprintf(LO_WARN, "Unknown song lump in line %d action.\n", i);
2146 ld->special = 0;
2147 }
2148 }
2149
2150 if (mld->flags & UDMF_ML_PLAYERCROSS)
2151 ld->activation |= SPAC_CROSS;
2152
2153 if (mld->flags & UDMF_ML_PLAYERUSE)
2154 ld->activation |= SPAC_USE;
2155
2156 if (mld->flags & UDMF_ML_MONSTERCROSS)
2157 ld->activation |= SPAC_MCROSS;
2158
2159 if (mld->flags & UDMF_ML_IMPACT)
2160 ld->activation |= SPAC_IMPACT;
2161
2162 if (mld->flags & UDMF_ML_PLAYERPUSH)
2163 ld->activation |= SPAC_PUSH;
2164
2165 if (mld->flags & UDMF_ML_MISSILECROSS)
2166 ld->activation |= SPAC_PCROSS;
2167
2168 if (mld->flags & UDMF_ML_ANYCROSS)
2169 ld->activation |= SPAC_ANYCROSS | SPAC_CROSS | SPAC_MCROSS;
2170
2171 if (mld->flags & UDMF_ML_PLAYERUSEBACK)
2172 ld->activation |= SPAC_USEBACK;
2173
2174 if (mld->flags & UDMF_ML_MONSTERPUSH)
2175 ld->activation |= SPAC_MPUSH;
2176
2177 if (mld->flags & UDMF_ML_MONSTERUSE)
2178 ld->activation |= SPAC_MUSE;
2179
2180 if (mld->flags & UDMF_ML_DAMAGESPECIAL)
2181 ld->activation |= SPAC_DAMAGE;
2182
2183 if (mld->flags & UDMF_ML_DEATHSPECIAL)
2184 ld->activation |= SPAC_DEATH;
2185
2186 if (mld->flags & UDMF_ML_REPEATSPECIAL)
2187 ld->flags |= ML_REPEATSPECIAL;
2188
2189 if (mld->flags & UDMF_ML_MONSTERACTIVATE)
2190 ld->flags |= ML_MONSTERSCANACTIVATE;
2191
2192 if (mld->flags & UDMF_ML_BLOCKPLAYERS)
2193 ld->flags |= ML_BLOCKPLAYERS;
2194
2195 if (mld->flags & UDMF_ML_BLOCKEVERYTHING)
2196 ld->flags |= ML_BLOCKING | ML_BLOCKEVERYTHING;
2197
2198 if (mld->flags & UDMF_ML_BLOCKLANDMONSTERS)
2199 ld->flags |= ML_BLOCKLANDMONSTERS;
2200
2201 if (mld->flags & UDMF_ML_BLOCKFLOATERS)
2202 ld->flags |= ML_BLOCKFLOATERS;
2203
2204 if (mld->flags & UDMF_ML_BLOCKSIGHT)
2205 ld->flags |= ML_BLOCKSIGHT;
2206
2207 if (mld->flags & UDMF_ML_BLOCKHITSCAN)
2208 ld->flags |= ML_BLOCKHITSCAN;
2209
2210 if (mld->flags & UDMF_ML_BLOCKPROJECTILES)
2211 ld->flags |= ML_BLOCKPROJECTILES;
2212
2213 if (mld->flags & UDMF_ML_BLOCKUSE)
2214 ld->flags |= ML_BLOCKUSE;
2215
2216 if (mld->flags & UDMF_ML_CLIPMIDTEX)
2217 ld->flags |= ML_CLIPMIDTEX;
2218
2219 if (mld->flags & UDMF_ML_JUMPOVER)
2220 ld->flags |= ML_JUMPOVER;
2221
2222 if (mld->flags & UDMF_ML_MIDTEX3D)
2223 ld->flags |= ML_3DMIDTEX;
2224
2225 if (mld->flags & UDMF_ML_MIDTEX3DIMPASSIBLE)
2226 ld->flags |= ML_3DMIDTEXIMPASSIBLE;
2227
2228 if (mld->flags & UDMF_ML_FIRSTSIDEONLY)
2229 ld->flags |= ML_FIRSTSIDEONLY;
2230
2231 if (mld->flags & UDMF_ML_REVEALED)
2232 ld->flags |= ML_REVEALED;
2233
2234 if (mld->flags & UDMF_ML_CHECKSWITCHRANGE)
2235 ld->flags |= ML_CHECKSWITCHRANGE;
2236
2237 if (mld->flags & UDMF_ML_TRANSLUCENT)
2238 ld->alpha = 0.75f;
2239
2240 if (mld->flags & UDMF_ML_TRANSPARENT)
2241 ld->alpha = 0.25f;
2242
2243 if (mld->flags & UDMF_ML_WRAPMIDTEX)
2244 ld->flags |= ML_WRAPMIDTEX;
2245
2246 P_CalculateLineDefProperties(ld);
2247
2248 if (ld->alpha < 1.f)
2249 ld->tranmap = dsda_TranMap(dsda_FloatToPercent(ld->alpha));
2250
2251 if (ld->healthgroup)
2252 dsda_AddLineToHealthGroup(ld);
2253
2254 if (ld->tag > 0)
2255 dsda_AddLineID(ld->tag, i);
2256
2257 if (mld->moreids)
2258 {
2259 char **more_ids;
2260
2261 more_ids = dsda_SplitString(mld->moreids, " ");
2262
2263 if (more_ids)
2264 {
2265 int j, id;
2266
2267 for (j = 0; more_ids[j]; ++j)
2268 if (sscanf(more_ids[j], "%d", &id) == 1 && id > 0)
2269 dsda_AddLineID(id, i);
2270
2271 Z_Free(more_ids);
2272 }
2273 }
2274
2275 if (ld->flags & ML_WRAPMIDTEX)
2276 dsda_PreferOpenGL();
2277 }
2278 }
2279
2280 void P_PostProcessCompatibleLineSpecial(line_t *ld)
2281 {
2282 switch (ld->special)
2283 { // killough 4/11/98: handle special types
2284 case 260: // killough 4/11/98: translucent 2s textures
2285 {
2286 const byte* tranmap;
2287 int lump, j;
2288
2289 lump = sides[*ld->sidenum].special; // translucency from sidedef
2290
2291 if (!lump)
2292 tranmap = main_tranmap;
2293 else
2294 tranmap = W_LumpByNum(lump - 1);
2295
2296 if (!ld->tag) // if tag==0,
2297 {
2298 ld->tranmap = tranmap; // affect this linedef only
2299 ld->alpha = 0.66f;
2300 }
2301 else
2302 for (j=0;j<numlines;j++) // if tag!=0,
2303 if (lines[j].tag == ld->tag) // affect all matching linedefs
2304 {
2305 lines[j].tranmap = tranmap;
2306 lines[j].alpha = 0.66f;
2307 }
2308 break;
2309 }
2310 }
2311 }
2312
2313 void P_PostProcessHereticLineSpecial(line_t *ld)
2314 {
2315 // nothing in heretic
2316 }
2317
2318 void P_PostProcessHexenLineSpecial(line_t *ld)
2319 {
2320 // nothing in hexen
2321 }
2322
2323 void P_PostProcessZDoomLineSpecial(line_t *ld)
2324 {
2325 switch (ld->special)
2326 {
2327 case zl_translucent_line:
2328 {
2329 float alpha;
2330 const int *id_p;
2331
2332 alpha = (float) ld->special_args[1] / 256.f;
2333 alpha = BETWEEN(0.f, 1.f, alpha);
2334
2335 if (!ld->special_args[0])
2336 {
2337 ld->tranmap = dsda_TranMap(dsda_FloatToPercent(alpha));
2338 ld->alpha = alpha;
2339 }
2340 else
2341 {
2342 FIND_LINES(id_p, ld->special_args[0])
2343 {
2344 lines[*id_p].tranmap = dsda_TranMap(dsda_FloatToPercent(alpha));
2345 lines[*id_p].alpha = alpha;
2346 }
2347 }
2348
2349 ld->special = 0;
2350 }
2351 break;
2352 }
2353 }
2354
2355 // killough 4/4/98: delay using sidedefs until they are loaded
2356 // killough 5/3/98: reformatted, cleaned up
2357
2358 static void P_PostProcessLineDefs(void)
2359 {
2360 int i = numlines;
2361 register line_t *ld = lines;
2362
2363 for (; i--; ld++)
2364 {
2365 ld->frontsector = sides[ld->sidenum[0]].sector; //e6y: Can't be NO_INDEX here
2366 ld->backsector = ld->sidenum[1] != NO_INDEX ? sides[ld->sidenum[1]].sector : 0;
2367
2368 map_format.post_process_line_special(ld);
2369 }
2370 }
2371
2372 //
2373 // P_LoadSideDefs
2374 //
2375 // killough 4/4/98: split into two functions
2376
2377 static void P_AllocateSideDefs (int lump)
2378 {
2379 numsides = W_LumpLength(lump) / sizeof(mapsidedef_t);
2380 sides = calloc_IfSameLevel(sides, numsides, sizeof(side_t));
2381 }
2382
2383 static void P_AllocateUDMFSideDefs(int lump)
2384 {
2385 numsides = udmf.num_sides;
2386 sides = calloc_IfSameLevel(sides, numsides, sizeof(side_t));
2387 }
2388
2389 void P_PostProcessCompatibleSidedefSpecial(side_t *sd, const mapsidedef_t *msd, sector_t *sec, int i)
2390 {
2391 // killough 4/4/98: allow sidedef texture names to be overloaded
2392 // killough 4/11/98: refined to allow colormaps to work as wall
2393 // textures if invalid as colormaps but valid as textures.
2394 switch (sd->special)
2395 {
2396 case 242: // variable colormap via 242 linedef
2397 sd->bottomtexture =
2398 (sec->bottommap = R_ColormapNumForName(msd->bottomtexture)) < 0 ?
2399 sec->bottommap = 0, R_TextureNumForName(msd->bottomtexture): 0 ;
2400 sd->midtexture =
2401 (sec->midmap = R_ColormapNumForName(msd->midtexture)) < 0 ?
2402 sec->midmap = 0, R_TextureNumForName(msd->midtexture) : 0 ;
2403 sd->toptexture =
2404 (sec->topmap = R_ColormapNumForName(msd->toptexture)) < 0 ?
2405 sec->topmap = 0, R_TextureNumForName(msd->toptexture) : 0 ;
2406 break;
2407
2408 case 260: // killough 4/11/98: apply translucency to 2s normal texture
2409 sd->midtexture = strncasecmp("TRANMAP", msd->midtexture, 8) ?
2410 (sd->special = W_CheckNumForName(msd->midtexture)) == LUMP_NOT_FOUND ||
2411 W_LumpLength(sd->special) != 65536 ?
2412 sd->special=0, R_TextureNumForName(msd->midtexture) :
2413 (sd->special++, 0) : (sd->special=0);
2414 sd->toptexture = R_TextureNumForName(msd->toptexture);
2415 sd->bottomtexture = R_TextureNumForName(msd->bottomtexture);
2416 break;
2417
2418 default: // normal cases
2419 sd->midtexture = R_SafeTextureNumForName(msd->midtexture, i);
2420 sd->toptexture = R_SafeTextureNumForName(msd->toptexture, i);
2421 sd->bottomtexture = R_SafeTextureNumForName(msd->bottomtexture, i);
2422 break;
2423 }
2424 }
2425
2426 void P_PostProcessHereticSidedefSpecial(side_t *sd, const mapsidedef_t *msd, sector_t *sec, int i)
2427 {
2428 sd->midtexture = R_SafeTextureNumForName(msd->midtexture, i);
2429 sd->toptexture = R_SafeTextureNumForName(msd->toptexture, i);
2430 sd->bottomtexture = R_SafeTextureNumForName(msd->bottomtexture, i);
2431 }
2432
2433 void P_PostProcessHexenSidedefSpecial(side_t *sd, const mapsidedef_t *msd, sector_t *sec, int i)
2434 {
2435 sd->midtexture = R_SafeTextureNumForName(msd->midtexture, i);
2436 sd->toptexture = R_SafeTextureNumForName(msd->toptexture, i);
2437 sd->bottomtexture = R_SafeTextureNumForName(msd->bottomtexture, i);
2438 }
2439
2440 void P_PostProcessZDoomSidedefSpecial(side_t *sd, const mapsidedef_t *msd, sector_t *sec, int i)
2441 {
2442 sd->midtexture = R_SafeTextureNumForName(msd->midtexture, i);
2443 sd->toptexture = R_SafeTextureNumForName(msd->toptexture, i);
2444 sd->bottomtexture = R_SafeTextureNumForName(msd->bottomtexture, i);
2445 }
2446
2447 // killough 4/4/98: delay using texture names until
2448 // after linedefs are loaded, to allow overloading.
2449 // killough 5/3/98: reformatted, cleaned up
2450
2451 static void P_LoadSideDefs(int lump)
2452 {
2453 const byte *data = W_LumpByNum(lump); // cph - const*, wad lump handling updated
2454 int i;
2455
2456 for (i=0; i<numsides; i++)
2457 {
2458 register const mapsidedef_t *msd = (const mapsidedef_t *) data + i;
2459 register side_t *sd = sides + i;
2460 register sector_t *sec;
2461
2462 sd->textureoffset = LittleShort(msd->textureoffset)<<FRACBITS;
2463 sd->rowoffset = LittleShort(msd->rowoffset)<<FRACBITS;
2464
2465 sd->scalex_top = FRACUNIT;
2466 sd->scaley_top = FRACUNIT;
2467 sd->scalex_mid = FRACUNIT;
2468 sd->scaley_mid = FRACUNIT;
2469 sd->scalex_bottom = FRACUNIT;
2470 sd->scaley_bottom = FRACUNIT;
2471
2472 { /* cph 2006/09/30 - catch out-of-range sector numbers; use sector 0 instead */
2473 unsigned short sector_num = LittleShort(msd->sector);
2474 if (sector_num >= numsectors) {
2475 lprintf(LO_DEBUG,"P_LoadSideDefs: sidedef %i has out-of-range sector num %u\n", i, sector_num);
2476 sector_num = 0;
2477 }
2478 sd->sector = sec = &sectors[sector_num];
2479 }
2480
2481 map_format.post_process_sidedef_special(sd, msd, sec, i);
2482 }
2483 }
2484
2485 static void P_LoadUDMFSideDefs(int lump)
2486 {
2487 int i;
2488
2489 for (i = 0; i < numsides; ++i)
2490 {
2491 const udmf_side_t *msd = &udmf.sides[i];
2492 side_t *sd = &sides[i];
2493
2494 sd->textureoffset = dsda_IntToFixed(msd->offsetx);
2495 sd->rowoffset = dsda_IntToFixed(msd->offsety);
2496
2497 sd->textureoffset_top = dsda_IntToFixed(msd->offsetx_top);
2498 sd->textureoffset_mid = dsda_IntToFixed(msd->offsetx_mid);
2499 sd->textureoffset_bottom = dsda_IntToFixed(msd->offsetx_bottom);
2500 sd->rowoffset_top = dsda_IntToFixed(msd->offsety_top);
2501 sd->rowoffset_mid = dsda_IntToFixed(msd->offsety_mid);
2502 sd->rowoffset_bottom = dsda_IntToFixed(msd->offsety_bottom);
2503
2504 sd->scalex_top = dsda_FloatToFixed(msd->scalex_top);
2505 sd->scaley_top = dsda_FloatToFixed(msd->scaley_top);
2506 sd->scalex_mid = dsda_FloatToFixed(msd->scalex_mid);
2507 sd->scaley_mid = dsda_FloatToFixed(msd->scaley_mid);
2508 sd->scalex_bottom = dsda_FloatToFixed(msd->scalex_bottom);
2509 sd->scaley_bottom = dsda_FloatToFixed(msd->scaley_bottom);
2510
2511 sd->lightlevel = msd->light;
2512 sd->lightlevel_top = msd->light_top;
2513 sd->lightlevel_mid = msd->light_mid;
2514 sd->lightlevel_bottom = msd->light_bottom;
2515
2516 if (msd->xscroll || msd->yscroll)
2517 dsda_AddSideScroller(dsda_FloatToFixed(msd->xscroll),
2518 dsda_FloatToFixed(msd->yscroll), i, 0);
2519
2520 if (msd->xscrolltop || msd->yscrolltop)
2521 dsda_AddSideScroller(dsda_FloatToFixed(msd->xscrolltop),
2522 dsda_FloatToFixed(msd->yscrolltop), i, SCROLL_TOP);
2523
2524 if (msd->xscrollmid || msd->yscrollmid)
2525 dsda_AddSideScroller(dsda_FloatToFixed(msd->xscrollmid),
2526 dsda_FloatToFixed(msd->yscrollmid), i, SCROLL_MID);
2527
2528 if (msd->xscrollbottom || msd->yscrollbottom)
2529 dsda_AddSideScroller(dsda_FloatToFixed(msd->xscrollbottom),
2530 dsda_FloatToFixed(msd->yscrollbottom), i, SCROLL_BOTTOM);
2531
2532 sd->flags = msd->flags;
2533
2534 if (msd->sector >= numsectors)
2535 I_Error("Invalid level data: sidedef %d's sector index is out of range", i);
2536
2537 sd->sector = &sectors[msd->sector];
2538
2539 sd->midtexture = R_SafeTextureNumForName(msd->texturemiddle, i);
2540 sd->toptexture = R_SafeTextureNumForName(msd->texturetop, i);
2541 sd->bottomtexture = R_SafeTextureNumForName(msd->texturebottom, i);
2542
2543 if (sd->scalex_top != FRACUNIT || sd->scaley_top != FRACUNIT ||
2544 sd->scalex_mid != FRACUNIT || sd->scaley_mid != FRACUNIT ||
2545 sd->scalex_bottom != FRACUNIT || sd->scaley_bottom != FRACUNIT ||
2546 sd->flags & SF_WRAPMIDTEX)
2547 dsda_PreferOpenGL();
2548 }
2549 }
2550
2551 //
2552 // jff 10/6/98
2553 // New code added to speed up calculation of internal blockmap
2554 // Algorithm is order of nlines*(ncols+nrows) not nlines*ncols*nrows
2555 //
2556
2557 #define blkshift 7 /* places to shift rel position for cell num */
2558 #define blkmask ((1<<blkshift)-1)/* mask for rel position within cell */
2559 #define blkmargin 0 /* size guardband around map used */
2560 // jff 10/8/98 use guardband>0
2561 // jff 10/12/98 0 ok with + 1 in rows,cols
2562
2563 typedef struct linelist_t // type used to list lines in each block
2564 {
2565 long num;
2566 struct linelist_t *next;
2567 } linelist_t;
2568
2569 //
2570 // Subroutine to add a line number to a block list
2571 // It simply returns if the line is already in the block
2572 //
2573
2574 static void AddBlockLine
2575 (
2576 linelist_t **lists,
2577 int *count,
2578 int *done,
2579 int blockno,
2580 long lineno
2581 )
2582 {
2583 linelist_t *l;
2584
2585 if (done[blockno])
2586 return;
2587
2588 l = Z_Malloc(sizeof(linelist_t));
2589 l->num = lineno;
2590 l->next = lists[blockno];
2591 lists[blockno] = l;
2592 count[blockno]++;
2593 done[blockno] = 1;
2594 }
2595
2596 blockmap_t original_blockmap;
2597
2598 static void RememberOriginalBlockMap(void)
2599 {
2600 original_blockmap.width = bmapwidth;
2601 original_blockmap.height = bmapheight;
2602 original_blockmap.orgx = bmaporgx;
2603 original_blockmap.orgy = bmaporgy;
2604 }
2605
2606 void P_RestoreOriginalBlockMap(void)
2607 {
2608 bmapwidth = original_blockmap.width;
2609 bmapheight = original_blockmap.height;
2610 bmaporgx = original_blockmap.orgx;
2611 bmaporgy = original_blockmap.orgy;
2612 }
2613
2614 //
2615 // Actually construct the blockmap lump from the level data
2616 //
2617 // This finds the intersection of each linedef with the column and
2618 // row lines at the left and bottom of each blockmap cell. It then
2619 // adds the line to all block lists touching the intersection.
2620 //
2621
2622 static void P_CreateBlockMap(void)
2623 {
2624 int xorg,yorg; // blockmap origin (lower left)
2625 int nrows,ncols; // blockmap dimensions
2626 linelist_t **blocklists=NULL; // array of pointers to lists of lines
2627 int *blockcount=NULL; // array of counters of line lists
2628 int *blockdone=NULL; // array keeping track of blocks/line
2629 int NBlocks; // number of cells = nrows*ncols
2630 long linetotal=0; // total length of all blocklists
2631 int i,j;
2632 int map_minx=INT_MAX; // init for map limits search
2633 int map_miny=INT_MAX;
2634 int map_maxx=INT_MIN;
2635 int map_maxy=INT_MIN;
2636
2637 // scan for map limits, which the blockmap must enclose
2638
2639 // This fixes MBF's code, which has a bug where maxx/maxy
2640 // are wrong if the 0th node has the largest x or y
2641 if (numvertexes)
2642 {
2643 map_minx = map_maxx = vertexes[0].x;
2644 map_miny = map_maxy = vertexes[0].y;
2645 }
2646
2647 for (i=0;i<numvertexes;i++)
2648 {
2649 fixed_t t;
2650
2651 if ((t=vertexes[i].x) < map_minx)
2652 map_minx = t;
2653 else if (t > map_maxx)
2654 map_maxx = t;
2655 if ((t=vertexes[i].y) < map_miny)
2656 map_miny = t;
2657 else if (t > map_maxy)
2658 map_maxy = t;
2659 }
2660 map_minx >>= FRACBITS; // work in map coords, not fixed_t
2661 map_maxx >>= FRACBITS;
2662 map_miny >>= FRACBITS;
2663 map_maxy >>= FRACBITS;
2664
2665 // set up blockmap area to enclose level plus margin
2666
2667 xorg = map_minx-blkmargin;
2668 yorg = map_miny-blkmargin;
2669 ncols = (map_maxx+blkmargin-xorg+1+blkmask)>>blkshift; //jff 10/12/98
2670 nrows = (map_maxy+blkmargin-yorg+1+blkmask)>>blkshift; //+1 needed for
2671 NBlocks = ncols*nrows; //map exactly 1 cell
2672
2673 // create the array of pointers on NBlocks to blocklists
2674 // also create an array of linelist counts on NBlocks
2675 // finally make an array in which we can mark blocks done per line
2676
2677 // CPhipps - calloc's
2678 blocklists = Z_Calloc(NBlocks,sizeof(linelist_t *));
2679 blockcount = Z_Calloc(NBlocks,sizeof(int));
2680 blockdone = Z_Malloc(NBlocks*sizeof(int));
2681
2682 // initialize each blocklist, and enter the trailing -1 in all blocklists
2683 // note the linked list of lines grows backwards
2684
2685 for (i=0;i<NBlocks;i++)
2686 {
2687 blocklists[i] = Z_Malloc(sizeof(linelist_t));
2688 blocklists[i]->num = -1;
2689 blocklists[i]->next = NULL;
2690 blockcount[i]++;
2691 }
2692
2693 // For each linedef in the wad, determine all blockmap blocks it touches,
2694 // and add the linedef number to the blocklists for those blocks
2695
2696 for (i=0;i<numlines;i++)
2697 {
2698 int x1 = lines[i].v1->x>>FRACBITS; // lines[i] map coords
2699 int y1 = lines[i].v1->y>>FRACBITS;
2700 int x2 = lines[i].v2->x>>FRACBITS;
2701 int y2 = lines[i].v2->y>>FRACBITS;
2702 int dx = x2-x1;
2703 int dy = y2-y1;
2704 int vert = !dx; // lines[i] slopetype
2705 int horiz = !dy;
2706 int spos = (dx^dy) > 0;
2707 int sneg = (dx^dy) < 0;
2708 int bx,by; // block cell coords
2709 int minx = x1>x2? x2 : x1; // extremal lines[i] coords
2710 int maxx = x1>x2? x1 : x2;
2711 int miny = y1>y2? y2 : y1;
2712 int maxy = y1>y2? y1 : y2;
2713
2714 // no blocks done for this linedef yet
2715
2716 memset(blockdone,0,NBlocks*sizeof(int));
2717
2718 // The line always belongs to the blocks containing its endpoints
2719
2720 bx = (x1-xorg)>>blkshift;
2721 by = (y1-yorg)>>blkshift;
2722 AddBlockLine(blocklists,blockcount,blockdone,by*ncols+bx,i);
2723 bx = (x2-xorg)>>blkshift;
2724 by = (y2-yorg)>>blkshift;
2725 AddBlockLine(blocklists,blockcount,blockdone,by*ncols+bx,i);
2726
2727
2728 // For each column, see where the line along its left edge, which
2729 // it contains, intersects the Linedef i. Add i to each corresponding
2730 // blocklist.
2731
2732 if (!vert) // don't interesect vertical lines with columns
2733 {
2734 for (j=0;j<ncols;j++)
2735 {
2736 // intersection of Linedef with x=xorg+(j<<blkshift)
2737 // (y-y1)*dx = dy*(x-x1)
2738 // y = dy*(x-x1)+y1*dx;
2739
2740 int x = xorg+(j<<blkshift); // (x,y) is intersection
2741 int y = (dy*(x-x1))/dx+y1;
2742 int yb = (y-yorg)>>blkshift; // block row number
2743 int yp = (y-yorg)&blkmask; // y position within block
2744
2745 if (yb<0 || yb>nrows-1) // outside blockmap, continue
2746 continue;
2747
2748 if (x<minx || x>maxx) // line doesn't touch column
2749 continue;
2750
2751 // The cell that contains the intersection point is always added
2752
2753 AddBlockLine(blocklists,blockcount,blockdone,ncols*yb+j,i);
2754
2755 // if the intersection is at a corner it depends on the slope
2756 // (and whether the line extends past the intersection) which
2757 // blocks are hit
2758
2759 if (yp==0) // intersection at a corner
2760 {
2761 if (sneg) // \ - blocks x,y-, x-,y
2762 {
2763 if (yb>0 && miny<y)
2764 AddBlockLine(blocklists,blockcount,blockdone,ncols*(yb-1)+j,i);
2765 if (j>0 && minx<x)
2766 AddBlockLine(blocklists,blockcount,blockdone,ncols*yb+j-1,i);
2767 }
2768 else if (spos) // / - block x-,y-
2769 {
2770 if (yb>0 && j>0 && minx<x)
2771 AddBlockLine(blocklists,blockcount,blockdone,ncols*(yb-1)+j-1,i);
2772 }
2773 else if (horiz) // - - block x-,y
2774 {
2775 if (j>0 && minx<x)
2776 AddBlockLine(blocklists,blockcount,blockdone,ncols*yb+j-1,i);
2777 }
2778 }
2779 else if (j>0 && minx<x) // else not at corner: x-,y
2780 AddBlockLine(blocklists,blockcount,blockdone,ncols*yb+j-1,i);
2781 }
2782 }
2783
2784 // For each row, see where the line along its bottom edge, which
2785 // it contains, intersects the Linedef i. Add i to all the corresponding
2786 // blocklists.
2787
2788 if (!horiz)
2789 {
2790 for (j=0;j<nrows;j++)
2791 {
2792 // intersection of Linedef with y=yorg+(j<<blkshift)
2793 // (x,y) on Linedef i satisfies: (y-y1)*dx = dy*(x-x1)
2794 // x = dx*(y-y1)/dy+x1;
2795
2796 int y = yorg+(j<<blkshift); // (x,y) is intersection
2797 int x = (dx*(y-y1))/dy+x1;
2798 int xb = (x-xorg)>>blkshift; // block column number
2799 int xp = (x-xorg)&blkmask; // x position within block
2800
2801 if (xb<0 || xb>ncols-1) // outside blockmap, continue
2802 continue;
2803
2804 if (y<miny || y>maxy) // line doesn't touch row
2805 continue;
2806
2807 // The cell that contains the intersection point is always added
2808
2809 AddBlockLine(blocklists,blockcount,blockdone,ncols*j+xb,i);
2810
2811 // if the intersection is at a corner it depends on the slope
2812 // (and whether the line extends past the intersection) which
2813 // blocks are hit
2814
2815 if (xp==0) // intersection at a corner
2816 {
2817 if (sneg) // \ - blocks x,y-, x-,y
2818 {
2819 if (j>0 && miny<y)
2820 AddBlockLine(blocklists,blockcount,blockdone,ncols*(j-1)+xb,i);
2821 if (xb>0 && minx<x)
2822 AddBlockLine(blocklists,blockcount,blockdone,ncols*j+xb-1,i);
2823 }
2824 else if (vert) // | - block x,y-
2825 {
2826 if (j>0 && miny<y)
2827 AddBlockLine(blocklists,blockcount,blockdone,ncols*(j-1)+xb,i);
2828 }
2829 else if (spos) // / - block x-,y-
2830 {
2831 if (xb>0 && j>0 && miny<y)
2832 AddBlockLine(blocklists,blockcount,blockdone,ncols*(j-1)+xb-1,i);
2833 }
2834 }
2835 else if (j>0 && miny<y) // else not on a corner: x,y-
2836 AddBlockLine(blocklists,blockcount,blockdone,ncols*(j-1)+xb,i);
2837 }
2838 }
2839 }
2840
2841 // Add initial 0 to all blocklists
2842 // count the total number of lines (and 0's and -1's)
2843
2844 memset(blockdone,0,NBlocks*sizeof(int));
2845 for (i=0,linetotal=0;i<NBlocks;i++)
2846 {
2847 AddBlockLine(blocklists,blockcount,blockdone,i,0);
2848 linetotal += blockcount[i];
2849 }
2850
2851 // Create the blockmap lump
2852
2853 blockmaplump = malloc_IfSameLevel(blockmaplump, sizeof(*blockmaplump) * (4 + NBlocks + linetotal));
2854 // blockmap header
2855
2856 blockmaplump[0] = bmaporgx = xorg << FRACBITS;
2857 blockmaplump[1] = bmaporgy = yorg << FRACBITS;
2858 blockmaplump[2] = bmapwidth = ncols;
2859 blockmaplump[3] = bmapheight = nrows;
2860
2861 // offsets to lists and block lists
2862
2863 for (i=0;i<NBlocks;i++)
2864 {
2865 linelist_t *bl = blocklists[i];
2866 long offs = blockmaplump[4+i] = // set offset to block's list
2867 (i? blockmaplump[4+i-1] : 4+NBlocks) + (i? blockcount[i-1] : 0);
2868
2869 // add the lines in each block's list to the blockmaplump
2870 // delete each list node as we go
2871
2872 while (bl)
2873 {
2874 linelist_t *tmp = bl->next;
2875 blockmaplump[offs++] = bl->num;
2876 Z_Free(bl);
2877 bl = tmp;
2878 }
2879 }
2880
2881 // free all temporary storage
2882
2883 Z_Free (blocklists);
2884 Z_Free (blockcount);
2885 Z_Free (blockdone);
2886 }
2887
2888 // jff 10/6/98
2889 // End new code added to speed up calculation of internal blockmap
2890
2891 //
2892 // P_VerifyBlockMap
2893 //
2894 // haleyjd 03/04/10: do verification on validity of blockmap.
2895 //
2896 static dboolean P_VerifyBlockMap(int count)
2897 {
2898 int x, y;
2899 int *maxoffs = blockmaplump + count;
2900
2901 skipblstart = true;
2902
2903 for(y = 0; y < bmapheight; y++)
2904 {
2905 for(x = 0; x < bmapwidth; x++)
2906 {
2907 int offset;
2908 int *list, *tmplist;
2909 int *blockoffset;
2910
2911 offset = y * bmapwidth + x;
2912 blockoffset = blockmaplump + offset + 4;
2913
2914 // check that block offset is in bounds
2915 if(blockoffset >= maxoffs)
2916 {
2917 lprintf(LO_ERROR, "P_VerifyBlockMap: block offset overflow\n");
2918 return false;
2919 }
2920
2921 offset = *blockoffset;
2922
2923 // check that list offset is in bounds
2924 if(offset < 4 || offset >= count)
2925 {
2926 lprintf(LO_ERROR, "P_VerifyBlockMap: list offset overflow\n");
2927 return false;
2928 }
2929
2930 list = blockmaplump + offset;
2931
2932 if (*list != 0)
2933 skipblstart = false;
2934
2935 // scan forward for a -1 terminator before maxoffs
2936 for(tmplist = list; ; tmplist++)
2937 {
2938 // we have overflowed the lump?
2939 if(tmplist >= maxoffs)
2940 {
2941 lprintf(LO_ERROR, "P_VerifyBlockMap: open blocklist\n");
2942 return false;
2943 }
2944 if(*tmplist == -1) // found -1
2945 break;
2946 }
2947
2948 // scan the list for out-of-range linedef indicies in list
2949 for(tmplist = list; *tmplist != -1; tmplist++)
2950 {
2951 if(*tmplist < 0 || *tmplist >= numlines)
2952 {
2953 lprintf(LO_ERROR, "P_VerifyBlockMap: index >= numlines\n");
2954 return false;
2955 }
2956 }
2957 }
2958 }
2959
2960 return true;
2961 }
2962
2963 //
2964 // P_LoadBlockMap
2965 //
2966 // killough 3/1/98: substantially modified to work
2967 // towards removing blockmap limit (a wad limitation)
2968 //
2969 // killough 3/30/98: Rewritten to remove blockmap limit,
2970 // though current algorithm is brute-force and unoptimal.
2971 //
2972
2973 static void P_LoadBlockMap (int lump)
2974 {
2975 long count;
2976
2977 count = W_SafeLumpLength(lump);
2978
2979 if (
2980 dsda_Flag(dsda_arg_blockmap) ||
2981 count < 8 ||
2982 (count /= 2) >= 0x10000 //e6y
2983 )
2984 {
2985 P_CreateBlockMap();
2986 }
2987 else
2988 {
2989 long i;
2990 // cph - const*, wad lump handling updated
2991 const short *wadblockmaplump = W_LumpByNum(lump);
2992 blockmaplump = malloc_IfSameLevel(blockmaplump, sizeof(*blockmaplump) * count);
2993
2994 // killough 3/1/98: Expand wad blockmap into larger internal one,
2995 // by treating all offsets except -1 as unsigned and zero-extending
2996 // them. This potentially doubles the size of blockmaps allowed,
2997 // because Doom originally considered the offsets as always signed.
2998
2999 blockmaplump[0] = LittleShort(wadblockmaplump[0]);
3000 blockmaplump[1] = LittleShort(wadblockmaplump[1]);
3001 blockmaplump[2] = (long)(LittleShort(wadblockmaplump[2])) & 0xffff;
3002 blockmaplump[3] = (long)(LittleShort(wadblockmaplump[3])) & 0xffff;
3003
3004 for (i=4 ; i<count ; i++)
3005 {
3006 short t = LittleShort(wadblockmaplump[i]); // killough 3/1/98
3007 blockmaplump[i] = t == -1 ? -1l : (long) t & 0xffff;
3008 }
3009
3010 bmaporgx = blockmaplump[0]<<FRACBITS;
3011 bmaporgy = blockmaplump[1]<<FRACBITS;
3012 bmapwidth = blockmaplump[2];
3013 bmapheight = blockmaplump[3];
3014
3015 // haleyjd 03/04/10: check for blockmap problems
3016 // http://www.doomworld.com/idgames/index.php?id=12935
3017 if (!P_VerifyBlockMap(count))
3018 {
3019 lprintf(LO_INFO, "P_LoadBlockMap: erroneous BLOCKMAP lump may cause crashes.\n");
3020 lprintf(LO_INFO, "P_LoadBlockMap: use \"-blockmap\" command line switch for rebuilding\n");
3021 }
3022 }
3023
3024 RememberOriginalBlockMap();
3025
3026 // clear out mobj chains - CPhipps - use calloc
3027 blocklinks_count = bmapwidth * bmapheight;
3028 blocklinks = calloc_IfSameLevel(blocklinks, blocklinks_count, sizeof(*blocklinks));
3029 blockmap = blockmaplump+4;
3030
3031 // MAES: set blockmapxneg and blockmapyneg
3032 // E.g. for a full 512x512 map, they should be both
3033 // -1. For a 257*257, they should be both -255 etc.
3034 blockmapxneg = (bmapwidth > 255 ? bmapwidth - 512 : -257);
3035 blockmapyneg = (bmapheight > 255 ? bmapheight - 512 : -257);
3036 if (blockmapxneg != -257 || blockmapyneg != -257)
3037 {
3038 lprintf(LO_WARN,
3039 "P_LoadBlockMap: This map uses a large blockmap which may cause no-clipping bugs. "
3040 "Toggle the \"Fix clipping problems in large levels\" option "
3041 "in the \"Compatibility with common mapping errors\" menu in order to activate a fix. "
3042 "That fix won't be applied during demo playback or recording.\n");
3043 }
3044 }
3045
3046 //
3047 // P_GroupLines
3048 // Builds sector line lists and subsector sector numbers.
3049 // Finds block bounding boxes for sectors.
3050 //
3051 // killough 5/3/98: reformatted, cleaned up
3052 // cph 18/8/99: rewritten to avoid O(numlines * numsectors) section
3053 // It makes things more complicated, but saves seconds on big levels
3054 // figgi 09/18/00 -- adapted for gl-nodes
3055
3056 // cph - convenient sub-function
3057 static void P_AddLineToSector(line_t* li, sector_t* sector)
3058 {
3059 fixed_t *bbox = (void*)sector->blockbox;
3060
3061 sector->lines[sector->linecount++] = li;
3062 M_AddToBox (bbox, li->v1->x, li->v1->y);
3063 M_AddToBox (bbox, li->v2->x, li->v2->y);
3064 }
3065
3066 // modified to return totallines (needed by P_LoadReject)
3067 static int P_GroupLines (void)
3068 {
3069 register line_t *li;
3070 register sector_t *sector;
3071 int i,j, total = numlines;
3072
3073 // figgi
3074 for (i=0 ; i<numsubsectors ; i++)
3075 {
3076 seg_t *seg = &segs[subsectors[i].firstline];
3077 subsectors[i].sector = NULL;
3078 for(j=0; j<subsectors[i].numlines; j++)
3079 {
3080 if(seg->sidedef)
3081 {
3082 subsectors[i].sector = seg->sidedef->sector;
3083 break;
3084 }
3085 seg++;
3086 }
3087 if(subsectors[i].sector == NULL)
3088 I_Error("P_GroupLines: Subsector a part of no sector!\n");
3089 }
3090
3091 // count number of lines in each sector
3092 for (i=0,li=lines; i<numlines; i++, li++)
3093 {
3094 li->frontsector->linecount++;
3095 if (li->backsector && li->backsector != li->frontsector)
3096 {
3097 li->backsector->linecount++;
3098 total++;
3099 }
3100 }
3101
3102 { // allocate line tables for each sector
3103 line_t **linebuffer = Z_MallocLevel(total*sizeof(line_t *));
3104 // e6y: REJECT overrun emulation code
3105 // moved to P_LoadReject
3106
3107 for (i=0, sector = sectors; i<numsectors; i++, sector++)
3108 {
3109 sector->lines = linebuffer;
3110 linebuffer += sector->linecount;
3111 sector->linecount = 0;
3112 M_ClearBox(sector->blockbox);
3113 }
3114 }
3115
3116 // Enter those lines
3117 for (i=0,li=lines; i<numlines; i++, li++)
3118 {
3119 P_AddLineToSector(li, li->frontsector);
3120 if (li->backsector && li->backsector != li->frontsector)
3121 P_AddLineToSector(li, li->backsector);
3122 }
3123
3124 for (i=0, sector = sectors; i<numsectors; i++, sector++)
3125 {
3126 fixed_t *bbox = (void*)sector->blockbox; // cph - For convenience, so
3127 // I can sue the old code unchanged
3128 int block;
3129
3130 sector->bbox[0] = sector->blockbox[0] >> FRACTOMAPBITS;
3131 sector->bbox[1] = sector->blockbox[1] >> FRACTOMAPBITS;
3132 sector->bbox[2] = sector->blockbox[2] >> FRACTOMAPBITS;
3133 sector->bbox[3] = sector->blockbox[3] >> FRACTOMAPBITS;
3134
3135 // set the degenmobj_t to the middle of the bounding box
3136 if (comp[comp_sound])
3137 {
3138 sector->soundorg.x = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
3139 sector->soundorg.y = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
3140 }
3141 else
3142 {
3143 //e6y: fix sound origin for large levels
3144 sector->soundorg.x = bbox[BOXRIGHT]/2+bbox[BOXLEFT]/2;
3145 sector->soundorg.y = bbox[BOXTOP]/2+bbox[BOXBOTTOM]/2;
3146 }
3147
3148 // adjust bounding box to map blocks
3149 block = P_GetSafeBlockY(bbox[BOXTOP]-bmaporgy+MAXRADIUS);
3150 block = block >= bmapheight ? bmapheight-1 : block;
3151 sector->blockbox[BOXTOP]=block;
3152
3153 block = P_GetSafeBlockY(bbox[BOXBOTTOM]-bmaporgy-MAXRADIUS);
3154 block = block < 0 ? 0 : block;
3155 sector->blockbox[BOXBOTTOM]=block;
3156
3157 block = P_GetSafeBlockX(bbox[BOXRIGHT]-bmaporgx+MAXRADIUS);
3158 block = block >= bmapwidth ? bmapwidth-1 : block;
3159 sector->blockbox[BOXRIGHT]=block;
3160
3161 block = P_GetSafeBlockX(bbox[BOXLEFT]-bmaporgx-MAXRADIUS);
3162 block = block < 0 ? 0 : block;
3163 sector->blockbox[BOXLEFT]=block;
3164 }
3165
3166 return total; // this value is needed by the reject overrun emulation code
3167 }
3168
3169 //
3170 // P_LoadReject - load the reject table
3171 //
3172
3173 static void P_LoadReject(int lump)
3174 {
3175 unsigned int length;
3176
3177 length = W_SafeLumpLength(lump);
3178 rejectmatrix = W_SafeLumpByNum(lump);
3179
3180 //e6y: check for overflow
3181 RejectOverrun(length, &rejectmatrix, P_GroupLines());
3182 }
3183
3184 //
3185 // killough 10/98
3186 //
3187 // Remove slime trails.
3188 //
3189 // Slime trails are inherent to Doom's coordinate system -- i.e. there is
3190 // nothing that a node builder can do to prevent slime trails ALL of the time,
3191 // because it's a product of the integer coodinate system, and just because
3192 // two lines pass through exact integer coordinates, doesn't necessarily mean
3193 // that they will intersect at integer coordinates. Thus we must allow for
3194 // fractional coordinates if we are to be able to split segs with node lines,
3195 // as a node builder must do when creating a BSP tree.
3196 //
3197 // A wad file does not allow fractional coordinates, so node builders are out
3198 // of luck except that they can try to limit the number of splits (they might
3199 // also be able to detect the degree of roundoff error and try to avoid splits
3200 // with a high degree of roundoff error). But we can use fractional coordinates
3201 // here, inside the engine. It's like the difference between square inches and
3202 // square miles, in terms of granularity.
3203 //
3204 // For each vertex of every seg, check to see whether it's also a vertex of
3205 // the linedef associated with the seg (i.e, it's an endpoint). If it's not
3206 // an endpoint, and it wasn't already moved, move the vertex towards the
3207 // linedef by projecting it using the law of cosines. Formula:
3208 //
3209 // 2 2 2 2
3210 // dx x0 + dy x1 + dx dy (y0 - y1) dy y0 + dx y1 + dx dy (x0 - x1)
3211 // {---------------------------------, ---------------------------------}
3212 // 2 2 2 2
3213 // dx + dy dx + dy
3214 //
3215 // (x0,y0) is the vertex being moved, and (x1,y1)-(x1+dx,y1+dy) is the
3216 // reference linedef.
3217 //
3218 // Segs corresponding to orthogonal linedefs (exactly vertical or horizontal
3219 // linedefs), which comprise at least half of all linedefs in most wads, don't
3220 // need to be considered, because they almost never contribute to slime trails
3221 // (because then any roundoff error is parallel to the linedef, which doesn't
3222 // cause slime). Skipping simple orthogonal lines lets the code finish quicker.
3223 //
3224 // Please note: This section of code is not interchangable with TeamTNT's
3225 // code which attempts to fix the same problem.
3226 //
3227 // Firelines (TM) is a Rezistered Trademark of MBF Productions
3228 //
3229
3230 static void P_RemoveSlimeTrails(void) // killough 10/98
3231 {
3232 byte *hit = Z_Calloc(1, numvertexes); // Hitlist for vertices
3233 int i;
3234 // Correction of desync on dv04-423.lmp/dv.wad
3235 // http://www.doomworld.com/vb/showthread.php?s=&postid=627257#post627257
3236 int apply_for_real_vertexes = (compatibility_level>=lxdoom_1_compatibility || prboom_comp[PC_REMOVE_SLIME_TRAILS].state);
3237
3238 for (i=0; i<numvertexes; i++)
3239 {
3240 // [crispy] initialize pseudovertexes with actual vertex coordinates
3241 vertexes[i].px = vertexes[i].x;
3242 vertexes[i].py = vertexes[i].y;
3243 }
3244
3245 for (i=0; i<numsegs; i++) // Go through each seg
3246 {
3247 const line_t *l;
3248
3249 if (!segs[i].linedef)
3250 break; //e6y: probably 'continue;'?
3251
3252 l = segs[i].linedef; // The parent linedef
3253 if (l->dx && l->dy) // We can ignore orthogonal lines
3254 {
3255 vertex_t *v = segs[i].v1;
3256 do
3257 if (!hit[v - vertexes]) // If we haven't processed vertex
3258 {
3259 hit[v - vertexes] = 1; // Mark this vertex as processed
3260 if (v != l->v1 && v != l->v2) // Exclude endpoints of linedefs
3261 { // Project the vertex back onto the parent linedef
3262 int64_t dx2 = (l->dx >> FRACBITS) * (l->dx >> FRACBITS);
3263 int64_t dy2 = (l->dy >> FRACBITS) * (l->dy >> FRACBITS);
3264 int64_t dxy = (l->dx >> FRACBITS) * (l->dy >> FRACBITS);
3265 int64_t s = dx2 + dy2;
3266 int x0 = v->x, y0 = v->y, x1 = l->v1->x, y1 = l->v1->y;
3267 v->px = (int)((dx2 * x0 + dy2 * x1 + dxy * (y0 - y1)) / s);
3268 v->py = (int)((dy2 * y0 + dx2 * y1 + dxy * (x0 - x1)) / s);
3269
3270 // [crispy] wait a minute... moved more than 8 map units?
3271 // maybe that's a linguortal then, back to the original coordinates
3272 // http://www.doomworld.com/vb/doom-editing/74354-stupid-bsp-tricks/
3273 if (!apply_for_real_vertexes && (D_abs(v->px - v->x) > 8*FRACUNIT || D_abs(v->py - v->y) > 8*FRACUNIT))
3274 {
3275 v->px = v->x;
3276 v->py = v->y;
3277 }
3278
3279 if (apply_for_real_vertexes)
3280 {
3281 v->x = v->px;
3282 v->y = v->py;
3283 }
3284 }
3285 } // Obsfucated C contest entry: :)
3286 while ((v != segs[i].v2) && (v = segs[i].v2));
3287 }
3288 }
3289 Z_Free(hit);
3290 }
3291
3292 // [crispy] fix long wall wobble
3293 static angle_t anglediff(angle_t a, angle_t b)
3294 {
3295 if (b > a)
3296 return anglediff(b, a);
3297
3298 if (a - b < ANG180)
3299 return a - b;
3300 else // [crispy] wrap around
3301 return b - a;
3302 }
3303
3304 static void R_CalcSegsLength(void)
3305 {
3306 int i;
3307 for (i=0; i<numsegs; i++)
3308 {
3309 double length;
3310 seg_t *li = segs+i;
3311 int64_t dx = (int64_t)li->v2->px - li->v1->px;
3312 int64_t dy = (int64_t)li->v2->py - li->v1->py;
3313 length = sqrt((double)dx*dx + (double)dy*dy);
3314 li->halflength = (uint32_t)(length / 2.0);
3315 // [crispy] re-calculate angle used for rendering
3316 li->pangle = R_PointToAngleEx2(li->v1->px, li->v1->py, li->v2->px, li->v2->py);
3317 // [crispy] more than just a little adjustment?
3318 // back to the original angle then
3319 if (anglediff(li->pangle, li->angle) > ANG60/2)
3320 {
3321 li->pangle = li->angle;
3322 }
3323 }
3324 }
3325
3326 //
3327 // P_CheckLumpsForSameSource
3328 //
3329 // Are these lumps in the same wad file?
3330 //
3331
3332 dboolean P_CheckLumpsForSameSource(int lump1, int lump2)
3333 {
3334 int wad1_index, wad2_index;
3335 wadfile_info_t *wad1, *wad2;
3336
3337 if (((unsigned)lump1 >= (unsigned)numlumps) || ((unsigned)lump2 >= (unsigned)numlumps))
3338 return false;
3339
3340 wad1 = lumpinfo[lump1].wadfile;
3341 wad2 = lumpinfo[lump2].wadfile;
3342
3343 if (!wad1 || !wad2)
3344 return false;
3345
3346 wad1_index = (int)(wad1 - wadfiles);
3347 wad2_index = (int)(wad2 - wadfiles);
3348
3349 if (wad1_index != wad2_index)
3350 return false;
3351
3352 if ((wad1_index < 0) || ((size_t)wad1_index >= numwadfiles))
3353 return false;
3354
3355 if ((wad2_index < 0) || ((size_t)wad2_index >= numwadfiles))
3356 return false;
3357
3358 return true;
3359 }
3360
3361 static dboolean P_CheckForUDMF(int lumpnum)
3362 {
3363 int i;
3364
3365 i = lumpnum + ML_TEXTMAP;
3366 if (P_CheckLumpsForSameSource(lumpnum, i))
3367 {
3368 if (!strncasecmp(lumpinfo[i].name, "TEXTMAP", 8))
3369 {
3370 dsda_ParseUDMF(W_LumpByNum(i), W_LumpLength(i), I_Error);
3371 return true;
3372 }
3373 }
3374
3375 return false;
3376 }
3377
3378 static dboolean P_CheckForBehavior(int lumpnum)
3379 {
3380 int i;
3381
3382 i = level_components.behavior;
3383 if (P_CheckLumpsForSameSource(lumpnum, i))
3384 {
3385 if (!strncasecmp(lumpinfo[i].name, "BEHAVIOR", 8))
3386 {
3387 return true;
3388 }
3389 }
3390
3391 return false;
3392 }
3393
3394 static void P_VerifyLevelComponents(int lumpnum)
3395 {
3396 int i;
3397
3398 static const char *ml_labels[] = {
3399 "LABEL", // A separator, name, ExMx or MAPxx
3400 "THINGS", // Monsters, items..
3401 "LINEDEFS", // LineDefs, from editing
3402 "SIDEDEFS", // SideDefs, from editing
3403 "VERTEXES", // Vertices, edited and BSP splits generated
3404 "SEGS", // LineSegs, from LineDefs split by BSP
3405 "SSECTORS", // SubSectors, list of LineSegs
3406 "NODES", // BSP nodes
3407 "SECTORS", // Sectors, from editing
3408 "REJECT", // LUT, sector-sector visibility
3409 "BLOCKMAP", // LUT, motion clipping, walls/grid element
3410 };
3411
3412 for (i = ML_THINGS + 1; i <= ML_SECTORS; i++)
3413 {
3414 if (!P_CheckLumpsForSameSource(lumpnum, lumpnum + i))
3415 {
3416 I_Error("P_SetupLevel: Level wad structure is incomplete. There is no %s lump.", ml_labels[i]);
3417 }
3418 }
3419 }
3420
3421 static void P_UpdateMapFormat()
3422 {
3423 if (udmf_map)
3424 {
3425 if (heretic)
3426 I_Error("UDMF maps are not supported in Heretic yet");
3427
3428 if (hexen)
3429 I_Error("UDMF maps are not supported in Hexen yet");
3430
3431 dsda_ApplyZDoomMapFormat();
3432 }
3433 else
3434 {
3435 if (dsda_UseMapinfo())
3436 DO_ONCE
3437 lprintf(LO_WARN, "Some features of MAPINFO may not work with non-udmf maps!\n");
3438 END_ONCE
3439
3440 if (has_behavior && !hexen)
3441 {
3442 if (heretic)
3443 I_Error("Hexen format maps are not supported in Heretic yet");
3444
3445 dsda_ApplyZDoomMapFormat();
3446 }
3447 else
3448 {
3449 dsda_ApplyDefaultMapFormat();
3450 }
3451 }
3452 }
3453
3454 static void P_UpdateLevelComponents(int lumpnum) {
3455 level_components.label = lumpnum;
3456 level_components.things = lumpnum + ML_THINGS;
3457 level_components.linedefs = lumpnum + ML_LINEDEFS;
3458 level_components.sidedefs = lumpnum + ML_SIDEDEFS;
3459 level_components.vertexes = lumpnum + ML_VERTEXES;
3460 level_components.segs = lumpnum + ML_SEGS;
3461 level_components.ssectors = lumpnum + ML_SSECTORS;
3462 level_components.nodes = lumpnum + ML_NODES;
3463 level_components.sectors = lumpnum + ML_SECTORS;
3464 level_components.reject = lumpnum + ML_REJECT;
3465 level_components.blockmap = lumpnum + ML_BLOCKMAP;
3466 level_components.behavior = lumpnum + ML_BEHAVIOR;
3467
3468 level_components.znodes = LUMP_NOT_FOUND;
3469
3470 P_VerifyLevelComponents(lumpnum);
3471
3472 has_behavior = P_CheckForBehavior(lumpnum);
3473 }
3474
3475 static void P_UpdateUDMFLevelComponents(int lumpnum)
3476 {
3477 int i;
3478
3479 level_components.label = lumpnum;
3480 level_components.things = LUMP_NOT_FOUND;
3481 level_components.linedefs = LUMP_NOT_FOUND;
3482 level_components.sidedefs = LUMP_NOT_FOUND;
3483 level_components.vertexes = LUMP_NOT_FOUND;
3484 level_components.segs = LUMP_NOT_FOUND;
3485 level_components.ssectors = LUMP_NOT_FOUND;
3486 level_components.nodes = LUMP_NOT_FOUND;
3487 level_components.sectors = LUMP_NOT_FOUND;
3488 level_components.reject = LUMP_NOT_FOUND;
3489 level_components.blockmap = LUMP_NOT_FOUND;
3490 level_components.behavior = LUMP_NOT_FOUND;
3491 level_components.znodes = LUMP_NOT_FOUND;
3492
3493 for (i = lumpnum + ML_TEXTMAP + 1; ; ++i)
3494 {
3495 const char* name;
3496
3497 name = W_LumpName(i);
3498 if (!name || !strncasecmp(name, "ENDMAP", 8))
3499 break;
3500 else if (!strncasecmp(name, "ZNODES", 8))
3501 level_components.znodes = i;
3502 else if (!strncasecmp(name, "BLOCKMAP", 8))
3503 level_components.blockmap = i;
3504 else if (!strncasecmp(name, "REJECT", 8))
3505 level_components.reject = i;
3506 }
3507
3508 if (level_components.znodes == LUMP_NOT_FOUND)
3509 I_Error("P_SetupLevel: Level wad structure is incomplete. There is no ZNODES lump.");
3510 }
3511
3512 void PO_LoadThings(int lump);
3513 void PO_LoadUDMFThings(int lump);
3514
3515 map_loader_t udmf_map_loader = {
3516 .load_vertexes = P_LoadUDMFVertexes,
3517 .load_sectors = P_LoadUDMFSectors,
3518 .load_things = P_LoadUDMFThings,
3519 .load_linedefs = P_LoadUDMFLineDefs,
3520 .allocate_sidedefs = P_AllocateUDMFSideDefs,
3521 .load_sidedefs = P_LoadUDMFSideDefs,
3522 .update_level_components = P_UpdateUDMFLevelComponents,
3523 .po_load_things = PO_LoadUDMFThings,
3524 };
3525
3526 map_loader_t legacy_map_loader = {
3527 .load_vertexes = P_LoadVertexes,
3528 .load_sectors = P_LoadSectors,
3529 .load_things = P_LoadThings,
3530 .load_linedefs = P_LoadLineDefs,
3531 .allocate_sidedefs = P_AllocateSideDefs,
3532 .load_sidedefs = P_LoadSideDefs,
3533 .update_level_components = P_UpdateLevelComponents,
3534 .po_load_things = PO_LoadThings,
3535 };
3536
3537 map_loader_t map_loader;
3538
3539 void P_UpdateMapLoader(int lumpnum)
3540 {
3541 udmf_map = P_CheckForUDMF(lumpnum);
3542
3543 map_loader = udmf_map ? udmf_map_loader : legacy_map_loader;
3544 }
3545
3546 //
3547 // P_CheckLevelFormat
3548 //
3549 // Checking for presence of necessary lumps
3550 //
3551
3552 void P_CheckLevelWadStructure(int lumpnum)
3553 {
3554 P_UpdateMapLoader(lumpnum);
3555
3556 map_loader.update_level_components(lumpnum);
3557
3558 P_UpdateMapFormat();
3559 }
3560
3561 void P_InitSubsectorsLines(void)
3562 {
3563 int num, count;
3564
3565 if (sslines_indexes)
3566 {
3567 Z_Free(sslines_indexes);
3568 sslines_indexes = NULL;
3569 }
3570
3571 if (sslines)
3572 {
3573 Z_Free(sslines);
3574 sslines = NULL;
3575 }
3576
3577 count = 0;
3578 sslines_indexes = Z_Malloc((numsubsectors + 1) * sizeof(sslines_indexes[0]));
3579
3580 for (num = 0; num < numsubsectors; num++)
3581 {
3582 seg_t *seg;
3583 const seg_t *seg_last = segs + subsectors[num].firstline + subsectors[num].numlines;
3584
3585 sslines_indexes[num] = count;
3586
3587 for (seg = segs + subsectors[num].firstline; seg < seg_last; seg++)
3588 {
3589 if (!seg->linedef) continue;
3590 seg->linedef->validcount = 0;
3591 seg->linedef->validcount2 = 0;
3592 }
3593
3594 for (seg = segs + subsectors[num].firstline; seg < seg_last; seg++)
3595 {
3596 if (!seg->linedef) continue;
3597
3598 if (seg->linedef->validcount == 1)
3599 continue;
3600
3601 seg->linedef->validcount = 1;
3602 count++;
3603 }
3604 }
3605
3606 sslines_indexes[numsubsectors] = count;
3607
3608 sslines = Z_Malloc(count * sizeof(sslines[0]));
3609 count = 0;
3610
3611 for (num = 0; num < numsubsectors; num++)
3612 {
3613 seg_t *seg;
3614 const seg_t *seg_last = segs + subsectors[num].firstline + subsectors[num].numlines;
3615
3616 for (seg = segs + subsectors[num].firstline; seg < seg_last; seg++)
3617 {
3618 if (!seg->linedef) continue;
3619 seg->linedef->validcount = 0;
3620 seg->linedef->validcount2 = 0;
3621 }
3622
3623 for (seg = segs + subsectors[num].firstline; seg < seg_last; seg++)
3624 {
3625 ssline_t *ssline = &sslines[count];
3626 if (!seg->linedef) continue;
3627
3628 if (seg->linedef->validcount == 1)
3629 continue;
3630
3631 seg->linedef->validcount = 1;
3632
3633 ssline->seg = seg;
3634 ssline->linedef = seg->linedef;
3635
3636 ssline->x1 = seg->linedef->v1->x;
3637 ssline->y1 = seg->linedef->v1->y;
3638 ssline->x2 = seg->linedef->v2->x;
3639 ssline->y2 = seg->linedef->v2->y;
3640 ssline->bbox[0] = seg->linedef->bbox[0];
3641 ssline->bbox[1] = seg->linedef->bbox[1];
3642 ssline->bbox[2] = seg->linedef->bbox[2];
3643 ssline->bbox[3] = seg->linedef->bbox[3];
3644
3645 count++;
3646 }
3647 }
3648
3649 for (num = 0; num < numlines; num++)
3650 {
3651 lines[num].validcount = 0;
3652 lines[num].validcount2 = 0;
3653 }
3654 }
3655
3656 static dboolean must_rebuild_blockmap;
3657
3658 void P_MustRebuildBlockmap(void)
3659 {
3660 must_rebuild_blockmap = true;
3661 }
3662
3663 //
3664 // P_SetupLevel
3665 //
3666 // killough 5/3/98: reformatted, cleaned up
3667
3668 void P_SetupLevel(int episode, int map, int playermask, int skill)
3669 {
3670 int i;
3671 char lumpname[9];
3672 int lumpnum;
3673
3674 //e6y
3675 totallive = 0;
3676
3677 main_tranmap = dsda_DefaultTranMap();
3678
3679 dsda_WatchBeforeLevelSetup();
3680
3681 R_StopAllInterpolations();
3682
3683 totallive = totalkills = totalitems = totalsecret = wminfo.maxfrags = 0;
3684 wminfo.partime = 180;
3685 wminfo.fake_partime = 0;
3686 wminfo.modified_partime = false;
3687
3688 for (i = 0; i < g_maxplayers; i++)
3689 {
3690 players[i].killcount = players[i].secretcount = players[i].itemcount = 0;
3691 players[i].maxkilldiscount = 0;//e6y
3692 }
3693
3694 // find map name
3695 snprintf(lumpname, sizeof(lumpname), "%s", dsda_MapLumpName(episode, map));
3696 lumpnum = W_GetNumForName(lumpname);
3697
3698 // Must process musinfo to get default track before calling S_Start
3699 S_ParseMusInfo(lumpname);
3700
3701 // Make sure all sounds are stopped before Z_FreeTag.
3702 S_Start();
3703
3704 Z_FreeLevel();
3705
3706 P_InitThinkers();
3707
3708 // e6y
3709 // Refuse to load a map with incomplete pwad structure.
3710 // Avoid segfaults on levels without nodes.
3711 P_CheckLevelWadStructure(lumpnum);
3712
3713 dsda_ApplyLevelCompatibility(lumpnum);
3714
3715 leveltime = 0; totallive = 0;
3716
3717 // note: most of this ordering is important
3718
3719 // killough 3/1/98: P_LoadBlockMap call moved down to below
3720 // killough 4/4/98: split load of sidedefs into two parts,
3721 // to allow texture names to be used in special linedefs
3722
3723 // figgi 10/19/00 -- check for gl lumps and load them
3724 P_GetNodesVersion();
3725
3726 samelevel = !inconsistent_nodes &&
3727 map == current_map &&
3728 episode == current_episode &&
3729 nodesVersion == current_nodesVersion;
3730
3731 inconsistent_nodes = false;
3732 current_episode = episode;
3733 current_map = map;
3734 current_nodesVersion = nodesVersion;
3735
3736 dsda_WatchNewLevel();
3737
3738 if (!samelevel)
3739 {
3740 // proff 11/99: clean the memory from textures etc.
3741 gld_CleanMemory();
3742
3743 Z_Free(segs);
3744 Z_Free(nodes);
3745 Z_Free(subsectors);
3746 Z_Free(map_subsectors);
3747
3748 Z_Free(blocklinks);
3749 Z_Free(blockmaplump);
3750
3751 Z_Free(lines);
3752 Z_Free(sides);
3753 Z_Free(sectors);
3754 Z_Free(vertexes);
3755 }
3756
3757 dsda_ResetHealthGroups();
3758
3759 map_loader.load_vertexes(level_components.vertexes);
3760 map_loader.load_sectors(level_components.sectors);
3761 map_loader.allocate_sidedefs(level_components.sidedefs);
3762 map_loader.load_linedefs(level_components.linedefs);
3763 map_loader.load_sidedefs(level_components.sidedefs);
3764
3765 P_PostProcessLineDefs();
3766
3767 // e6y: speedup of level reloading
3768 // Do not reload BlockMap for same level,
3769 // because in case of big level P_CreateBlockMap eats much time
3770 //
3771 // BlockMap should be reloaded after OVERFLOW_INTERCEPT,
3772 // because bmapwidth/bmapheight/bmaporgx/bmaporgy can be overwritten
3773 if (!samelevel || must_rebuild_blockmap)
3774 {
3775 must_rebuild_blockmap = false;
3776 P_LoadBlockMap(level_components.blockmap);
3777 }
3778 else
3779 {
3780 memset(blocklinks, 0, bmapwidth*bmapheight*sizeof(*blocklinks));
3781 }
3782
3783 switch (nodesVersion)
3784 {
3785 case ZDOOM_XNOD_NODES:
3786 case ZDOOM_ZNOD_NODES:
3787 P_LoadZNodes(level_components.nodes, 0);
3788
3789 break;
3790
3791 case ZDOOM_XGLN_NODES:
3792 case ZDOOM_ZGLN_NODES:
3793 P_LoadZNodes(level_components.znodes, 1);
3794
3795 break;
3796
3797 case ZDOOM_XGL2_NODES:
3798 case ZDOOM_ZGL2_NODES:
3799 P_LoadZNodes(level_components.znodes, 2);
3800
3801 break;
3802
3803 case ZDOOM_XGL3_NODES:
3804 case ZDOOM_ZGL3_NODES:
3805 P_LoadZNodes(level_components.znodes, 3);
3806
3807 break;
3808
3809 case DEEP_BSP_V4_NODES:
3810 P_LoadSubsectors_V4(level_components.ssectors);
3811 P_LoadNodes_V4(level_components.nodes);
3812 P_LoadSegs_V4(level_components.segs);
3813
3814 break;
3815
3816 case UNKNOWN_NODES:
3817 case DEFAULT_BSP_NODES:
3818 P_LoadSubsectors(level_components.ssectors);
3819 P_LoadNodes(level_components.nodes);
3820 P_LoadSegs(level_components.segs);
3821
3822 break;
3823 }
3824
3825 if (!samelevel)
3826 {
3827 P_InitSubsectorsLines();
3828 }
3829
3830 map_subsectors = calloc_IfSameLevel(map_subsectors,
3831 numsubsectors, sizeof(map_subsectors[0]));
3832
3833 // reject loading and underflow padding separated out into new function
3834 P_LoadReject(level_components.reject);
3835
3836 P_RemoveSlimeTrails(); // killough 10/98: remove slime trails from wad
3837
3838 // should be after P_RemoveSlimeTrails, because it changes vertexes
3839 R_CalcSegsLength();
3840
3841 {
3842 void A_ResetPlayerCorpseQueue(void);
3843
3844 A_ResetPlayerCorpseQueue();
3845 }
3846
3847 po_NumPolyobjs = 0; // hexen
3848
3849 /* cph - reset all multiplayer starts */
3850 memset(playerstarts,0,sizeof(playerstarts));
3851 deathmatch_p = deathmatchstarts;
3852 for (i = 0; i < g_maxplayers; i++)
3853 players[i].mo = NULL;
3854
3855 P_MapStart();
3856
3857 if (heretic)
3858 {
3859 P_InitAmbientSound();
3860 P_InitMonsters();
3861 P_OpenWeapons();
3862 }
3863
3864 if (map_format.polyobjs)
3865 {
3866 PO_ResetBlockMap(true);
3867 }
3868
3869 map_loader.load_things(level_components.things);
3870
3871 if (map_format.polyobjs)
3872 {
3873 PO_Init(level_components.things); // Initialize the polyobjs
3874 }
3875
3876 if (map_format.acs)
3877 {
3878 P_LoadACScripts(level_components.behavior); // ACS object code
3879 }
3880
3881 if (heretic)
3882 {
3883 P_CloseWeapons();
3884 }
3885
3886 // if deathmatch, randomly spawn the active players
3887 if (deathmatch)
3888 {
3889 for (i = 0; i < g_maxplayers; i++)
3890 if (playeringame[i])
3891 {
3892 players[i].mo = NULL; // not needed? - done before P_LoadThings
3893 G_DeathMatchSpawnPlayer(i);
3894 }
3895 }
3896 else // if !deathmatch, check all necessary player starts actually exist
3897 {
3898 for (i = 0; i < g_maxplayers; i++)
3899 if (playeringame[i] && !players[i].mo)
3900 I_Error("P_SetupLevel: missing player %d start\n", i+1);
3901 }
3902
3903 players[consoleplayer].viewz = players[consoleplayer].mo->z +
3904 players[consoleplayer].viewheight;
3905
3906 if (players[consoleplayer].cheats & CF_FLY)
3907 {
3908 players[consoleplayer].mo->flags |= (MF_NOGRAVITY | MF_FLY);
3909 }
3910
3911 // killough 3/26/98: Spawn icon landings:
3912 if (gamemode == commercial && !hexen)
3913 P_SpawnBrainTargets();
3914
3915 // clear special respawning que
3916 iquehead = iquetail = 0;
3917
3918 // set up world state
3919 P_SpawnSpecials();
3920
3921 dsda_WatchAfterLevelSetup();
3922
3923 P_MapEnd();
3924
3925 dsda_HandleMapPreferences();
3926
3927 dsda_ApplyFadeTable();
3928
3929 // preload graphics
3930 R_PrecacheLevel();
3931
3932 if (V_IsOpenGLMode())
3933 {
3934 // e6y
3935 // Do not preprocess GL data during skipping,
3936 // because it potentially will not be used.
3937 // But preprocessing must be called immediately after stop of skipping.
3938 if (!dsda_SkipMode())
3939 {
3940 // proff 11/99: calculate all OpenGL specific tables etc.
3941 gld_PreprocessLevel();
3942 }
3943 }
3944
3945 //e6y
3946 if (!samelevel)
3947 {
3948 P_SyncWalkcam(true, true);
3949 }
3950
3951 R_SmoothPlaying_Reset(NULL);
3952
3953 P_InitLightning();
3954
3955 if (map_format.sndseq)
3956 {
3957 SN_StopAllSequences();
3958 }
3959
3960 if (dsda_ShowMinimap())
3961 {
3962 AM_Start(false);
3963 }
3964 }
3965
3966 //
3967 // P_Init
3968 //
3969 void P_Init (void)
3970 {
3971 P_InitSwitchList();
3972 P_InitFTAnims();
3973 P_InitPicAnims();
3974 P_InitTerrainTypes();
3975 P_InitLava();
3976 R_InitSprites(sprnames);
3977 }