/
r_util.c
326 lines (273 loc) · 8.06 KB
/
r_util.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
//**************************************************************************
//**
//** R_UTIL.C
//**
//**************************************************************************
// HEADER FILES ------------------------------------------------------------
#include "de_base.h"
#include "de_refresh.h"
// MACROS ------------------------------------------------------------------
#define SLOPERANGE 2048
#define SLOPEBITS 11
#define DBITS (FRACBITS-SLOPEBITS)
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern int tantoangle[SLOPERANGE+1]; // get from tables.c
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CODE --------------------------------------------------------------------
//===========================================================================
// R_PointOnSide
// Returns side 0 (front) or 1 (back).
//===========================================================================
int R_PointOnSide (fixed_t x, fixed_t y, node_t *node)
{
fixed_t dx,dy;
fixed_t left, right;
if (!node->dx)
{
if (x <= node->x)
return node->dy > 0;
return node->dy < 0;
}
if (!node->dy)
{
if (y <= node->y)
return node->dx < 0;
return node->dx > 0;
}
dx = (x - node->x);
dy = (y - node->y);
// Try to quickly decide by looking at sign bits.
if ( (node->dy ^ node->dx ^ dx ^ dy)&0x80000000 )
{
if ( (node->dy ^ dx) & 0x80000000 )
return 1; // (left is negative)
return 0;
}
left = FixedMul ( node->dy>>FRACBITS , dx );
right = FixedMul ( dy , node->dx>>FRACBITS );
if (right < left) return 0; // front side
return 1; // back side
}
//===========================================================================
// R_PointOnSegSide
//===========================================================================
int R_PointOnSegSide (fixed_t x, fixed_t y, seg_t *line)
{
fixed_t lx, ly;
fixed_t ldx, ldy;
fixed_t dx,dy;
fixed_t left, right;
lx = line->v1->x;
ly = line->v1->y;
ldx = line->v2->x - lx;
ldy = line->v2->y - ly;
if (!ldx)
{
if (x <= lx)
return ldy > 0;
return ldy < 0;
}
if (!ldy)
{
if (y <= ly)
return ldx < 0;
return ldx > 0;
}
dx = (x - lx);
dy = (y - ly);
// try to quickly decide by looking at sign bits
if ( (ldy ^ ldx ^ dx ^ dy)&0x80000000 )
{
if ( (ldy ^ dx) & 0x80000000 )
return 1; // (left is negative)
return 0;
}
left = FixedMul ( ldy>>FRACBITS , dx );
right = FixedMul ( dy , ldx>>FRACBITS );
if (right < left)
return 0; // front side
return 1; // back side
}
//===========================================================================
// R_SlopeDiv
//===========================================================================
int R_SlopeDiv (unsigned num, unsigned den)
{
unsigned ans;
if (den < 512)
return SLOPERANGE;
ans = (num<<3)/(den>>8);
return ans <= SLOPERANGE ? ans : SLOPERANGE;
}
//===========================================================================
// R_PointToAngle
// to get a global angle from cartesian coordinates, the coordinates are
// flipped until they are in the first octant of the coordinate system, then
// the y (<=x) is scaled and divided by x to get a tangent (slope) value
// which is looked up in the tantoangle[] table. The +1 size is to handle
// the case when x==y without additional checking.
//===========================================================================
angle_t R_PointToAngle (fixed_t x, fixed_t y)
{
x -= viewx;
y -= viewy;
if ( (!x) && (!y) )
return 0;
if (x>= 0)
{ // x >=0
if (y>= 0)
{ // y>= 0
if (x>y)
return tantoangle[ R_SlopeDiv(y,x)]; // octant 0
else
return ANG90-1-tantoangle[ R_SlopeDiv(x,y)]; // octant 1
}
else
{ // y<0
y = -y;
if (x>y)
return -tantoangle[R_SlopeDiv(y,x)]; // octant 8
else
return ANG270+tantoangle[ R_SlopeDiv(x,y)]; // octant 7
}
}
else
{ // x<0
x = -x;
if (y>= 0)
{ // y>= 0
if (x>y)
return ANG180-1-tantoangle[ R_SlopeDiv(y,x)]; // octant 3
else
return ANG90+ tantoangle[ R_SlopeDiv(x,y)]; // octant 2
}
else
{ // y<0
y = -y;
if (x>y)
return ANG180+tantoangle[ R_SlopeDiv(y,x)]; // octant 4
else
return ANG270-1-tantoangle[ R_SlopeDiv(x,y)]; // octant 5
}
}
return 0;
}
//===========================================================================
// R_PointToAngle2
//===========================================================================
angle_t R_PointToAngle2 (fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2)
{
viewx = x1;
viewy = y1;
return R_PointToAngle (x2, y2);
}
//===========================================================================
// R_PointToDist
//===========================================================================
fixed_t R_PointToDist (fixed_t x, fixed_t y)
{
int angle;
fixed_t dx, dy, temp;
fixed_t dist;
dx = abs(x - viewx);
dy = abs(y - viewy);
if (dy>dx)
{
temp = dx;
dx = dy;
dy = temp;
}
angle = (tantoangle[ FixedDiv(dy,dx)>>DBITS ]+ANG90) >> ANGLETOFINESHIFT;
dist = FixedDiv (dx, finesine[angle] ); // use as cosine
return dist;
}
//===========================================================================
// R_PointInSubsector
//===========================================================================
subsector_t *R_PointInSubsector (fixed_t x, fixed_t y)
{
node_t *node;
int side, nodenum;
if(!numnodes) // single subsector is a special case
return (subsector_t*) subsectors;
nodenum = numnodes-1;
while(!(nodenum & NF_SUBSECTOR))
{
node = NODE_PTR(nodenum);
side = R_PointOnSide (x, y, node);
nodenum = node->children[side];
}
return SUBSECTOR_PTR(nodenum & ~NF_SUBSECTOR);
}
//===========================================================================
// R_GetLineForSide
//===========================================================================
line_t *R_GetLineForSide(int sideNumber)
{
side_t *side = SIDE_PTR(sideNumber);
sector_t *sector = side->sector;
int i;
// All sides may not have a sector.
if(!sector) return NULL;
for(i = 0; i < sector->linecount; i++)
if(sector->lines[i]->sidenum[0] == sideNumber
|| sector->lines[i]->sidenum[1] == sideNumber)
{
return sector->lines[i];
}
return NULL;
}
//===========================================================================
// R_IsPointInSector
// Returns true if the point is inside the sector, according to the lines
// that completely surround the sector. Uses the well-known algorithm
// described here: http://www.alienryderflex.com/polygon/
//===========================================================================
boolean R_IsPointInSector(fixed_t x, fixed_t y, sector_t *sector)
{
int i;
boolean isOdd = false;
vertex_t *vi, *vj;
for(i = 0; i < sector->linecount; i++)
{
// Skip lines that aren't sector boundaries.
if(sector->lines[i]->frontsector == sector
&& sector->lines[i]->backsector == sector) continue;
// It shouldn't matter whether the line faces inward or outward.
vi = sector->lines[i]->v1;
vj = sector->lines[i]->v2;
if(vi->y < y && vj->y >= y || vj->y < y && vi->y >= y)
{
if(vi->x + FixedMul(FixedDiv(y - vi->y, vj->y - vi->y),
vj->x - vi->x) < x)
{
// Toggle oddness.
isOdd = !isOdd;
}
}
}
// The point is inside if the number of crossed nodes is odd.
return isOdd;
}
//===========================================================================
// R_GetSectorNumForDegen
// Returns the index of the sector who owns the given degenmobj.
//===========================================================================
int R_GetSectorNumForDegen(void *degenmobj)
{
int i;
// Check all sectors; find where the sound is coming from.
for(i = 0; i < numsectors; i++)
{
if(degenmobj == &SECTOR_PTR(i)->soundorg)
{
return i;
}
}
return -1;
}