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