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