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