]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_common.qc
special support for object density -1, which is always passed by ballistic bullets...
[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 dt, dst, velfactor, v0, vs;
251         float maxdist;
252         float E0_m, Es_m;
253         float constant = self.dmg_radius * (other.ballistics_density ? other.ballistics_density : 1);
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         // special case for zero density and zero bullet constant: 
260
261         if(self.dmg_radius == 0)
262         {
263                 if(other.ballistics_density < 0)
264                         constant = 0; // infinite travel distance
265                 else
266                         return 0; // no penetration
267         }
268         else
269         {
270                 if(other.ballistics_density < 0)
271                         constant = 0; // infinite travel distance
272                 else if(other.ballistics_density == 0)
273                         constant = self.dmg_radius;
274                 else
275                         constant = self.dmg_radius * other.ballistics_density;
276         }
277
278         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
279         v0 = vlen(vel);
280
281         E0_m = 0.5 * v0 * v0;
282
283         if(constant)
284         {
285                 maxdist = E0_m / constant;
286                 // maxdist = 0.5 * v0 * v0 / constant
287                 // dprint("max dist = ", ftos(maxdist), "\n");
288
289                 if(maxdist <= autocvar_g_ballistics_mindistance)
290                         return 0;
291         }
292         else
293         {
294                 maxdist = vlen(other.maxs - other.mins) + 1; // any distance, as long as we leave the entity
295         }
296
297         traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self, TRUE);
298         if(trace_fraction == 1) // 1: we never got out of solid
299                 return 0;
300
301         self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
302
303         dst = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - self.origin));
304         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
305         Es_m = E0_m - constant * dst;
306         if(Es_m <= 0)
307         {
308                 // roundoff errors got us
309                 return 0;
310         }
311         vs = sqrt(2 * Es_m);
312         velfactor = vs / v0;
313
314         dt = dst / (0.5 * (v0 + vs));
315         // this is not correct, but the differential equations have no analytic
316         // solution - and these times are very small anyway
317         //print("dt = ", ftos(dt), "\n");
318
319         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
320         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
321         self.think = W_BallisticBullet_LeaveSolid_think;
322         self.nextthink = time + dt;
323
324         vel = vel * velfactor;
325
326         self.velocity = '0 0 0';
327         self.flags |= FL_ONGROUND; // prevent moving
328         self.W_BallisticBullet_LeaveSolid_velocity = vel;
329
330         if(eff >= 0)
331                 if(vlen(trace_endpos - self.origin) > 4)
332                 {
333                         endzcurveparticles();
334                         trailparticles(self, eff, self.origin, trace_endpos);
335                 }
336
337         return 1;
338 }
339
340 void W_BallisticBullet_Touch (void)
341 {
342         float density;
343
344         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
345                 return;
346
347         PROJECTILE_TOUCH;
348         W_BallisticBullet_Hit ();
349
350         // if we hit "weapclip", bail out
351         //
352         // rationale of this check:
353         //
354         // any shader that is solid, nodraw AND trans is meant to clip weapon
355         // shots and players, but has no other effect!
356         //
357         // if it is not trans, it is caulk and should not have this side effect
358         //
359         // matching shaders:
360         //   common/weapclip (intended)
361         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
362         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
363         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
364         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
365         {
366                 remove(self);
367                 return;
368         }
369
370         // go through solid!
371         if(!W_BallisticBullet_LeaveSolid(-1))
372         {
373                 remove(self);
374                 return;
375         }
376
377         self.projectiledeathtype |= HITTYPE_BOUNCE;
378 }
379
380 void endFireBallisticBullet()
381 {
382         endzcurveparticles();
383 }
384
385 entity fireBallisticBullet_trace_callback_ent;
386 float fireBallisticBullet_trace_callback_eff;
387 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
388 {
389         if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
390                 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
391         WarpZone_trace_forent = world;
392         self.owner = world;
393 }
394
395 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)
396 {
397         float lag, dt, savetime, density;
398         entity pl, oldself;
399         float antilagging;
400
401         antilagging = (autocvar_g_antilag_bullets && (pSpeed >= autocvar_g_antilag_bullets));
402
403         entity proj;
404         proj = spawn();
405         proj.classname = "bullet";
406         proj.owner = proj.realowner = self;
407         PROJECTILE_MAKETRIGGER(proj);
408         if(gravityfactor > 0)
409         {
410                 proj.movetype = MOVETYPE_TOSS;
411                 proj.gravity = gravityfactor;
412         }
413         else
414                 proj.movetype = MOVETYPE_FLY;
415         proj.think = SUB_Remove;
416         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
417         W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
418         proj.angles = vectoangles(proj.velocity);
419         if(bulletconstant)
420                 proj.dmg_radius = autocvar_g_ballistics_materialconstant / bulletconstant;
421         else
422                 proj.dmg_radius = 0;
423         // so: bulletconstant = bullet mass / area of bullet circle
424         setorigin(proj, start);
425         proj.flags = FL_PROJECTILE;
426
427         proj.touch = W_BallisticBullet_Touch;
428         proj.dmg = damage;
429         proj.dmg_edge = headshotbonus;
430         proj.dmg_force = force;
431         proj.projectiledeathtype = dtype;
432
433         proj.oldvelocity = proj.velocity;
434
435         other = proj; MUTATOR_CALLHOOK(EditProjectile);
436
437         if(antilagging)
438         {
439                 float eff;
440
441                 if(tracereffects & EF_RED)
442                         eff = particleeffectnum("tr_rifle");
443                 else if(tracereffects & EF_BLUE)
444                         eff = particleeffectnum("tr_rifle_weak");
445                 else
446                         eff = particleeffectnum("tr_bullet");
447
448                 // NOTE: this may severely throw off weapon balance
449                 lag = ANTILAG_LATENCY(self);
450                 if(lag < 0.001)
451                         lag = 0;
452                 if(clienttype(self) != CLIENTTYPE_REAL)
453                         lag = 0;
454                 if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
455                         lag = 0; // only do hitscan, but no antilag
456
457                 if(lag)
458                         FOR_EACH_PLAYER(pl)
459                                 if(pl != self)
460                                         antilag_takeback(pl, time - lag);
461
462                 oldself = self;
463                 self = proj;
464
465                 savetime = frametime;
466                 frametime = 0.05;
467
468                 for(;;)
469                 {
470                         // DP tracetoss is stupid and always traces in 0.05s
471                         // ticks. This makes it trace in 0.05*0.125s ticks
472                         // instead.
473                         vector v0;
474                         float g0;
475                         v0 = self.velocity;
476                         g0 = self.gravity;
477                         self.velocity = self.velocity * 0.125;
478                         self.gravity *= 0.125 * 0.125;
479                         trace_fraction = 0;
480                         fireBallisticBullet_trace_callback_ent = self;
481                         fireBallisticBullet_trace_callback_eff = eff;
482                         WarpZone_TraceToss_ThroughZone(self, self.owner, world, fireBallisticBullet_trace_callback);
483                         self.velocity = v0;
484                         self.gravity = g0;
485
486                         if(trace_fraction == 1)
487                                 break;
488                                 // won't hit anything anytime soon (DP's
489                                 // tracetoss does 200 tics of, here,
490                                 // 0.05*0.125s, that is, 1.25 seconds
491
492                         other = trace_ent;
493                         dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
494                         setorigin(self, trace_endpos);
495                         self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
496
497                         if(!SUB_OwnerCheck())
498                         {
499                                 if(SUB_NoImpactCheck())
500                                         break;
501
502                                 // hit the player
503                                 W_BallisticBullet_Hit();
504                         }
505
506                         // if we hit "weapclip", bail out
507                         //
508                         // rationale of this check:
509                         //
510                         // any shader that is solid, nodraw AND trans is meant to clip weapon
511                         // shots and players, but has no other effect!
512                         //
513                         // if it is not trans, it is caulk and should not have this side effect
514                         //
515                         // matching shaders:
516                         //   common/weapclip (intended)
517                         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
518                         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
519                         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
520                         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
521                                 break;
522
523                         // go through solid!
524                         if(!W_BallisticBullet_LeaveSolid((other && (other.solid != SOLID_BSP)) ? eff : -1))
525                                 break;
526
527                         W_BallisticBullet_LeaveSolid_think();
528
529                         self.projectiledeathtype |= HITTYPE_BOUNCE;
530                 }
531                 frametime = savetime;
532                 self = oldself;
533
534                 if(lag)
535                         FOR_EACH_PLAYER(pl)
536                                 if(pl != self)
537                                         antilag_restore(pl);
538
539                 remove(proj);
540
541                 return;
542         }
543
544         if(tracereffects & EF_RED)
545                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
546         else if(tracereffects & EF_BLUE)
547                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
548         else
549                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
550 }
551
552 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
553 {
554         vector  end;
555
556         dir = normalize(dir + randomvec() * spread);
557         end = start + dir * MAX_SHOT_DISTANCE;
558         if(self.antilag_debug)
559                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
560         else
561                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
562
563         end = trace_endpos;
564
565         if (pointcontents (trace_endpos) != CONTENT_SKY)
566         {
567                 if not (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
568                         Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, trace_ent.species, self);                    
569
570                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
571         }
572         trace_endpos = end;
573 }
574
575 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
576 {
577         float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
578         float is_from_owner = (inflictor == projowner);
579         float is_from_exception = (exception != -1);
580         
581         //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")));
582
583         if(autocvar_g_projectiles_damage <= -2)
584         {
585                 return FALSE; // no damage to projectiles at all, not even with the exceptions
586         }
587         else if(autocvar_g_projectiles_damage == -1)
588         {
589                 if(is_from_exception)
590                         return (exception); // if exception is detected, allow it to override
591                 else
592                         return FALSE; // otherwise, no other damage is allowed
593         }
594         else if(autocvar_g_projectiles_damage == 0)
595         {
596                 if(is_from_exception)
597                         return (exception); // if exception is detected, allow it to override
598                 else if not(is_from_contents)
599                         return FALSE; // otherwise, only allow damage from contents
600         }       
601         else if(autocvar_g_projectiles_damage == 1)
602         {
603                 if(is_from_exception)
604                         return (exception); // if exception is detected, allow it to override
605                 else if not(is_from_contents || is_from_owner)
606                         return FALSE; // otherwise, only allow self damage and damage from contents
607         }
608         else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
609         {
610                 if(is_from_exception)
611                         return (exception); // if exception is detected, allow it to override
612         }
613
614         return TRUE; // if none of these return, then allow damage anyway.
615 }
616
617 void W_PrepareExplosionByDamage(entity attacker, void() explode)
618 {
619         self.takedamage = DAMAGE_NO;
620         self.event_damage = SUB_Null;
621         
622         if((attacker.flags & FL_CLIENT) && !autocvar_g_projectiles_keep_owner)
623         {
624                 self.owner = attacker;
625                 self.realowner = attacker;
626         }
627         
628         // do not explode NOW but in the NEXT FRAME!
629         // because recursive calls to RadiusDamage are not allowed
630         self.nextthink = time;
631         self.think = explode;
632 }