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