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