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