1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
6 // Copyright (C) 1993-1996 by id Software, Inc.
8 // This source is available for distribution and/or modification
9 // only under the terms of the DOOM Source Code License as
10 // published by id Software. All rights reserved.
12 // The source is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
20 // LineOfSight/Visibility checks, uses REJECT Lookup Table.
22 //-----------------------------------------------------------------------------
25 rcsid[] = "$Id: p_sight.c,v 1.3 1997/01/28 22:08:28 b1 Exp $";
39 fixed_t sightzstart; // eye z of looker
41 fixed_t bottomslope; // slopes to top and bottom of target
43 divline_t strace; // from t1 to t2
52 // Returns side 0 (front), 1 (back), or 2 (on).
90 left = (node->dy>>FRACBITS) * (dx>>FRACBITS);
91 right = (dy>>FRACBITS) * (node->dx>>FRACBITS);
94 return 0; // front side
98 return 1; // back side
103 // P_InterceptVector2
104 // Returns the fractional intercept point
105 // along the first divline.
106 // This is only called by the addthings and addlines traversers.
117 den = FixedMul (v1->dy>>8,v2->dx) - FixedMul(v1->dx>>8,v2->dy);
121 // I_Error ("P_InterceptVector: parallel");
123 num = FixedMul ( (v1->x - v2->x)>>8 ,v1->dy) +
124 FixedMul ( (v2->y - v1->y)>>8 , v1->dx);
125 frac = FixedDiv (num , den);
133 // if strace crosses the given subsector successfully.
135 boolean P_CrossSubsector (int num)
154 if (num>=numsubsectors)
155 I_Error ("P_CrossSubsector: ss %i with numss = %i",
160 sub = &subsectors[num];
163 count = sub->numlines;
164 seg = &segs[sub->firstline];
166 for ( ; count ; seg++, count--)
170 // allready checked other side?
171 if (line->validcount == validcount)
174 line->validcount = validcount;
178 s1 = P_DivlineSide (v1->x,v1->y, &strace);
179 s2 = P_DivlineSide (v2->x, v2->y, &strace);
181 // line isn't crossed?
187 divl.dx = v2->x - v1->x;
188 divl.dy = v2->y - v1->y;
189 s1 = P_DivlineSide (strace.x, strace.y, &divl);
190 s2 = P_DivlineSide (t2x, t2y, &divl);
192 // line isn't crossed?
196 // stop because it is not two sided anyway
197 // might do this after updating validcount?
198 if ( !(line->flags & ML_TWOSIDED) )
201 // crosses a two sided line
202 front = seg->frontsector;
203 back = seg->backsector;
205 // no wall to block sight with?
206 if (front->floorheight == back->floorheight
207 && front->ceilingheight == back->ceilingheight)
211 // because of ceiling height differences
212 if (front->ceilingheight < back->ceilingheight)
213 opentop = front->ceilingheight;
215 opentop = back->ceilingheight;
217 // because of ceiling height differences
218 if (front->floorheight > back->floorheight)
219 openbottom = front->floorheight;
221 openbottom = back->floorheight;
223 // quick test for totally closed doors
224 if (openbottom >= opentop)
225 return false; // stop
227 frac = P_InterceptVector2 (&strace, &divl);
229 if (front->floorheight != back->floorheight)
231 slope = FixedDiv (openbottom - sightzstart , frac);
232 if (slope > bottomslope)
236 if (front->ceilingheight != back->ceilingheight)
238 slope = FixedDiv (opentop - sightzstart , frac);
239 if (slope < topslope)
243 if (topslope <= bottomslope)
244 return false; // stop
246 // passed the subsector ok
255 // if strace crosses the given node successfully.
257 boolean P_CrossBSPNode (int bspnum)
262 if (bspnum & NF_SUBSECTOR)
265 return P_CrossSubsector (0);
267 return P_CrossSubsector (bspnum&(~NF_SUBSECTOR));
270 bsp = &nodes[bspnum];
272 // decide which side the start point is on
273 side = P_DivlineSide (strace.x, strace.y, (divline_t *)bsp);
275 side = 0; // an "on" should cross both sides
277 // cross the starting side
278 if (!P_CrossBSPNode (bsp->children[side]) )
281 // the partition plane is crossed here
282 if (side == P_DivlineSide (t2x, t2y,(divline_t *)bsp))
284 // the line doesn't touch the other side
288 // cross the ending side
289 return P_CrossBSPNode (bsp->children[side^1]);
296 // if a straight line between t1 and t2 is unobstructed.
310 // First check for trivial rejection.
312 // Determine subsector entries in REJECT table.
313 s1 = (t1->subsector->sector - sectors);
314 s2 = (t2->subsector->sector - sectors);
315 pnum = s1*numsectors + s2;
317 bitnum = 1 << (pnum&7);
319 // Check in REJECT table.
320 if (rejectmatrix[bytenum]&bitnum)
324 // can't possibly be connected
328 // An unobstructed LOS is possible.
329 // Now look from eyes of t1 to any part of t2.
334 sightzstart = t1->z + t1->height - (t1->height>>2);
335 topslope = (t2->z+t2->height) - sightzstart;
336 bottomslope = (t2->z) - sightzstart;
342 strace.dx = t2->x - t1->x;
343 strace.dy = t2->y - t1->y;
345 // the head node is the last node output
346 return P_CrossBSPNode (numnodes-1);