-
Notifications
You must be signed in to change notification settings - Fork 5
/
p_setup.pas
932 lines (825 loc) · 25.4 KB
/
p_setup.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
//------------------------------------------------------------------------------
//
// FPCDoom - Port of Doom to Free Pascal Compiler
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2004-2007 by Jim Valavanis
// Copyright (C) 2017-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/fpcdoom/
//------------------------------------------------------------------------------
{$I FPCDoom.inc}
unit p_setup;
interface
uses
d_fpc,
doomdef,
doomdata,
m_fixed,
p_mobj_h,
r_defs;
//==============================================================================
//
// P_GetMapName
//
//==============================================================================
function P_GetMapName(const episode, map: integer): string;
//==============================================================================
// P_SetupLevel
//
// NOT called by W_Ticker. Fixme.
//
//==============================================================================
procedure P_SetupLevel(episode, map: integer);
//==============================================================================
// P_Init
//
// Called by startup code.
//
//==============================================================================
procedure P_Init;
var
// origin of block map
bmaporgx: fixed_t;
bmaporgy: fixed_t;
numvertexes: integer;
vertexes: Pvertex_tArray;
numsegs: integer;
segs: Pseg_tArray;
numsectors: integer;
sectors: Psector_tArray;
numsubsectors: integer;
subsectors: Psubsector_tArray;
numnodes: integer;
nodes: Pnode_tArray;
numlines: integer;
lines: Pline_tArray;
numsides: integer;
sides: Pside_tArray;
//
// MAP related Lookup tables.
// Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
//
var
// BLOCKMAP
// Created from axis aligned bounding box
// of the map, a rectangular array of
// blocks of size ...
// Used to speed up collision detection
// by spatial subdivision in 2D.
//
// Blockmap size.
bmapwidth: integer;
bmapheight: integer; // size in mapblocks
blockmap: PSmallIntArray; // int for larger maps
// offsets in blockmap are from here
blockmaplump: PSmallIntArray;
// for thing chains
blocklinks: Pmobj_tPArray;
// REJECT
// For fast sight rejection.
// Speeds up enemy AI by skipping detailed
// LineOf Sight calculation.
// Without special effect, this could be
// used as a PVS lookup as well.
//
rejectmatrix: PByteArray;
p_justspawned: boolean = false;
const
// Maintain single and multi player starting spots.
MAX_DEATHMATCH_STARTS = 10;
var
deathmatchstarts: array[0..MAX_DEATHMATCH_STARTS - 1] of mapthing_t;
deathmatch_p: integer;
playerstarts: array[0..MAXPLAYERS - 1] of mapthing_t;
//==============================================================================
//
// P_GameValidThing
//
//==============================================================================
function P_GameValidThing(const doomdnum: integer): boolean;
implementation
uses
d_player,
z_memory,
m_bbox,
m_rnd,
g_game,
i_system,
w_wad,
info,
info_rnd,
p_local,
p_mobj,
p_tick,
p_telept,
p_spec,
p_switch,
r_data,
r_segs,
r_things,
r_intrpl,
r_externaltextures,
s_sound,
doomstat;
//==============================================================================
//
// P_LoadVertexes
//
//==============================================================================
procedure P_LoadVertexes(lump: integer);
var
data: pointer;
i: integer;
ml: Pmapvertex_t;
li: Pvertex_t;
begin
// Determine number of lumps:
// total lump length / vertex record length.
numvertexes := W_LumpLength(lump) div SizeOf(mapvertex_t);
// Allocate zone memory for buffer.
vertexes := Z_Malloc(numvertexes * SizeOf(vertex_t), PU_LEVEL, nil);
// Load data into cache.
data := W_CacheLumpNum(lump, PU_STATIC);
ml := Pmapvertex_t(data);
// Copy and convert vertex coordinates,
// internal representation as fixed.
li := @vertexes[0];
for i := 0 to numvertexes - 1 do
begin
li.x := ml.x * FRACUNIT;
li.y := ml.y * FRACUNIT;
li.r_x := li.x;
li.r_y := li.y;
inc(ml);
inc(li);
end;
// Free buffer memory.
Z_Free(data);
end;
//==============================================================================
//
// P_LoadSegs
//
//==============================================================================
procedure P_LoadSegs(lump: integer);
var
data: pointer;
i: integer;
ml: Pmapseg_t;
li: Pseg_t;
ldef: Pline_t;
linedef: integer;
side: integer;
sidenum: integer;
begin
numsegs := W_LumpLength(lump) div SizeOf(mapseg_t);
segs := Z_Malloc(numsegs * SizeOf(seg_t), PU_LEVEL, nil);
ZeroMemory(segs, numsegs * SizeOf(seg_t));
data := W_CacheLumpNum(lump, PU_STATIC);
ml := Pmapseg_t(data);
li := @segs[0];
for i := 0 to numsegs - 1 do
begin
li.v1 := @vertexes[ml.v1];
li.v2 := @vertexes[ml.v2];
li.angle := ml.angle * FRACUNIT;
li.offset := ml.offset * FRACUNIT;
linedef := ml.linedef;
ldef := @lines[linedef];
li.linedef := ldef;
side := ml.side;
li.sidedef := @sides[ldef.sidenum[side]];
li.frontsector := li.sidedef.sector;
if ldef.flags and ML_TWOSIDED <> 0 then
begin
sidenum := ldef.sidenum[side xor 1];
if sidenum = -1 then
begin
I_Warning('P_LoadSegs(): Line %d is marked with ML_TWOSIDED flag without backsector'#13#10, [linedef]);
ldef.flags := ldef.flags and not ML_TWOSIDED;
li.backsector := nil;
end
else
li.backsector := sides[sidenum].sector;
end
else
li.backsector := nil;
inc(ml);
inc(li);
end;
Z_Free(data);
end;
//==============================================================================
//
// P_CalcSegs
//
//==============================================================================
procedure P_CalcSegs;
var
i: integer;
li: Pseg_t;
begin
li := @segs[0];
for i := 0 to numsegs - 1 do
begin
R_CalcSeg(li);
inc(li);
end;
end;
//==============================================================================
//
// P_LoadSubsectors
//
//==============================================================================
procedure P_LoadSubsectors(lump: integer);
var
data: pointer;
i: integer;
ms: Pmapsubsector_t;
ss: Psubsector_t;
begin
numsubsectors := W_LumpLength(lump) div SizeOf(mapsubsector_t);
subsectors := Z_Malloc(numsubsectors * SizeOf(subsector_t), PU_LEVEL, nil);
data := W_CacheLumpNum(lump, PU_STATIC);
ms := Pmapsubsector_t(data);
ZeroMemory(subsectors, numsubsectors * SizeOf(subsector_t));
ss := @subsectors[0];
for i := 0 to numsubsectors - 1 do
begin
ss.numlines := ms.numsegs;
ss.firstline := ms.firstseg;
inc(ms);
inc(ss);
end;
Z_Free(data);
end;
//==============================================================================
//
// P_LoadSectors
//
//==============================================================================
procedure P_LoadSectors(lump: integer);
var
data: pointer;
i: integer;
ms: Pmapsector_t;
ss: Psector_t;
begin
numsectors := W_LumpLength(lump) div SizeOf(mapsector_t);
sectors := Z_Malloc(numsectors * SizeOf(sector_t), PU_LEVEL, nil);
ZeroMemory(sectors, numsectors * SizeOf(sector_t));
data := W_CacheLumpNum(lump, PU_STATIC);
ms := Pmapsector_t(data);
ss := @sectors[0];
for i := 0 to numsectors - 1 do
begin
ss.floorheight := ms.floorheight * FRACUNIT;
ss.ceilingheight := ms.ceilingheight * FRACUNIT;
ss.floorpic := R_FlatNumForName(ms.floorpic);
ss.ceilingpic := R_FlatNumForName(ms.ceilingpic);
ss.lightlevel := ms.lightlevel;
ss.special := ms.special;
ss.tag := ms.tag;
ss.thinglist := nil;
// [kb] For R_WiggleFix
ss.cachedheight := 0;
ss.scaleindex := 0;
inc(ms);
inc(ss);
end;
Z_Free (data);
end;
//==============================================================================
//
// P_LoadNodes
//
//==============================================================================
procedure P_LoadNodes(lump: integer);
var
data: pointer;
i: integer;
j: integer;
k: integer;
mn: Pmapnode_t;
no: Pnode_t;
begin
numnodes := W_LumpLength(lump) div SizeOf(mapnode_t);
nodes := Z_Malloc(numnodes * SizeOf(node_t), PU_LEVEL, nil);
data := W_CacheLumpNum(lump, PU_STATIC);
mn := Pmapnode_t(data);
no := @nodes[0];
for i := 0 to numnodes - 1 do
begin
no.x := mn.x * FRACUNIT;
no.y := mn.y * FRACUNIT;
no.dx := mn.dx * FRACUNIT;
no.dy := mn.dy * FRACUNIT;
for j := 0 to 1 do
begin
no.children[j] := mn.children[j];
for k := 0 to 3 do
no.bbox[j, k] := mn.bbox[j, k] * FRACUNIT;
end;
inc(mn);
inc(no);
end;
Z_Free (data);
end;
//==============================================================================
//
// P_GameValidThing
//
//==============================================================================
function P_GameValidThing(const doomdnum: integer): boolean;
begin
result := true;
// Do not spawn cool, new monsters if !commercial
if gamemode <> commercial then
begin
case doomdnum of
68, // Arachnotron
64, // Archvile
88, // Boss Brain
89, // Boss Shooter
69, // Hell Knight
67, // Mancubus
71, // Pain Elemental
65, // Former Human Commando
66, // Revenant
84: // Wolf SS
result := false;
end;
end;
end;
//==============================================================================
//
// P_LoadThings
//
//==============================================================================
procedure P_LoadThings(lump: integer);
var
data: pointer;
i: integer;
mt: Pmapthing_t;
numthings: integer;
begin
data := W_CacheLumpNum(lump, PU_STATIC);
numthings := W_LumpLength(lump) div SizeOf(mapthing_t);
mt := Pmapthing_t(data);
for i := 0 to numthings - 1 do
begin
if P_GameValidThing(mt._type) then // Do spawn all other stuff.
P_SpawnMapThing(mt);
inc(mt);
end;
Z_Free(data);
end;
//==============================================================================
//
// P_LoadLineDefs
// Also counts secret lines for intermissions.
//
//==============================================================================
procedure P_LoadLineDefs(lump: integer);
var
data: pointer;
i: integer;
mld: Pmaplinedef_t;
ld: Pline_t;
v1: Pvertex_t;
v2: Pvertex_t;
begin
numlines := W_LumpLength(lump) div SizeOf(maplinedef_t);
lines := Z_Malloc(numlines * SizeOf(line_t), PU_LEVEL, nil);
ZeroMemory(lines, numlines * SizeOf(line_t));
data := W_CacheLumpNum(lump, PU_STATIC);
mld := Pmaplinedef_t(data);
ld := @lines[0];
for i := 0 to numlines - 1 do
begin
ld.flags := mld.flags;
ld.special := mld.special;
ld.tag := mld.tag;
ld.v1 := @vertexes[mld.v1];
v1 := ld.v1;
ld.v2 := @vertexes[mld.v2];
v2 := ld.v2;
ld.dx := v2.x - v1.x;
ld.dy := v2.y - v1.y;
if ld.dx = 0 then
ld.slopetype := ST_VERTICAL
else if ld.dy = 0 then
ld.slopetype := ST_HORIZONTAL
else
begin
if FixedDiv(ld.dy, ld.dx) > 0 then
ld.slopetype := ST_POSITIVE
else
ld.slopetype := ST_NEGATIVE;
end;
ld.len := Trunc(sqrt(sqr(ld.dx / FRACUNIT) + sqr(ld.dy / FRACUNIT)) * FRACUNIT);
if v1.x < v2.x then
begin
ld.bbox[BOXLEFT] := v1.x;
ld.bbox[BOXRIGHT] := v2.x;
end
else
begin
ld.bbox[BOXLEFT] := v2.x;
ld.bbox[BOXRIGHT] := v1.x;
end;
if v1.y < v2.y then
begin
ld.bbox[BOXBOTTOM] := v1.y;
ld.bbox[BOXTOP] := v2.y;
end
else
begin
ld.bbox[BOXBOTTOM] := v2.y;
ld.bbox[BOXTOP] := v1.y;
end;
ld.sidenum[0] := mld.sidenum[0];
ld.sidenum[1] := mld.sidenum[1];
if ld.sidenum[0] <> -1 then
ld.frontsector := sides[ld.sidenum[0]].sector
else
begin
ld.sidenum[0] := 0;
ld.frontsector := sides[0].sector;
end;
if ld.sidenum[1] <> -1 then
ld.backsector := sides[ld.sidenum[1]].sector
else
ld.backsector := nil;
inc(mld);
inc(ld);
end;
Z_Free (data);
end;
//==============================================================================
//
// P_LoadSideDefs
//
//==============================================================================
procedure P_LoadSideDefs(lump: integer);
var
data: pointer;
i: integer;
msd: Pmapsidedef_t;
sd: Pside_t;
begin
numsides := W_LumpLength(lump) div SizeOf(mapsidedef_t);
sides := Z_Malloc(numsides * SizeOf(side_t), PU_LEVEL, nil);
ZeroMemory(sides, numsides * SizeOf(side_t));
data := W_CacheLumpNum(lump, PU_STATIC);
msd := Pmapsidedef_t(data);
sd := @sides[0];
for i := 0 to numsides - 1 do
begin
sd.textureoffset := msd.textureoffset * FRACUNIT;
sd.rowoffset := msd.rowoffset * FRACUNIT;
sd.toptexture := R_TextureNumForName(msd.toptexture);
sd.bottomtexture := R_TextureNumForName(msd.bottomtexture);
sd.midtexture := R_TextureNumForName(msd.midtexture);
sd.sector := @sectors[msd.sector];
inc(msd);
inc(sd);
end;
Z_Free(data);
end;
//==============================================================================
//
// P_LoadBlockMap
//
//==============================================================================
procedure P_LoadBlockMap(lump: integer);
var
count: integer;
begin
blockmaplump := W_CacheLumpNum(lump, PU_LEVEL);
blockmap := @blockmaplump[4];
bmaporgx := blockmaplump[0] * FRACUNIT;
bmaporgy := blockmaplump[1] * FRACUNIT;
bmapwidth := blockmaplump[2];
bmapheight := blockmaplump[3];
// clear out mobj chains
count := SizeOf(Pmobj_t) * bmapwidth * bmapheight;
blocklinks := Z_Malloc(count, PU_LEVEL, nil);
ZeroMemory(blocklinks, count);
end;
//==============================================================================
//
// P_GroupLines
// Builds sector line lists and subsector sector numbers.
// Finds block bounding boxes for sectors.
//
//==============================================================================
procedure P_GroupLines;
var
linebuffer: Pline_tPArray; // pointer to an array of pointers Pline_t
i: integer;
j: integer;
total: integer;
li: Pline_t;
sector: Psector_t;
psd: Psubsector_t;
seg: Pseg_t;
bbox: array[0..3] of fixed_t;
block: integer;
begin
// look up sector number for each subsector
psd := @subsectors[0];
for i := 0 to numsubsectors - 1 do
begin
seg := @segs[psd.firstline];
psd.sector := seg.sidedef.sector;
inc(psd);
end;
// count number of lines in each sector
total := 0;
for i := 0 to numlines - 1 do
begin
li := @lines[i];
inc(total);
if li.frontsector <> nil then
li.frontsector.linecount := li.frontsector.linecount + 1;
if (li.backsector <> nil) and (li.backsector <> li.frontsector) then
begin
li.backsector.linecount := li.backsector.linecount + 1;
inc(total);
end;
end;
// build line tables for each sector
linebuffer := Z_Malloc(total * SizeOf(Pline_t), PU_LEVEL, nil);
sector := @sectors[0];
for i := 0 to numsectors - 1 do
begin
M_ClearBox(@bbox);
sector.lines := linebuffer;
li := @lines[0];
for j := 0 to numlines - 1 do
begin
if (li.frontsector = sector) or (li.backsector = sector) then
begin
linebuffer[0] := li;
linebuffer := @linebuffer[1];
M_AddToBox(@bbox, li.v1.x, li.v1.y);
M_AddToBox(@bbox, li.v2.x, li.v2.y);
end;
inc(li);
end;
if pDiff(linebuffer, sector.lines, SizeOf(pointer)) <> sector.linecount then
I_Error('P_GroupLines(): miscounted');
// set the degenmobj_t to the middle of the bounding box
sector.soundorg.x := (bbox[BOXRIGHT] + bbox[BOXLEFT]) div 2;
sector.soundorg.y := (bbox[BOXTOP] + bbox[BOXBOTTOM]) div 2;
// adjust bounding box to map blocks
block := MapBlockInt(bbox[BOXTOP] - bmaporgy + MAXRADIUS);
if block >= bmapheight then
block := bmapheight - 1;
sector.blockbox[BOXTOP] := block;
block := MapBlockInt(bbox[BOXBOTTOM] - bmaporgy - MAXRADIUS);
if block < 0 then
block := 0;
sector.blockbox[BOXBOTTOM] := block;
block := MapBlockInt(bbox[BOXRIGHT] - bmaporgx + MAXRADIUS);
if block >= bmapwidth then
block := bmapwidth - 1;
sector.blockbox[BOXRIGHT] := block;
block := MapBlockInt(bbox[BOXLEFT] - bmaporgx - MAXRADIUS);
if block < 0 then
block := 0;
sector.blockbox[BOXLEFT] := block;
inc(sector);
end;
end;
//==============================================================================
//
// P_GetMapName
//
//==============================================================================
function P_GetMapName(const episode, map: integer): string;
begin
// find map name
if gamemode = commercial then
begin
if map < 10 then
sprintf(result,'MAP0%d', [map])
else
sprintf(result,'MAP%d', [map]);
end
else
sprintf(result, 'E%dM%d', [episode, map]);
end;
//==============================================================================
// P_RemoveSlimeTrails
//
// killough 10/98
//
// Remove slime trails.
//
// Slime trails are inherent to Doom's coordinate system -- i.e. there is
// nothing that a node builder can do to prevent slime trails ALL of the time,
// because it's a product of the integer coodinate system, and just because
// two lines pass through exact integer coordinates, doesn't necessarily mean
// that they will intersect at integer coordinates. Thus we must allow for
// fractional coordinates if we are to be able to split segs with node lines,
// as a node builder must do when creating a BSP tree.
//
// A wad file does not allow fractional coordinates, so node builders are out
// of luck except that they can try to limit the number of splits (they might
// also be able to detect the degree of roundoff error and try to avoid splits
// with a high degree of roundoff error). But we can use fractional coordinates
// here, inside the engine. It's like the difference between square inches and
// square miles, in terms of granularity.
//
// For each vertex of every seg, check to see whether it's also a vertex of
// the linedef associated with the seg (i.e, it's an endpoint). If it's not
// an endpoint, and it wasn't already moved, move the vertex towards the
// linedef by projecting it using the law of cosines. Formula:
//
// 2 2 2 2
// dx x0 + dy x1 + dx dy (y0 - y1) dy y0 + dx y1 + dx dy (x0 - x1)
// {---------------------------------, ---------------------------------}
// 2 2 2 2
// dx + dy dx + dy
//
// (x0,y0) is the vertex being moved, and (x1,y1)-(x1+dx,y1+dy) is the
// reference linedef.
//
// Segs corresponding to orthogonal linedefs (exactly vertical or horizontal
// linedefs), which comprise at least half of all linedefs in most wads, don't
// need to be considered, because they almost never contribute to slime trails
// (because then any roundoff error is parallel to the linedef, which doesn't
// cause slime). Skipping simple orthogonal lines lets the code finish quicker.
//
// Please note: This section of code is not interchangable with TeamTNT's
// code which attempts to fix the same problem.
//
// Firelines (TM) is a Rezistered Trademark of MBF Productions
//
//==============================================================================
procedure P_RemoveSlimeTrails; // killough 10/98
var
hit: PByteArray;
i: integer;
l: Pline_t;
v: Pvertex_t;
v_id: integer;
dx2, dy2, dxy, s: int64;
x0, y0, x1, y1: integer;
begin
hit := mallocz(numvertexes); // Hitlist for vertices
for i := 0 to numsegs - 1 do // Go through each seg
begin
l := segs[i].linedef; // The parent linedef
if (l.dx <> 0) and (l.dy <> 0) then // We can ignore orthogonal lines
begin
v := segs[i].v1;
while true do
begin
v_id := pDiff(v, vertexes, SizeOf(vertex_t));
if hit[v_id] = 0 then // If we haven't processed vertex
begin
hit[v_id] := 1; // Mark this vertex as processed
if (v <> l.v1) and (v <> l.v2) then // Exclude endpoints of linedefs
begin // Project the vertex back onto the parent linedef
dx2 := (l.dx div FRACUNIT) * (l.dx div FRACUNIT);
dy2 := (l.dy div FRACUNIT) * (l.dy div FRACUNIT);
dxy := (l.dx div FRACUNIT) * (l.dy div FRACUNIT);
s := dx2 + dy2;
x0 := v.x;
y0 := v.y;
x1 := l.v1.x;
y1 := l.v1.y;
v.r_x := Round((dx2 * x0 + dy2 * x1 + dxy * (y0 - y1)) / s);
v.r_y := Round((dy2 * y0 + dx2 * y1 + dxy * (x0 - x1)) / s);
// [crispy] wait a minute... moved more than 8 map units?
// maybe that's a linguortal then, back to the original coordinates
// https://www.doomworld.com/vb/doom-editing/74354-stupid-bsp-tricks
if (abs(v.r_x - v.x) > 8 * FRACUNIT) or (abs(v.r_y - v.y) > 8 * FRACUNIT) then
begin
v.r_x := v.x;
v.r_y := v.y;
end;
end;
end;
if v = segs[i].v2 then
break;
v := segs[i].v2;
end;
end;
end;
memfree(hit, numvertexes);
end;
//==============================================================================
//
// P_SetupLevel
//
//==============================================================================
procedure P_SetupLevel(episode, map: integer);
var
i: integer;
lumpname: string;
lumpnum: integer;
begin
totalkills := 0;
totalitems := 0;
totalsecret := 0;
isgamefreezed := false;
if not preparingdemoplayback then
rnd_monster_seed := I_Random;
wminfo.maxfrags := 0;
wminfo.partime := 180;
for i := 0 to MAXPLAYERS - 1 do
begin
players[i].killcount := 0;
players[i].secretcount := 0;
players[i].itemcount := 0;
end;
ZeroMemory(@teleporttics, SizeOf(teleporttics));
// ZeroMemory(@psprdefs, SizeOf(psprdefs));
// Initial height of PointOfView
// will be set by player think.
players[consoleplayer].viewz := 1;
// Make sure all sounds are stopped before Z_FreeTags.
S_Start;
Z_FreeTags(PU_LEVEL, PU_PURGELEVEL - 1);
R_SetupLevel;
P_InitThinkers;
// if working with a devlopment map, reload it
W_Reload;
// find map name
lumpname := P_GetMapName(episode, map);
printf(#13#10'-------------'#13#10);
printf('Loading %s'#13#10, [lumpname]);
lumpnum := W_GetNumForName(lumpname);
leveltime := 0;
// note: most of this ordering is important
P_LoadBlockMap(lumpnum + Ord(ML_BLOCKMAP));
P_LoadVertexes(lumpnum + Ord(ML_VERTEXES));
P_LoadSectors(lumpnum + Ord(ML_SECTORS));
P_LoadSideDefs(lumpnum + Ord(ML_SIDEDEFS));
P_LoadLineDefs(lumpnum + Ord(ML_LINEDEFS));
P_LoadSubsectors(lumpnum + Ord(ML_SSECTORS));
P_LoadNodes(lumpnum + Ord(ML_NODES));
P_LoadSegs(lumpnum + Ord(ML_SEGS));
rejectmatrix := W_CacheLumpNum(lumpnum + Ord(ML_REJECT), PU_LEVEL);
P_GroupLines;
P_RemoveSlimeTrails; // killough 10/98: remove slime trails from wad
P_CalcSegs;
bodyqueslot := 0;
deathmatch_p := 0;
P_LoadThings(lumpnum + Ord(ML_THINGS));
// if deathmatch, randomly spawn the active players
if deathmatch <> 0 then
begin
for i := 0 to MAXPLAYERS - 1 do
if playeringame[i] then
begin
players[i].mo := nil;
G_DeathMatchSpawnPlayer(i);
end;
end;
// clear special respawning que
iquehead := 0;
iquetail := 0;
// set up world state
P_SpawnSpecials;
R_Clear32Cache;
// preload graphics
// JVAL
// Precache if we have external textures
if precache or externalpakspresent then
R_PrecacheLevel;
R_SetInterpolateSkipTicks(2);
end;
//==============================================================================
//
// P_Init
//
//==============================================================================
procedure P_Init;
begin
P_InitSwitchList;
P_InitPicAnims;
R_InitSprites(sprnames);
end;
end.