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