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