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