]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/w_common.qc
typo fix, and some StartFrame speedup
[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);
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         // outside the world? forget it
196         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)
197                 return 0;
198
199         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
200         v0 = vlen(vel);
201
202         E0_m = 0.5 * v0 * v0;
203         maxdist = E0_m / constant;
204         // maxdist = 0.5 * v0 * v0 / constant
205         // dprint("max dist = ", ftos(maxdist), "\n");
206
207         if(maxdist <= 0.5)
208                 return 0;
209
210         traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self);
211
212         if(trace_fraction == 1) // 1: we never got out of solid
213                 return 0;
214
215         self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
216
217         dst = vlen(trace_endpos - self.origin);
218         // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
219         Es_m = E0_m - constant * dst;
220         if(Es_m <= 0)
221         {
222                 // roundoff errors got us
223                 return 0;
224         }
225         vs = sqrt(2 * Es_m);
226         velfactor = vs / v0;
227
228         dt = dst / (0.5 * (v0 + vs));
229         // this is not correct, but the differential equations have no analytic
230         // solution - and these times are very small anyway
231         //print("dt = ", ftos(dt), "\n");
232
233         self.W_BallisticBullet_LeaveSolid_think_save = self.think;
234         self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
235         self.think = W_BallisticBullet_LeaveSolid_think;
236         self.nextthink = time + dt;
237
238         vel = vel * velfactor;
239
240         self.velocity = '0 0 0';
241         self.flags |= FL_ONGROUND; // prevent moving
242         self.W_BallisticBullet_LeaveSolid_velocity = vel;
243
244         return 1;
245 }
246
247 void W_BallisticBullet_Touch (void)
248 {
249         if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
250                 return;
251
252         PROJECTILE_TOUCH;
253         W_BallisticBullet_Hit ();
254
255         // go through solid!
256         if(!W_BallisticBullet_LeaveSolid(self, self.velocity, self.dmg_radius))
257         {
258                 remove(self);
259                 return;
260         }
261
262         self.projectiledeathtype |= HITTYPE_BOUNCE;
263 }
264
265 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)
266 {
267         float lag, dt, savetime;
268         entity pl, oldself;
269
270         entity proj;
271         proj = spawn();
272         proj.classname = "bullet";
273         proj.owner = self;
274         proj.solid = SOLID_BBOX;
275         if(gravityfactor > 0)
276         {
277                 proj.movetype = MOVETYPE_TOSS;
278                 proj.gravity = gravityfactor;
279         }
280         else
281                 proj.movetype = MOVETYPE_FLY;
282         proj.think = SUB_Remove;
283         proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
284         proj.velocity = (dir + randomvec() * spread) * pSpeed;
285         W_SetupProjectileVelocity(proj);
286         proj.angles = vectoangles(proj.velocity);
287         proj.dmg_radius = cvar("g_ballistics_materialconstant") / bulletconstant;
288         // so: bulletconstant = bullet mass / area of bullet circle
289         setorigin(proj, start);
290         proj.flags = FL_PROJECTILE;
291
292         proj.touch = W_BallisticBullet_Touch;
293         proj.dmg = damage;
294         proj.dmg_edge = headshotbonus;
295         proj.dmg_force = force;
296         proj.projectiledeathtype = dtype;
297
298         proj.oldvelocity = proj.velocity;
299
300         if(cvar("g_antilag_bullets"))
301         if(pSpeed >= cvar("g_antilag_bullets"))
302         {
303                 // NOTE: this may severely throw off weapon balance
304                 lag = ANTILAG_LATENCY(self);
305                 if(lag < 0.001)
306                         lag = 0;
307                 if(clienttype(self) != CLIENTTYPE_REAL)
308                         lag = 0;
309                 if(cvar("g_antilag") == 0)
310                         lag = 0; // only do hitscan, but no antilag
311
312                 if(lag)
313                         FOR_EACH_PLAYER(pl)
314                                 antilag_takeback(pl, time - lag);
315
316                 oldself = self;
317                 self = proj;
318
319                 savetime = frametime;
320                 frametime = 0.05;
321
322                 for(;;)
323                 {
324                         // DP tracetoss is stupid and always traces in 0.05s
325                         // ticks. This makes it trace in 0.05*0.125s ticks
326                         // instead.
327                         vector v0;
328                         float g0;
329                         v0 = self.velocity;
330                         g0 = self.gravity;
331                         self.velocity = self.velocity * 0.125;
332                         self.gravity *= 0.125 * 0.125;
333                         trace_fraction = 0;
334                         tracetoss(self, oldself);
335                         self.velocity = v0;
336                         self.gravity = g0;
337
338                         if(vlen(trace_endpos - self.origin) > 32)
339                                 zcurveparticles_from_tracetoss(particleeffectnum("tr_bullet"), self.origin, trace_endpos, self.velocity);
340                         if(trace_fraction == 1)
341                                 break;
342                                 // won't hit anything anytime soon (DP's
343                                 // tracetoss does 200 tics of, here,
344                                 // 0.05*0.125s, that is, 1.25 seconds
345
346                         other = trace_ent;
347                         dt = vlen(trace_endpos - self.origin) / vlen(self.velocity); // this is only approximate!
348                         setorigin(self, trace_endpos);
349                         self.velocity_z -= sv_gravity * dt;
350
351                         if(!SUB_OwnerCheck())
352                         {
353                                 if(SUB_NoImpactCheck())
354                                         break;
355
356                                 // hit the player
357                                 W_BallisticBullet_Hit ();
358                         }
359
360                         // go through solid!
361                         if(!W_BallisticBullet_LeaveSolid(self, self.velocity, self.dmg_radius))
362                                 break;
363
364                         W_BallisticBullet_LeaveSolid_think();
365                 }
366                 frametime = savetime;
367                 self = oldself;
368
369                 if(lag)
370                         FOR_EACH_PLAYER(pl)
371                                 antilag_restore(pl);
372
373                 remove(proj);
374
375                 return;
376         }
377
378         if(tracereffects & EF_RED)
379                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
380         else
381                 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
382 }
383
384 /*
385  * not used any more
386 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
387 {
388         vector  end;
389         local entity e;
390
391         if(cvar("g_ballistics_force"))
392         {
393                 if (DEATH_ISWEAPON(dtype, WEP_SHOTGUN))
394                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_shotgun_speed"), 5, damage, 0, force, dtype, 0, 1, cvar("g_ballistics_force_shotgun_bulletconstant"));
395                 else
396                         fireBallisticBullet(start, dir, spread, cvar("g_ballistics_force_uzi_speed"), 5, damage, 0, force, dtype, 0, 1, cvar("g_ballistics_force_shotgun_bulletconstant"));
397                 return;
398         }
399
400         dir = dir + randomvec() * spread;
401         end = start + dir * MAX_SHOT_DISTANCE;
402         if(self.antilag_debug)
403                 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
404         else
405                 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
406
407         if (tracer)
408         {
409                 e = spawn();
410                 e.owner = self;
411                 e.movetype = MOVETYPE_FLY;
412                 e.solid = SOLID_NOT;
413                 e.think = SUB_Remove;
414                 e.nextthink = time + vlen(trace_endpos - start) / 6000;
415                 e.velocity = dir * 6000;
416                 e.angles = vectoangles(e.velocity);
417                 setorigin (e, start);
418                 e.flags = FL_PROJECTILE;
419
420                 CSQCProjectile(e, TRUE, PROJECTILE_BULLET, TRUE);
421         }
422
423         if ((trace_fraction != 1.0) && (pointcontents (trace_endpos) != CONTENT_SKY))
424         {
425                 if (trace_ent.solid == SOLID_BSP && !(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
426                         Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * force, dtype);
427                 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
428         }
429 }
430 */
431
432 void W_PrepareExplosionByDamage(entity attacker, void() explode)
433 {
434         self.takedamage = DAMAGE_NO;
435         self.event_damage = SUB_Null;
436         self.owner = attacker;
437
438         // do not explode NOW but in the NEXT FRAME!
439         // because recursive calls to RadiusDamage are not allowed
440         self.nextthink = time;
441         self.think = explode;
442 }