-
Notifications
You must be signed in to change notification settings - Fork 14
/
p_move.c
449 lines (372 loc) · 12.2 KB
/
p_move.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
CALICO
Actor movement and clipping
The MIT License (MIT)
Copyright (c) 2015 James Haley, Olde Skuul, id Software and ZeniMax Media
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "doomdef.h"
#include "p_local.h"
// CALICO_TODO: these ought to be in headers.
typedef struct
{
// input
mobj_t *tmthing;
fixed_t tmx, tmy;
fixed_t tmfloorz; // current floor z for P_TryMove2
fixed_t tmceilingz; // current ceiling z for P_TryMove2
line_t *blockline; // possibly a special to activate
fixed_t tmbbox[4];
int tmflags;
fixed_t tmdropoffz; // lowest point contacted
int numspechit;
line_t **spechit;
subsector_t *newsubsec; // destination subsector
} pmovework_t;
boolean PIT_CheckThing(mobj_t* thing, pmovework_t *mw) ATTR_DATA_CACHE_ALIGN;
static boolean PM_BoxCrossLine(line_t* ld, pmovework_t *mw) ATTR_DATA_CACHE_ALIGN;
static boolean PIT_CheckLine(line_t* ld, pmovework_t *mw) ATTR_DATA_CACHE_ALIGN;
static boolean PM_CrossCheck(line_t* ld, pmovework_t *mw) ATTR_DATA_CACHE_ALIGN;
static boolean PM_CheckPosition(pmovework_t *mw) ATTR_DATA_CACHE_ALIGN;
boolean P_TryMove2(ptrymove_t *tm, boolean checkposonly) ATTR_DATA_CACHE_ALIGN;
//
// Check a single mobj in one of the contacted blockmap cells.
//
boolean PIT_CheckThing(mobj_t *thing, pmovework_t *mw)
{
fixed_t blockdist;
int delta;
int damage;
boolean solid;
mobj_t *tmthing = mw->tmthing;
int tmflags = mw->tmflags;
const mobjinfo_t* thinfo = &mobjinfo[tmthing->type];
if(!(thing->flags & (MF_SOLID|MF_SPECIAL|MF_SHOOTABLE)))
return true;
blockdist = thing->radius + tmthing->radius;
delta = thing->x - mw->tmx;
if(delta < 0)
delta = -delta;
if(delta >= blockdist)
return true; // didn't hit it
delta = thing->y - mw->tmy;
if(delta < 0)
delta = -delta;
if(delta >= blockdist)
return true; // didn't hit it
if(thing == tmthing)
return true; // don't clip against self
// check for skulls slamming into things
if(tmthing->flags & MF_SKULLFLY)
{
damage = ((P_Random()&7)+1)* thinfo->damage;
P_DamageMobj (thing, tmthing, tmthing, damage);
tmthing->flags &= ~MF_SKULLFLY;
tmthing->momx = tmthing->momy = tmthing->momz = 0;
P_SetMobjState (tmthing, thinfo->spawnstate);
return false; // stop moving
}
// missiles can hit other things
if(tmthing->flags & MF_MISSILE)
{
if(tmthing->z > thing->z + thing->height)
return true; // went overhead
if(tmthing->z + tmthing->height < thing->z)
return true; // went underneath
if(tmthing->target->type == thing->type) // don't hit same species as originator
{
if(thing == tmthing->target) // don't hit originator
return true;
if(thing->type != MT_PLAYER) // let players missile each other
return false; // explode, but do no damage
}
if(!(thing->flags & MF_SHOOTABLE))
return !(thing->flags & MF_SOLID); // didn't do any damage
// damage/explode
damage = ((P_Random()&7)+1)* thinfo->damage;
P_DamageMobj (thing, tmthing, tmthing->target, damage);
return false; // don't traverse any more
}
solid = (thing->flags & MF_SOLID) != 0;
// check for special pickup
if((thing->flags & MF_SPECIAL) && (tmflags & MF_PICKUP))
{
P_TouchSpecialThing (thing,tmthing);
}
return !solid;
}
//
// Check if the thing intersects a linedef
//
static boolean PM_BoxCrossLine(line_t *ld, pmovework_t *mw)
{
fixed_t x1, x2, y1, y2;
fixed_t lx, ly, ldx, ldy;
fixed_t dx1, dx2, dy1, dy2;
boolean side1, side2;
fixed_t ldbbox[4];
P_LineBBox(ld, ldbbox);
if(mw->tmbbox[BOXRIGHT ] <= ldbbox[BOXLEFT ] ||
mw->tmbbox[BOXLEFT ] >= ldbbox[BOXRIGHT ] ||
mw->tmbbox[BOXTOP ] <= ldbbox[BOXBOTTOM] ||
mw->tmbbox[BOXBOTTOM] >= ldbbox[BOXTOP ])
{
return false; // bounding boxes don't intersect
}
y1 = mw->tmbbox[BOXTOP ];
y2 = mw->tmbbox[BOXBOTTOM];
if(ld->flags & ML_ST_POSITIVE)
{
x1 = mw->tmbbox[BOXLEFT ];
x2 = mw->tmbbox[BOXRIGHT];
}
else
{
x1 = mw->tmbbox[BOXRIGHT];
x2 = mw->tmbbox[BOXLEFT ];
}
lx = vertexes[ld->v1].x;
ly = vertexes[ld->v1].y;
ldx = (vertexes[ld->v2].x - lx) >> FRACBITS;
ldy = (vertexes[ld->v2].y - ly) >> FRACBITS;
dx1 = (x1 - lx) >> FRACBITS;
dy1 = (y1 - ly) >> FRACBITS;
dx2 = (x2 - lx) >> FRACBITS;
dy2 = (y2 - ly) >> FRACBITS;
side1 = (ldy * dx1 < dy1 * ldx);
side2 = (ldy * dx2 < dy2 * ldx);
return (side1 != side2);
}
//
// Adjusts tmfloorz and tmceilingz as lines are contacted.
//
static boolean PIT_CheckLine(line_t *ld, pmovework_t *mw)
{
fixed_t opentop, openbottom, lowfloor;
sector_t *front, *back;
mobj_t *tmthing = mw->tmthing;
// The moving thing's destination positoin will cross the given line.
// If this should not be allowed, return false.
if(ld->sidenum[1] == -1)
return false; // one-sided line
if(!(tmthing->flags & MF_MISSILE))
{
if(ld->flags & ML_BLOCKING)
return false; // explicitly blocking everything
if(!tmthing->player && (ld->flags & ML_BLOCKMONSTERS))
return false; // block monsters only
}
front = LD_FRONTSECTOR(ld);
back = LD_BACKSECTOR(ld);
if(front->ceilingheight == front->floorheight ||
back->ceilingheight == back->floorheight)
{
mw->blockline = ld;
return false; // probably a closed door
}
if(front->ceilingheight < back->ceilingheight)
opentop = front->ceilingheight;
else
opentop = back->ceilingheight;
if(front->floorheight > back->floorheight)
{
openbottom = front->floorheight;
lowfloor = back->floorheight;
}
else
{
openbottom = back->floorheight;
lowfloor = front->floorheight;
}
// adjust floor/ceiling heights
if(opentop < mw->tmceilingz)
mw->tmceilingz = opentop;
if(openbottom >mw->tmfloorz)
mw->tmfloorz = openbottom;
if(lowfloor < mw->tmdropoffz)
mw->tmdropoffz = lowfloor;
if (ld->special)
{
if (mw->numspechit < MAXSPECIALCROSS)
mw->spechit[mw->numspechit++] = ld;
}
return true;
}
//
// Check a single linedef in a blockmap cell.
//
static boolean PM_CrossCheck(line_t *ld, pmovework_t *mw)
{
if(PM_BoxCrossLine(ld, mw))
{
if(!PIT_CheckLine(ld, mw))
return false;
}
return true;
}
//
// This is purely informative, nothing is modified (except things picked up)
//
static boolean PM_CheckPosition(pmovework_t *mw)
{
int xl, xh, yl, yh, bx, by;
mobj_t *tmthing = mw->tmthing;
VINT *lvalidcount;
mw->tmflags = tmthing->flags;
mw->tmbbox[BOXTOP ] = mw->tmy + tmthing->radius;
mw->tmbbox[BOXBOTTOM] = mw->tmy - tmthing->radius;
mw->tmbbox[BOXRIGHT ] = mw->tmx + tmthing->radius;
mw->tmbbox[BOXLEFT ] = mw->tmx - tmthing->radius;
mw->newsubsec = R_PointInSubsector(mw->tmx, mw->tmy);
// the base floor/ceiling is from the subsector that contains the point.
// Any contacted lines the step closer together will adjust them.
mw->tmfloorz = mw->tmdropoffz = mw->newsubsec->sector->floorheight;
mw->tmceilingz = mw->newsubsec->sector->ceilingheight;
I_GetThreadLocalVar(DOOMTLS_VALIDCOUNT, lvalidcount);
*lvalidcount = *lvalidcount + 1;
if (*lvalidcount == 0)
*lvalidcount = 1;
mw->blockline = NULL;
mw->numspechit = 0;
if(mw->tmflags & MF_NOCLIP) // thing has no clipping?
return true;
// Check things first, possibly picking things up.
// The bounding box is extended by MAXRADIUS because mobj_ts are grouped
// into mapblocks based on their origin point, and can overlap into adjacent
// blocks by up to MAXRADIUS units.
xl = mw->tmbbox[BOXLEFT ] - bmaporgx - MAXRADIUS;
xh = mw->tmbbox[BOXRIGHT ] - bmaporgx + MAXRADIUS;
yl = mw->tmbbox[BOXBOTTOM] - bmaporgy - MAXRADIUS;
yh = mw->tmbbox[BOXTOP ] - bmaporgy + MAXRADIUS;
if(xl < 0)
xl = 0;
if(yl < 0)
yl = 0;
if(yh < 0)
return true;
if(xh < 0)
return true;
xl = (unsigned)xl >> MAPBLOCKSHIFT;
xh = (unsigned)xh >> MAPBLOCKSHIFT;
yl = (unsigned)yl >> MAPBLOCKSHIFT;
yh = (unsigned)yh >> MAPBLOCKSHIFT;
if(xh >= bmapwidth)
xh = bmapwidth - 1;
if(yh >= bmapheight)
yh = bmapheight - 1;
// check things
for(bx = xl; bx <= xh; bx++)
{
for(by = yl; by <= yh; by++)
{
if(!P_BlockThingsIterator(bx, by, (blockthingsiter_t)PIT_CheckThing, mw))
return false;
}
}
// check lines
xl = mw->tmbbox[BOXLEFT ] - bmaporgx;
xh = mw->tmbbox[BOXRIGHT ] - bmaporgx;
yl = mw->tmbbox[BOXBOTTOM] - bmaporgy;
yh = mw->tmbbox[BOXTOP ] - bmaporgy;
if(xl < 0)
xl = 0;
if(yl < 0)
yl = 0;
if(yh < 0)
return true;
if(xh < 0)
return true;
xl = (unsigned)xl >> MAPBLOCKSHIFT;
xh = (unsigned)xh >> MAPBLOCKSHIFT;
yl = (unsigned)yl >> MAPBLOCKSHIFT;
yh = (unsigned)yh >> MAPBLOCKSHIFT;
if(xh >= bmapwidth)
xh = bmapwidth - 1;
if(yh >= bmapheight)
yh = bmapheight - 1;
for(bx = xl; bx <= xh; bx++)
{
for(by = yl; by <= yh; by++)
{
if(!P_BlockLinesIterator(bx, by, (blocklinesiter_t)PM_CrossCheck, mw))
return false;
}
}
return true;
}
//
// Attempt to move to a new position, crossing special lines unless MF_TELEPORT
// is set.
//
boolean P_TryMove2(ptrymove_t *tm, boolean checkposonly)
{
pmovework_t mw;
boolean trymove2; // result from P_TryMove2
mobj_t *tmthing = tm->tmthing;
mw.tmx = tm->tmx;
mw.tmy = tm->tmy;
mw.tmthing = tm->tmthing;
mw.spechit = &tm->spechit[0];
trymove2 = PM_CheckPosition(&mw);
tm->floatok = false;
tm->blockline = mw.blockline;
tm->tmfloorz = mw.tmfloorz;
tm->tmceilingz = mw.tmceilingz;
tm->tmdropoffz = mw.tmdropoffz;
tm->numspechit = mw.numspechit;
if(checkposonly)
return trymove2;
if(!trymove2)
return false;
if(!(tmthing->flags & MF_NOCLIP))
{
if(mw.tmceilingz - mw.tmfloorz < tmthing->height)
return false; // doesn't fit
tm->floatok = true;
if(!(tmthing->flags & MF_TELEPORT) && mw.tmceilingz - tmthing->z < tmthing->height)
return false; // mobj must lower itself to fit
if(!(tmthing->flags & MF_TELEPORT) && mw.tmfloorz - tmthing->z > 24*FRACUNIT)
return false; // too big a step up
if(!(tmthing->flags & (MF_DROPOFF|MF_FLOAT)) && mw.tmfloorz - mw.tmdropoffz > 24*FRACUNIT)
return false; // don't stand over a dropoff
}
// the move is ok, so link the thing into its new position.
P_UnsetThingPosition(tmthing);
tmthing->floorz = mw.tmfloorz;
tmthing->ceilingz = mw.tmceilingz;
tmthing->x = mw.tmx;
tmthing->y = mw.tmy;
P_SetThingPosition2(tmthing, mw.newsubsec);
return true;
}
void P_MoveCrossSpecials(mobj_t *tmthing, int numspechit, line_t **spechit, fixed_t oldx, fixed_t oldy)
{
int i;
if ((tmthing->flags&(MF_TELEPORT|MF_NOCLIP)) )
return;
for (i = 0; i < numspechit; i++)
{
line_t *ld = spechit[i];
int side, oldside;
// see if the line was crossed
side = P_PointOnLineSide (tmthing->x, tmthing->y, ld);
oldside = P_PointOnLineSide (oldx, oldy, ld);
if (side != oldside)
P_CrossSpecialLine(ld, tmthing);
}
}
// EOF