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