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