]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_common.qc
ballistics: small fixes for hitting players who stand directly at a wall
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / w_common.qc
1
2 void W_GiveWeapon (entity e, float wep, string name)
3 {
4         entity oldself;
5
6         if (!wep)
7                 return;
8
9         WEPSET_OR_EW(e, wep);
10
11         oldself = self;
12         self = e;
13
14         if not(g_minstagib)
15         if (other.classname == "player")
16         {
17                 sprint (other, "You got the ^2");
18                 sprint (other, name);
19                 sprint (other, "\n");
20         }
21
22         self = oldself;
23 }
24
25 .float railgundistance;
26 .vector railgunforce;
27 void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, float deathtype)
28 {
29         vector hitloc, force, endpoint, dir;
30         entity ent, endent;
31         float endq3surfaceflags;
32         float totaldmg;
33         entity o;
34
35         float length;
36         vector beampos;
37         string snd;
38         entity pseudoprojectile;
39         float f, ffs;
40
41         pseudoprojectile = world;
42
43         railgun_start = start;
44         railgun_end = end;
45
46         dir = normalize(end - start);
47         length = vlen(end - start);
48         force = dir * bforce;
49
50         // go a little bit into the wall because we need to hit this wall later
51         end = end + dir;
52
53         totaldmg = 0;
54
55         // trace multiple times until we hit a wall, each obstacle will be made
56         // non-solid so we can hit the next, while doing this we spawn effects and
57         // note down which entities were hit so we can damage them later
58         o = self;
59         while (1)
60         {
61                 if(self.antilag_debug)
62                         WarpZone_traceline_antilag (self, start, end, FALSE, o, self.antilag_debug);
63                 else
64                         WarpZone_traceline_antilag (self, start, end, FALSE, o, ANTILAG_LATENCY(self));
65                 if(o && WarpZone_trace_firstzone)
66                 {
67                         o = world;
68                         continue;
69                 }
70
71                 if(trace_ent.solid == SOLID_BSP || trace_ent.solid == SOLID_SLIDEBOX)
72                         Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, self);
73
74                 // if it is world we can't hurt it so stop now
75                 if (trace_ent == world || trace_fraction == 1)
76                         break;
77
78                 // make the entity non-solid so we can hit the next one
79                 trace_ent.railgunhit = TRUE;
80                 trace_ent.railgunhitloc = end;
81                 trace_ent.railgunhitsolidbackup = trace_ent.solid;
82                 trace_ent.railgundistance = vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - start);
83                 trace_ent.railgunforce = WarpZone_TransformVelocity(WarpZone_trace_transform, force);
84
85                 // stop if this is a wall
86                 if (trace_ent.solid == SOLID_BSP)
87                         break;
88
89                 // make the entity non-solid
90                 trace_ent.solid = SOLID_NOT;
91         }
92
93         endpoint = trace_endpos;
94         endent = trace_ent;
95         endq3surfaceflags = trace_dphitq3surfaceflags;
96
97         // find all the entities the railgun hit and restore their solid state
98         ent = findfloat(world, railgunhit, TRUE);
99         while (ent)
100         {
101                 // restore their solid type
102                 ent.solid = ent.railgunhitsolidbackup;
103                 ent = findfloat(ent, railgunhit, TRUE);
104         }
105
106         // spawn a temporary explosion entity for RadiusDamage calls
107         //explosion = spawn();
108
109         // Find all non-hit players the beam passed close by
110         if(deathtype == WEP_MINSTANEX || deathtype == WEP_NEX)
111         {
112                 FOR_EACH_REALCLIENT(msg_entity) if(msg_entity != self) if(!msg_entity.railgunhit) if not(msg_entity.classname == "spectator" && msg_entity.enemy == self) // we use realclient, so spectators can hear the whoosh too
113                 {
114                         // nearest point on the beam
115                         beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
116
117                         f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
118                         if(f <= 0)
119                                 continue;
120
121                         snd = strcat("weapons/nexwhoosh", ftos(floor(random() * 3) + 1), ".wav");
122
123                         if(!pseudoprojectile)
124                                 pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
125                         soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTN_NONE);
126                 }
127
128                 if(pseudoprojectile)
129                         remove(pseudoprojectile);
130         }
131
132         // find all the entities the railgun hit and hurt them
133         ent = findfloat(world, railgunhit, TRUE);
134         while (ent)
135         {
136                 // get the details we need to call the damage function
137                 hitloc = ent.railgunhitloc;
138
139                 f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
140                 ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
141
142                 if(accuracy_isgooddamage(self.realowner, ent))
143                         totaldmg += bdamage * f;
144
145                 // apply the damage
146                 if (ent.takedamage)
147                         Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
148
149                 // create a small explosion to throw gibs around (if applicable)
150                 //setorigin (explosion, hitloc);
151                 //RadiusDamage (explosion, self, 10, 0, 50, world, 300, deathtype);
152
153                 ent.railgunhitloc = '0 0 0';
154                 ent.railgunhitsolidbackup = SOLID_NOT;
155                 ent.railgunhit = FALSE;
156                 ent.railgundistance = 0;
157
158                 // advance to the next entity
159                 ent = findfloat(ent, railgunhit, TRUE);
160         }
161
162         // calculate hits and fired shots for hitscan
163         accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
164
165         trace_endpos = endpoint;
166         trace_ent = endent;
167         trace_dphitq3surfaceflags = endq3surfaceflags;
168 }
169
170 .float dmg_edge;
171 .float dmg_force;
172 .float dmg_radius;
173 .float dmg_total;
174 void W_BallisticBullet_Hit (void)
175 {
176         float f, q, g;
177
178         f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
179         q = 1 + self.dmg_edge / self.dmg;
180
181         if(other.solid == SOLID_BSP || other.solid == SOLID_SLIDEBOX)
182                 Damage_DamageInfo(self.origin, self.dmg * f, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * f, self.projectiledeathtype, other.species, self);
183
184         if(other && other != self.enemy)
185         {
186                 endzcurveparticles();
187
188                 headshot = 0;
189                 yoda = 0;
190                 damage_headshotbonus = self.dmg_edge * f;
191                 railgun_start = self.origin - 2 * frametime * self.velocity;
192                 railgun_end = self.origin + 2 * frametime * self.velocity;
193                 g = accuracy_isgooddamage(self.realowner, other);
194                 Damage(other, self, self.realowner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
195                 damage_headshotbonus = 0;
196
197                 if(headshot)
198                         f *= q;
199                 if(self.dmg_edge > 0)
200                 {
201                         if(headshot)
202                                 AnnounceTo(self.realowner, "headshot");
203                         if(yoda)
204                                 AnnounceTo(self.realowner, "awesome");
205                 }
206
207                 // calculate hits for ballistic weapons
208                 if(g)
209                 {
210                         // do not exceed 100%
211                         q = min(self.dmg * q, self.dmg_total + f * self.dmg) - self.dmg_total;
212                         self.dmg_total += f * self.dmg;
213                         accuracy_add(self.realowner, self.realowner.weapon, 0, q);
214                 }
215         }
216
217         self.enemy = other; // don't hit the same player twice with the same bullet
218 }
219
220 .void(void) W_BallisticBullet_LeaveSolid_think_save;
221 .float W_BallisticBullet_LeaveSolid_nextthink_save;
222 .vector W_BallisticBullet_LeaveSolid_origin;
223 .vector W_BallisticBullet_LeaveSolid_velocity;
224
225 void W_BallisticBullet_LeaveSolid_think()
226 {
227         setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
228         self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
229
230         self.think = self.W_BallisticBullet_LeaveSolid_think_save;
231         self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save);
232         self.W_BallisticBullet_LeaveSolid_think_save = SUB_Null;
233
234         self.flags &~= FL_ONGROUND;
235
236         if(self.enemy.solid == SOLID_BSP)
237         {
238                 float f;
239                 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
240                 Damage_DamageInfo(self.origin, 0, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * -f, self.projectiledeathtype, 0, self);
241         }
242
243         UpdateCSQCProjectile(self);
244 }
245
246 float W_BallisticBullet_LeaveSolid(float eff)
247 {
248         // move the entity along its velocity until it's out of solid, then let it resume
249         vector vel = self.velocity;
250         float constant = self.dmg_radius * (other.ballistics_density ? other.ballistics_density : 1);
251         float dt, dst, velfactor, v0, vs;
252         float maxdist;
253         float E0_m, Es_m;
254
255         // outside the world? forget it
256         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)
257                 return 0;
258
259         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
260         v0 = vlen(vel);
261
262         E0_m = 0.5 * v0 * v0;
263         maxdist = E0_m / constant;
264         // maxdist = 0.5 * v0 * v0 / constant
265         // dprint("max dist = ", ftos(maxdist), "\n");
266
267         if(maxdist <= autocvar_g_ballistics_mindistance)
268                 return 0;
269
270         traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self, TRUE);
271         if(trace_fraction == 1) // 1: we never got out of solid
272                 return 0;
273
274         self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
275
276         dst = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - self.origin));
277         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
278         Es_m = E0_m - constant * dst;
279         if(Es_m <= 0)
280         {
281                 // roundoff errors got us
282                 return 0;
283         }
284         vs = sqrt(2 * Es_m);
285         velfactor = vs / v0;
286
287         dt = dst / (0.5 * (v0 + vs));
288         // this is not correct, but the differential equations have no analytic
289         // solution - and these times are very small anyway
290         //print("dt = ", ftos(dt), "\n");
291
292         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
293         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
294         self.think = W_BallisticBullet_LeaveSolid_think;
295         self.nextthink = time + dt;
296
297         vel = vel * velfactor;
298
299         self.velocity = '0 0 0';
300         self.flags |= FL_ONGROUND; // prevent moving
301         self.W_BallisticBullet_LeaveSolid_velocity = vel;
302
303         if(eff >= 0)
304                 if(vlen(trace_endpos - self.origin) > 4)
305                 {
306                         endzcurveparticles();
307                         trailparticles(self, eff, self.origin, trace_endpos);
308                 }
309
310         return 1;
311 }
312
313 void W_BallisticBullet_Touch (void)
314 {
315         float density;
316
317         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
318                 return;
319
320         PROJECTILE_TOUCH;
321         W_BallisticBullet_Hit ();
322
323         // if we hit "weapclip", bail out
324         //
325         // rationale of this check:
326         //
327         // any shader that is solid, nodraw AND trans is meant to clip weapon
328         // shots and players, but has no other effect!
329         //
330         // if it is not trans, it is caulk and should not have this side effect
331         //
332         // matching shaders:
333         //   common/weapclip (intended)
334         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
335         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
336         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
337         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
338         {
339                 remove(self);
340                 return;
341         }
342
343         // go through solid!
344         if(!W_BallisticBullet_LeaveSolid(-1))
345         {
346                 remove(self);
347                 return;
348         }
349
350         self.projectiledeathtype |= HITTYPE_BOUNCE;
351 }
352
353 void endFireBallisticBullet()
354 {
355         endzcurveparticles();
356 }
357
358 entity fireBallisticBullet_trace_callback_ent;
359 float fireBallisticBullet_trace_callback_eff;
360 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
361 {
362         if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
363                 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
364         WarpZone_trace_forent = world;
365         self.owner = world;
366 }
367
368 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float headshotbonus, float force, float dtype, float tracereffects, float gravityfactor, float bulletconstant)
369 {
370         float lag, dt, savetime, density;
371         entity pl, oldself;
372         float antilagging;
373
374         antilagging = (autocvar_g_antilag_bullets && (pSpeed >= autocvar_g_antilag_bullets));
375
376         entity proj;
377         proj = spawn();
378         proj.classname = "bullet";
379         proj.owner = proj.realowner = self;
380         PROJECTILE_MAKETRIGGER(proj);
381         if(gravityfactor > 0)
382         {
383                 proj.movetype = MOVETYPE_TOSS;
384                 proj.gravity = gravityfactor;
385         }
386         else
387                 proj.movetype = MOVETYPE_FLY;
388         proj.think = SUB_Remove;
389         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
390         W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
391         proj.angles = vectoangles(proj.velocity);
392         proj.dmg_radius = autocvar_g_ballistics_materialconstant / bulletconstant;
393         // so: bulletconstant = bullet mass / area of bullet circle
394         setorigin(proj, start);
395         proj.flags = FL_PROJECTILE;
396
397         proj.touch = W_BallisticBullet_Touch;
398         proj.dmg = damage;
399         proj.dmg_edge = headshotbonus;
400         proj.dmg_force = force;
401         proj.projectiledeathtype = dtype;
402
403         proj.oldvelocity = proj.velocity;
404
405         other = proj; MUTATOR_CALLHOOK(EditProjectile);
406
407         if(antilagging)
408         {
409                 float eff;
410
411                 if(tracereffects & EF_RED)
412                         eff = particleeffectnum("tr_rifle");
413                 else if(tracereffects & EF_BLUE)
414                         eff = particleeffectnum("tr_rifle_weak");
415                 else
416                         eff = particleeffectnum("tr_bullet");
417
418                 // NOTE: this may severely throw off weapon balance
419                 lag = ANTILAG_LATENCY(self);
420                 if(lag < 0.001)
421                         lag = 0;
422                 if(clienttype(self) != CLIENTTYPE_REAL)
423                         lag = 0;
424                 if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
425                         lag = 0; // only do hitscan, but no antilag
426
427                 if(lag)
428                         FOR_EACH_PLAYER(pl)
429                                 if(pl != self)
430                                         antilag_takeback(pl, time - lag);
431
432                 oldself = self;
433                 self = proj;
434
435                 savetime = frametime;
436                 frametime = 0.05;
437
438                 for(;;)
439                 {
440                         // DP tracetoss is stupid and always traces in 0.05s
441                         // ticks. This makes it trace in 0.05*0.125s ticks
442                         // instead.
443                         vector v0;
444                         float g0;
445                         v0 = self.velocity;
446                         g0 = self.gravity;
447                         self.velocity = self.velocity * 0.125;
448                         self.gravity *= 0.125 * 0.125;
449                         trace_fraction = 0;
450                         fireBallisticBullet_trace_callback_ent = self;
451                         fireBallisticBullet_trace_callback_eff = eff;
452                         WarpZone_TraceToss_ThroughZone(self, self.owner, world, fireBallisticBullet_trace_callback);
453                         self.velocity = v0;
454                         self.gravity = g0;
455
456                         if(trace_fraction == 1)
457                                 break;
458                                 // won't hit anything anytime soon (DP's
459                                 // tracetoss does 200 tics of, here,
460                                 // 0.05*0.125s, that is, 1.25 seconds
461
462                         other = trace_ent;
463                         dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
464                         setorigin(self, trace_endpos);
465                         self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
466
467                         if(!SUB_OwnerCheck())
468                         {
469                                 if(SUB_NoImpactCheck())
470                                         break;
471
472                                 // hit the player
473                                 W_BallisticBullet_Hit();
474                         }
475
476                         // if we hit "weapclip", bail out
477                         //
478                         // rationale of this check:
479                         //
480                         // any shader that is solid, nodraw AND trans is meant to clip weapon
481                         // shots and players, but has no other effect!
482                         //
483                         // if it is not trans, it is caulk and should not have this side effect
484                         //
485                         // matching shaders:
486                         //   common/weapclip (intended)
487                         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
488                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
489                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
490                         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
491                                 break;
492
493                         // go through solid!
494                         if(!W_BallisticBullet_LeaveSolid((other && (other.solid != SOLID_BSP)) ? eff : -1))
495                                 break;
496
497                         W_BallisticBullet_LeaveSolid_think();
498
499                         self.projectiledeathtype |= HITTYPE_BOUNCE;
500                 }
501                 frametime = savetime;
502                 self = oldself;
503
504                 if(lag)
505                         FOR_EACH_PLAYER(pl)
506                                 if(pl != self)
507                                         antilag_restore(pl);
508
509                 remove(proj);
510
511                 return;
512         }
513
514         if(tracereffects & EF_RED)
515                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
516         else if(tracereffects & EF_BLUE)
517                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
518         else
519                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
520 }
521
522 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
523 {
524         vector  end;
525
526         dir = normalize(dir + randomvec() * spread);
527         end = start + dir * MAX_SHOT_DISTANCE;
528         if(self.antilag_debug)
529                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
530         else
531                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
532
533         end = trace_endpos;
534
535         if (pointcontents (trace_endpos) != CONTENT_SKY)
536         {
537                 if not (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
538                         Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, trace_ent.species, self);                    
539
540                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
541         }
542         trace_endpos = end;
543 }
544
545 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
546 {
547         float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
548         float is_from_owner = (inflictor == projowner);
549         float is_from_exception = (exception != -1);
550         
551         //dprint(strcat("W_CheckProjectileDamage: from_contents ", ftos(is_from_contents), " : from_owner ", ftos(is_from_owner), " : exception ", strcat(ftos(is_from_exception), " (", ftos(exception), "). \n")));
552
553         if(autocvar_g_projectiles_damage <= -2)
554         {
555                 return FALSE; // no damage to projectiles at all, not even with the exceptions
556         }
557         else if(autocvar_g_projectiles_damage == -1)
558         {
559                 if(is_from_exception)
560                         return (exception); // if exception is detected, allow it to override
561                 else
562                         return FALSE; // otherwise, no other damage is allowed
563         }
564         else if(autocvar_g_projectiles_damage == 0)
565         {
566                 if(is_from_exception)
567                         return (exception); // if exception is detected, allow it to override
568                 else if not(is_from_contents)
569                         return FALSE; // otherwise, only allow damage from contents
570         }       
571         else if(autocvar_g_projectiles_damage == 1)
572         {
573                 if(is_from_exception)
574                         return (exception); // if exception is detected, allow it to override
575                 else if not(is_from_contents || is_from_owner)
576                         return FALSE; // otherwise, only allow self damage and damage from contents
577         }
578         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
579         {
580                 if(is_from_exception)
581                         return (exception); // if exception is detected, allow it to override
582         }
583
584         return TRUE; // if none of these return, then allow damage anyway.
585 }
586
587 void W_PrepareExplosionByDamage(entity attacker, void() explode)
588 {
589         self.takedamage = DAMAGE_NO;
590         self.event_damage = SUB_Null;
591         
592         if((attacker.flags & FL_CLIENT) && !autocvar_g_projectiles_keep_owner)
593         {
594                 self.owner = attacker;
595                 self.realowner = attacker;
596         }
597         
598         // do not explode NOW but in the NEXT FRAME!
599         // because recursive calls to RadiusDamage are not allowed
600         self.nextthink = time;
601         self.think = explode;
602 }