-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathp_maputl.c
More file actions
612 lines (505 loc) · 12.4 KB
/
p_maputl.c
File metadata and controls
612 lines (505 loc) · 12.4 KB
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
/* P_maputl.c */
#include "doomdef.h"
#include "p_local.h"
fixed_t P_AproxDistance(fixed_t dx, fixed_t dy) ATTR_DATA_CACHE_ALIGN;
int P_PointOnLineSide(fixed_t x, fixed_t y, line_t* line) ATTR_DATA_CACHE_ALIGN;
boolean P_BoxCrossLine(line_t *ld, fixed_t testbbox[4]) ATTR_DATA_CACHE_ALIGN;
int P_PointOnDivlineSide(fixed_t x, fixed_t y, divline_t* line) ATTR_DATA_CACHE_ALIGN;
fixed_t P_LineOpening(line_t* linedef) ATTR_DATA_CACHE_ALIGN;
void P_LineBBox(line_t* ld, fixed_t* bbox) ATTR_DATA_CACHE_ALIGN;
void P_UnsetThingPosition(mobj_t* thing) ATTR_DATA_CACHE_ALIGN;
void P_SetThingPosition(mobj_t* thing) ATTR_DATA_CACHE_ALIGN;
void P_SetThingPosition2(mobj_t* thing, subsector_t *ss) ATTR_DATA_CACHE_ALIGN;
boolean P_BlockLinesIterator(int x, int y, boolean(*func)(line_t*, void*), void *userp) ATTR_DATA_CACHE_ALIGN;
boolean P_BlockThingsIterator(int x, int y, boolean(*func)(mobj_t*, void*), void *userp) ATTR_DATA_CACHE_ALIGN;
/*
===================
=
= P_AproxDistance
=
= Gives an estimation of distance (not exact)
=
===================
*/
fixed_t P_AproxDistance (fixed_t dx, fixed_t dy)
{
dx = D_abs(dx);
dy = D_abs(dy);
if (dx < dy)
return dx+dy-(dx>>1);
return dx+dy-(dy>>1);
}
/*
==================
=
= P_PointOnLineSide
=
= Returns 0 or 1
==================
*/
int P_PointOnLineSide (fixed_t x, fixed_t y, line_t *line)
{
fixed_t dx,dy;
fixed_t left, right;
fixed_t ldx,ldy;
ldx = vertexes[line->v2].x - vertexes[line->v1].x;
ldy = vertexes[line->v2].y - vertexes[line->v1].y;
dx = x - (vertexes[line->v1].x << FRACBITS);
dy = y - (vertexes[line->v1].y << FRACBITS);
#ifdef MARS
dx = (unsigned)dx >> FRACBITS;
__asm volatile(
"muls.w %0,%1\n\t"
: : "r"(ldy), "r"(dx) : "macl", "mach");
#else
left = (ldy) * (dx>>16);
#endif
#ifdef MARS
dy = (unsigned)dy >> FRACBITS;
__asm volatile(
"sts macl, %0\n\t"
"muls.w %2,%3\n\t"
"sts macl, %1\n\t"
: "=&r"(left), "=&r"(right) : "r"(dy), "r"(ldx) : "macl", "mach");
#else
right = (dy>>16) * (ldx);
#endif
if (right < left)
return 0; /* front side */
return 1; /* back side */
}
//
// Check if the thing intersects a linedef
//
boolean P_BoxCrossLine(line_t *ld, fixed_t testbbox[4])
{
fixed_t x1, x2;
fixed_t lx, ly;
fixed_t ldx, ldy;
fixed_t dx1, dy1, dx2, dy2;
boolean side1, side2;
fixed_t ldbbox[4];
P_LineBBox(ld, ldbbox);
// entirely outside bounding box of line?
if(testbbox[BOXRIGHT ] <= ldbbox[BOXLEFT ] ||
testbbox[BOXLEFT ] >= ldbbox[BOXRIGHT ] ||
testbbox[BOXTOP ] <= ldbbox[BOXBOTTOM] ||
testbbox[BOXBOTTOM] >= ldbbox[BOXTOP ])
{
return false;
}
lx = vertexes[ld->v1].x << FRACBITS;
ly = vertexes[ld->v1].y << FRACBITS;
ldx = vertexes[ld->v2].x - vertexes[ld->v1].x;
ldy = vertexes[ld->v2].y - vertexes[ld->v1].y;
if (ldx && ldy && ( (ldx ^ ldy) & 0x80000000 ) == 0) /* positive */
{
x1 = testbbox[BOXLEFT ];
x2 = testbbox[BOXRIGHT];
}
else
{
x1 = testbbox[BOXRIGHT];
x2 = testbbox[BOXLEFT ];
}
dx1 = (x1 - lx) >> FRACBITS;
dy1 = (testbbox[BOXTOP] - ly) >> FRACBITS;
dx2 = (x2 - lx) >> FRACBITS;
dy2 = (testbbox[BOXBOTTOM] - ly) >> FRACBITS;
side1 = (ldy * dx1 < dy1 * ldx);
side2 = (ldy * dx2 < dy2 * ldx);
return (side1 != side2);
}
/*
==================
=
= P_PointOnDivlineSide
=
= Returns 0 or 1
==================
*/
int P_PointOnDivlineSide (fixed_t x, fixed_t y, divline_t *line)
{
fixed_t dx,dy;
fixed_t left, right;
dx = (x - line->x);
dy = (y - line->y);
#ifndef MARS
/* try to quickly decide by looking at sign bits */
if ( (line->dy ^ line->dx ^ dx ^ dy)&0x80000000 )
{
if ( (line->dy ^ dx) & 0x80000000 )
return 1; /* (left is negative) */
return 0;
}
#endif
left = FixedMul(line->dy>>8, dx>>8);
right = FixedMul(dy>>8, line->dx>>8);
if (right < left)
return 0; /* front side */
return 1; /* back side */
}
/*
==================
=
= P_LineOpening
=
= Sets opentop and openbottom to the window through a two sided line
= OPTIMIZE: keep this precalculated
==================
*/
fixed_t P_LineOpening (line_t *linedef)
{
sector_t *front, *back;
fixed_t opentop, openbottom;
if (linedef->sidenum[1] < 0)
{ /* single sided line */
return 0;
}
front = LD_FRONTSECTOR(linedef);
back = LD_BACKSECTOR(linedef);
if (front->ceilingheight < back->ceilingheight)
opentop = front->ceilingheight;
else
opentop = back->ceilingheight;
if (front->floorheight > back->floorheight)
openbottom = front->floorheight;
else
openbottom = back->floorheight;
return opentop - openbottom;
}
void P_LineBBox(line_t* ld, fixed_t *bbox)
{
mapvertex_t* v1 = &vertexes[ld->v1], * v2 = &vertexes[ld->v2];
if (v1->x < v2->x)
{
bbox[BOXLEFT] = v1->x;
bbox[BOXRIGHT] = v2->x;
}
else
{
bbox[BOXLEFT] = v2->x;
bbox[BOXRIGHT] = v1->x;
}
if (v1->y < v2->y)
{
bbox[BOXBOTTOM] = v1->y;
bbox[BOXTOP] = v2->y;
}
else
{
bbox[BOXBOTTOM] = v2->y;
bbox[BOXTOP] = v1->y;
}
bbox[BOXTOP] <<= FRACBITS;
bbox[BOXBOTTOM] <<= FRACBITS;
bbox[BOXLEFT] <<= FRACBITS;
bbox[BOXRIGHT] <<= FRACBITS;
}
/*
===============================================================================
THING POSITION SETTING
===============================================================================
*/
/*
===================
=
= P_UnsetThingPosition
=
= Unlinks a thing from block map and sectors
=
===================
*/
void P_UnsetThingPosition (mobj_t *thing)
{
int blockx, blocky;
if ( ! (thing->flags & MF_NOSECTOR) )
{ /* inert things don't need to be in blockmap */
/* unlink from subsector */
mobj_t *snext = SPTR_TO_LPTR(thing->snext);
mobj_t *sprev = SPTR_TO_LPTR(thing->sprev);
if (snext)
snext->sprev = thing->sprev;
if (sprev)
sprev->snext = thing->snext;
else
SSEC_SECTOR(thing->subsector)->thinglist = thing->snext;
}
if ( ! (thing->flags & MF_NOBLOCKMAP) )
{ /* inert things don't need to be in blockmap */
/* unlink from block map */
mobj_t *bnext = SPTR_TO_LPTR(thing->bnext);
mobj_t *bprev = SPTR_TO_LPTR(thing->bprev);
if (bnext)
bnext->bprev = thing->bprev;
if (bprev)
bprev->bnext = thing->bnext;
else
{
blockx = thing->x - bmaporgx;
blocky = thing->y - bmaporgy;
if (blockx >= 0 && blocky >= 0)
{
blockx = (unsigned)blockx >> MAPBLOCKSHIFT;
blocky = (unsigned)blocky >> MAPBLOCKSHIFT;
if (blockx < bmapwidth && blocky <bmapheight)
blocklinks[blocky*bmapwidth+blockx] = thing->bnext;
}
}
}
}
/*
===================
=
= P_SetThingPosition2
=
= Links a thing into both a block and a subsector based on it's x y
=
===================
*/
void P_SetThingPosition2 (mobj_t *thing, subsector_t *ss)
{
sector_t *sec;
int blockx, blocky;
SPTR *link;
/* */
/* link into subsector */
/* */
thing->subsector = ss;
if ( ! (thing->flags & MF_NOSECTOR) )
{ /* invisible things don't go into the sector links */
sec = SSEC_SECTOR(ss);
thing->sprev = (SPTR)0;
thing->snext = sec->thinglist;
if (sec->thinglist)
((mobj_t *)SPTR_TO_LPTR(sec->thinglist))->sprev = LPTR_TO_SPTR(thing);
sec->thinglist = LPTR_TO_SPTR(thing);
}
/* */
/* link into blockmap */
/* */
if ( ! (thing->flags & MF_NOBLOCKMAP) )
{ /* inert things don't need to be in blockmap */
blockx = thing->x - bmaporgx;
blocky = thing->y - bmaporgy;
if (blockx>=0 && blocky>=0)
{
blockx = (unsigned)blockx >> MAPBLOCKSHIFT;
blocky = (unsigned)blocky >> MAPBLOCKSHIFT;
if (blockx < bmapwidth && blocky <bmapheight)
{
link = &blocklinks[blocky*bmapwidth+blockx];
thing->bprev = (SPTR)0;
thing->bnext = *link;
if (*link)
((mobj_t *)(SPTR_TO_LPTR(*link)))->bprev = LPTR_TO_SPTR(thing);
*link = LPTR_TO_SPTR(thing);
}
else
{ /* thing is off the map */
thing->bnext = thing->bprev = (SPTR)0;
}
}
else
{ /* thing is off the map */
thing->bnext = thing->bprev = (SPTR)0;
}
}
}
/*
===================
=
= P_SetThingPosition
=
= Links a thing into both a block and a subsector based on it's x y
= Sets thing->subsector properly
=
===================
*/
void P_SetThingPosition (mobj_t *thing)
{
P_SetThingPosition2(thing, R_PointInSubsector (thing->x,thing->y));
}
/*
===============================================================================
BLOCK MAP ITERATORS
For each line/thing in the given mapblock, call the passed function.
If the function returns false, exit with false without checking anything else.
===============================================================================
*/
/*
==================
=
= P_BlockLinesIterator
=
= The validcount flags are used to avoid checking lines
= that are marked in multiple mapblocks, so increment validcount before
= the first call to P_BlockLinesIterator, then make one or more calls to it
===================
*/
boolean P_BlockLinesIterator (int x, int y, blocklinesiter_t func, void *userp )
{
int offset;
short *list;
line_t *ld;
VINT *lvalidcount, vc;
//if (x<0 || y<0 || x>=bmapwidth || y>=bmapheight)
// return true;
offset = y*bmapwidth+x;
offset = *(blockmaplump+4+offset);
I_GetThreadLocalVar(DOOMTLS_VALIDCOUNT, lvalidcount);
vc = *lvalidcount;
++lvalidcount;
for ( list = blockmaplump+offset ; *list != -1 ; list++)
{
int l = *list;
ld = &lines[*list];
if (lvalidcount[l] == vc)
continue; /* line has already been checked */
lvalidcount[l] = vc;
if ( !func(ld, userp) )
return false;
}
return true; /* everything was checked */
}
/*
==================
=
= P_BlockThingsIterator
=
==================
*/
boolean P_BlockThingsIterator (int x, int y, blockthingsiter_t func, void *userp )
{
mobj_t *mobj;
//if (x<0 || y<0 || x>=bmapwidth || y>=bmapheight)
// return true;
for (mobj = SPTR_TO_LPTR(blocklinks[y*bmapwidth+x]) ; mobj ; mobj = SPTR_TO_LPTR(mobj->bnext))
if (!func( mobj, userp ) )
return false;
return true;
}
void P_SectorBBox(sector_t* sector, fixed_t *bbox)
{
int j;
if (!sector)
return;
M_ClearBox (bbox);
for (j=0 ; j<sector->linecount ; j++)
{
line_t *li = lines + sector->lines[j];
M_AddToBox (bbox, vertexes[li->v1].x, vertexes[li->v1].y);
M_AddToBox (bbox, vertexes[li->v2].x, vertexes[li->v2].y);
}
for (j=0 ; j<4 ; j++)
bbox[j] = bbox[j] << FRACBITS;
}
void P_SectorOrg(sector_t* sector, fixed_t *org)
{
fixed_t bbox[4];
if (!sector)
return;
P_SectorBBox(sector, bbox);
/* set the degenmobj_t to the middle of the bounding box */
org[0] = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
org[1] = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
}
int P_GetTag (int objnum, VINT *tags, int numtags)
{
VINT j;
VINT rowsize = (unsigned)numtags / LINETAGS_HASH_SIZE;
VINT ld = objnum;
VINT h = (unsigned)ld % LINETAGS_HASH_SIZE;
VINT s = h * rowsize;
for (j = 0; j < numtags; j++)
{
VINT *l;
VINT e;
e = s + j;
if (e >= numtags)
e -= numtags;
l = &tags[e * 2];
if (l[0] == ld) {
return l[1];
}
}
return 0;
}
void P_SetTag (int objnum, int tag, VINT *tags, int numtags)
{
VINT j;
VINT rowsize = (unsigned)numtags / LINETAGS_HASH_SIZE;
VINT ld = objnum;
VINT h = (unsigned)ld % LINETAGS_HASH_SIZE;
VINT s = h * rowsize;
for (j = 0; j < numtags; j++)
{
VINT *l;
VINT e;
e = s + j;
if (e >= numtags)
e -= numtags;
l = &tags[e * 2];
if (l[0] == -1 || l[0] == ld) {
l[0] = ld;
l[1] = tag;
break;
}
}
}
int P_IterateTag (int tag, VINT *tags, int numtags, int *start)
{
VINT j;
if (*start < 0)
return -1;
for (j = *start; j < numtags; j++)
{
VINT *l;
l = &tags[j * 2];
if (l[1] == tag) {
*start = j + 1;
return l[0];
}
}
*start = numtags;
return -1;
}
int P_GetLineTag (line_t *line)
{
return P_GetTag(line - lines, linetags, numlinetags);
}
void P_SetLineTag (int ld, int tag)
{
P_SetTag(ld, tag, linetags, numlinetags);
}
int P_GetSectorTag (sector_t *sec)
{
return P_GetTag(sec - sectors, sectortags, numsectortags);
}
void P_SetSectorTag (int sec, int tag)
{
P_SetTag(sec, tag, sectortags, numsectortags);
}
int P_FindNextSectorByTagNum(int tag, int *start)
{
return P_IterateTag(tag, sectortags, numsectortags, start);
}
void P_StartSectorSound(sector_t* sec, int sound_id)
{
fixed_t org[2];
if (!sec)
return;
P_SectorOrg(sec, org);
S_StartFixedSound(sound_id,org);
}
void P_MoverSound(mover_t* mover, int sound_id)
{
fixed_t *bbox = mover->secbbox;
fixed_t org[2];
if (!mover)
return;
/* set the degenmobj_t to the middle of the bounding box */
org[0] = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
org[1] = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
S_StartFixedSound(sound_id, org);
}