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