-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_sight.c
189 lines (126 loc) · 5.35 KB
/
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
#include <stdlib.h>
#include "doomdef.h"
#include "d_think.h"
#include "p_pspr.h"
#include "doomstat.h"
#include "r_main.h"
#include "p_map.h"
#include "p_maputl.h"
#include "p_setup.h"
typedef struct
{
int sightzstart, t2x, t2y;
divline_t strace;
int topslope, bottomslope;
int bbox[4];
int maxz,minz;
} los_t;
static los_t los;
inline static int P_DivlineSide(int x, int y, const divline_t *node)
{
int left, right;
return !node->dx ? x == node->x ? 2 : x <= node->x ? node->dy > 0 : node->dy < 0 : !node->dy ? y == node->y ? 2 : y <= node->y ? node->dx < 0 : node->dx > 0 : (right = ((y - node->y) >> FRACBITS) * (node->dx >> FRACBITS)) < (left = ((x - node->x) >> FRACBITS) * (node->dy >> FRACBITS)) ? 0 : right == left ? 2 : 1;
}
static boolean P_CrossSubsector(int num)
{
seg_t *seg = segs + subsectors[num].firstline;
int count;
int opentop = 0, openbottom = 0;
const sector_t *front = NULL, *back = NULL;
for (count = subsectors[num].numlines; --count >= 0; seg++)
{
line_t *line = seg->linedef;
divline_t divl;
if (!line)
continue;
if (line->validcount == validcount)
continue;
line->validcount = validcount;
if (line->bbox[BOXLEFT] > los.bbox[BOXRIGHT] || line->bbox[BOXRIGHT] < los.bbox[BOXLEFT] || line->bbox[BOXBOTTOM] > los.bbox[BOXTOP] || line->bbox[BOXTOP] < los.bbox[BOXBOTTOM])
continue;
if (line->flags & ML_TWOSIDED)
{
if ((front = seg->frontsector)->floorheight == (back = seg->backsector)->floorheight && front->ceilingheight == back->ceilingheight)
continue;
opentop = front->ceilingheight < back->ceilingheight ? front->ceilingheight : back->ceilingheight;
openbottom = front->floorheight > back->floorheight ? front->floorheight : back->floorheight;
if ((opentop >= los.maxz) && (openbottom <= los.minz))
continue;
}
{
const vertex_t *v1,*v2;
v1 = line->v1;
v2 = line->v2;
if (P_DivlineSide(v1->x, v1->y, &los.strace) == P_DivlineSide(v2->x, v2->y, &los.strace))
continue;
divl.dx = v2->x - (divl.x = v1->x);
divl.dy = v2->y - (divl.y = v1->y);
if (P_DivlineSide(los.strace.x, los.strace.y, &divl) == P_DivlineSide(los.t2x, los.t2y, &divl))
continue;
}
if (!(line->flags & ML_TWOSIDED) || (openbottom >= opentop) || (opentop < los.minz) || (openbottom > los.maxz))
return false;
{
int frac = P_InterceptVector2(&los.strace, &divl);
if (front->floorheight != back->floorheight)
{
int slope = FixedDiv(openbottom - los.sightzstart, frac);
if (slope > los.bottomslope)
los.bottomslope = slope;
}
if (front->ceilingheight != back->ceilingheight)
{
int slope = FixedDiv(opentop - los.sightzstart, frac);
if (slope < los.topslope)
los.topslope = slope;
}
if (los.topslope <= los.bottomslope)
return false;
}
}
return true;
}
static boolean P_CrossBSPNode(int bspnum)
{
while (!(bspnum & NF_SUBSECTOR))
{
register const node_t *bsp = nodes + bspnum;
int side, side2;
side = P_DivlineSide(los.strace.x, los.strace.y, (const divline_t *)bsp) & 1;
side2 = P_DivlineSide(los.t2x, los.t2y, (const divline_t *)bsp);
if (side == side2)
bspnum = bsp->children[side];
else if (!P_CrossBSPNode(bsp->children[side]))
return 0;
else
bspnum = bsp->children[side ^ 1];
}
return P_CrossSubsector(bspnum == -1 ? 0 : bspnum & ~NF_SUBSECTOR);
}
boolean P_CheckSight(mobj_t *t1, mobj_t *t2)
{
const sector_t *s1 = t1->subsector->sector;
const sector_t *s2 = t2->subsector->sector;
int pnum = (s1-sectors)*numsectors + (s2-sectors);
if (rejectmatrix[pnum >> 3] & (1 << (pnum & 7)))
return false;
if ((s1->heightsec != -1 && ((t1->z + t1->height <= sectors[s1->heightsec].floorheight && t2->z >= sectors[s1->heightsec].floorheight) || (t1->z >= sectors[s1->heightsec].ceilingheight && t2->z + t1->height <= sectors[s1->heightsec].ceilingheight))) || (s2->heightsec != -1 && ((t2->z + t2->height <= sectors[s2->heightsec].floorheight && t1->z >= sectors[s2->heightsec].floorheight) || (t2->z >= sectors[s2->heightsec].ceilingheight && t1->z + t2->height <= sectors[s2->heightsec].ceilingheight))))
return false;
if (t1->subsector == t2->subsector)
return true;
validcount++;
los.topslope = (los.bottomslope = t2->z - (los.sightzstart = t1->z + t1->height - (t1->height >> 2))) + t2->height;
los.strace.dx = (los.t2x = t2->x) - (los.strace.x = t1->x);
los.strace.dy = (los.t2y = t2->y) - (los.strace.y = t1->y);
if (t1->x > t2->x)
los.bbox[BOXRIGHT] = t1->x, los.bbox[BOXLEFT] = t2->x;
else
los.bbox[BOXRIGHT] = t2->x, los.bbox[BOXLEFT] = t1->x;
if (t1->y > t2->y)
los.bbox[BOXTOP] = t1->y, los.bbox[BOXBOTTOM] = t2->y;
else
los.bbox[BOXTOP] = t2->y, los.bbox[BOXBOTTOM] = t1->y;
los.maxz = INT_MAX;
los.minz = INT_MIN;
return P_CrossBSPNode(numnodes - 1);
}