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