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