Debian Codegraph @

prboom2/src/p_maputl.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 * Movement/collision utility functions,
31 * as used by function in p_map.c.
32 * BLOCKMAP Iterator functions,
33 * and some PIT_* functions to use for iteration.
34 *
35 *-----------------------------------------------------------------------------*/
36
37 #include "doomstat.h"
38 #include "doomtype.h"
39 #include "m_bbox.h"
40 #include "r_main.h"
41 #include "p_maputl.h"
42 #include "p_map.h"
43 #include "p_setup.h"
44 #include "lprintf.h"
45 #include "g_game.h"
46 #include "g_overflow.h"
47 #include "e6y.h"//e6y
48
49 #include "dsda/map_format.h"
50
51 //
52 // P_AproxDistance
53 // Gives an estimation of distance (not exact)
54 //
55
56 fixed_t CONSTFUNC P_AproxDistance(fixed_t dx, fixed_t dy)
57 {
58 dx = D_abs(dx);
59 dy = D_abs(dy);
60 if (dx < dy)
61 return dx+dy-(dx>>1);
62 return dx+dy-(dy>>1);
63 }
64
65 //
66 // P_PointOnLineSide
67 // Returns 0 or 1
68 //
69 // killough 5/3/98: reformatted, cleaned up
70
71 int PUREFUNC P_CompatiblePointOnLineSide(fixed_t x, fixed_t y, const line_t *line)
72 {
73 return
74 !line->dx ? x <= line->v1->x ? line->dy > 0 : line->dy < 0 :
75 !line->dy ? y <= line->v1->y ? line->dx < 0 : line->dx > 0 :
76 FixedMul(y-line->v1->y, line->dx>>FRACBITS) >=
77 FixedMul(line->dy>>FRACBITS, x-line->v1->x);
78 }
79
80 int PUREFUNC P_ZDoomPointOnLineSide(fixed_t x, fixed_t y, const line_t *line)
81 {
82 return
83 !line->dx ? x <= line->v1->x ? line->dy > 0 : line->dy < 0 :
84 !line->dy ? y <= line->v1->y ? line->dx < 0 : line->dx > 0 :
85 ((long long) y - line->v1->y) * line->dx >= ((long long) x - line->v1->x) * line->dy;
86 }
87
88 int (*P_PointOnLineSide)(fixed_t x, fixed_t y, const line_t *line);
89
90 //
91 // P_BoxOnLineSide
92 // Considers the line to be infinite
93 // Returns side 0 or 1, -1 if box crosses the line.
94 //
95 // killough 5/3/98: reformatted, cleaned up
96
97 int PUREFUNC P_BoxOnLineSide(const fixed_t *tmbox, const line_t *ld)
98 {
99 switch (ld->slopetype)
100 {
101 int p;
102 default: // shut up compiler warnings -- killough
103 case ST_HORIZONTAL:
104 return
105 (tmbox[BOXBOTTOM] > ld->v1->y) == (p = tmbox[BOXTOP] > ld->v1->y) ?
106 p ^ (ld->dx < 0) : -1;
107 case ST_VERTICAL:
108 return
109 (tmbox[BOXLEFT] < ld->v1->x) == (p = tmbox[BOXRIGHT] < ld->v1->x) ?
110 p ^ (ld->dy < 0) : -1;
111 case ST_POSITIVE:
112 return
113 P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXBOTTOM], ld) ==
114 (p = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXTOP], ld)) ? p : -1;
115 case ST_NEGATIVE:
116 return
117 (P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXBOTTOM], ld)) ==
118 (p = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXTOP], ld)) ? p : -1;
119 }
120 }
121
122 //
123 // P_PointOnDivlineSide
124 // Returns 0 or 1.
125 //
126 // killough 5/3/98: reformatted, cleaned up
127
128 int PUREFUNC P_CompatiblePointOnDivlineSide(fixed_t x, fixed_t y, const divline_t *line)
129 {
130 return
131 !line->dx ? x <= line->x ? line->dy > 0 : line->dy < 0 :
132 !line->dy ? y <= line->y ? line->dx < 0 : line->dx > 0 :
133 (line->dy^line->dx^(x -= line->x)^(y -= line->y)) < 0 ? (line->dy^x) < 0 :
134 FixedMul(y>>8, line->dx>>8) >= FixedMul(line->dy>>8, x>>8);
135 }
136
137 int PUREFUNC P_ZDoomPointOnDivlineSide(fixed_t x, fixed_t y, const divline_t *line)
138 {
139 return
140 !line->dx ? x <= line->x ? line->dy > 0 : line->dy < 0 :
141 !line->dy ? y <= line->y ? line->dx < 0 : line->dx > 0 :
142 (line->dy^line->dx^(x -= line->x)^(y -= line->y)) < 0 ? (line->dy^x) < 0 :
143 (long long) y * line->dx >= (long long) x * line->dy;
144 }
145
146 int (*P_PointOnDivlineSide)(fixed_t x, fixed_t y, const divline_t *line);
147
148 //
149 // P_MakeDivline
150 //
151
152 void P_MakeDivline(const line_t *li, divline_t *dl)
153 {
154 dl->x = li->v1->x;
155 dl->y = li->v1->y;
156 dl->dx = li->dx;
157 dl->dy = li->dy;
158 }
159
160 //
161 // P_InterceptVector
162 // Returns the fractional intercept point
163 // along the first divline.
164 // This is only called by the addthings
165 // and addlines traversers.
166 //
167
168 /* cph - this is killough's 4/19/98 version of P_InterceptVector and
169 * P_InterceptVector2 (which were interchangeable). We still use this
170 * in compatibility mode. */
171 fixed_t PUREFUNC P_InterceptVector2(const divline_t *v2, const divline_t *v1)
172 {
173 fixed_t den;
174 return (den = FixedMul(v1->dy>>8, v2->dx) - FixedMul(v1->dx>>8, v2->dy)) ?
175 FixedDiv(FixedMul((v1->x - v2->x)>>8, v1->dy) +
176 FixedMul((v2->y - v1->y)>>8, v1->dx), den) : 0;
177 }
178
179 fixed_t PUREFUNC P_InterceptVector(const divline_t *v2, const divline_t *v1)
180 {
181 if (compatibility_level < prboom_4_compatibility)
182 return P_InterceptVector2(v2, v1);
183 else {
184 /* cph - This was introduced at prboom_4_compatibility - no precision/overflow problems */
185 int64_t den = (int64_t)v1->dy * v2->dx - (int64_t)v1->dx * v2->dy;
186 den >>= 16;
187 if (!den)
188 return 0;
189 return (fixed_t)(((int64_t)(v1->x - v2->x) * v1->dy - (int64_t)(v1->y - v2->y) * v1->dx) / den);
190 }
191 }
192
193 //
194 // P_LineOpening
195 // Sets line_opening to the window through a two sided line.
196 // OPTIMIZE: keep this precalculated
197 //
198
199 line_opening_t line_opening;
200
201 dboolean P_GetMidTexturePosition(const line_t *line, int sideno, fixed_t *top, fixed_t *bottom)
202 {
203 short texnum;
204 const side_t *side;
205
206 if (line->sidenum[0] == NO_INDEX || line->sidenum[1] == NO_INDEX)
207 {
208 return false;
209 }
210
211 side = &sides[line->sidenum[0]];
212
213 if (!side->midtexture)
214 {
215 return false;
216 }
217
218 texnum = side->midtexture;
219 texnum = texturetranslation[texnum];
220
221 if (line->flags & ML_DONTPEGBOTTOM)
222 {
223 *bottom = side->rowoffset + side->rowoffset_mid +
224 MAX(line->frontsector->floorheight, line->backsector->floorheight);
225 *top = *bottom + textureheight[texnum];
226 }
227 else
228 {
229 *top = side->rowoffset + side->rowoffset_mid +
230 MIN(line->frontsector->ceilingheight, line->backsector->ceilingheight);
231 *bottom = *top - textureheight[texnum];
232 }
233
234 return true;
235 }
236
237 void P_LineOpening_3dMidtex(const line_t *line, const mobj_t *actor)
238 {
239 fixed_t bottom3d, top3d;
240
241 if (line->flags & ML_3DMIDTEXIMPASSIBLE && actor->flags & (MF_MISSILE | MF_BOUNCES))
242 {
243 return;
244 }
245
246 if (!P_GetMidTexturePosition(line, 0, &top3d, &bottom3d))
247 {
248 return;
249 }
250
251 if (actor->z + actor->height / 2 < (top3d + bottom3d) / 2)
252 {
253 line_opening.top = bottom3d;
254 }
255 else
256 {
257 if (top3d > line_opening.bottom)
258 {
259 line_opening.bottom = top3d;
260 line_opening.abovemidtex = true;
261 }
262 line_opening.touchmidtex = (abs(actor->z - top3d) < 24 * FRACUNIT);
263 }
264 }
265
266 void P_LineOpening(const line_t *linedef, const mobj_t *actor)
267 {
268 extern int tmfloorpic;
269
270 if (linedef->sidenum[1] == NO_INDEX) // single sided line
271 {
272 line_opening.range = 0;
273 return;
274 }
275
276 line_opening.frontsector = linedef->frontsector;
277 line_opening.backsector = linedef->backsector;
278
279 if (line_opening.frontsector->ceilingheight < line_opening.backsector->ceilingheight)
280 line_opening.top = line_opening.frontsector->ceilingheight;
281 else
282 line_opening.top = line_opening.backsector->ceilingheight;
283
284 if (line_opening.frontsector->floorheight > line_opening.backsector->floorheight)
285 {
286 line_opening.bottom = line_opening.frontsector->floorheight;
287 line_opening.lowfloor = line_opening.backsector->floorheight;
288 tmfloorpic = line_opening.frontsector->floorpic;
289 }
290 else
291 {
292 line_opening.bottom = line_opening.backsector->floorheight;
293 line_opening.lowfloor = line_opening.frontsector->floorheight;
294 tmfloorpic = line_opening.backsector->floorpic;
295 }
296
297 line_opening.abovemidtex = false;
298 line_opening.touchmidtex = false;
299
300 if (actor && linedef->frontsector && linedef->backsector && linedef->flags & ML_3DMIDTEX)
301 {
302 P_LineOpening_3dMidtex(linedef, actor);
303 }
304
305 line_opening.range = line_opening.top - line_opening.bottom;
306 }
307
308 //
309 // THING POSITION SETTING
310 //
311
312 //
313 // P_UnsetThingPosition
314 // Unlinks a thing from block map and sectors.
315 // On each position change, BLOCKMAP and other
316 // lookups maintaining lists ot things inside
317 // these structures need to be updated.
318 //
319
320 void P_UnsetThingPosition (mobj_t *thing)
321 {
322 if (!(thing->flags & MF_NOSECTOR))
323 {
324 /* invisible things don't need to be in sector list
325 * unlink from subsector
326 *
327 * killough 8/11/98: simpler scheme using pointers-to-pointers for prev
328 * pointers, allows head node pointers to be treated like everything else
329 */
330
331 mobj_t **sprev = thing->sprev;
332 mobj_t *snext = thing->snext;
333 if ((*sprev = snext)) // unlink from sector list
334 snext->sprev = sprev;
335
336 // phares 3/14/98
337 //
338 // Save the sector list pointed to by touching_sectorlist.
339 // In P_SetThingPosition, we'll keep any nodes that represent
340 // sectors the Thing still touches. We'll add new ones then, and
341 // delete any nodes for sectors the Thing has vacated. Then we'll
342 // put it back into touching_sectorlist. It's done this way to
343 // avoid a lot of deleting/creating for nodes, when most of the
344 // time you just get back what you deleted anyway.
345 //
346 // If this Thing is being removed entirely, then the calling
347 // routine will clear out the nodes in sector_list.
348
349 sector_list = thing->touching_sectorlist;
350 thing->touching_sectorlist = NULL; //to be restored by P_SetThingPosition
351 }
352
353 if (!(thing->flags & MF_NOBLOCKMAP))
354 {
355 /* inert things don't need to be in blockmap
356 *
357 * killough 8/11/98: simpler scheme using pointers-to-pointers for prev
358 * pointers, allows head node pointers to be treated like everything else
359 *
360 * Also more robust, since it doesn't depend on current position for
361 * unlinking. Old method required computing head node based on position
362 * at time of unlinking, assuming it was the same position as during
363 * linking.
364 */
365
366 mobj_t *bnext, **bprev = thing->bprev;
367 if (bprev && (*bprev = bnext = thing->bnext)) // unlink from block map
368 bnext->bprev = bprev;
369 }
370 }
371
372 //
373 // P_SetThingPosition
374 // Links a thing into both a block and a subsector
375 // based on it's x y.
376 // Sets thing->subsector properly
377 //
378 // killough 5/3/98: reformatted, cleaned up
379
380 void P_SetThingPosition(mobj_t *thing)
381 { // link into subsector
382 subsector_t *ss = thing->subsector = R_PointInSubsector(thing->x, thing->y);
383 if (!(thing->flags & MF_NOSECTOR))
384 {
385 // invisible things don't go into the sector links
386
387 // killough 8/11/98: simpler scheme using pointer-to-pointer prev
388 // pointers, allows head nodes to be treated like everything else
389
390 mobj_t **link = &ss->sector->thinglist;
391 mobj_t *snext = *link;
392 if ((thing->snext = snext))
393 snext->sprev = &thing->snext;
394 thing->sprev = link;
395 *link = thing;
396
397 // phares 3/16/98
398 //
399 // If sector_list isn't NULL, it has a collection of sector
400 // nodes that were just removed from this Thing.
401
402 // Collect the sectors the object will live in by looking at
403 // the existing sector_list and adding new nodes and deleting
404 // obsolete ones.
405
406 // When a node is deleted, its sector links (the links starting
407 // at sector_t->touching_thinglist) are broken. When a node is
408 // added, new sector links are created.
409
410 P_CreateSecNodeList(thing,thing->x,thing->y);
411 thing->touching_sectorlist = sector_list; // Attach to Thing's mobj_t
412 sector_list = NULL; // clear for next time
413 }
414
415 // link into blockmap
416 if (!(thing->flags & MF_NOBLOCKMAP))
417 {
418 // inert things don't need to be in blockmap
419 int blockx = P_GetSafeBlockX(thing->x - bmaporgx);
420 int blocky = P_GetSafeBlockY(thing->y - bmaporgy);
421 if (blockx>=0 && blockx < bmapwidth && blocky>=0 && blocky < bmapheight)
422 {
423 // killough 8/11/98: simpler scheme using pointer-to-pointer prev
424 // pointers, allows head nodes to be treated like everything else
425
426 mobj_t **link = &blocklinks[blocky*bmapwidth+blockx];
427 mobj_t *bnext = *link;
428 if ((thing->bnext = bnext))
429 bnext->bprev = &thing->bnext;
430 thing->bprev = link;
431 *link = thing;
432 }
433 else // thing is off the map
434 thing->bnext = NULL, thing->bprev = NULL;
435 }
436 }
437
438 //
439 // BLOCK MAP ITERATORS
440 // For each line/thing in the given mapblock,
441 // call the passed PIT_* function.
442 // If the function returns false,
443 // exit with false without checking anything else.
444 //
445
446 //
447 // P_BlockLinesIterator
448 // The validcount flags are used to avoid checking lines
449 // that are marked in multiple mapblocks,
450 // so increment validcount before the first call
451 // to P_BlockLinesIterator, then make one or more calls
452 // to it.
453 //
454 // killough 5/3/98: reformatted, cleaned up
455
456 dboolean P_BlockLinesIterator(int x, int y, dboolean func(line_t*))
457 {
458 int offset;
459 const int *list; // killough 3/1/98: for removal of blockmap limit
460
461 if (x<0 || y<0 || x>=bmapwidth || y>=bmapheight)
462 return true;
463 offset = y*bmapwidth+x;
464
465 if (map_format.polyobjs)
466 {
467 int i;
468 seg_t **tempSeg;
469 polyblock_t *polyLink;
470 extern polyblock_t **PolyBlockMap;
471
472 polyLink = PolyBlockMap[offset];
473 while (polyLink)
474 {
475 if (polyLink->polyobj)
476 {
477 if (polyLink->polyobj->validcount != validcount)
478 {
479 polyLink->polyobj->validcount = validcount;
480 tempSeg = polyLink->polyobj->segs;
481 for (i = 0; i < polyLink->polyobj->numsegs; i++, tempSeg++)
482 {
483 if ((*tempSeg)->linedef->validcount == validcount)
484 {
485 continue;
486 }
487 (*tempSeg)->linedef->validcount = validcount;
488 if (!func((*tempSeg)->linedef))
489 {
490 return false;
491 }
492 }
493 }
494 }
495 polyLink = polyLink->next;
496 }
497 }
498
499 offset = *(blockmap+offset);
500 list = blockmaplump+offset; // original was reading // phares
501 // delmiting 0 as linedef 0 // phares
502
503 // killough 1/31/98: for compatibility we need to use the old method.
504 // Most demos go out of sync, and maybe other problems happen, if we
505 // don't consider linedef 0. For safety this should be qualified.
506
507 // killough 2/22/98: demo_compatibility check
508 // In mbf21, skip if all blocklists start w/ 0 (fixes btsx e2 map 20)
509 if ((!demo_compatibility && !mbf21) || (mbf21 && skipblstart))
510 list++; // skip 0 starting delimiter // phares
511 for ( ; *list != -1 ; list++) // phares
512 {
513 line_t *ld;
514 #ifdef RANGECHECK
515 if(*list < 0 || *list >= numlines)
516 I_Error("P_BlockLinesIterator: index >= numlines");
517 #endif
518 ld = &lines[*list];
519 if (ld->validcount == validcount)
520 continue; // line has already been checked
521 ld->validcount = validcount;
522 if (!func(ld))
523 return false;
524 }
525 return true; // everything was checked
526 }
527
528 // MBF's P_SetThingPosition code injects an increment to validcount
529 // There is a bug in P_CheckPosition where the validcount is not
530 // incremented at the correct time. The bug is exposed in MBF.
531 // Under most cases, this is irrelevant, but sometimes it matters.
532 // Ripper projectiles in mbf21, heretic, and hexen require decoupling.
533 dboolean P_BlockLinesIterator2(int x, int y, dboolean func(line_t*))
534 {
535 int offset;
536 const int *list; // killough 3/1/98: for removal of blockmap limit
537
538 if (x<0 || y<0 || x>=bmapwidth || y>=bmapheight)
539 return true;
540 offset = y*bmapwidth+x;
541
542 if (map_format.polyobjs)
543 {
544 int i;
545 seg_t **tempSeg;
546 polyblock_t *polyLink;
547 extern polyblock_t **PolyBlockMap;
548
549 polyLink = PolyBlockMap[offset];
550 while (polyLink)
551 {
552 if (polyLink->polyobj)
553 {
554 if (polyLink->polyobj->validcount2 != validcount2)
555 {
556 polyLink->polyobj->validcount2 = validcount2;
557 tempSeg = polyLink->polyobj->segs;
558 for (i = 0; i < polyLink->polyobj->numsegs; i++, tempSeg++)
559 {
560 if ((*tempSeg)->linedef->validcount2 == validcount2)
561 {
562 continue;
563 }
564 (*tempSeg)->linedef->validcount2 = validcount2;
565 if (!func((*tempSeg)->linedef))
566 {
567 return false;
568 }
569 }
570 }
571 }
572 polyLink = polyLink->next;
573 }
574 }
575
576 offset = *(blockmap+offset);
577 list = blockmaplump+offset; // original was reading // phares
578 // delmiting 0 as linedef 0 // phares
579
580 // killough 1/31/98: for compatibility we need to use the old method.
581 // Most demos go out of sync, and maybe other problems happen, if we
582 // don't consider linedef 0. For safety this should be qualified.
583
584 // killough 2/22/98: demo_compatibility check
585 // In mbf21, skip if all blocklists start w/ 0 (fixes btsx e2 map 20)
586 if ((!demo_compatibility && !mbf21) || (mbf21 && skipblstart))
587 list++; // skip 0 starting delimiter // phares
588 for ( ; *list != -1 ; list++) // phares
589 {
590 line_t *ld;
591 #ifdef RANGECHECK
592 if(*list < 0 || *list >= numlines)
593 I_Error("P_BlockLinesIterator2: index >= numlines");
594 #endif
595 ld = &lines[*list];
596 if (ld->validcount2 == validcount2)
597 continue; // line has already been checked
598 ld->validcount2 = validcount2;
599 if (!func(ld))
600 return false;
601 }
602 return true; // everything was checked
603 }
604
605 //
606 // P_BlockThingsIterator
607 //
608 // killough 5/3/98: reformatted, cleaned up
609
610 dboolean P_BlockThingsIterator(int x, int y, dboolean func(mobj_t*))
611 {
612 mobj_t *mobj;
613 if (!(x<0 || y<0 || x>=bmapwidth || y>=bmapheight))
614 for (mobj = blocklinks[y*bmapwidth+x]; mobj; mobj = mobj->bnext)
615 if (!func(mobj))
616 return false;
617 return true;
618 }
619
620 //
621 // INTERCEPT ROUTINES
622 //
623
624 // 1/11/98 killough: Intercept limit removed
625 intercept_t *intercepts, *intercept_p;
626
627 // Check for limit and double size if necessary -- killough
628 void check_intercept(void)
629 {
630 static size_t num_intercepts;
631 size_t offset = intercept_p - intercepts;
632 if (offset >= num_intercepts)
633 {
634 num_intercepts = num_intercepts ? num_intercepts*2 : 128;
635 intercepts = Z_Realloc(intercepts, sizeof(*intercepts)*num_intercepts);
636 intercept_p = intercepts + offset;
637 }
638 }
639
640 divline_t trace;
641
642 // PIT_AddLineIntercepts.
643 // Looks for lines in the given block
644 // that intercept the given trace
645 // to add to the intercepts list.
646 //
647 // A line is crossed if its endpoints
648 // are on opposite sides of the trace.
649 //
650 // killough 5/3/98: reformatted, cleaned up
651
652 dboolean PIT_AddLineIntercepts(line_t *ld)
653 {
654 int s1;
655 int s2;
656 fixed_t frac;
657 divline_t dl;
658
659 // avoid precision problems with two routines
660 if (trace.dx > FRACUNIT*16 || trace.dy > FRACUNIT*16 ||
661 trace.dx < -FRACUNIT*16 || trace.dy < -FRACUNIT*16)
662 {
663 s1 = P_PointOnDivlineSide (ld->v1->x, ld->v1->y, &trace);
664 s2 = P_PointOnDivlineSide (ld->v2->x, ld->v2->y, &trace);
665 }
666 else
667 {
668 s1 = P_PointOnLineSide (trace.x, trace.y, ld);
669 s2 = P_PointOnLineSide (trace.x+trace.dx, trace.y+trace.dy, ld);
670 }
671
672 if (s1 == s2)
673 return true; // line isn't crossed
674
675 // hit the line
676 P_MakeDivline(ld, &dl);
677 frac = P_InterceptVector(&trace, &dl);
678
679 if (frac < 0)
680 return true; // behind source
681
682 check_intercept(); // killough
683
684 intercept_p->frac = frac;
685 intercept_p->isaline = true;
686 intercept_p->d.line = ld;
687 InterceptsOverrun(intercept_p - intercepts, intercept_p);//e6y
688 intercept_p++;
689
690 return true; // continue
691 }
692
693 //
694 // PIT_AddThingIntercepts
695 //
696 // killough 5/3/98: reformatted, cleaned up
697
698 dboolean PIT_AddThingIntercepts(mobj_t *thing)
699 {
700 fixed_t x1, y1;
701 fixed_t x2, y2;
702 int s1, s2;
703 divline_t dl;
704 fixed_t frac;
705
706 // check a corner to corner crossection for hit
707 if ((trace.dx ^ trace.dy) > 0)
708 {
709 x1 = thing->x - thing->radius;
710 y1 = thing->y + thing->radius;
711 x2 = thing->x + thing->radius;
712 y2 = thing->y - thing->radius;
713 }
714 else
715 {
716 x1 = thing->x - thing->radius;
717 y1 = thing->y - thing->radius;
718 x2 = thing->x + thing->radius;
719 y2 = thing->y + thing->radius;
720 }
721
722 s1 = P_PointOnDivlineSide (x1, y1, &trace);
723 s2 = P_PointOnDivlineSide (x2, y2, &trace);
724
725 if (s1 == s2)
726 return true; // line isn't crossed
727
728 dl.x = x1;
729 dl.y = y1;
730 dl.dx = x2-x1;
731 dl.dy = y2-y1;
732
733 frac = P_InterceptVector (&trace, &dl);
734
735 if (frac < 0)
736 return true; // behind source
737
738 check_intercept(); // killough
739
740 intercept_p->frac = frac;
741 intercept_p->isaline = false;
742 intercept_p->d.thing = thing;
743 InterceptsOverrun(intercept_p - intercepts, intercept_p);//e6y
744 intercept_p++;
745
746 return true; // keep going
747 }
748
749 //
750 // P_TraverseIntercepts
751 // Returns true if the traverser function returns true
752 // for all lines.
753 //
754 // killough 5/3/98: reformatted, cleaned up
755
756 dboolean P_TraverseIntercepts(traverser_t func, fixed_t maxfrac)
757 {
758 intercept_t *in = NULL;
759 int count = intercept_p - intercepts;
760 while (count--)
761 {
762 fixed_t dist = INT_MAX;
763 intercept_t *scan;
764 for (scan = intercepts; scan < intercept_p; scan++)
765 if (scan->frac < dist)
766 dist = (in=scan)->frac;
767 if (dist > maxfrac)
768 return true; // checked everything in range
769 if (!func(in))
770 return false; // don't bother going farther
771 in->frac = INT_MAX;
772 }
773 return true; // everything was traversed
774 }
775
776 //
777 // P_PathTraverse
778 // Traces a line from x1,y1 to x2,y2,
779 // calling the traverser function for each.
780 // Returns true if the traverser function returns true
781 // for all lines.
782 //
783 // killough 5/3/98: reformatted, cleaned up
784
785 dboolean P_PathTraverse(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2,
786 int flags, dboolean trav(intercept_t *))
787 {
788 fixed_t xt1, yt1;
789 fixed_t xt2, yt2;
790 fixed_t xstep, ystep;
791 fixed_t partial;
792 fixed_t xintercept, yintercept;
793 int mapx, mapy;
794 int mapx1, mapy1;
795 int mapxstep, mapystep;
796 int count;
797
798 validcount++;
799 intercept_p = intercepts;
800
801 if (!((x1-bmaporgx)&(MAPBLOCKSIZE-1)))
802 x1 += FRACUNIT; // don't side exactly on a line
803
804 if (!((y1-bmaporgy)&(MAPBLOCKSIZE-1)))
805 y1 += FRACUNIT; // don't side exactly on a line
806
807 trace.x = x1;
808 trace.y = y1;
809 trace.dx = x2 - x1;
810 trace.dy = y2 - y1;
811
812 if (comperr(comperr_blockmap))
813 {
814 int64_t _x1, _x2, _y1, _y2;
815
816 _x1 = (int64_t)x1 - bmaporgx;
817 _y1 = (int64_t)y1 - bmaporgy;
818 xt1 = (int)(_x1>>MAPBLOCKSHIFT);
819 yt1 = (int)(_y1>>MAPBLOCKSHIFT);
820
821 mapx1 = (int)(_x1>>MAPBTOFRAC);
822 mapy1 = (int)(_y1>>MAPBTOFRAC);
823
824 _x2 = (int64_t)x2 - bmaporgx;
825 _y2 = (int64_t)y2 - bmaporgy;
826 xt2 = (int)(_x2>>MAPBLOCKSHIFT);
827 yt2 = (int)(_y2>>MAPBLOCKSHIFT);
828
829 x1 -= bmaporgx;
830 y1 -= bmaporgy;
831 x2 -= bmaporgx;
832 y2 -= bmaporgy;
833 }
834 else
835 {
836 x1 -= bmaporgx;
837 y1 -= bmaporgy;
838 xt1 = x1>>MAPBLOCKSHIFT;
839 yt1 = y1>>MAPBLOCKSHIFT;
840
841 mapx1 = x1>>MAPBTOFRAC;
842 mapy1 = y1>>MAPBTOFRAC;
843
844 x2 -= bmaporgx;
845 y2 -= bmaporgy;
846 xt2 = x2>>MAPBLOCKSHIFT;
847 yt2 = y2>>MAPBLOCKSHIFT;
848 }
849
850 if (xt2 > xt1)
851 {
852 mapxstep = 1;
853 partial = FRACUNIT - (mapx1&(FRACUNIT-1));
854 ystep = FixedDiv (y2-y1,D_abs(x2-x1));
855 }
856 else
857 if (xt2 < xt1)
858 {
859 mapxstep = -1;
860 partial = mapx1&(FRACUNIT-1);
861 ystep = FixedDiv (y2-y1,D_abs(x2-x1));
862 }
863 else
864 {
865 mapxstep = 0;
866 partial = FRACUNIT;
867 ystep = 256*FRACUNIT;
868 }
869
870 yintercept = mapy1 + FixedMul(partial, ystep);
871
872 if (yt2 > yt1)
873 {
874 mapystep = 1;
875 partial = FRACUNIT - (mapy1&(FRACUNIT-1));
876 xstep = FixedDiv (x2-x1,D_abs(y2-y1));
877 }
878 else
879 if (yt2 < yt1)
880 {
881 mapystep = -1;
882 partial = mapy1&(FRACUNIT-1);
883 xstep = FixedDiv (x2-x1,D_abs(y2-y1));
884 }
885 else
886 {
887 mapystep = 0;
888 partial = FRACUNIT;
889 xstep = 256*FRACUNIT;
890 }
891
892 xintercept = mapx1 + FixedMul(partial, xstep);
893
894 // Step through map blocks.
895 // Count is present to prevent a round off error
896 // from skipping the break.
897
898 mapx = xt1;
899 mapy = yt1;
900
901 for (count = 0; count < 64; count++)
902 {
903 if (flags & PT_ADDLINES)
904 if (!P_BlockLinesIterator(mapx, mapy,PIT_AddLineIntercepts))
905 return false; // early out
906
907 if (flags & PT_ADDTHINGS)
908 if (!P_BlockThingsIterator(mapx, mapy,PIT_AddThingIntercepts))
909 return false; // early out
910
911 if (mapx == xt2 && mapy == yt2)
912 break;
913
914 if ((yintercept >> FRACBITS) == mapy)
915 {
916 yintercept += ystep;
917 mapx += mapxstep;
918 }
919 else
920 if ((xintercept >> FRACBITS) == mapx)
921 {
922 xintercept += xstep;
923 mapy += mapystep;
924 }
925 }
926
927 // go through the sorted list
928 return P_TraverseIntercepts(trav, FRACUNIT);
929 }
930
931 //
932 // RoughBlockCheck
933 // [XA] adapted from Hexen -- used by P_RoughTargetSearch
934 //
935
936 static mobj_t *Hexen_RoughBlockCheck(mobj_t * mo, int index);
937
938 static mobj_t *RoughBlockCheck(mobj_t *mo, int index, angle_t fov)
939 {
940 mobj_t *link;
941
942 if (hexen) return Hexen_RoughBlockCheck(mo, index);
943
944 link = blocklinks[index];
945 while (link)
946 {
947 // skip non-shootable actors
948 if (!(link->flags & MF_SHOOTABLE))
949 {
950 link = link->bnext;
951 continue;
952 }
953
954 // skip dormant actors
955 if (link->flags2 & MF2_DORMANT)
956 {
957 link = link->bnext;
958 continue;
959 }
960
961 // skip the projectile's owner
962 if (link == mo->target)
963 {
964 link = link->bnext;
965 continue;
966 }
967
968 // skip actors on the same "team", unless infighting or deathmatching
969 if (mo->target &&
970 !((link->flags ^ mo->target->flags) & MF_FRIEND) &&
971 mo->target->target != link &&
972 !(deathmatch && link->player && mo->target->player))
973 {
974 link = link->bnext;
975 continue;
976 }
977
978 // skip actors outside of specified FOV
979 if (fov > 0 && !P_CheckFov(mo, link, fov))
980 {
981 link = link->bnext;
982 continue;
983 }
984
985 // skip actors not in line of sight
986 if (!P_CheckSight(mo, link))
987 {
988 link = link->bnext;
989 continue;
990 }
991
992 // all good! return it.
993 return link;
994 }
995
996 // couldn't find a valid target
997 return NULL;
998 }
999
1000 //
1001 // P_RoughTargetSearch
1002 // Searches though the surrounding mapblocks for monsters/players
1003 // based on Hexen's P_RoughMonsterSearch
1004 //
1005 // distance is in MAPBLOCKUNITS
1006
1007 mobj_t *P_RoughTargetSearch(mobj_t *mo, angle_t fov, int distance)
1008 {
1009 int blockX;
1010 int blockY;
1011 int startX, startY;
1012 int blockIndex;
1013 int firstStop;
1014 int secondStop;
1015 int thirdStop;
1016 int finalStop;
1017 int count;
1018 mobj_t *target;
1019
1020 startX = (mo->x - bmaporgx) >> MAPBLOCKSHIFT;
1021 startY = (mo->y - bmaporgy) >> MAPBLOCKSHIFT;
1022
1023 if (startX >= 0 && startX < bmapwidth && startY >= 0 && startY < bmapheight)
1024 {
1025 if ((target = RoughBlockCheck(mo, startY*bmapwidth + startX, fov)))
1026 { // found a target right away
1027 return target;
1028 }
1029 }
1030 for (count = 1; count <= distance; count++)
1031 {
1032 blockX = startX - count;
1033 blockY = startY - count;
1034
1035 if (blockY < 0)
1036 {
1037 blockY = 0;
1038 }
1039 else if (blockY >= bmapheight)
1040 {
1041 blockY = bmapheight - 1;
1042 }
1043 if (blockX < 0)
1044 {
1045 blockX = 0;
1046 }
1047 else if (blockX >= bmapwidth)
1048 {
1049 blockX = bmapwidth - 1;
1050 }
1051 blockIndex = blockY * bmapwidth + blockX;
1052 firstStop = startX + count;
1053 if (firstStop < 0)
1054 {
1055 continue;
1056 }
1057 if (firstStop >= bmapwidth)
1058 {
1059 firstStop = bmapwidth - 1;
1060 }
1061 secondStop = startY + count;
1062 if (secondStop < 0)
1063 {
1064 continue;
1065 }
1066 if (secondStop >= bmapheight)
1067 {
1068 secondStop = bmapheight - 1;
1069 }
1070 thirdStop = secondStop * bmapwidth + blockX;
1071 secondStop = secondStop * bmapwidth + firstStop;
1072 firstStop += blockY * bmapwidth;
1073 finalStop = blockIndex;
1074
1075 // Trace the first block section (along the top)
1076 for (; blockIndex <= firstStop; blockIndex++)
1077 {
1078 if ((target = RoughBlockCheck(mo, blockIndex, fov)))
1079 {
1080 return target;
1081 }
1082 }
1083 // Trace the second block section (right edge)
1084 for (blockIndex--; blockIndex <= secondStop; blockIndex += bmapwidth)
1085 {
1086 if ((target = RoughBlockCheck(mo, blockIndex, fov)))
1087 {
1088 return target;
1089 }
1090 }
1091 // Trace the third block section (bottom edge)
1092 for (blockIndex -= bmapwidth; blockIndex >= thirdStop; blockIndex--)
1093 {
1094 if ((target = RoughBlockCheck(mo, blockIndex, fov)))
1095 {
1096 return target;
1097 }
1098 }
1099 // Trace the final block section (left edge)
1100 for (blockIndex++; blockIndex > finalStop; blockIndex -= bmapwidth)
1101 {
1102 if ((target = RoughBlockCheck(mo, blockIndex, fov)))
1103 {
1104 return target;
1105 }
1106 }
1107 }
1108 return NULL;
1109 }
1110
1111 // MAES: support 512x512 blockmaps.
1112 int P_GetSafeBlockX(int coord)
1113 {
1114 coord >>= MAPBLOCKSHIFT;
1115
1116 // If x is LE than those special values, interpret as positive.
1117 // Otherwise, leave it as it is.
1118 if (comperr(comperr_blockmap) && coord <= blockmapxneg)
1119 return coord & 0x1FF; // Broke width boundary
1120
1121 return coord;
1122 }
1123
1124 // MAES: support 512x512 blockmaps.
1125 int P_GetSafeBlockY(int coord)
1126 {
1127 coord >>= MAPBLOCKSHIFT;
1128
1129 // If y is LE than those special values, interpret as positive.
1130 // Otherwise, leave it as it is.
1131 if (comperr(comperr_blockmap) && coord <= blockmapyneg)
1132 return coord & 0x1FF; // Broke width boundary
1133
1134 return coord;
1135 }
1136
1137 // e6y
1138 //
1139 // Intercepts memory table. This is where various variables are located
1140 // in memory in Vanilla Doom. When the intercepts table overflows, we
1141 // need to write to them.
1142 //
1143 // Almost all of the values to overwrite are 32-bit integers, except for
1144 // playerstarts, which is effectively an array of 16-bit integers and
1145 // must be treated differently.
1146
1147 extern fixed_t bulletslope;
1148
1149 intercepts_overrun_t intercepts_overrun[] =
1150 {
1151 {4, NULL, NULL},
1152 {4, NULL, /* &earlyout, */ NULL},
1153 {4, NULL, /* &intercept_p, */ NULL},
1154 {4, &line_opening.lowfloor, NULL},
1155 {4, &line_opening.bottom, NULL},
1156 {4, &line_opening.top, NULL},
1157 {4, &line_opening.range, NULL},
1158 {4, NULL, NULL},
1159 {120, NULL, /* &activeplats, */ NULL},
1160 {8, NULL, NULL},
1161 {4, &bulletslope, NULL},
1162 {4, NULL, /* &swingx, */ NULL},
1163 {4, NULL, /* &swingy, */ NULL},
1164 {4, NULL, NULL},
1165 {4, &playerstarts[0][0].x, &playerstarts[0][0].y},
1166 {4, &playerstarts[0][0].angle, &playerstarts[0][0].type},
1167 {4, &playerstarts[0][0].options, &playerstarts[0][1].x},
1168 {4, &playerstarts[0][1].y, &playerstarts[0][1].angle},
1169 {4, &playerstarts[0][1].type, &playerstarts[0][1].options},
1170 {4, &playerstarts[0][2].x, &playerstarts[0][2].y},
1171 {4, &playerstarts[0][2].angle, &playerstarts[0][2].type},
1172 {4, &playerstarts[0][2].options, &playerstarts[0][3].x},
1173 {4, &playerstarts[0][3].y, &playerstarts[0][3].angle},
1174 {4, &playerstarts[0][3].type, &playerstarts[0][3].options},
1175 {4, NULL, /* &blocklinks, */ NULL},
1176 {4, &bmapwidth, NULL},
1177 {4, NULL, /* &blockmap, */ NULL},
1178 {4, &bmaporgx, NULL},
1179 {4, &bmaporgy, NULL},
1180 {4, NULL, /* &blockmaplump, */ NULL},
1181 {4, &bmapheight, NULL},
1182 {0, NULL, NULL},
1183 };
1184
1185 // hexen
1186
1187 static mobj_t *Hexen_RoughBlockCheck(mobj_t * mo, int index)
1188 {
1189 mobj_t *link;
1190 mobj_t *master;
1191 angle_t angle;
1192
1193 link = blocklinks[index];
1194 while (link)
1195 {
1196 if (mo->player) // Minotaur looking around player
1197 {
1198 if ((link->flags & MF_COUNTKILL) ||
1199 (link->player && (link != mo)))
1200 {
1201 if (!(link->flags & MF_SHOOTABLE))
1202 {
1203 link = link->bnext;
1204 continue;
1205 }
1206 if (link->flags2 & MF2_DORMANT)
1207 {
1208 link = link->bnext;
1209 continue;
1210 }
1211 if ((link->type == HEXEN_MT_MINOTAUR) &&
1212 (link->special1.m == mo))
1213 {
1214 link = link->bnext;
1215 continue;
1216 }
1217 if (netgame && !deathmatch && link->player)
1218 {
1219 link = link->bnext;
1220 continue;
1221 }
1222 if (P_CheckSight(mo, link))
1223 {
1224 return link;
1225 }
1226 }
1227 link = link->bnext;
1228 }
1229 else if (mo->type == HEXEN_MT_MINOTAUR) // looking around minotaur
1230 {
1231 master = mo->special1.m;
1232 if ((link->flags & MF_COUNTKILL) ||
1233 (link->player && (link != master)))
1234 {
1235 if (!(link->flags & MF_SHOOTABLE))
1236 {
1237 link = link->bnext;
1238 continue;
1239 }
1240 if (link->flags2 & MF2_DORMANT)
1241 {
1242 link = link->bnext;
1243 continue;
1244 }
1245 if ((link->type == HEXEN_MT_MINOTAUR) &&
1246 (link->special1.m == mo->special1.m))
1247 {
1248 link = link->bnext;
1249 continue;
1250 }
1251 if (netgame && !deathmatch && link->player)
1252 {
1253 link = link->bnext;
1254 continue;
1255 }
1256 if (P_CheckSight(mo, link))
1257 {
1258 return link;
1259 }
1260 }
1261 link = link->bnext;
1262 }
1263 else if (mo->type == HEXEN_MT_MSTAFF_FX2) // bloodscourge
1264 {
1265 if ((link->flags & MF_COUNTKILL ||
1266 (link->player && link != mo->target))
1267 && !(link->flags2 & MF2_DORMANT))
1268 {
1269 if (!(link->flags & MF_SHOOTABLE))
1270 {
1271 link = link->bnext;
1272 continue;
1273 }
1274 if (netgame && !deathmatch && link->player)
1275 {
1276 link = link->bnext;
1277 continue;
1278 }
1279 else if (P_CheckSight(mo, link))
1280 {
1281 master = mo->target;
1282 angle = R_PointToAngle2(master->x, master->y,
1283 link->x, link->y) - master->angle;
1284 angle >>= 24;
1285 if (angle > 226 || angle < 30)
1286 {
1287 return link;
1288 }
1289 }
1290 }
1291 link = link->bnext;
1292 }
1293 else // spirits
1294 {
1295 if ((link->flags & MF_COUNTKILL ||
1296 (link->player && link != mo->target))
1297 && !(link->flags2 & MF2_DORMANT))
1298 {
1299 if (!(link->flags & MF_SHOOTABLE))
1300 {
1301 link = link->bnext;
1302 continue;
1303 }
1304 if (netgame && !deathmatch && link->player)
1305 {
1306 link = link->bnext;
1307 continue;
1308 }
1309 if (link == mo->target)
1310 {
1311 link = link->bnext;
1312 continue;
1313 }
1314 else if (P_CheckSight(mo, link))
1315 {
1316 return link;
1317 }
1318 }
1319 link = link->bnext;
1320 }
1321 }
1322 return NULL;
1323 }