-
Notifications
You must be signed in to change notification settings - Fork 14
/
p_shoot.c
498 lines (404 loc) · 13.4 KB
/
p_shoot.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
CALICO
Tracer attack code
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"
// A line will be shootdiv'd from the middle of shooter in the direction of
// attackangle until either a shootable mobj is within the visible
// aimtopslope / aimbottomslope range, or a solid wall blocks further
// tracing. If no thing is targeted along the entire range, the first line
// that blocks the midpoint of the shootdiv will be hit.
// CALICO: removed type punning by bringing back intercept_t
typedef struct intercept_s
{
union ptr_u
{
mobj_t* mo;
line_t* line;
} d;
fixed_t frac;
boolean isaline;
} intercept_t;
typedef struct
{
mobj_t *shooter;
fixed_t attackrange;
fixed_t aimmidslope; // for detecting first wall hit
fixed_t aimbottomslope;
fixed_t aimtopslope;
divline_t shootdiv;
fixed_t shootx2, shooty2;
fixed_t firstlinefrac;
int shootdivpositive;
int ssx1, ssy1, ssx2, ssy2;
intercept_t old_intercept;
line_t *shootline;
mobj_t *shootmobj;
fixed_t shootslope; // between aimtop and aimbottom
fixed_t shootx, shooty, shootz; // location for puff/blood
} shootWork_t;
static fixed_t PA_SightCrossLine(shootWork_t *sw, vertex_t *v1, vertex_t *v2) ATTR_DATA_CACHE_ALIGN;
static boolean PA_ShootLine(shootWork_t *sw, line_t* li, fixed_t interceptfrac) ATTR_DATA_CACHE_ALIGN;
static boolean PA_ShootThing(shootWork_t *sw, mobj_t* th, fixed_t interceptfrac) ATTR_DATA_CACHE_ALIGN;
static boolean PA_DoIntercept(shootWork_t *sw, intercept_t* in) ATTR_DATA_CACHE_ALIGN;
static boolean PA_CrossSubsector(shootWork_t *sw, int bspnum) ATTR_DATA_CACHE_ALIGN;
static int PA_DivlineSide(fixed_t x, fixed_t y, divline_t* line) ATTR_DATA_CACHE_ALIGN;
static boolean PA_CrossBSPNode(shootWork_t *sw, int bspnum) ATTR_DATA_CACHE_ALIGN;
void P_Shoot2(lineattack_t *la) ATTR_DATA_CACHE_ALIGN;
//
// First checks the endpoints of the line to make sure that they cross the
// sight trace treated as an infinite line.
//
// If so, it calculates the fractional distance along the sight trace that
// the intersection occurs at. If 0 < intercept < 1.0, the line will block
// the sight.
//
static fixed_t PA_SightCrossLine(shootWork_t *sw, vertex_t *v1, vertex_t *v2)
{
fixed_t s1, s2;
fixed_t p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y, dx, dy, ndx, ndy;
// p1, p2 are endpoints
p1x = v1->x >> FRACBITS;
p1y = v1->y >> FRACBITS;
p2x = v2->x >> FRACBITS;
p2y = v2->y >> FRACBITS;
// p3, p4 are sight endpoints
p3x = sw->ssx1;
p3y = sw->ssy1;
p4x = sw->ssx2;
p4y = sw->ssy2;
dx = p2x - p3x;
dy = p2y - p3y;
ndx = p4x - p3x;
ndy = p4y - p3y;
s1 = (ndy * dx) < (dy * ndx);
dx = p1x - p3x;
dy = p1y - p3y;
s2 = (ndy * dx) < (dy * ndx);
if(s1 == s2)
return -1; // line isn't crossed
ndx = p1y - p2y; // vector normal to world line
ndy = p2x - p1x;
s1 = ndx * dx + ndy * dy; // distance projected onto normal
dx = p4x - p1x;
dy = p4y - p1y;
s2 = ndx * dx + ndy * dy; // distance projected onto normal
/* try to quickly decide by looking at sign bits */
if ( (s1 ^ (s1 + s2))&0x80000000 )
return -1;
return FixedDiv(s1, (s1 + s2));
}
//
// Handle shooting a line.
//
static boolean PA_ShootLine(shootWork_t *sw, line_t *li, fixed_t interceptfrac)
{
fixed_t slope;
fixed_t dist;
sector_t *front, *back;
fixed_t opentop, openbottom;
if(!(li->flags & ML_TWOSIDED))
{
if(!sw->shootline)
{
sw->shootline = li;
sw->firstlinefrac = interceptfrac;
}
sw->old_intercept.frac = 0; // don't shoot anything past this
return false;
}
// crosses a two-sided line
front = §ors[sides[li->sidenum[0]].sector];
back = §ors[sides[li->sidenum[1]].sector];
if(front->ceilingheight < back->ceilingheight)
opentop = front->ceilingheight;
else
opentop = back->ceilingheight;
if(front->floorheight > back->floorheight)
openbottom = front->floorheight;
else
openbottom = back->floorheight;
dist = FixedMul(sw->attackrange, interceptfrac);
if(front->floorheight != back->floorheight)
{
slope = FixedDiv(openbottom - sw->shootz, dist);
if(slope >= sw->aimmidslope && !sw->shootline)
{
sw->shootline = li;
sw->firstlinefrac = interceptfrac;
}
if(slope > sw->aimbottomslope)
sw->aimbottomslope = slope;
}
if(front->ceilingheight != back->ceilingheight)
{
slope = FixedDiv(opentop - sw->shootz, dist);
if(slope <= sw->aimmidslope && !sw->shootline)
{
sw->shootline = li;
sw->firstlinefrac = interceptfrac;
}
if(slope < sw->aimtopslope)
sw->aimtopslope = slope;
}
if(sw->aimtopslope <= sw->aimbottomslope)
return false;
return true;
}
//
// Handle shooting a thing.
//
static boolean PA_ShootThing(shootWork_t *sw, mobj_t *th, fixed_t interceptfrac)
{
fixed_t frac, dist, tmp;
fixed_t thingaimtopslope, thingaimbottomslope;
if(th == sw->shooter)
return true; // can't shoot self
if(!(th->flags & MF_SHOOTABLE))
return true; // corpse or something
// check angles to see if the thing can be aimed at
dist = FixedMul(sw->attackrange, interceptfrac);
thingaimtopslope = FixedDiv(th->z + th->height - sw->shootz, dist);
if(thingaimtopslope < sw->aimbottomslope)
return true; // shot over the thing
thingaimbottomslope = FixedDiv(th->z - sw->shootz, dist);
if(thingaimbottomslope > sw->aimtopslope)
return true; // shot under the thing
// this thing can be hit!
if(thingaimtopslope > sw->aimtopslope)
thingaimtopslope = sw->aimtopslope;
if(thingaimbottomslope < sw->aimbottomslope)
thingaimbottomslope = sw->aimbottomslope;
// shoot midway in the visible part of the thing
sw->shootslope = (thingaimtopslope + thingaimbottomslope) / 2;
sw->shootmobj = th;
// position a bit closer
frac = interceptfrac - FixedDiv(10*FRACUNIT, sw->attackrange);
sw->shootx = FixedMul(sw->shootdiv.dx, frac);
sw->shooty = FixedMul(sw->shootdiv.dy, frac);
tmp = FixedMul(frac, sw->attackrange);
tmp = FixedMul(sw->shootslope, tmp);
sw->shootx = sw->shootdiv.x + sw->shootx;
sw->shooty = sw->shootdiv.y + sw->shooty;
sw->shootz = sw->shootz + tmp;
return false; // don't go any further
}
//
// Process an intercept
//
#define COPY_INTERCEPT(dst,src) do { (dst)->d.line = (src)->d.line, (dst)->frac = (src)->frac, (dst)->isaline = (src)->isaline; } while(0)
static boolean PA_DoIntercept(shootWork_t *sw, intercept_t *in)
{
intercept_t temp;
if(sw->old_intercept.frac < in->frac)
{
COPY_INTERCEPT(&temp, &sw->old_intercept);
COPY_INTERCEPT(&sw->old_intercept, in);
COPY_INTERCEPT(in, &temp);
}
if(in->frac == 0 || in->frac >= FRACUNIT)
return true;
if(in->isaline)
return PA_ShootLine(sw, in->d.line, in->frac);
return PA_ShootThing(sw, in->d.mo, in->frac);
}
//
// Returns true if strace crosses the given subsector successfuly
//
static boolean PA_CrossSubsector(shootWork_t *sw, int bspnum)
{
seg_t *seg;
line_t *line;
int count;
fixed_t frac;
mobj_t *thing;
subsector_t *sub = &subsectors[bspnum];
intercept_t in;
vertex_t tv1, tv2;
VINT *lvalidcount, vc;
// check things
for(thing = sub->sector->thinglist; thing; thing = thing->snext)
{
if(thing->subsector != sub)
continue;
if(!(thing->flags & MF_SHOOTABLE))
continue; // corpse or something
// check a corner to corner cross-section for hit
if(sw->shootdivpositive)
{
tv1.x = thing->x - thing->radius;
tv1.y = thing->y + thing->radius;
tv2.x = thing->x + thing->radius;
tv2.y = thing->y - thing->radius;
}
else
{
tv1.x = thing->x - thing->radius;
tv1.y = thing->y - thing->radius;
tv2.x = thing->x + thing->radius;
tv2.y = thing->y + thing->radius;
}
frac = PA_SightCrossLine(sw, &tv1, &tv2);
if(frac < 0 || frac > FRACUNIT)
continue;
in.d.mo = thing;
in.isaline = false;
in.frac = frac;
if(!PA_DoIntercept(sw, &in))
return false;
}
// check lines
count = sub->numlines;
seg = &segs[sub->firstline];
I_GetThreadLocalVar(DOOMTLS_VALIDCOUNT, lvalidcount);
vc = *lvalidcount + 1;
if (vc == 0)
vc = 1;
*lvalidcount = vc;
++lvalidcount;
for(; count; seg++, count--)
{
int ld = seg->linedef;
line = &lines[ld];
if(lvalidcount[ld] == vc)
continue; // already checked other side
lvalidcount[ld] = vc;
frac = PA_SightCrossLine(sw, &vertexes[line->v1], &vertexes[line->v2]);
if(frac < 0 || frac > FRACUNIT)
continue;
in.d.line = line;
in.isaline = true;
in.frac = frac;
if(!PA_DoIntercept(sw, &in))
return false;
}
return true; // passed the subsector ok
}
/*
=====================
=
= PA_DivlineSide
=
=====================
*/
static int PA_DivlineSide(fixed_t x, fixed_t y, divline_t *line)
{
fixed_t dx, dy;
x = (x - line->x) >> FRACBITS;
y = (y - line->y) >> FRACBITS;
dx = x * (line->dy >> FRACBITS);
dy = y * (line->dx >> FRACBITS);
return (dy < dx) ^ 1;
}
//
// Walk the BSP tree to follow the trace.
//
static boolean PA_CrossBSPNode(shootWork_t *sw, int bspnum)
{
node_t *bsp;
int side, side2;
check:
if(bspnum & NF_SUBSECTOR)
{
// CALICO: bspnum == -1 case not originally handled here
return PA_CrossSubsector(sw, bspnum == -1 ? 0 : bspnum & ~NF_SUBSECTOR);
}
bsp = &nodes[bspnum];
// decide which side the start point is on
side = PA_DivlineSide(sw->shootdiv.x, sw->shootdiv.y, (divline_t*)bsp);
side2 = PA_DivlineSide(sw->shootx2, sw->shooty2, (divline_t*)bsp);
// cross the starting side
if(!PA_CrossBSPNode(sw, bsp->children[side]))
return false;
// the partition plane is crossed here
if(side == side2)
return true; // the line doesn't touch the other side
// cross the ending side
bspnum = bsp->children[side ^ 1];
goto check;
}
//
// Main function to trace a line attack.
//
void P_Shoot2(lineattack_t *la)
{
mobj_t *t1;
angle_t angle;
fixed_t tmp;
shootWork_t sw;
t1 = la->shooter;
sw.shooter = la->shooter;
sw.shootline = NULL;
sw.shootmobj = NULL;
angle = la->attackangle >> ANGLETOFINESHIFT;
sw.attackrange = la->attackrange;
sw.shootdiv.x = t1->x;
sw.shootdiv.y = t1->y;
sw.shootx2 = t1->x + (la->attackrange >> FRACBITS) * finecosine(angle);
sw.shooty2 = t1->y + (la->attackrange >> FRACBITS) * finesine(angle);
sw.shootdiv.dx = sw.shootx2 - sw.shootdiv.x;
sw.shootdiv.dy = sw.shooty2 - sw.shootdiv.y;
sw.shootz = t1->z + (t1->height >> 1) + 8*FRACUNIT;
sw.shootdivpositive = (sw.shootdiv.dx ^ sw.shootdiv.dy) > 0;
sw.ssx1 = sw.shootdiv.x >> FRACBITS;
sw.ssy1 = sw.shootdiv.y >> FRACBITS;
sw.ssx2 = sw.shootx2 >> FRACBITS;
sw.ssy2 = sw.shooty2 >> FRACBITS;
sw.aimtopslope = la->aimtopslope;
sw.aimbottomslope = la->aimbottomslope;
sw.aimmidslope = (la->aimtopslope + la->aimbottomslope) / 2;
// cross everything
sw.old_intercept.d.line = NULL;
sw.old_intercept.frac = 0;
sw.old_intercept.isaline = false;
PA_CrossBSPNode(&sw, numnodes - 1);
// check the last intercept if needed
if(!sw.shootmobj)
{
intercept_t in;
in.d.mo = NULL;
in.isaline = false;
in.frac = FRACUNIT;
PA_DoIntercept(&sw, &in);
}
la->shootline = sw.shootline;
la->shootmobj = sw.shootmobj;
la->shootslope = sw.shootslope;
la->shootx = sw.shootx;
la->shooty = sw.shooty;
la->shootz = sw.shootz;
// post-process
if(la->shootmobj)
return;
if(!la->shootline)
return;
// calculate the intercept point for the first line hit
// position a bit closer
sw.firstlinefrac -= FixedDiv(4*FRACUNIT, la->attackrange);
la->shootx = FixedMul(sw.shootdiv.dx, sw.firstlinefrac);
la->shooty = FixedMul(sw.shootdiv.dy, sw.firstlinefrac);
tmp = FixedMul(sw.firstlinefrac, la->attackrange);
tmp = FixedMul(tmp, sw.aimmidslope);
la->shootx = sw.shootdiv.x + la->shootx;
la->shooty = sw.shootdiv.y + la->shooty;
la->shootz += tmp;
}
// EOF