]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/weapons/tracing.qc
Move tracing methods into their own file
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / tracing.qc
1 .float antilag_debug;
2
3 vector w_shotorg;
4 vector w_shotdir;
5 vector w_shotend;
6
7 // this function calculates w_shotorg and w_shotdir based on the weapon model
8 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
9 // make sure you call makevectors first (FIXME?)
10 void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector mi, vector ma, float antilag, float recoil, string snd, float chan, float maxdamage, float range)
11 {
12         float nudge = 1; // added to traceline target and subtracted from result
13         float oldsolid;
14         vector vecs, dv;
15         oldsolid = ent.dphitcontentsmask;
16         if(ent.weapon == WEP_RIFLE)
17                 ent.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
18         else
19                 ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
20         if(antilag)
21                 WarpZone_traceline_antilag(world, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
22                 // passing world, because we do NOT want it to touch dphitcontentsmask
23         else
24                 WarpZone_TraceLine(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NOMONSTERS, ent);
25         ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
26
27         vector vf, vr, vu;
28         vf = v_forward;
29         vr = v_right;
30         vu = v_up;
31         w_shotend = WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos); // warpzone support
32         v_forward = vf;
33         v_right = vr;
34         v_up = vu;
35
36         // un-adjust trueaim if shotend is too close
37         if(vlen(w_shotend - (ent.origin + ent.view_ofs)) < autocvar_g_trueaim_minrange)
38                 w_shotend = ent.origin + ent.view_ofs + s_forward * autocvar_g_trueaim_minrange;
39
40         // track max damage
41         if(accuracy_canbegooddamage(ent))
42                 accuracy_add(ent, ent.weapon, maxdamage, 0);
43
44         W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
45
46         if(ent.weaponentity.movedir_x > 0)
47                 vecs = ent.weaponentity.movedir;
48         else
49                 vecs = '0 0 0';
50
51         dv = v_right * -vecs_y + v_up * vecs_z;
52         w_shotorg = ent.origin + ent.view_ofs + dv;
53
54         // now move the shotorg forward as much as requested if possible
55         if(antilag)
56         {
57                 if(ent.antilag_debug)
58                         tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
59                 else
60                         tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
61         }
62         else
63                 tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs_x + nudge), MOVE_NORMAL, ent);
64         w_shotorg = trace_endpos - v_forward * nudge;
65         // calculate the shotdir from the chosen shotorg
66         w_shotdir = normalize(w_shotend - w_shotorg);
67
68         if (antilag)
69         if (!ent.cvar_cl_noantilag)
70         {
71                 if (autocvar_g_antilag == 1) // switch to "ghost" if not hitting original
72                 {
73                         traceline(w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent);
74                         if (!trace_ent.takedamage)
75                         {
76                                 traceline_antilag_force (ent, w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
77                                 if (trace_ent.takedamage && IS_PLAYER(trace_ent))
78                                 {
79                                         entity e;
80                                         e = trace_ent;
81                                         traceline(w_shotorg, e.origin, MOVE_NORMAL, ent);
82                                         if(trace_ent == e)
83                                                 w_shotdir = normalize(trace_ent.origin - w_shotorg);
84                                 }
85                         }
86                 }
87                 else if(autocvar_g_antilag == 3) // client side hitscan
88                 {
89                         // this part MUST use prydon cursor
90                         if (ent.cursor_trace_ent)                 // client was aiming at someone
91                         if (ent.cursor_trace_ent != ent)         // just to make sure
92                         if (ent.cursor_trace_ent.takedamage)      // and that person is killable
93                         if (IS_PLAYER(ent.cursor_trace_ent)) // and actually a player
94                         {
95                                 // verify that the shot would miss without antilag
96                                 // (avoids an issue where guns would always shoot at their origin)
97                                 traceline(w_shotorg, w_shotorg + w_shotdir * range, MOVE_NORMAL, ent);
98                                 if (!trace_ent.takedamage)
99                                 {
100                                         // verify that the shot would hit if altered
101                                         traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
102                                         if (trace_ent == ent.cursor_trace_ent)
103                                                 w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
104                                         else
105                                                 print("antilag fail\n");
106                                 }
107                         }
108                 }
109         }
110
111         ent.dphitcontentsmask = oldsolid; // restore solid type (generally SOLID_SLIDEBOX)
112
113         if (!g_norecoil)
114                 ent.punchangle_x = recoil * -1;
115
116         if (snd != "")
117         {
118                 sound (ent, chan, snd, VOL_BASE, ATTN_NORM);
119                 W_PlayStrengthSound(ent);
120         }
121
122         // nudge w_shotend so a trace to w_shotend hits
123         w_shotend = w_shotend + normalize(w_shotend - w_shotorg) * nudge;
124 }
125
126 #define W_SetupShot_Dir_ProjectileSize(ent,s_forward,mi,ma,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize_Range(ent, s_forward, mi, ma, antilag, recoil, snd, chan, maxdamage, MAX_SHOT_DISTANCE)
127 #define W_SetupShot_ProjectileSize(ent,mi,ma,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, v_forward, mi, ma, antilag, recoil, snd, chan, maxdamage)
128 #define W_SetupShot_Dir(ent,s_forward,antilag,recoil,snd,chan,maxdamage) W_SetupShot_Dir_ProjectileSize(ent, s_forward, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage)
129 #define W_SetupShot(ent,antilag,recoil,snd,chan,maxdamage) W_SetupShot_ProjectileSize(ent, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage)
130 #define W_SetupShot_Range(ent,antilag,recoil,snd,chan,maxdamage,range) W_SetupShot_Dir_ProjectileSize_Range(ent, v_forward, '0 0 0', '0 0 0', antilag, recoil, snd, chan, maxdamage, range)
131
132 vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity, float forceAbsolute)
133 {
134         vector mdirection;
135         float mspeed;
136         vector outvelocity;
137
138         mvelocity = mvelocity * g_weaponspeedfactor;
139
140         mdirection = normalize(mvelocity);
141         mspeed = vlen(mvelocity);
142
143         outvelocity = get_shotvelocity(pvelocity, mdirection, mspeed, (forceAbsolute ? 0 : autocvar_g_projectiles_newton_style), autocvar_g_projectiles_newton_style_2_minfactor, autocvar_g_projectiles_newton_style_2_maxfactor);
144
145         return outvelocity;
146 }
147
148 #if 0
149 float mspercallsum;
150 float mspercallsstyle;
151 float mspercallcount;
152 #endif
153 void W_SetupProjectileVelocityEx(entity missile, vector dir, vector upDir, float pSpeed, float pUpSpeed, float pZSpeed, float spread, float forceAbsolute)
154 {
155         if(missile.owner == world)
156                 error("Unowned missile");
157
158         dir = dir + upDir * (pUpSpeed / pSpeed);
159         dir_z += pZSpeed / pSpeed;
160         pSpeed *= vlen(dir);
161         dir = normalize(dir);
162
163 #if 0
164         if(autocvar_g_projectiles_spread_style != mspercallsstyle)
165         {
166                 mspercallsum = mspercallcount = 0;
167                 mspercallsstyle = autocvar_g_projectiles_spread_style;
168         }
169         mspercallsum -= gettime(GETTIME_HIRES);
170 #endif
171         dir = W_CalculateSpread(dir, spread, g_weaponspreadfactor, autocvar_g_projectiles_spread_style);
172 #if 0
173         mspercallsum += gettime(GETTIME_HIRES);
174         mspercallcount += 1;
175         print("avg: ", ftos(mspercallcount / mspercallsum), " per sec\n");
176 #endif
177
178         missile.velocity = W_CalculateProjectileVelocity(missile.owner.velocity, pSpeed * dir, forceAbsolute);
179 }
180
181 void W_SetupProjectileVelocity(entity missile, float pSpeed, float spread)
182 {
183         W_SetupProjectileVelocityEx(missile, w_shotdir, v_up, pSpeed, 0, 0, spread, FALSE);
184 }
185
186 #define W_SETUPPROJECTILEVELOCITY_UP(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), cvar(#s "_speed_up"), cvar(#s "_speed_z"), cvar(#s "_spread"), FALSE)
187 #define W_SETUPPROJECTILEVELOCITY(m,s) W_SetupProjectileVelocityEx(m, w_shotdir, v_up, cvar(#s "_speed"), 0, 0, cvar(#s "_spread"), FALSE)
188
189
190 // ====================
191 //  Ballistics Tracing
192 // ====================
193
194 .float railgundistance;
195 .vector railgunforce;
196 void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, float deathtype)
197 {
198         vector hitloc, force, endpoint, dir;
199         entity ent, endent;
200         float endq3surfaceflags;
201         float totaldmg;
202         entity o;
203
204         float length;
205         vector beampos;
206         string snd;
207         entity pseudoprojectile;
208         float f, ffs;
209
210         pseudoprojectile = world;
211
212         railgun_start = start;
213         railgun_end = end;
214
215         dir = normalize(end - start);
216         length = vlen(end - start);
217         force = dir * bforce;
218
219         // go a little bit into the wall because we need to hit this wall later
220         end = end + dir;
221
222         totaldmg = 0;
223
224         // trace multiple times until we hit a wall, each obstacle will be made
225         // non-solid so we can hit the next, while doing this we spawn effects and
226         // note down which entities were hit so we can damage them later
227         o = self;
228         while (1)
229         {
230                 if(self.antilag_debug)
231                         WarpZone_traceline_antilag (self, start, end, FALSE, o, self.antilag_debug);
232                 else
233                         WarpZone_traceline_antilag (self, start, end, FALSE, o, ANTILAG_LATENCY(self));
234                 if(o && WarpZone_trace_firstzone)
235                 {
236                         o = world;
237                         continue;
238                 }
239
240                 if(trace_ent.solid == SOLID_BSP || trace_ent.solid == SOLID_SLIDEBOX)
241                         Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, self);
242
243                 // if it is world we can't hurt it so stop now
244                 if (trace_ent == world || trace_fraction == 1)
245                         break;
246
247                 // make the entity non-solid so we can hit the next one
248                 trace_ent.railgunhit = TRUE;
249                 trace_ent.railgunhitloc = end;
250                 trace_ent.railgunhitsolidbackup = trace_ent.solid;
251                 trace_ent.railgundistance = vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - start);
252                 trace_ent.railgunforce = WarpZone_TransformVelocity(WarpZone_trace_transform, force);
253
254                 // stop if this is a wall
255                 if (trace_ent.solid == SOLID_BSP)
256                         break;
257
258                 // make the entity non-solid
259                 trace_ent.solid = SOLID_NOT;
260         }
261
262         endpoint = trace_endpos;
263         endent = trace_ent;
264         endq3surfaceflags = trace_dphitq3surfaceflags;
265
266         // find all the entities the railgun hit and restore their solid state
267         ent = findfloat(world, railgunhit, TRUE);
268         while (ent)
269         {
270                 // restore their solid type
271                 ent.solid = ent.railgunhitsolidbackup;
272                 ent = findfloat(ent, railgunhit, TRUE);
273         }
274
275         // spawn a temporary explosion entity for RadiusDamage calls
276         //explosion = spawn();
277
278         // Find all non-hit players the beam passed close by
279         if(deathtype == WEP_MINSTANEX || deathtype == WEP_NEX)
280         {
281                 FOR_EACH_REALCLIENT(msg_entity) if(msg_entity != self) if(!msg_entity.railgunhit) if not(IS_SPEC(msg_entity) && msg_entity.enemy == self) // we use realclient, so spectators can hear the whoosh too
282                 {
283                         // nearest point on the beam
284                         beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
285
286                         f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
287                         if(f <= 0)
288                                 continue;
289
290                         snd = strcat("weapons/nexwhoosh", ftos(floor(random() * 3) + 1), ".wav");
291
292                         if(!pseudoprojectile)
293                                 pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
294                         soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTN_NONE);
295                 }
296
297                 if(pseudoprojectile)
298                         remove(pseudoprojectile);
299         }
300
301         // find all the entities the railgun hit and hurt them
302         ent = findfloat(world, railgunhit, TRUE);
303         while (ent)
304         {
305                 // get the details we need to call the damage function
306                 hitloc = ent.railgunhitloc;
307
308                 f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
309                 ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
310
311                 if(accuracy_isgooddamage(self.realowner, ent))
312                         totaldmg += bdamage * f;
313
314                 // apply the damage
315                 if (ent.takedamage)
316                         Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
317
318                 // create a small explosion to throw gibs around (if applicable)
319                 //setorigin (explosion, hitloc);
320                 //RadiusDamage (explosion, self, 10, 0, 50, world, world, 300, deathtype);
321
322                 ent.railgunhitloc = '0 0 0';
323                 ent.railgunhitsolidbackup = SOLID_NOT;
324                 ent.railgunhit = FALSE;
325                 ent.railgundistance = 0;
326
327                 // advance to the next entity
328                 ent = findfloat(ent, railgunhit, TRUE);
329         }
330
331         // calculate hits and fired shots for hitscan
332         accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
333
334         trace_endpos = endpoint;
335         trace_ent = endent;
336         trace_dphitq3surfaceflags = endq3surfaceflags;
337 }
338
339 .float dmg_force;
340 .float dmg_radius;
341 .float dmg_total;
342 //.float last_yoda;
343 void W_BallisticBullet_Hit (void)
344 {
345         float f, q, g;
346
347         f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
348         q = 1 + self.dmg_edge / self.dmg;
349
350         if(other.solid == SOLID_BSP || other.solid == SOLID_SLIDEBOX)
351                 Damage_DamageInfo(self.origin, self.dmg * f, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * f, self.projectiledeathtype, other.species, self);
352
353         if(other && other != self.enemy)
354         {
355                 endzcurveparticles();
356
357                 yoda = 0;
358                 railgun_start = self.origin - 2 * frametime * self.velocity;
359                 railgun_end = self.origin + 2 * frametime * self.velocity;
360                 g = accuracy_isgooddamage(self.realowner, other);
361                 Damage(other, self, self.realowner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
362
363                 /*if(yoda && (time > (self.last_yoda + 5)))
364                 {
365                         Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_YODA);
366                         self.last_yoda = time; 
367                 }*/
368
369                 // calculate hits for ballistic weapons
370                 if(g)
371                 {
372                         // do not exceed 100%
373                         q = min(self.dmg * q, self.dmg_total + f * self.dmg) - self.dmg_total;
374                         self.dmg_total += f * self.dmg;
375                         accuracy_add(self.realowner, self.realowner.weapon, 0, q);
376                 }
377         }
378
379         self.enemy = other; // don't hit the same player twice with the same bullet
380 }
381
382 .void(void) W_BallisticBullet_LeaveSolid_think_save;
383 .float W_BallisticBullet_LeaveSolid_nextthink_save;
384 .vector W_BallisticBullet_LeaveSolid_origin;
385 .vector W_BallisticBullet_LeaveSolid_velocity;
386
387 void W_BallisticBullet_LeaveSolid_think()
388 {
389         setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
390         self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
391
392         self.think = self.W_BallisticBullet_LeaveSolid_think_save;
393         self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save);
394         self.W_BallisticBullet_LeaveSolid_think_save = func_null;
395
396         self.flags &~= FL_ONGROUND;
397
398         if(self.enemy.solid == SOLID_BSP)
399         {
400                 float f;
401                 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
402                 Damage_DamageInfo(self.origin, 0, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * -f, self.projectiledeathtype, 0, self);
403         }
404
405         UpdateCSQCProjectile(self);
406 }
407
408 float W_BallisticBullet_LeaveSolid(float eff)
409 {
410         // move the entity along its velocity until it's out of solid, then let it resume
411         vector vel = self.velocity;
412         float dt, dst, velfactor, v0, vs;
413         float maxdist;
414         float E0_m, Es_m;
415         float constant = self.dmg_radius * (other.ballistics_density ? other.ballistics_density : 1);
416
417         // outside the world? forget it
418         if(self.origin_x > world.maxs_x || self.origin_y > world.maxs_y || self.origin_z > world.maxs_z || self.origin_x < world.mins_x || self.origin_y < world.mins_y || self.origin_z < world.mins_z)
419                 return 0;
420
421         // special case for zero density and zero bullet constant: 
422
423         if(self.dmg_radius == 0)
424         {
425                 if(other.ballistics_density < 0)
426                         constant = 0; // infinite travel distance
427                 else
428                         return 0; // no penetration
429         }
430         else
431         {
432                 if(other.ballistics_density < 0)
433                         constant = 0; // infinite travel distance
434                 else if(other.ballistics_density == 0)
435                         constant = self.dmg_radius;
436                 else
437                         constant = self.dmg_radius * other.ballistics_density;
438         }
439
440         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
441         v0 = vlen(vel);
442
443         E0_m = 0.5 * v0 * v0;
444
445         if(constant)
446         {
447                 maxdist = E0_m / constant;
448                 // maxdist = 0.5 * v0 * v0 / constant
449                 // dprint("max dist = ", ftos(maxdist), "\n");
450
451                 if(maxdist <= autocvar_g_ballistics_mindistance)
452                         return 0;
453         }
454         else
455         {
456                 maxdist = vlen(other.maxs - other.mins) + 1; // any distance, as long as we leave the entity
457         }
458
459         traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self, TRUE);
460         if(trace_fraction == 1) // 1: we never got out of solid
461                 return 0;
462
463         self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
464
465         dst = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - self.origin));
466         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
467         Es_m = E0_m - constant * dst;
468         if(Es_m <= 0)
469         {
470                 // roundoff errors got us
471                 return 0;
472         }
473         vs = sqrt(2 * Es_m);
474         velfactor = vs / v0;
475
476         dt = dst / (0.5 * (v0 + vs));
477         // this is not correct, but the differential equations have no analytic
478         // solution - and these times are very small anyway
479         //print("dt = ", ftos(dt), "\n");
480
481         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
482         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
483         self.think = W_BallisticBullet_LeaveSolid_think;
484         self.nextthink = time + dt;
485
486         vel = vel * velfactor;
487
488         self.velocity = '0 0 0';
489         self.flags |= FL_ONGROUND; // prevent moving
490         self.W_BallisticBullet_LeaveSolid_velocity = vel;
491
492         if(eff >= 0)
493                 if(vlen(trace_endpos - self.origin) > 4)
494                 {
495                         endzcurveparticles();
496                         trailparticles(self, eff, self.origin, trace_endpos);
497                 }
498
499         return 1;
500 }
501
502 void W_BallisticBullet_Touch (void)
503 {
504         //float density;
505
506         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
507                 return;
508
509         PROJECTILE_TOUCH;
510         W_BallisticBullet_Hit ();
511
512         if(self.dmg_radius < 0) // these NEVER penetrate solid
513         {
514                 remove(self);
515                 return;
516         }
517
518         // if we hit "weapclip", bail out
519         //
520         // rationale of this check:
521         //
522         // any shader that is solid, nodraw AND trans is meant to clip weapon
523         // shots and players, but has no other effect!
524         //
525         // if it is not trans, it is caulk and should not have this side effect
526         //
527         // matching shaders:
528         //   common/weapclip (intended)
529         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
530         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
531         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
532         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
533         {
534                 remove(self);
535                 return;
536         }
537
538         // go through solid!
539         if(!W_BallisticBullet_LeaveSolid(-1))
540         {
541                 remove(self);
542                 return;
543         }
544
545         self.projectiledeathtype |= HITTYPE_BOUNCE;
546 }
547
548 void endFireBallisticBullet()
549 {
550         endzcurveparticles();
551 }
552
553 entity fireBallisticBullet_trace_callback_ent;
554 float fireBallisticBullet_trace_callback_eff;
555 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
556 {
557         if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
558                 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
559         WarpZone_trace_forent = world;
560         self.owner = world;
561 }
562
563 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float force, float dtype, float tracereffects, float gravityfactor, float bulletconstant)
564 {
565         float lag, dt, savetime; //, density;
566         entity pl, oldself;
567         float antilagging;
568
569         antilagging = (autocvar_g_antilag_bullets && (pSpeed >= autocvar_g_antilag_bullets));
570
571         entity proj;
572         proj = spawn();
573         proj.classname = "bullet";
574         proj.owner = proj.realowner = self;
575         PROJECTILE_MAKETRIGGER(proj);
576         if(gravityfactor > 0)
577         {
578                 proj.movetype = MOVETYPE_TOSS;
579                 proj.gravity = gravityfactor;
580         }
581         else
582                 proj.movetype = MOVETYPE_FLY;
583         proj.think = SUB_Remove;
584         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
585         W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
586         proj.angles = vectoangles(proj.velocity);
587         if(bulletconstant > 0)
588                 proj.dmg_radius = autocvar_g_ballistics_materialconstant / bulletconstant;
589         else if(bulletconstant == 0)
590                 proj.dmg_radius = 0;
591         else
592                 proj.dmg_radius = -1;
593         // so: bulletconstant = bullet mass / area of bullet circle
594         setorigin(proj, start);
595         proj.flags = FL_PROJECTILE;
596
597         proj.touch = W_BallisticBullet_Touch;
598         proj.dmg = damage;
599         proj.dmg_force = force;
600         proj.projectiledeathtype = dtype;
601
602         proj.oldvelocity = proj.velocity;
603
604         other = proj; MUTATOR_CALLHOOK(EditProjectile);
605
606         if(antilagging)
607         {
608                 float eff;
609
610                 if(tracereffects & EF_RED)
611                         eff = particleeffectnum("tr_rifle");
612                 else if(tracereffects & EF_BLUE)
613                         eff = particleeffectnum("tr_rifle_weak");
614                 else
615                         eff = particleeffectnum("tr_bullet");
616
617                 // NOTE: this may severely throw off weapon balance
618                 lag = ANTILAG_LATENCY(self);
619                 if(lag < 0.001)
620                         lag = 0;
621                 if not(IS_REAL_CLIENT(self))
622                         lag = 0;
623                 if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
624                         lag = 0; // only do hitscan, but no antilag
625
626                 if(lag)
627                         FOR_EACH_PLAYER(pl)
628                                 if(pl != self)
629                                         antilag_takeback(pl, time - lag);
630
631                 oldself = self;
632                 self = proj;
633
634                 savetime = frametime;
635                 frametime = 0.05;
636
637                 for(;;)
638                 {
639                         // DP tracetoss is stupid and always traces in 0.05s
640                         // ticks. This makes it trace in 0.05*0.125s ticks
641                         // instead.
642                         vector v0;
643                         float g0;
644                         v0 = self.velocity;
645                         g0 = self.gravity;
646                         self.velocity = self.velocity * 0.125;
647                         self.gravity *= 0.125 * 0.125;
648                         trace_fraction = 0;
649                         fireBallisticBullet_trace_callback_ent = self;
650                         fireBallisticBullet_trace_callback_eff = eff;
651                         WarpZone_TraceToss_ThroughZone(self, self.owner, world, fireBallisticBullet_trace_callback);
652                         self.velocity = v0;
653                         self.gravity = g0;
654
655                         if(trace_fraction == 1)
656                                 break;
657                                 // won't hit anything anytime soon (DP's
658                                 // tracetoss does 200 tics of, here,
659                                 // 0.05*0.125s, that is, 1.25 seconds
660
661                         other = trace_ent;
662                         dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
663                         setorigin(self, trace_endpos);
664                         self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
665
666                         if(!SUB_OwnerCheck())
667                         {
668                                 if(SUB_NoImpactCheck())
669                                         break;
670
671                                 // hit the player
672                                 W_BallisticBullet_Hit();
673                         }
674
675                         if(proj.dmg_radius < 0) // these NEVER penetrate solid
676                                 break;
677
678                         // if we hit "weapclip", bail out
679                         //
680                         // rationale of this check:
681                         //
682                         // any shader that is solid, nodraw AND trans is meant to clip weapon
683                         // shots and players, but has no other effect!
684                         //
685                         // if it is not trans, it is caulk and should not have this side effect
686                         //
687                         // matching shaders:
688                         //   common/weapclip (intended)
689                         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
690                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
691                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
692                         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
693                                 break;
694
695                         // go through solid!
696                         if(!W_BallisticBullet_LeaveSolid((other && (other.solid != SOLID_BSP)) ? eff : -1))
697                                 break;
698
699                         W_BallisticBullet_LeaveSolid_think();
700
701                         self.projectiledeathtype |= HITTYPE_BOUNCE;
702                 }
703                 frametime = savetime;
704                 self = oldself;
705
706                 if(lag)
707                         FOR_EACH_PLAYER(pl)
708                                 if(pl != self)
709                                         antilag_restore(pl);
710
711                 remove(proj);
712
713                 return;
714         }
715
716         if(tracereffects & EF_RED)
717                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
718         else if(tracereffects & EF_BLUE)
719                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
720         else
721                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
722 }
723
724 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
725 {
726         vector  end;
727
728         dir = normalize(dir + randomvec() * spread);
729         end = start + dir * MAX_SHOT_DISTANCE;
730         if(self.antilag_debug)
731                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
732         else
733                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
734
735         end = trace_endpos;
736
737         if (pointcontents (trace_endpos) != CONTENT_SKY)
738         {
739                 if not (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
740                         Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, trace_ent.species, self);                    
741
742                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
743         }
744         trace_endpos = end;
745 }