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