-
Notifications
You must be signed in to change notification settings - Fork 14
/
p_maputl.c
383 lines (320 loc) · 7.86 KB
/
p_maputl.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
/* P_maputl.c */
#include "doomdef.h"
#include "p_local.h"
fixed_t P_AproxDistance(fixed_t dx, fixed_t dy) ATTR_DATA_CACHE_ALIGN;
int P_PointOnLineSide(fixed_t x, fixed_t y, line_t* line) ATTR_DATA_CACHE_ALIGN;
int P_PointOnDivlineSide(fixed_t x, fixed_t y, divline_t* line) ATTR_DATA_CACHE_ALIGN;
fixed_t P_LineOpening(line_t* linedef) ATTR_DATA_CACHE_ALIGN;
void P_LineBBox(line_t* ld, fixed_t* bbox) ATTR_DATA_CACHE_ALIGN;
void P_UnsetThingPosition(mobj_t* thing) ATTR_DATA_CACHE_ALIGN;
void P_SetThingPosition(mobj_t* thing) ATTR_DATA_CACHE_ALIGN;
void P_SetThingPosition2(mobj_t* thing, subsector_t *ss) ATTR_DATA_CACHE_ALIGN;
boolean P_BlockLinesIterator(int x, int y, boolean(*func)(line_t*, void*), void *userp) ATTR_DATA_CACHE_ALIGN;
boolean P_BlockThingsIterator(int x, int y, boolean(*func)(mobj_t*, void*), void *userp) ATTR_DATA_CACHE_ALIGN;
/*
===================
=
= P_AproxDistance
=
= Gives an estimation of distance (not exact)
=
===================
*/
fixed_t P_AproxDistance (fixed_t dx, fixed_t dy)
{
dx = D_abs(dx);
dy = D_abs(dy);
if (dx < dy)
return dx+dy-(dx>>1);
return dx+dy-(dy>>1);
}
/*
==================
=
= P_PointOnLineSide
=
= Returns 0 or 1
==================
*/
int P_PointOnLineSide (fixed_t x, fixed_t y, line_t *line)
{
fixed_t dx,dy;
fixed_t left, right;
fixed_t ldx,ldy;
ldx = vertexes[line->v2].x - vertexes[line->v1].x;
ldy = vertexes[line->v2].y - vertexes[line->v1].y;
dx = (x - vertexes[line->v1].x);
dy = (y - vertexes[line->v1].y);
left = (ldy>>16) * (dx>>16);
right = (dy>>16) * (ldx>>16);
if (right < left)
return 0; /* front side */
return 1; /* back side */
}
/*
==================
=
= P_PointOnDivlineSide
=
= Returns 0 or 1
==================
*/
int P_PointOnDivlineSide (fixed_t x, fixed_t y, divline_t *line)
{
fixed_t dx,dy;
fixed_t left, right;
dx = (x - line->x);
dy = (y - line->y);
#ifndef MARS
/* try to quickly decide by looking at sign bits */
if ( (line->dy ^ line->dx ^ dx ^ dy)&0x80000000 )
{
if ( (line->dy ^ dx) & 0x80000000 )
return 1; /* (left is negative) */
return 0;
}
#endif
left = FixedMul(line->dy>>8, dx>>8);
right = FixedMul(dy>>8, line->dx>>8);
if (right < left)
return 0; /* front side */
return 1; /* back side */
}
/*
==================
=
= P_LineOpening
=
= Sets opentop and openbottom to the window through a two sided line
= OPTIMIZE: keep this precalculated
==================
*/
fixed_t P_LineOpening (line_t *linedef)
{
sector_t *front, *back;
fixed_t opentop, openbottom;
if (linedef->sidenum[1] == -1)
{ /* single sided line */
return 0;
}
front = LD_FRONTSECTOR(linedef);
back = LD_BACKSECTOR(linedef);
if (front->ceilingheight < back->ceilingheight)
opentop = front->ceilingheight;
else
opentop = back->ceilingheight;
if (front->floorheight > back->floorheight)
openbottom = front->floorheight;
else
openbottom = back->floorheight;
return opentop - openbottom;
}
void P_LineBBox(line_t* ld, fixed_t *bbox)
{
vertex_t* v1 = &vertexes[ld->v1], * v2 = &vertexes[ld->v2];
if (v1->x < v2->x)
{
bbox[BOXLEFT] = v1->x;
bbox[BOXRIGHT] = v2->x;
}
else
{
bbox[BOXLEFT] = v2->x;
bbox[BOXRIGHT] = v1->x;
}
if (v1->y < v2->y)
{
bbox[BOXBOTTOM] = v1->y;
bbox[BOXTOP] = v2->y;
}
else
{
bbox[BOXBOTTOM] = v2->y;
bbox[BOXTOP] = v1->y;
}
}
/*
===============================================================================
THING POSITION SETTING
===============================================================================
*/
/*
===================
=
= P_UnsetThingPosition
=
= Unlinks a thing from block map and sectors
=
===================
*/
void P_UnsetThingPosition (mobj_t *thing)
{
int blockx, blocky;
if ( ! (thing->flags & MF_NOSECTOR) )
{ /* inert things don't need to be in blockmap */
/* unlink from subsector */
if (thing->snext)
thing->snext->sprev = thing->sprev;
if (thing->sprev)
thing->sprev->snext = thing->snext;
else
thing->subsector->sector->thinglist = thing->snext;
}
if ( ! (thing->flags & MF_NOBLOCKMAP) )
{ /* inert things don't need to be in blockmap */
/* unlink from block map */
if (thing->bnext)
thing->bnext->bprev = thing->bprev;
if (thing->bprev)
thing->bprev->bnext = thing->bnext;
else
{
blockx = thing->x - bmaporgx;
blocky = thing->y - bmaporgy;
if (blockx >= 0 && blocky >= 0)
{
blockx = (unsigned)blockx >> MAPBLOCKSHIFT;
blocky = (unsigned)blocky >> MAPBLOCKSHIFT;
if (blockx < bmapwidth && blocky <bmapheight)
blocklinks[blocky*bmapwidth+blockx] = thing->bnext;
}
}
}
}
/*
===================
=
= P_SetThingPosition2
=
= Links a thing into both a block and a subsector based on it's x y
=
===================
*/
void P_SetThingPosition2 (mobj_t *thing, subsector_t *ss)
{
sector_t *sec;
int blockx, blocky;
mobj_t **link;
/* */
/* link into subsector */
/* */
thing->subsector = ss;
if ( ! (thing->flags & MF_NOSECTOR) )
{ /* invisible things don't go into the sector links */
sec = ss->sector;
thing->sprev = NULL;
thing->snext = sec->thinglist;
if (sec->thinglist)
sec->thinglist->sprev = thing;
sec->thinglist = thing;
}
/* */
/* link into blockmap */
/* */
if ( ! (thing->flags & MF_NOBLOCKMAP) )
{ /* inert things don't need to be in blockmap */
blockx = thing->x - bmaporgx;
blocky = thing->y - bmaporgy;
if (blockx>=0 && blocky>=0)
{
blockx = (unsigned)blockx >> MAPBLOCKSHIFT;
blocky = (unsigned)blocky >> MAPBLOCKSHIFT;
if (blockx < bmapwidth && blocky <bmapheight)
{
link = &blocklinks[blocky*bmapwidth+blockx];
thing->bprev = NULL;
thing->bnext = *link;
if (*link)
(*link)->bprev = thing;
*link = thing;
}
else
{ /* thing is off the map */
thing->bnext = thing->bprev = NULL;
}
}
else
{ /* thing is off the map */
thing->bnext = thing->bprev = NULL;
}
}
}
/*
===================
=
= P_SetThingPosition
=
= Links a thing into both a block and a subsector based on it's x y
= Sets thing->subsector properly
=
===================
*/
void P_SetThingPosition (mobj_t *thing)
{
P_SetThingPosition2(thing, R_PointInSubsector (thing->x,thing->y));
}
/*
===============================================================================
BLOCK MAP ITERATORS
For each line/thing in the given mapblock, call the passed function.
If the function returns false, exit with false without checking anything else.
===============================================================================
*/
/*
==================
=
= P_BlockLinesIterator
=
= The validcount flags are used to avoid checking lines
= that are marked in multiple mapblocks, so increment validcount before
= the first call to P_BlockLinesIterator, then make one or more calls to it
===================
*/
boolean P_BlockLinesIterator (int x, int y, blocklinesiter_t func, void *userp )
{
int offset;
short *list;
line_t *ld;
VINT *lvalidcount, vc;
//if (x<0 || y<0 || x>=bmapwidth || y>=bmapheight)
// return true;
offset = y*bmapwidth+x;
offset = *(blockmaplump+4+offset);
I_GetThreadLocalVar(DOOMTLS_VALIDCOUNT, lvalidcount);
vc = *lvalidcount;
++lvalidcount;
for ( list = blockmaplump+offset ; *list != -1 ; list++)
{
int l = *list;
ld = &lines[*list];
if (lvalidcount[l] == vc)
continue; /* line has already been checked */
lvalidcount[l] = vc;
if ( !func(ld, userp) )
return false;
}
return true; /* everything was checked */
}
/*
==================
=
= P_BlockThingsIterator
=
==================
*/
boolean P_BlockThingsIterator (int x, int y, blockthingsiter_t func, void *userp )
{
mobj_t *mobj;
//if (x<0 || y<0 || x>=bmapwidth || y>=bmapheight)
// return true;
for (mobj = blocklinks[y*bmapwidth+x] ; mobj ; mobj = mobj->bnext)
if (!func( mobj, userp ) )
return false;
return true;
}
void P_SectorOrg(mobj_t* sec_, fixed_t *org)
{
sector_t *sec = (void *)sec_;
org[0] = sec->soundorg[0] << FRACBITS;
org[1] = sec->soundorg[1] << FRACBITS;
}