Debian Codegraph @

prboom2/src/p_sight.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 * LineOfSight/Visibility checks, uses REJECT Lookup Table.
31 *
32 *-----------------------------------------------------------------------------*/
33
34 #include "doomstat.h"
35 #include "doomtype.h"
36 #include "r_main.h"
37 #include "p_map.h"
38 #include "p_maputl.h"
39 #include "p_setup.h"
40 #include "m_bbox.h"
41 #include "lprintf.h"
42 #include "g_overflow.h"
43 #include "e6y.h" //e6y
44
45 #include "dsda/map_format.h"
46
47 /*
48 ==============================================================================
49
50 P_CheckSight
51 This uses specialized forms of the maputils routines for optimized performance
52
53 ==============================================================================
54 */
55
56 fixed_t sightzstart; // eye z of looker
57 fixed_t topslope, bottomslope; // slopes to top and bottom of target
58 int sightcounts[3];
59
60 CrossSubsectorFunc P_CrossSubsector;
61
62 /*
63 ==============
64 =
65 = PTR_SightTraverse
66 =
67 ==============
68 */
69
70 dboolean PTR_SightTraverse(intercept_t *in)
71 {
72 line_t *li;
73 fixed_t slope;
74
75 li = in->d.line;
76
77 //
78 // crosses a two sided line
79 //
80 P_LineOpening(li, NULL);
81
82 if (line_opening.bottom >= line_opening.top) // quick test for totally closed doors
83 return false; // stop
84
85 if (li->frontsector->floorheight != li->backsector->floorheight)
86 {
87 slope = FixedDiv(line_opening.bottom - sightzstart , in->frac);
88 if (slope > bottomslope)
89 bottomslope = slope;
90 }
91
92 if (li->frontsector->ceilingheight != li->backsector->ceilingheight)
93 {
94 slope = FixedDiv(line_opening.top - sightzstart, in->frac);
95 if (slope < topslope)
96 topslope = slope;
97 }
98
99 if (topslope <= bottomslope)
100 return false; // stop
101
102 return true; // keep going
103 }
104
105
106
107 /*
108 ==================
109 =
110 = P_SightBlockLinesIterator
111 =
112 ===================
113 */
114
115 static dboolean P_SightCheckLine(line_t *ld)
116 {
117 int s1, s2;
118 divline_t dl;
119
120 if (ld->validcount == validcount)
121 return true; // line has already been checked
122
123 ld->validcount = validcount;
124
125 s1 = P_PointOnDivlineSide(ld->v1->x, ld->v1->y, &trace);
126 s2 = P_PointOnDivlineSide(ld->v2->x, ld->v2->y, &trace);
127 if (s1 == s2)
128 return true; // line isn't crossed
129
130 P_MakeDivline(ld, &dl);
131
132 s1 = P_PointOnDivlineSide(trace.x, trace.y, &dl);
133 s2 = P_PointOnDivlineSide(trace.x + trace.dx, trace.y + trace.dy, &dl);
134 if (s1 == s2)
135 return true; // line isn't crossed
136
137 if (!ld->backsector)
138 return false; // stop checking
139
140 check_intercept(); // killough
141
142 // store the line for later intersection testing
143 intercept_p->d.line = ld;
144 intercept_p++;
145
146 return true;
147 }
148
149 dboolean P_SightBlockLinesIterator(int x, int y)
150 {
151 int offset;
152 int *list;
153
154 offset = y * bmapwidth + x;
155
156 // Invalid block, ignore this one
157 if (offset < 0)
158 return true;
159
160 if (map_format.polyobjs)
161 {
162 polyblock_t *polyLink;
163 seg_t **segList;
164 int i;
165 extern polyblock_t **PolyBlockMap;
166
167 polyLink = PolyBlockMap[offset];
168 while (polyLink)
169 {
170 if (polyLink->polyobj)
171 { // only check non-empty links
172 if (polyLink->polyobj->validcount != validcount)
173 {
174 segList = polyLink->polyobj->segs;
175
176 for (i = 0; i < polyLink->polyobj->numsegs; i++, segList++)
177 if (!P_SightCheckLine((*segList)->linedef))
178 return false;
179
180 polyLink->polyobj->validcount = validcount;
181 }
182 }
183 polyLink = polyLink->next;
184 }
185 }
186
187 offset = *(blockmap+offset);
188
189 for (list = blockmaplump + offset; *list != -1; list++)
190 if (!P_SightCheckLine(&lines[*list]))
191 return false;
192
193 return true; // everything was checked
194 }
195
196 /*
197 ====================
198 =
199 = P_SightTraverseIntercepts
200 =
201 = Returns true if the traverser function returns true for all lines
202 ====================
203 */
204
205 dboolean P_SightTraverseIntercepts(void)
206 {
207 int count;
208 fixed_t dist;
209 intercept_t *scan, *in;
210 divline_t dl;
211
212 count = intercept_p - intercepts;
213 //
214 // calculate intercept distance
215 //
216 for (scan = intercepts; scan<intercept_p; scan++)
217 {
218 P_MakeDivline(scan->d.line, &dl);
219 scan->frac = P_InterceptVector(&trace, &dl);
220 }
221
222 //
223 // go through in order
224 //
225 in = 0; // shut up compiler warning
226
227 while (count--)
228 {
229 dist = INT_MAX;
230 for (scan = intercepts ; scan<intercept_p ; scan++)
231 if (scan->frac < dist)
232 {
233 dist = scan->frac;
234 in = scan;
235 }
236
237 if (!PTR_SightTraverse(in))
238 return false; // don't bother going farther
239 in->frac = INT_MAX;
240 }
241
242 return true; // everything was traversed
243 }
244
245
246
247 /*
248 ==================
249 =
250 = P_SightPathTraverse
251 =
252 = Traces a line from x1,y1 to x2,y2, calling the traverser function for each
253 = Returns true if the traverser function returns true for all lines
254 ==================
255 */
256
257 dboolean P_SightPathTraverse(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2)
258 {
259 fixed_t xt1,yt1,xt2,yt2;
260 fixed_t xstep,ystep;
261 fixed_t partial;
262 fixed_t xintercept, yintercept;
263 int mapx, mapy, mapxstep, mapystep;
264 int count;
265
266 validcount++;
267 intercept_p = intercepts;
268
269 if (((x1-bmaporgx)&(MAPBLOCKSIZE-1)) == 0)
270 x1 += FRACUNIT; // don't side exactly on a line
271 if (((y1-bmaporgy)&(MAPBLOCKSIZE-1)) == 0)
272 y1 += FRACUNIT; // don't side exactly on a line
273 trace.x = x1;
274 trace.y = y1;
275 trace.dx = x2 - x1;
276 trace.dy = y2 - y1;
277
278 x1 -= bmaporgx;
279 y1 -= bmaporgy;
280 xt1 = P_GetSafeBlockX(x1);
281 yt1 = P_GetSafeBlockY(y1);
282
283 x2 -= bmaporgx;
284 y2 -= bmaporgy;
285 xt2 = P_GetSafeBlockX(x2);
286 yt2 = P_GetSafeBlockY(y2);
287
288 // points should never be out of bounds, but check once instead of
289 // each block
290 if (xt1<0 || yt1<0 || xt1>=bmapwidth || yt1>=bmapheight
291 || xt2<0 || yt2<0 || xt2>=bmapwidth || yt2>=bmapheight)
292 return false;
293
294 if (xt2 > xt1)
295 {
296 mapxstep = 1;
297 partial = FRACUNIT - ((x1>>MAPBTOFRAC)&(FRACUNIT-1));
298 ystep = FixedDiv (y2-y1,abs(x2-x1));
299 }
300 else if (xt2 < xt1)
301 {
302 mapxstep = -1;
303 partial = (x1>>MAPBTOFRAC)&(FRACUNIT-1);
304 ystep = FixedDiv (y2-y1,abs(x2-x1));
305 }
306 else
307 {
308 mapxstep = 0;
309 partial = FRACUNIT;
310 ystep = 256*FRACUNIT;
311 }
312 yintercept = (y1>>MAPBTOFRAC) + FixedMul (partial, ystep);
313
314
315 if (yt2 > yt1)
316 {
317 mapystep = 1;
318 partial = FRACUNIT - ((y1>>MAPBTOFRAC)&(FRACUNIT-1));
319 xstep = FixedDiv (x2-x1,abs(y2-y1));
320 }
321 else if (yt2 < yt1)
322 {
323 mapystep = -1;
324 partial = (y1>>MAPBTOFRAC)&(FRACUNIT-1);
325 xstep = FixedDiv (x2-x1,abs(y2-y1));
326 }
327 else
328 {
329 mapystep = 0;
330 partial = FRACUNIT;
331 xstep = 256*FRACUNIT;
332 }
333 xintercept = (x1>>MAPBTOFRAC) + FixedMul (partial, xstep);
334
335
336 //
337 // step through map blocks
338 // Count is present to prevent a round off error from skipping the break
339 mapx = xt1;
340 mapy = yt1;
341
342
343 for (count = 0; count < 64; count++)
344 {
345 if (!P_SightBlockLinesIterator(mapx, mapy))
346 {
347 sightcounts[1]++;
348 return false; // early out
349 }
350
351 if (mapx == xt2 && mapy == yt2)
352 break;
353
354 if ((yintercept >> FRACBITS) == mapy)
355 {
356 yintercept += ystep;
357 mapx += mapxstep;
358 }
359 else if ((xintercept >> FRACBITS) == mapx)
360 {
361 xintercept += xstep;
362 mapy += mapystep;
363 }
364
365 }
366
367
368 //
369 // couldn't early out, so go through the sorted list
370 //
371 sightcounts[2]++;
372
373 return P_SightTraverseIntercepts();
374 }
375
376
377
378 /*
379 =====================
380 =
381 = P_CheckSight
382 =
383 = Returns true if a straight line between t1 and t2 is unobstructed
384 = look from eyes of t1 to any part of t2
385 =
386 =====================
387 */
388
389 dboolean P_CheckSight_12(mobj_t *t1, mobj_t *t2)
390 {
391 int s1, s2;
392 int pnum, bytenum, bitnum;
393
394 //
395 // check for trivial rejection
396 //
397 s1 = (t1->subsector->sector->iSectorID);
398 s2 = (t2->subsector->sector->iSectorID);
399 pnum = s1*numsectors + s2;
400 bytenum = pnum>>3;
401 bitnum = 1 << (pnum&7);
402
403 if (rejectmatrix[bytenum]&bitnum)
404 {
405 sightcounts[0]++;
406 return false; // can't possibly be connected
407 }
408
409 //
410 // check precisely
411 //
412 sightzstart = t1->z + t1->height - (t1->height>>2);
413 topslope = (t2->z+t2->height) - sightzstart;
414 bottomslope = (t2->z) - sightzstart;
415
416 return P_SightPathTraverse (t1->x, t1->y, t2->x, t2->y);
417 }
418
419 //
420 // P_CheckSight
421 //
422 // killough 4/19/98:
423 // Convert LOS info to struct for reentrancy and efficiency of data locality
424
425 typedef struct {
426 fixed_t sightzstart, t2x, t2y; // eye z of looker
427 divline_t strace; // from t1 to t2
428 fixed_t topslope, bottomslope; // slopes to top and bottom of target
429 fixed_t bbox[4];
430 fixed_t maxz,minz; // cph - z optimisations for 2sided lines
431 } los_t;
432
433 static los_t los; // cph - made static
434
435 //
436 // P_DivlineSide
437 // Returns side 0 (front), 1 (back), or 2 (on).
438 //
439 // killough 4/19/98: made static, cleaned up
440
441 INLINE static int P_DivlineSide(fixed_t x, fixed_t y, const divline_t *node)
442 {
443 fixed_t left, right;
444 return
445 !node->dx ? x == node->x ? 2 : x <= node->x ? node->dy > 0 : node->dy < 0 :
446 !node->dy ? ( compatibility_level < prboom_4_compatibility ? x : y) == node->y ? 2 : y <= node->y ? node->dx < 0 : node->dx > 0 :
447 (right = ((y - node->y) >> FRACBITS) * (node->dx >> FRACBITS)) <
448 (left = ((x - node->x) >> FRACBITS) * (node->dy >> FRACBITS)) ? 0 :
449 right == left ? 2 : 1;
450 }
451
452 INLINE static int P_DivlineCrossed(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2, divline_t *node)
453 {
454 return (P_DivlineSide(x1, y1, node) == P_DivlineSide(x2, y2, node));
455 }
456
457
458 //
459 // P_CrossSubsector
460 // Returns true
461 // if strace crosses the given subsector successfully.
462 //
463 // killough 4/19/98: made static and cleaned up
464
465 dboolean P_CrossSubsector_Poly(int num)
466 {
467 if (map_format.zdoom && subsectors[num].poly)
468 {
469 int polyCount;
470 seg_t **polySeg;
471
472 polyCount = subsectors[num].poly->numsegs;
473 polySeg = subsectors[num].poly->segs;
474
475 while (polyCount--)
476 {
477 divline_t divl;
478 line_t *line;
479
480 line = (*polySeg++)->linedef;
481 if (line->validcount == validcount)
482 continue;
483
484 // OPTIMIZE: killough 4/20/98: Added quick bounding-box rejection test
485
486 if (line->bbox[BOXLEFT ] > los.bbox[BOXRIGHT ] ||
487 line->bbox[BOXRIGHT ] < los.bbox[BOXLEFT ] ||
488 line->bbox[BOXBOTTOM] > los.bbox[BOXTOP ] ||
489 line->bbox[BOXTOP] < los.bbox[BOXBOTTOM])
490 {
491 line->validcount = validcount;
492 continue;
493 }
494
495 // line isn't crossed?
496 if (P_DivlineCrossed(line->v1->x, line->v1->y, line->v2->x, line->v2->y, &los.strace))
497 {
498 line->validcount = validcount;
499 continue;
500 }
501
502 divl.dx = line->v2->x - (divl.x = line->v1->x);
503 divl.dy = line->v2->y - (divl.y = line->v1->y);
504
505 // line isn't crossed?
506 if (P_DivlineCrossed(los.strace.x, los.strace.y, los.t2x, los.t2y, &divl))
507 {
508 line->validcount = validcount;
509 continue;
510 }
511
512 // already checked other side?
513 if (line->validcount == validcount)
514 continue;
515
516 line->validcount = validcount;
517
518 // stop because it is not two sided anyway
519 return false;
520 }
521 }
522
523 return true;
524 }
525
526 dboolean P_CrossSubsector_PrBoom(int num)
527 {
528 ssline_t *ssline = &sslines[sslines_indexes[num]];
529 const ssline_t *ssline_last = &sslines[sslines_indexes[num + 1]];
530 fixed_t opentop = 0, openbottom = 0;
531 const sector_t *front = NULL, *back = NULL;
532
533 #ifdef RANGECHECK
534 if (num >= numsubsectors)
535 I_Error("P_CrossSubsector: ss %i with numss = %i", num, numsubsectors);
536 #endif
537
538 // check lines
539 for (; ssline < ssline_last; ssline++)
540 {
541 divline_t divl;
542
543 /* OPTIMIZE: killough 4/20/98: Added quick bounding-box rejection test
544 * cph - this is causing demo desyncs on original Doom demos.
545 * Who knows why. Exclude test for those.
546 */
547 if (ssline->bbox[BOXLEFT ] > los.bbox[BOXRIGHT ] ||
548 ssline->bbox[BOXRIGHT ] < los.bbox[BOXLEFT ] ||
549 ssline->bbox[BOXBOTTOM] > los.bbox[BOXTOP ] ||
550 ssline->bbox[BOXTOP] < los.bbox[BOXBOTTOM])
551 {
552 ssline->linedef->validcount = validcount;
553 continue;
554 }
555
556 // Forget this line if it doesn't cross the line of sight
557 if (P_DivlineCrossed(ssline->x1, ssline->y1, ssline->x2, ssline->y2, &los.strace))
558 {
559 ssline->linedef->validcount = validcount;
560 continue;
561 }
562
563 divl.dx = ssline->x2 - (divl.x = ssline->x1);
564 divl.dy = ssline->y2 - (divl.y = ssline->y1);
565
566 // line isn't crossed?
567 if (P_DivlineCrossed(los.strace.x, los.strace.y, los.t2x, los.t2y, &divl))
568 {
569 ssline->linedef->validcount = validcount;
570 continue;
571 }
572
573 // allready checked other side?
574 if (ssline->linedef->validcount == validcount)
575 continue;
576
577 ssline->linedef->validcount = validcount;
578
579 // cph - do what we can before forced to check intersection
580 if (ssline->linedef->flags & ML_TWOSIDED)
581 {
582 // crosses a two sided line
583 front = ssline->seg->frontsector;
584 back = ssline->seg->backsector;
585
586 // no wall to block sight with?
587 if (front->floorheight == back->floorheight
588 && front->ceilingheight == back->ceilingheight)
589 continue;
590
591 // possible occluder
592 // because of ceiling height differences
593 opentop = MIN(front->ceilingheight, back->ceilingheight);
594
595 // because of floor height differences
596 openbottom = MAX(front->floorheight, back->floorheight);
597
598 // cph - reject if does not intrude in the z-space of the possible LOS
599 if ((opentop >= los.maxz) && (openbottom <= los.minz))
600 continue;
601 }
602
603 // cph - if bottom >= top or top < minz or bottom > maxz then it must be
604 // solid wrt this LOS
605 if (!(ssline->linedef->flags & ML_TWOSIDED) || (openbottom >= opentop) ||
606 (prboom_comp[PC_FORCE_LXDOOM_DEMO_COMPATIBILITY].state ?
607 (opentop <= los.minz) || (openbottom >= los.maxz) :
608 (opentop < los.minz) || (openbottom > los.maxz)))
609 return false;
610
611 { // crosses a two sided line
612 /* cph 2006/07/15 - oops, we missed this in 2.4.0 & .1;
613 * use P_InterceptVector2 for those compat levels only. */
614 fixed_t frac = (compatibility_level == prboom_5_compatibility || compatibility_level == prboom_6_compatibility) ?
615 P_InterceptVector2(&los.strace, &divl) :
616 P_InterceptVector(&los.strace, &divl);
617
618 if (front->floorheight != back->floorheight)
619 {
620 fixed_t slope = FixedDiv(openbottom - los.sightzstart, frac);
621 if (slope > los.bottomslope)
622 los.bottomslope = slope;
623 }
624
625 if (front->ceilingheight != back->ceilingheight)
626 {
627 fixed_t slope = FixedDiv(opentop - los.sightzstart, frac);
628 if (slope < los.topslope)
629 los.topslope = slope;
630 }
631
632 if (los.topslope <= los.bottomslope)
633 return false; // stop
634 }
635 }
636 // passed the subsector ok
637 return true;
638 }
639
640 dboolean P_CrossSubsector_Doom(int num)
641 {
642 ssline_t *ssline = &sslines[sslines_indexes[num]];
643 const ssline_t *ssline_last = &sslines[sslines_indexes[num + 1]];
644
645 #ifdef RANGECHECK
646 if (num >= numsubsectors)
647 I_Error("P_CrossSubsector: ss %i with numss = %i", num, numsubsectors);
648 #endif
649
650 for (; ssline < ssline_last; ssline++)
651 {
652 divline_t divl;
653 fixed_t opentop, openbottom;
654 const sector_t *front, *back;
655 fixed_t frac;
656
657 // line isn't crossed?
658 if (P_DivlineCrossed(ssline->x1, ssline->y1, ssline->x2, ssline->y2, &los.strace))
659 {
660 ssline->linedef->validcount = validcount;
661 continue;
662 }
663
664 divl.dx = ssline->x2 - (divl.x = ssline->x1);
665 divl.dy = ssline->y2 - (divl.y = ssline->y1);
666
667 // line isn't crossed?
668 if (P_DivlineCrossed(los.strace.x, los.strace.y, los.t2x, los.t2y, &divl))
669 {
670 ssline->linedef->validcount = validcount;
671 continue;
672 }
673
674 // allready checked other side?
675 if (ssline->linedef->validcount == validcount)
676 continue;
677
678 ssline->linedef->validcount = validcount;
679
680 // stop because it is not two sided anyway
681 if (!(ssline->linedef->flags & ML_TWOSIDED))
682 return false;
683
684 // crosses a two sided line
685 front = ssline->seg->frontsector;
686 back = ssline->seg->backsector;
687
688 // missed back side on two-sided lines.
689 if (!back)
690 {
691 back = GetSectorAtNullAddress();
692 }
693
694 // no wall to block sight with?
695 if (front->floorheight == back->floorheight
696 && front->ceilingheight == back->ceilingheight)
697 continue;
698
699 // possible occluder
700 // because of ceiling height differences
701 opentop = MIN(front->ceilingheight, back->ceilingheight);
702
703 // because of floor height differences
704 openbottom = MAX(front->floorheight, back->floorheight);
705
706 // quick test for totally closed doors
707 if (openbottom >= opentop)
708 return false; // stop
709
710 frac = P_InterceptVector2(&los.strace, &divl);
711
712 if (front->floorheight != back->floorheight)
713 {
714 fixed_t slope = FixedDiv(openbottom - los.sightzstart, frac);
715 if (slope > los.bottomslope)
716 los.bottomslope = slope;
717 }
718
719 if (front->ceilingheight != back->ceilingheight)
720 {
721 fixed_t slope = FixedDiv(opentop - los.sightzstart, frac);
722 if (slope < los.topslope)
723 los.topslope = slope;
724 }
725
726 if (los.topslope <= los.bottomslope)
727 return false; // stop
728 }
729 // passed the subsector ok
730 return true;
731 }
732
733 dboolean P_CrossSubsector_Boom(int num)
734 {
735 ssline_t *ssline = &sslines[sslines_indexes[num]];
736 const ssline_t *ssline_last = &sslines[sslines_indexes[num + 1]];
737
738 #ifdef RANGECHECK
739 if (num >= numsubsectors)
740 I_Error("P_CrossSubsector: ss %i with numss = %i", num, numsubsectors);
741 #endif
742
743 for (; ssline < ssline_last; ssline++)
744 {
745 divline_t divl;
746 fixed_t opentop, openbottom;
747 const sector_t *front, *back;
748 fixed_t frac;
749
750 // OPTIMIZE: killough 4/20/98: Added quick bounding-box rejection test
751
752 if (ssline->bbox[BOXLEFT ] > los.bbox[BOXRIGHT ] ||
753 ssline->bbox[BOXRIGHT ] < los.bbox[BOXLEFT ] ||
754 ssline->bbox[BOXBOTTOM] > los.bbox[BOXTOP ] ||
755 ssline->bbox[BOXTOP] < los.bbox[BOXBOTTOM])
756 {
757 ssline->linedef->validcount = validcount;
758 continue;
759 }
760
761 // line isn't crossed?
762 if (P_DivlineCrossed(ssline->x1, ssline->y1, ssline->x2, ssline->y2, &los.strace))
763 {
764 ssline->linedef->validcount = validcount;
765 continue;
766 }
767
768 divl.dx = ssline->x2 - (divl.x = ssline->x1);
769 divl.dy = ssline->y2 - (divl.y = ssline->y1);
770
771 // line isn't crossed?
772 if (P_DivlineCrossed(los.strace.x, los.strace.y, los.t2x, los.t2y, &divl))
773 {
774 ssline->linedef->validcount = validcount;
775 continue;
776 }
777
778 // allready checked other side?
779 if (ssline->linedef->validcount == validcount)
780 continue;
781
782 ssline->linedef->validcount = validcount;
783
784 // stop because it is not two sided anyway
785 if (!(ssline->linedef->flags & ML_TWOSIDED) ||
786 ssline->linedef->flags & (ML_BLOCKEVERYTHING | ML_BLOCKSIGHT))
787 return false;
788
789 // crosses a two sided line
790 front = ssline->seg->frontsector;
791 back = ssline->seg->backsector;
792
793 // no wall to block sight with?
794 if (front->floorheight == back->floorheight
795 && front->ceilingheight == back->ceilingheight)
796 continue;
797
798 // possible occluder
799 // because of ceiling height differences
800 opentop = MIN(front->ceilingheight, back->ceilingheight);
801
802 // because of floor height differences
803 openbottom = MAX(front->floorheight, back->floorheight);
804
805 // quick test for totally closed doors
806 if (openbottom >= opentop)
807 return false; // stop
808
809 frac = P_InterceptVector2(&los.strace, &divl);
810
811 if (front->floorheight != back->floorheight)
812 {
813 fixed_t slope = FixedDiv(openbottom - los.sightzstart, frac);
814 if (slope > los.bottomslope)
815 los.bottomslope = slope;
816 }
817
818 if (front->ceilingheight != back->ceilingheight)
819 {
820 fixed_t slope = FixedDiv(opentop - los.sightzstart, frac);
821 if (slope < los.topslope)
822 los.topslope = slope;
823 }
824
825 if (los.topslope <= los.bottomslope)
826 return false; // stop
827 }
828
829 if (!P_CrossSubsector_Poly(num))
830 return false;
831
832 // passed the subsector ok
833 return true;
834 }
835
836 //
837 // P_CrossBSPNode
838 // Returns true
839 // if strace crosses the given node successfully.
840 //
841 // killough 4/20/98: rewritten to remove tail recursion, clean up, and optimize
842 // cph - Made to use R_PointOnSide instead of P_DivlineSide, since the latter
843 // could return 2 which was ambigous, and the former is
844 // better optimised; also removes two casts :-)
845
846 static dboolean P_CrossBSPNode_LxDoom(int bspnum)
847 {
848 while (!(bspnum & NF_SUBSECTOR))
849 {
850 register const node_t *bsp = nodes + bspnum;
851 int side,side2;
852 side = R_PointOnSide(los.strace.x, los.strace.y, bsp);
853 side2 = R_PointOnSide(los.t2x, los.t2y, bsp);
854 if (side == side2)
855 bspnum = bsp->children[side]; // doesn't touch the other side
856 else // the partition plane is crossed here
857 if (!P_CrossBSPNode_LxDoom(bsp->children[side]))
858 return 0; // cross the starting side
859 else
860 bspnum = bsp->children[side^1]; // cross the ending side
861 }
862 return P_CrossSubsector(bspnum == -1 ? 0 : bspnum & ~NF_SUBSECTOR);
863 }
864
865 static dboolean P_CrossBSPNode_PrBoom(int bspnum)
866 {
867 while (!(bspnum & NF_SUBSECTOR))
868 {
869 register const node_t *bsp = nodes + bspnum;
870 int side,side2;
871 side = P_DivlineSide(los.strace.x,los.strace.y,(const divline_t *)bsp)&1;
872 side2= P_DivlineSide(los.t2x, los.t2y, (const divline_t *) bsp);
873 if (side == side2)
874 bspnum = bsp->children[side]; // doesn't touch the other side
875 else // the partition plane is crossed here
876 if (!P_CrossBSPNode_PrBoom(bsp->children[side]))
877 return 0; // cross the starting side
878 else
879 bspnum = bsp->children[side^1]; // cross the ending side
880 }
881 return P_CrossSubsector(bspnum == -1 ? 0 : bspnum & ~NF_SUBSECTOR);
882 }
883
884 /* proff - Moved the compatibility check outside the functions
885 * this gives a slight speedup
886 */
887 static dboolean P_CrossBSPNode(int bspnum)
888 {
889 /* cph - LxDoom used some R_* funcs here */
890 if (compatibility_level == lxdoom_1_compatibility || prboom_comp[PC_FORCE_LXDOOM_DEMO_COMPATIBILITY].state)
891 return P_CrossBSPNode_LxDoom(bspnum);
892 else
893 return P_CrossBSPNode_PrBoom(bspnum);
894 }
895
896 //
897 // P_CheckSight
898 // Returns true
899 // if a straight line between t1 and t2 is unobstructed.
900 // Uses REJECT.
901 //
902 // killough 4/20/98: cleaned up, made to use new LOS struct
903
904 dboolean P_CheckSight(mobj_t *t1, mobj_t *t2)
905 {
906 const sector_t *s1, *s2;
907 int pnum;
908
909 if (compatibility_level == doom_12_compatibility)
910 {
911 return P_CheckSight_12(t1, t2);
912 }
913
914 s1 = t1->subsector->sector;
915 s2 = t2->subsector->sector;
916 pnum = (s1->iSectorID)*numsectors + (s2->iSectorID);
917
918 // First check for trivial rejection.
919 // Determine subsector entries in REJECT table.
920 //
921 // Check in REJECT table.
922
923 if (rejectmatrix[pnum>>3] & (1 << (pnum&7))) // can't possibly be connected
924 return false;
925
926 // killough 4/19/98: make fake floors and ceilings block monster view
927
928 if ((s1->heightsec != -1 &&
929 ((t1->z + t1->height <= sectors[s1->heightsec].floorheight &&
930 t2->z >= sectors[s1->heightsec].floorheight) ||
931 (t1->z >= sectors[s1->heightsec].ceilingheight &&
932 t2->z + t1->height <= sectors[s1->heightsec].ceilingheight)))
933 ||
934 (s2->heightsec != -1 &&
935 ((t2->z + t2->height <= sectors[s2->heightsec].floorheight &&
936 t1->z >= sectors[s2->heightsec].floorheight) ||
937 (t2->z >= sectors[s2->heightsec].ceilingheight &&
938 t1->z + t2->height <= sectors[s2->heightsec].ceilingheight))))
939 return false;
940
941 /* killough 11/98: shortcut for melee situations
942 * same subsector? obviously visible
943 * cph - compatibility optioned for demo sync, cf HR06-UV.LMP */
944 if ((t1->subsector == t2->subsector) &&
945 (compatibility_level >= mbf_compatibility))
946 return true;
947
948 // An unobstructed LOS is possible.
949 // Now look from eyes of t1 to any part of t2.
950
951 validcount++;
952
953 los.topslope = (los.bottomslope = t2->z - (los.sightzstart =
954 t1->z + t1->height -
955 (t1->height>>2))) + t2->height;
956 los.strace.dx = (los.t2x = t2->x) - (los.strace.x = t1->x);
957 los.strace.dy = (los.t2y = t2->y) - (los.strace.y = t1->y);
958
959 if (t1->x > t2->x)
960 los.bbox[BOXRIGHT] = t1->x, los.bbox[BOXLEFT] = t2->x;
961 else
962 los.bbox[BOXRIGHT] = t2->x, los.bbox[BOXLEFT] = t1->x;
963
964 if (t1->y > t2->y)
965 los.bbox[BOXTOP] = t1->y, los.bbox[BOXBOTTOM] = t2->y;
966 else
967 los.bbox[BOXTOP] = t2->y, los.bbox[BOXBOTTOM] = t1->y;
968
969 /* cph - calculate min and max z of the potential line of sight
970 * For old demos, we disable this optimisation by setting them to
971 * the extremes */
972 if (compatibility_level == lxdoom_1_compatibility || prboom_comp[PC_FORCE_LXDOOM_DEMO_COMPATIBILITY].state)
973 {
974 if (los.sightzstart < t2->z) {
975 los.maxz = t2->z + t2->height; los.minz = los.sightzstart;
976 } else if (los.sightzstart > t2->z + t2->height) {
977 los.maxz = los.sightzstart; los.minz = t2->z;
978 } else {
979 los.maxz = t2->z + t2->height; los.minz = t2->z;
980 }
981 }
982 else
983 {
984 los.maxz = INT_MAX; los.minz = INT_MIN;
985 }
986
987 // the head node is the last node output
988 return P_CrossBSPNode(numnodes-1);
989 }
990
991 //
992 // P_CheckFov
993 // Returns true if t2 is within t1's field of view.
994 // Not directly related to P_CheckSight, but often
995 // used in tandem.
996 //
997 // Adapted from Eternity, so big thanks to Quasar
998 //
999 dboolean P_CheckFov(mobj_t *t1, mobj_t *t2, angle_t fov)
1000 {
1001 angle_t angle, minang, maxang;
1002
1003 angle = R_PointToAngle2(t1->x, t1->y, t2->x, t2->y);
1004 minang = t1->angle - fov / 2;
1005 maxang = t1->angle + fov / 2;
1006
1007 return((minang > maxang) ? angle >= minang || angle <= maxang
1008 : angle >= minang && angle <= maxang);
1009 }