doom / src / p_sight.c

  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
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: p_sight.c 695 2010-07-09 03:10:17Z wesleyjohnson $
//
// Copyright (C) 1993-1996 by id Software, Inc.
// Portions Copyright (C) 1998-2000 by DooM Legacy Team.
//
// 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.
//
//
// $Log: p_sight.c,v $
// Revision 1.6  2003/03/22 22:35:58  hurdler
// Fix CR+LF issue
//
// Revision 1.5  2002/09/27 16:40:09  tonyd
// First commit of acbot
//
// Revision 1.4  2001/06/10 21:16:01  bpereira
// no message
//
// Revision 1.3  2001/01/25 22:15:44  bpereira
// added heretic support
//
// Revision 1.2  2000/02/27 00:42:10  hurdler
// fix CR+LF problem
//
// Revision 1.1.1.1  2000/02/22 20:32:32  hurdler
// Initial import into CVS (v1.29 pr3)
//
//
// DESCRIPTION:
//      LineOfSight/Visibility checks, uses REJECT Lookup Table.
//
//-----------------------------------------------------------------------------


#include "doomdef.h"
#include "doomstat.h"
#include "p_local.h"
#include "r_main.h"
#include "r_state.h"

//
// P_CheckSight
//
// Check Sight global return vars, also used by P_AimLineAttack
fixed_t         see_topslope;
fixed_t         see_bottomslope;   // slopes to top and bottom of target

// Check Sight internal global vars
divline_t       cs_trace;        // from t1 to t2
fixed_t         cs_t2x, cs_t2y;
fixed_t         cs_startz;       // eye z of looker

int             cs_sightcounts[2];	  // ??? debugging


//
// P_DivlineSide
// Returns side 0 (front), 1 (back), or 2 (on).
//
static int P_DivlineSide( fixed_t x, fixed_t y, divline_t* node )
{
    fixed_t     dx;
    fixed_t     dy;
    fixed_t     left;
    fixed_t     right;

    if (!node->dx)
    {
        if (x==node->x)
            return 2;

        if (x <= node->x)
            return node->dy > 0;

        return node->dy < 0;
    }

    if (!node->dy)
    {
//        if (x==node->y)
        if (y==node->y)	// [WDJ] Fix "Sleeping Sargeant" bug from DoomWiki.
            return 2;

        if (y <= node->y)
            return node->dx < 0;

        return node->dx > 0;
    }

    dx = (x - node->x);
    dy = (y - node->y);

    left =  (node->dy>>FRACBITS) * (dx>>FRACBITS);
    right = (dy>>FRACBITS) * (node->dx>>FRACBITS);

    if (right < left)
        return 0;       // front side

    if (left == right)
        return 2;
    return 1;           // back side
}


//
// P_InterceptVector2
// Returns the fractional intercept point
// along the first divline.
// This is only called by the addthings and addlines traversers.
//
static fixed_t P_InterceptVector2( divline_t* v2, divline_t* v1 )
{
    fixed_t     frac;
    fixed_t     num;
    fixed_t     den;

    den = FixedMul (v1->dy>>8,v2->dx) - FixedMul(v1->dx>>8,v2->dy);

    if (den == 0)
        return 0;
    //  I_Error ("P_InterceptVector: parallel");

    num = FixedMul ( (v1->x - v2->x)>>8 ,v1->dy) +
        FixedMul ( (v2->y - v1->y)>>8 , v1->dx);
    frac = FixedDiv (num , den);

    return frac;
}

//
// P_CrossSubsector
// Returns true
//  if cs_trace crosses the given subsector successfully.
//
static boolean P_CrossSubsector (int num)
{
    seg_t*              seg;
    line_t*             line;
    int                 s1;
    int                 s2;
    int                 count;
    subsector_t*        sub;
    sector_t*           front;
    sector_t*           back;
    fixed_t             opentop;
    fixed_t             openbottom;
    divline_t           divl;
    vertex_t*           v1;
    vertex_t*           v2;
    fixed_t             frac;
    fixed_t             slope;

#ifdef RANGECHECK
    if (num>=numsubsectors)
        I_Error ("P_CrossSubsector: ss %i with numss = %i",
                 num,
                 numsubsectors);
#endif

    sub = &subsectors[num];

    // check lines
    count = sub->numlines;
    seg = &segs[sub->firstline];

    for ( ; count ; seg++, count--)
    {
        line = seg->linedef;

        // allready checked other side?
        if (line->validcount == validcount)
            continue;

        line->validcount = validcount;

        v1 = line->v1;
        v2 = line->v2;
        s1 = P_DivlineSide (v1->x,v1->y, &cs_trace);
        s2 = P_DivlineSide (v2->x, v2->y, &cs_trace);

        // line isn't crossed?
        if (s1 == s2)
            continue;

        divl.x = v1->x;
        divl.y = v1->y;
        divl.dx = v2->x - v1->x;
        divl.dy = v2->y - v1->y;
        s1 = P_DivlineSide (cs_trace.x, cs_trace.y, &divl);
        s2 = P_DivlineSide (cs_t2x, cs_t2y, &divl);

        // line isn't crossed?
        if (s1 == s2)
            continue;

        // stop because it is not two sided anyway
        // might do this after updating validcount?
        if ( !(line->flags & ML_TWOSIDED) )
            return false;

        // crosses a two sided line
        front = seg->frontsector;
        back = seg->backsector;

        // no wall to block sight with?
        if (front->floorheight == back->floorheight
            && front->ceilingheight == back->ceilingheight)
            continue;

        // possible occluder
        // because of ceiling height differences
        if (front->ceilingheight < back->ceilingheight)
            opentop = front->ceilingheight;
        else
            opentop = back->ceilingheight;

        // because of ceiling height differences
        if (front->floorheight > back->floorheight)
            openbottom = front->floorheight;
        else
            openbottom = back->floorheight;

        // quick test for totally closed doors
        if (openbottom >= opentop)
            return false;               // stop

        frac = P_InterceptVector2 (&cs_trace, &divl);

        if (front->floorheight != back->floorheight)
        {
            slope = FixedDiv (openbottom - cs_startz , frac);
            if (slope > see_bottomslope)
                see_bottomslope = slope;
        }

        if (front->ceilingheight != back->ceilingheight)
        {
            slope = FixedDiv (opentop - cs_startz , frac);
            if (slope < see_topslope)
                see_topslope = slope;
        }

        if (see_topslope <= see_bottomslope)
            return false;               // stop
    }
    // passed the subsector ok
    return true;
}



//
// P_CrossBSPNode
// Returns true
//  if cs_trace crosses the given node successfully.
//
static boolean P_CrossBSPNode (int bspnum)
{
    node_t*     bsp;
    int         side;

    if (bspnum & NF_SUBSECTOR)
    {
        if (bspnum == -1)
            return P_CrossSubsector (0);
        else
            return P_CrossSubsector (bspnum&(~NF_SUBSECTOR));
    }

    bsp = &nodes[bspnum];

    // decide which side the start point is on
    side = P_DivlineSide (cs_trace.x, cs_trace.y, (divline_t *)bsp);
    if (side == 2)
        side = 0;       // an "on" should cross both sides

    // cross the starting side
    if (!P_CrossBSPNode (bsp->children[side]) )
        return false;

    // the partition plane is crossed here
    if (side == P_DivlineSide (cs_t2x, cs_t2y,(divline_t *)bsp))
    {
        // the line doesn't touch the other side
        return true;
    }

    // cross the ending side
    return P_CrossBSPNode (bsp->children[side^1]);
}


//
// P_CheckSight
// Returns true
//  if a straight line between t1 and t2 is unobstructed.
// Uses REJECT.
//
boolean P_CheckSight( mobj_t* t1, mobj_t* t2 )
{
    int         s1;
    int         s2;
    int         pnum;
    int         bytenum;
    int         bitnum;

    // First check for trivial rejection.

    // Determine subsector entries in REJECT table.
    s1 = (t1->subsector->sector - sectors);
    s2 = (t2->subsector->sector - sectors);
    pnum = s1*numsectors + s2;
    bytenum = pnum>>3;
    bitnum = 1 << (pnum&7);

    // Check in REJECT table.
    if (rejectmatrix[bytenum]&bitnum)
    {
        cs_sightcounts[0]++;

        // can't possibly be connected
        return false;
    }
/*  BP: it seam that it don't work :( TODO: fix it
    if (gamemode == heretic )
    {
        //
        // check precisely
        //              
        cs_startz = t1->z + t1->height - (t1->height>>2);
        see_topslope = (t2->z+t2->height) - cs_startz;
        see_bottomslope = (t2->z) - cs_startz;
        
        return P_SightPathTraverse ( t1->x, t1->y, t2->x, t2->y );
    }
*/    
    // An unobstructed LOS is possible.
    // Now look from eyes of t1 to any part of t2.
    cs_sightcounts[1]++;

    validcount++;

    cs_startz = t1->z + t1->height - (t1->height>>2);
    see_topslope = (t2->z+t2->height) - cs_startz;
    see_bottomslope = (t2->z) - cs_startz;

    cs_trace.x = t1->x;
    cs_trace.y = t1->y;
    cs_t2x = t2->x;
    cs_t2y = t2->y;
    cs_trace.dx = t2->x - t1->x;
    cs_trace.dy = t2->y - t1->y;

    // the head node is the last node output
    return P_CrossBSPNode (numnodes-1);
}

//	added by AC for missle prediction
// P_CheckSight2
// Returns true
//  if a straight line between t1 and t2's predicted location is unobstructed.
// Uses REJECT.
//
boolean P_CheckSight2( mobj_t* t1, mobj_t* t2, fixed_t px, fixed_t py, fixed_t pz )
{
    int         s1;
    int         s2;
    int         pnum;
    int         bytenum;
    int         bitnum;

    // First check for trivial rejection.

    // Determine subsector entries in REJECT table.
    s1 = (t1->subsector->sector - sectors);
    s2 = (R_PointInSubsector(px, py)->sector - sectors);
    pnum = s1*numsectors + s2;
    bytenum = pnum>>3;
    bitnum = 1 << (pnum&7);

    // Check in REJECT table.
    if (rejectmatrix[bytenum]&bitnum)
    {
        cs_sightcounts[0]++;

        // can't possibly be connected
        return false;
    }
/*  BP: it seem that it don't work :( TODO: fix it
    if (gamemode == heretic )
    {
        //
        // check precisely
        //              
        cs_startz = t1->z + t1->height - (t1->height>>2);
        see_topslope = (t2->z+t2->height) - cs_startz;
        see_bottomslope = (t2->z) - cs_startz;
        
        return P_SightPathTraverse ( t1->x, t1->y, t2->x, t2->y );
    }
*/    
    // An unobstructed LOS is possible.
    // Now look from eyes of t1 to any part of t2.
    cs_sightcounts[1]++;

    validcount++;

    cs_startz = t1->z + t1->height - (t1->height>>2);
    see_topslope = (pz+t2->height) - cs_startz;
    see_bottomslope = (pz) - cs_startz;

    cs_trace.x = t1->x;
    cs_trace.y = t1->y;
    cs_t2x = px;
    cs_t2y = py;
    cs_trace.dx = px - t1->x;
    cs_trace.dy = py - t1->y;

    // the head node is the last node output
    return P_CrossBSPNode (numnodes-1);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.