-
Notifications
You must be signed in to change notification settings - Fork 17
/
projectiles.qc
511 lines (417 loc) · 14.3 KB
/
projectiles.qc
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
498
499
500
501
502
503
504
505
506
507
508
509
/*
================================================================
PROJECTILES
================================================================
*/
/*
============
traceline2
a more robust wrapper for traceline() that can ignore a wider array of entities.
performs one or more staggered traceline()s until the trace_ent isn't something we
actually want to ignore, so what would be one trace can turn into several.
============
*/
void(vector start, vector end, entity ignore, float traceflags) traceline2 =
{
entity ig = ignore;
//entity lastent = world;
vector src = start;
vector lastend = start;
float traces = 0;
float nomonsters = 0;
if (traceflags & TRACE_NOMONSTERS) nomonsters = TRUE;
float t_allsolid = 2;
float t_startsolid = -1;
float t_inopen = -1;
float t_inwater = -1;
if (trace_debug)
{
dprint("tracing from ");
dprint_vector(src);
dprint( " to ");
dprint_vector(end);
dprint("\n");
}
// check for trace-through cases and repeat
do {
if (trace_debug)
{
dprint(" trace ");
dprint_vector(src);
dprint( " to ");
dprint_vector(end);
dprint("\n");
}
traceline (src, end, nomonsters, ig);
if (traces == 0)
if ((traceflags & TRACE_WATER) == 0 && (trace_fraction == 1.0 || trace_ent == world))
return;
// preserve the accuracy of the trace result flags
if (t_startsolid == -1) t_startsolid = trace_startsolid;
if (!trace_startsolid)
{
t_allsolid = min(t_allsolid, trace_allsolid);
t_inopen = max(t_inopen, trace_inopen);
t_inwater = max(t_inwater, trace_inwater);
if ( trace_ent.deadflag == DEAD_DYING || // don't let guys in death frames who aren't nonsolid yet act like solid walls to bullets
trace_ent == ignore || // always ignore the original ignore ent on future loops
trace_ent.movetype == MOVETYPE_NOCLIP || // don't shoot noclipping players
invis_miss_check(trace_ent, start, end - start) ||
trace_ent.notrace) // flag to make things game-functionally nonsolid (monsterclip/etc)
{
// if two entities that are both to be ignored are touching, the trace will
// fail with a distance of zero once 'ig' changes, so nudge forward one unit
// to make sure the point of origin isn't touching the old ig
lastend = trace_endpos;
src = trace_endpos + normalize(end - start);
ig = trace_ent;
if (trace_debug) dprint3(" hit ", trace_ent.classname, ", trying again\n");
}
else ig = world;
}
else
{
// if the nudge forward put the trace start in a solid, the last trace should
// have been the end. this can happen when a notrace surface and a legit solid
// surface are coincident in the path of the trace2, and the notrace one is the
// one quake 'sees' first.
if (trace_debug) dprint(" trace started solid!\n");
trace_endpos = lastend;
ig = world;
}
traces++;
} while (ig != world); // true if we hit a wall or hit nothing
if (trace_debug) dprint3("trace complete after ", ftos(traces), " traces \n");
// unify global trace results so the calling function can pretend it only made one trace
float lastfrac = vlen(start - trace_endpos) / vlen(start - end);
entity lastent = trace_ent;
if (traceflags & (TRACE_NOMONSTERS|TRACE_WATER) == TRACE_WATER)
{
// thanks to a structural oddity in SV_ClipToLinks (world.c), a traceline() without nomonsters
// doesn't properly populate the open/inwater trace flags, so we have to do another nomonsters
// traceline2 over the path of the last one to catch the case of crossing water contents.
// we can't just check pointcontents at start and end because it doesn't catch when mappers do
// wacky things with water volumes (ie the E4 entry in the original start.bsp), and it can't be
// a singular traceline() because a monster might be looking through a notrace bmodel into water.
//dprint(" tracing again with nomonsters to get trace flags right\n");
traceline2(start, trace_endpos, ignore, TRACE_NOMONSTERS);
}
else
{
trace_startsolid = t_startsolid;
trace_allsolid = t_allsolid;
trace_inopen = t_inopen;
trace_inwater = t_inwater;
}
trace_fraction = lastfrac;
trace_ent = lastent;
if (trace_debug) {
if (trace_ent)
dprint3(" entity: ", trace_ent.classname, "\n");
dprint3(" end: ", vtos(trace_endpos), "\n");
dprint3(" fraction: ", ftos(trace_fraction*100), "%\n");
dprint3(" startsolid: ", ftos(trace_startsolid), "\n");
dprint3(" allsolid: ", ftos(trace_allsolid), "\n");
dprint3(" inopen: ", ftos(trace_inopen), "\n");
dprint("----------\n");
// dprint3(" inwater: ", ftos(trace_inwater), "\n----------\n");
}
}
/*
================================================================
PENETRATION AND PASSTHRU
we "patch" the limited options for solid/nonsolid interaction in the quake physics by
overriding the results of said physics in the progs .touch() functions that are called
immediately after.
to emulate objects being nonsolid to each other when quake didn't want them to be,
we respond to their collisions by setting one as the .owner of the other, which causes
quake to ignore their collisions on subsequent frames, then restoring the physics to
roughly the state they'd be in if SV_Move had not altered their velocities in response to
that collision. in the case of linear projectiles, this means restoring their starting
velocity, but for TOSS/BOUNCE projectiles it means tracking their always-changing
velocities constantly with a frequent .think(). thus all grenades, as well as gibs,
think at 20hz until they come to rest. not ideal, but not seriously so.
this is further complicated by not being able to alter .velocity from the touch function
itself, because at that point quake is in the physics half of the game frame when such
monkeying is not allowed. thus, we have to make our corrections immediately thereafter
in a .think(), while preserving the previous .think() and .nextthink so as not to break
the projectile's normal behavior (voreball homing, particles, etc).
on all projectiles, .touch must be some function that calls CheckProjectilePassthru() and
halts if it returns TRUE (representing "not an actual collision"). the check handles all
consequences behind the scenes.
on ballistic projectiles, .think and .nextthink are overridden, so they cannot be used
for projectile timeout. set .lifetime_finished to the time when projectile should expire,
and .th_die to the function to call when it does.
================================================================
*/
/*
============
projectile_passthru_think
think function to make a projectile ignore what it just hit
the new owner has been set by this point
============
*/
void() projectile_passthru_think =
{
self.flags = not(self.flags, FL_ONGROUND);
self.velocity = self.oldvelocity;
self.think = self.think1;
self.nextthink = self.pain_finished;
// nudge the projectile forward a tiny bit to be inside its new owner
setorigin(self, self.origin + normalize(self.velocity) * 0.1);
// if the old owner and the new owner are touching at the point that the
// projectile crosses over, next frame it'll be coincident with both old
// and new, the trace in sv_move will wind up being allsolid, and the
// projectile will travel through worldspawn.
}
/*
============
projectile_toss_think
when a passthru overrides server physics for a TOSS projectile, we must
restore a recent enough velocity that the projectile appeared not to touch.
grenades change velocity every frame, so they have to save it near-continuously
============
*/
void() projectile_toss_think =
{
if (time > self.lifetime_finished)
{
self.th_die();
return;
}
if (self.velocity == VEC_ORIGIN) // stopped on the ground
{
self.think = self.th_die;
self.nextthink = self.lifetime_finished;
// bprint3("at rest, exploding in ", ftos(self.nextthink), " seconds\n");
self.th_die = SUB_Null;
self.lifetime_finished = 0;
self.solid = SOLID_NOT;
return;
}
self.flags = not(self.flags, FL_ONGROUND);
self.oldvelocity = self.velocity;
self.nextthink = time + 0.05; // 10hz is probably enough, but 20hz is surely twice as good
}
/*
============
projectile_valid_passthru
returns TRUE in cases where we pass through and should not collide
============
*/
float() projectile_valid_passthru =
{
// SOLID_NOT entities touch SOLID_BSP by default without generating touch events.
// gibs had to be made SOLID_BBOX in order to make selectively passing through
// notrace/monsterclip even possible, so they need special handling by our owner-
// changing hackery to ensure they stay nonsolid in the other cases too
if (self.type == "gib")
{
if (other.movetype == MOVETYPE_PUSH || other.solid == SOLID_BSP)
return other.notrace;
return TRUE;
}
if (other.deadflag == DEAD_DYING || // corpses not yet on the ground
other.notrace || // specially marked (monsterclip)
invis_miss_check(other,self.origin,self.velocity) ||
other.movetype == MOVETYPE_NOCLIP || // ignore noclipping players
other == self.trueowner ) // still track the original owner - there are cases when monsters
// or players firing fat FLYMISSILE projectiles into an adjacent
// monsterclip can be hit by them when the owner switches
return TRUE;
return FALSE;
}
/*
============
projectile_passthru
we can't alter physics stuff to make the projectile physically penetrate during
touch/physics, so set an immediate think to 'repair' the state of the projectile
============
*/
void() projectile_passthru =
{
self.owner = other;
// our nextthink was probably to explode or remove, so save that
self.think1 = self.think;
self.pain_finished = self.nextthink;
self.think = projectile_passthru_think;
self.nextthink = time;
}
/*
============
CheckProjectilePassthru
called within every projectile touch function, returns TRUE if pierced an otherwise solid entity
============
*/
float() CheckProjectilePassthru =
{
if (!other) return FALSE; // always collide with world
if (other == self.owner) return FALSE; // this actually shouldn't have been called, but: don't touch owner
if (other.classname == "func_void")
{
// go away at the bottoms of pits
SUB_RemoveSoon();
return TRUE;
}
// handle overriding the quake physics with our own desired result
if (projectile_valid_passthru())
{
projectile_passthru();
return TRUE;
}
return FALSE;
}
void() projectile_touch_null = { CheckProjectilePassthru(); }
/*
========
invis_miss_check
when invisible, to split the difference between the near-invulnerability of notrace
and the cheap weak feeling of catching a stray grenade because you dodged wrong,
projectiles and hitscan are verified against a thin cylinder (20u diameter) so most
incidental or glancing hits miraculously miss, while only the real dead-on shots land
========
*/
float(entity tgt, vector start, vector dir) invis_miss_check =
{
if (!has_invis(tgt))
return FALSE;
vector p = CrossProduct('0 0 1', normalize(dir));
float d = (start - tgt.origin) * p;
return (fabs(d) >= 10);
}
/*
================================================================
MISSILES
if you're looking for 'newmis' and 'launch_spike' this is where they went
projectile .type: for general behavior (ie any grenade type gets pulled by wind tunnels)
projectile .classname: for specific behavior (knightspike vs wizspike)
projectiles therefore require a classname at the function parameter level
to ensure it never gets left off
================================================================
*/
/*
========
launch_projectile
base linear projectile, does not set model, touch, or damage
speed expected to derive from the length of 'vel'
========
*/
entity(vector org, vector vel, string cname) launch_projectile =
{
entity prj;
prj = spawn();
prj.owner = prj.trueowner = self;
prj.movetype = MOVETYPE_FLYMISSILE;
prj.solid = SOLID_BBOX;
prj.angles = vectoangles(vel);
prj.type = "spike";
prj.touch = projectile_touch_null;
prj.th_die = SUB_Remove;
prj.think = SUB_Remove;
prj.nextthink = time + 6;
setsize (prj, VEC_ORIGIN, VEC_ORIGIN);
setorigin (prj, org);
prj.velocity = prj.oldvelocity = vel;
prj.classname = cname;
return prj;
}
/*
========
toss_projectile
base ballistic projectile, does not set model, touch, or damage
speed expected to derive from the length of 'vel'
========
*/
entity(vector org, vector vel, string cname) toss_projectile =
{
entity g;
g = launch_projectile(org, vel, cname);
g.movetype = MOVETYPE_BOUNCE;
g.type = "grenade";
g.think = projectile_toss_think;
g.nextthink = time;
g.touch = projectile_touch_null;
g.th_die = SUB_Remove;
g.lifetime_finished = time + 6;
return g;
}
//================================================================
/*
========
launch_nail
basic hurty projectile, used by trap_spikeshooter and player nailguns
inherited from by lots of other hurty projectiles
========
*/
entity(vector org, vector vel) launch_nail =
{
entity spike;
spike = launch_projectile(org, vel, "spike");
spike.touch = spike_touch;
SUB_ChangeModel (spike, "progs/spike.mdl");
spike.dmg = 9;
return spike;
}
/*
========
launch_laser
used by trapshooter and enforcers
========
*/
entity(vector org, vector vel) launch_laser =
{
entity spike;
spike = launch_projectile(org, vel, "laser");
spike.touch = spike_touch;
spike.effects = EF_DIMLIGHT;
spike.movetype = MOVETYPE_FLY; // enforcer lasers aren't FLYMISSILE in id106
SUB_ChangeModel (spike, "progs/laser.mdl");
spike.dmg = 12;
return spike;
}
/*
========
launch_rocket
basic missile, used by player and ogre rocket fire
========
*/
entity(vector org, vector vel) launch_rocket =
{
entity missile;
missile = launch_projectile(org, vel, "rocket"); // FLYMISSILE
missile.dmg = 100;
missile.touch = T_MissileTouch;
missile.type = "missile";
SUB_ChangeModel (missile, "progs/missile.mdl");
return missile;
}
/*
========
launch_lavamissile
exploding cthon lavaball
========
*/
entity(vector org, vector vel) launch_lavamissile =
{
entity missile;
missile = launch_rocket(org, vel); // FLYMISSILE
missile.classname = "lavaball";
missile.avelocity = '200 200 200';
SUB_ChangeModel (missile, "progs/lavaball.mdl");
return missile;
}
/*
========
launch_grenade
basic grenade, used by player and ogre
========
*/
entity(vector org, vector vel) launch_grenade =
{
entity grenade;
grenade = toss_projectile(org, vel, "grenade");
grenade.lifetime_finished = time + 2.5;
grenade.avelocity = '300 300 300';
SUB_ChangeModel (grenade, "progs/grenade.mdl");
return grenade;
}