]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/w_common.qc
600f260f434c212c4b99e0bd830cf5653b68703b
[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(self.dmg_edge != 0)
184                 {
185                         if(headshot)
186                         {       
187                                 f *= q;
188                                 AnnounceTo(self.owner, "headshot");
189                         }
190                 }
191                 if(yoda)
192                         AnnounceTo(self.owner, "awesome");
193
194                 // calculate hits for ballistic weapons
195                 if(g)
196                 {
197                         // do not exceed 100%
198                         q = min(self.dmg * q, self.dmg_total + f * self.dmg) - self.dmg_total;
199                         self.dmg_total += f * self.dmg;
200                         accuracy_add(self.owner, self.owner.weapon, 0, q);
201                 }
202         }
203
204         self.enemy = other; // don't hit the same player twice with the same bullet
205 }
206
207 .void(void) W_BallisticBullet_LeaveSolid_think_save;
208 .float W_BallisticBullet_LeaveSolid_nextthink_save;
209 .vector W_BallisticBullet_LeaveSolid_origin;
210 .vector W_BallisticBullet_LeaveSolid_velocity;
211
212 void W_BallisticBullet_LeaveSolid_think()
213 {
214         setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
215         self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
216
217         self.think = self.W_BallisticBullet_LeaveSolid_think_save;
218         self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save);
219         self.W_BallisticBullet_LeaveSolid_think_save = SUB_Null;
220
221         self.flags &~= FL_ONGROUND;
222
223         if(self.enemy.solid == SOLID_BSP)
224         {
225                 float f;
226                 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
227                 Damage_DamageInfo(self.origin, 0, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * -f, self.projectiledeathtype, self);
228         }
229
230         UpdateCSQCProjectile(self);
231 }
232
233 float W_BallisticBullet_LeaveSolid(entity e, vector vel, float constant)
234 {
235         // move the entity along its velocity until it's out of solid, then let it resume
236
237         float dt, dst, velfactor, v0, vs;
238         float maxdist;
239         float E0_m, Es_m;
240
241         // outside the world? forget it
242         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)
243                 return 0;
244
245         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
246         v0 = vlen(vel);
247
248         E0_m = 0.5 * v0 * v0;
249         maxdist = E0_m / constant;
250         // maxdist = 0.5 * v0 * v0 / constant
251         // dprint("max dist = ", ftos(maxdist), "\n");
252
253         if(maxdist <= cvar("g_ballistics_mindistance"))
254                 return 0;
255
256         traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self);
257
258         if(trace_fraction == 1) // 1: we never got out of solid
259                 return 0;
260
261         self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
262
263         dst = max(cvar("g_ballistics_mindistance"), vlen(trace_endpos - self.origin));
264         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
265         Es_m = E0_m - constant * dst;
266         if(Es_m <= 0)
267         {
268                 // roundoff errors got us
269                 return 0;
270         }
271         vs = sqrt(2 * Es_m);
272         velfactor = vs / v0;
273
274         dt = dst / (0.5 * (v0 + vs));
275         // this is not correct, but the differential equations have no analytic
276         // solution - and these times are very small anyway
277         //print("dt = ", ftos(dt), "\n");
278
279         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
280         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
281         self.think = W_BallisticBullet_LeaveSolid_think;
282         self.nextthink = time + dt;
283
284         vel = vel * velfactor;
285
286         self.velocity = '0 0 0';
287         self.flags |= FL_ONGROUND; // prevent moving
288         self.W_BallisticBullet_LeaveSolid_velocity = vel;
289
290         return 1;
291 }
292
293 void W_BallisticBullet_Touch (void)
294 {
295         float density;
296
297         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
298                 return;
299
300         PROJECTILE_TOUCH;
301         W_BallisticBullet_Hit ();
302
303         // if we hit "weapclip", bail out
304         //
305         // rationale of this check:
306         //
307         // any shader that is solid, nodraw AND trans is meant to clip weapon
308         // shots and players, but has no other effect!
309         //
310         // if it is not trans, it is caulk and should not have this side effect
311         //
312         // matching shaders:
313         //   common/weapclip (intended)
314         //   common/noimpact (is supposed to eat projectiles, but is erased farther above)
315         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
316         if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
317         if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
318         {
319                 remove(self);
320                 return;
321         }
322
323         density = other.ballistics_density;
324         if(density == 0)
325                 density = 1;
326
327         // go through solid!
328         if(!W_BallisticBullet_LeaveSolid(self, self.velocity, self.dmg_radius * density))
329         {
330                 remove(self);
331                 return;
332         }
333
334         self.projectiledeathtype |= HITTYPE_BOUNCE;
335 }
336
337 void endFireBallisticBullet()
338 {
339         endzcurveparticles();
340 }
341
342 entity fireBallisticBullet_trace_callback_ent;
343 float fireBallisticBullet_trace_callback_eff;
344 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
345 {
346         if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
347                 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
348 }
349
350 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)
351 {
352         float lag, dt, savetime, density;
353         entity pl, oldself;
354         float antilagging;
355
356         antilagging = (cvar("g_antilag_bullets") && (pSpeed >= cvar("g_antilag_bullets")));
357
358         entity proj;
359         proj = spawn();
360         proj.classname = "bullet";
361         proj.owner = self;
362         PROJECTILE_MAKETRIGGER(proj);
363         if(gravityfactor > 0)
364         {
365                 proj.movetype = MOVETYPE_TOSS;
366                 proj.gravity = gravityfactor;
367         }
368         else
369                 proj.movetype = MOVETYPE_FLY;
370         proj.think = SUB_Remove;
371         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
372         W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
373         proj.angles = vectoangles(proj.velocity);
374         proj.dmg_radius = cvar("g_ballistics_materialconstant") / bulletconstant;
375         // so: bulletconstant = bullet mass / area of bullet circle
376         setorigin(proj, start);
377         proj.flags = FL_PROJECTILE;
378
379         proj.touch = W_BallisticBullet_Touch;
380         proj.dmg = damage;
381         proj.dmg_edge = headshotbonus;
382         proj.dmg_force = force;
383         proj.projectiledeathtype = dtype;
384
385         proj.oldvelocity = proj.velocity;
386
387         other = proj; MUTATOR_CALLHOOK(EditProjectile);
388
389         if(antilagging)
390         {
391                 float eff;
392
393                 if(tracereffects & EF_RED)
394                         eff = particleeffectnum("tr_rifle");
395                 else
396                         eff = particleeffectnum("tr_bullet");
397
398                 // NOTE: this may severely throw off weapon balance
399                 lag = ANTILAG_LATENCY(self);
400                 if(lag < 0.001)
401                         lag = 0;
402                 if(clienttype(self) != CLIENTTYPE_REAL)
403                         lag = 0;
404                 if(cvar("g_antilag") == 0 || self.cvar_cl_noantilag)
405                         lag = 0; // only do hitscan, but no antilag
406
407                 if(lag)
408                         FOR_EACH_PLAYER(pl)
409                                 antilag_takeback(pl, time - lag);
410
411                 oldself = self;
412                 self = proj;
413
414                 savetime = frametime;
415                 frametime = 0.05;
416
417                 for(;;)
418                 {
419                         // DP tracetoss is stupid and always traces in 0.05s
420                         // ticks. This makes it trace in 0.05*0.125s ticks
421                         // instead.
422                         vector v0;
423                         float g0;
424                         v0 = self.velocity;
425                         g0 = self.gravity;
426                         self.velocity = self.velocity * 0.125;
427                         self.gravity *= 0.125 * 0.125;
428                         trace_fraction = 0;
429                         fireBallisticBullet_trace_callback_ent = self;
430                         fireBallisticBullet_trace_callback_eff = eff;
431                         WarpZone_TraceToss_ThroughZone(self, oldself, world, fireBallisticBullet_trace_callback);
432                         self.velocity = v0;
433                         self.gravity = g0;
434
435                         if(trace_fraction == 1)
436                                 break;
437                                 // won't hit anything anytime soon (DP's
438                                 // tracetoss does 200 tics of, here,
439                                 // 0.05*0.125s, that is, 1.25 seconds
440
441                         other = trace_ent;
442                         dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
443                         setorigin(self, trace_endpos);
444                         self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
445
446                         if(!SUB_OwnerCheck())
447                         {
448                                 if(SUB_NoImpactCheck())
449                                         break;
450
451                                 // hit the player
452                                 W_BallisticBullet_Hit();
453                         }
454
455                         density = other.ballistics_density;
456                         if(density == 0)
457                                 density = 1;
458
459                         // go through solid!
460                         if(!W_BallisticBullet_LeaveSolid(self, self.velocity, self.dmg_radius * density))
461                                 break;
462
463                         W_BallisticBullet_LeaveSolid_think();
464                 }
465                 frametime = savetime;
466                 self = oldself;
467
468                 if(lag)
469                         FOR_EACH_PLAYER(pl)
470                                 antilag_restore(pl);
471
472                 remove(proj);
473
474                 return;
475         }
476
477         if(tracereffects & EF_RED)
478                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
479         else if(tracereffects & EF_BLUE)
480                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
481         else
482                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
483 }
484
485 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
486 {
487         vector  end;
488
489         dir = normalize(dir + randomvec() * spread);
490         end = start + dir * MAX_SHOT_DISTANCE;
491         if(self.antilag_debug)
492                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
493         else
494                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
495
496         end = trace_endpos;
497
498         if ((trace_fraction != 1.0) && (pointcontents (trace_endpos) != CONTENT_SKY))
499         {
500                 pointparticles(particleeffectnum("TE_KNIGHTSPIKE"),end,trace_plane_normal * 2500,1);
501                 if (trace_ent.solid == SOLID_BSP && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
502                         Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, self);
503                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
504                 //void(float effectnum, vector org, vector vel, float howmany) pointparticles = #337; // same as in CSQC
505         }
506         trace_endpos = end;
507 }
508
509 void W_PrepareExplosionByDamage(entity attacker, void() explode)
510 {
511         self.takedamage = DAMAGE_NO;
512         self.event_damage = SUB_Null;
513         self.owner = attacker;
514         self.realowner = attacker;
515
516         // do not explode NOW but in the NEXT FRAME!
517         // because recursive calls to RadiusDamage are not allowed
518         self.nextthink = time;
519         self.think = explode;
520 }