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