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