2 void W_GiveWeapon (entity e, float wep, string name)
15 if (other.classname == "player")
17 sprint (other, "You got the ^2");
25 .float railgundistance;
27 void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, float deathtype)
29 vector hitloc, force, endpoint, dir;
31 float endq3surfaceflags;
38 entity pseudoprojectile;
41 pseudoprojectile = world;
43 railgun_start = start;
46 dir = normalize(end - start);
47 length = vlen(end - start);
50 // go a little bit into the wall because we need to hit this wall later
55 // trace multiple times until we hit a wall, each obstacle will be made
56 // non-solid so we can hit the next, while doing this we spawn effects and
57 // note down which entities were hit so we can damage them later
61 if(self.antilag_debug)
62 WarpZone_traceline_antilag (self, start, end, FALSE, o, self.antilag_debug);
64 WarpZone_traceline_antilag (self, start, end, FALSE, o, ANTILAG_LATENCY(self));
65 if(o && WarpZone_trace_firstzone)
71 if(trace_ent.solid == SOLID_BSP || trace_ent.solid == SOLID_SLIDEBOX)
72 Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, self);
74 // if it is world we can't hurt it so stop now
75 if (trace_ent == world || trace_fraction == 1)
78 // make the entity non-solid so we can hit the next one
79 trace_ent.railgunhit = TRUE;
80 trace_ent.railgunhitloc = end;
81 trace_ent.railgunhitsolidbackup = trace_ent.solid;
82 trace_ent.railgundistance = vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - start);
83 trace_ent.railgunforce = WarpZone_TransformVelocity(WarpZone_trace_transform, force);
85 // stop if this is a wall
86 if (trace_ent.solid == SOLID_BSP)
89 // make the entity non-solid
90 trace_ent.solid = SOLID_NOT;
93 endpoint = trace_endpos;
95 endq3surfaceflags = trace_dphitq3surfaceflags;
97 // find all the entities the railgun hit and restore their solid state
98 ent = findfloat(world, railgunhit, TRUE);
101 // restore their solid type
102 ent.solid = ent.railgunhitsolidbackup;
103 ent = findfloat(ent, railgunhit, TRUE);
106 // spawn a temporary explosion entity for RadiusDamage calls
107 //explosion = spawn();
109 // Find all non-hit players the beam passed close by
110 if(deathtype == WEP_MINSTANEX || deathtype == WEP_NEX)
112 FOR_EACH_REALCLIENT(msg_entity) if(msg_entity != self) if(!msg_entity.railgunhit) if not(msg_entity.classname == "spectator" && msg_entity.enemy == self) // we use realclient, so spectators can hear the whoosh too
114 // nearest point on the beam
115 beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
117 f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
121 snd = strcat("weapons/nexwhoosh", ftos(floor(random() * 3) + 1), ".wav");
123 if(!pseudoprojectile)
124 pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
125 soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTN_NONE);
129 remove(pseudoprojectile);
132 // find all the entities the railgun hit and hurt them
133 ent = findfloat(world, railgunhit, TRUE);
136 // get the details we need to call the damage function
137 hitloc = ent.railgunhitloc;
139 f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
140 ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
142 if(accuracy_isgooddamage(self.realowner, ent))
143 totaldmg += bdamage * f;
147 Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
149 // create a small explosion to throw gibs around (if applicable)
150 //setorigin (explosion, hitloc);
151 //RadiusDamage (explosion, self, 10, 0, 50, world, 300, deathtype);
153 ent.railgunhitloc = '0 0 0';
154 ent.railgunhitsolidbackup = SOLID_NOT;
155 ent.railgunhit = FALSE;
156 ent.railgundistance = 0;
158 // advance to the next entity
159 ent = findfloat(ent, railgunhit, TRUE);
162 // calculate hits and fired shots for hitscan
163 accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
165 trace_endpos = endpoint;
167 trace_dphitq3surfaceflags = endq3surfaceflags;
173 void W_BallisticBullet_Hit (void)
177 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
178 q = 1 + self.dmg_edge / self.dmg;
180 if(other.solid == SOLID_BSP || other.solid == SOLID_SLIDEBOX)
181 Damage_DamageInfo(self.origin, self.dmg * f, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * f, self.projectiledeathtype, other.species, self);
183 if(other && other != self.enemy)
185 endzcurveparticles();
188 railgun_start = self.origin - 2 * frametime * self.velocity;
189 railgun_end = self.origin + 2 * frametime * self.velocity;
190 g = accuracy_isgooddamage(self.realowner, other);
191 Damage(other, self, self.realowner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
194 AnnounceTo(self.realowner, "awesome");
196 // calculate hits for ballistic weapons
199 // do not exceed 100%
200 q = min(self.dmg * q, self.dmg_total + f * self.dmg) - self.dmg_total;
201 self.dmg_total += f * self.dmg;
202 accuracy_add(self.realowner, self.realowner.weapon, 0, q);
206 self.enemy = other; // don't hit the same player twice with the same bullet
209 .void(void) W_BallisticBullet_LeaveSolid_think_save;
210 .float W_BallisticBullet_LeaveSolid_nextthink_save;
211 .vector W_BallisticBullet_LeaveSolid_origin;
212 .vector W_BallisticBullet_LeaveSolid_velocity;
214 void W_BallisticBullet_LeaveSolid_think()
216 setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
217 self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
219 self.think = self.W_BallisticBullet_LeaveSolid_think_save;
220 self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save);
221 self.W_BallisticBullet_LeaveSolid_think_save = func_null;
223 self.flags &~= FL_ONGROUND;
225 if(self.enemy.solid == SOLID_BSP)
228 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
229 Damage_DamageInfo(self.origin, 0, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * -f, self.projectiledeathtype, 0, self);
232 UpdateCSQCProjectile(self);
235 float W_BallisticBullet_LeaveSolid(float eff)
237 // move the entity along its velocity until it's out of solid, then let it resume
238 vector vel = self.velocity;
239 float dt, dst, velfactor, v0, vs;
242 float constant = self.dmg_radius * (other.ballistics_density ? other.ballistics_density : 1);
244 // outside the world? forget it
245 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 // special case for zero density and zero bullet constant:
250 if(self.dmg_radius == 0)
252 if(other.ballistics_density < 0)
253 constant = 0; // infinite travel distance
255 return 0; // no penetration
259 if(other.ballistics_density < 0)
260 constant = 0; // infinite travel distance
261 else if(other.ballistics_density == 0)
262 constant = self.dmg_radius;
264 constant = self.dmg_radius * other.ballistics_density;
267 // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
270 E0_m = 0.5 * v0 * v0;
274 maxdist = E0_m / constant;
275 // maxdist = 0.5 * v0 * v0 / constant
276 // dprint("max dist = ", ftos(maxdist), "\n");
278 if(maxdist <= autocvar_g_ballistics_mindistance)
283 maxdist = vlen(other.maxs - other.mins) + 1; // any distance, as long as we leave the entity
286 traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self, TRUE);
287 if(trace_fraction == 1) // 1: we never got out of solid
290 self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
292 dst = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - self.origin));
293 // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
294 Es_m = E0_m - constant * dst;
297 // roundoff errors got us
303 dt = dst / (0.5 * (v0 + vs));
304 // this is not correct, but the differential equations have no analytic
305 // solution - and these times are very small anyway
306 //print("dt = ", ftos(dt), "\n");
308 self.W_BallisticBullet_LeaveSolid_think_save = self.think;
309 self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
310 self.think = W_BallisticBullet_LeaveSolid_think;
311 self.nextthink = time + dt;
313 vel = vel * velfactor;
315 self.velocity = '0 0 0';
316 self.flags |= FL_ONGROUND; // prevent moving
317 self.W_BallisticBullet_LeaveSolid_velocity = vel;
320 if(vlen(trace_endpos - self.origin) > 4)
322 endzcurveparticles();
323 trailparticles(self, eff, self.origin, trace_endpos);
329 void W_BallisticBullet_Touch (void)
333 if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
337 W_BallisticBullet_Hit ();
339 if(self.dmg_radius < 0) // these NEVER penetrate solid
345 // if we hit "weapclip", bail out
347 // rationale of this check:
349 // any shader that is solid, nodraw AND trans is meant to clip weapon
350 // shots and players, but has no other effect!
352 // if it is not trans, it is caulk and should not have this side effect
355 // common/weapclip (intended)
356 // common/noimpact (is supposed to eat projectiles, but is erased farther above)
357 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
358 if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
359 if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
366 if(!W_BallisticBullet_LeaveSolid(-1))
372 self.projectiledeathtype |= HITTYPE_BOUNCE;
375 void endFireBallisticBullet()
377 endzcurveparticles();
380 entity fireBallisticBullet_trace_callback_ent;
381 float fireBallisticBullet_trace_callback_eff;
382 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
384 if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
385 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
386 WarpZone_trace_forent = world;
390 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float force, float dtype, float tracereffects, float gravityfactor, float bulletconstant)
392 float lag, dt, savetime; //, density;
396 antilagging = (autocvar_g_antilag_bullets && (pSpeed >= autocvar_g_antilag_bullets));
400 proj.classname = "bullet";
401 proj.owner = proj.realowner = self;
402 PROJECTILE_MAKETRIGGER(proj);
403 if(gravityfactor > 0)
405 proj.movetype = MOVETYPE_TOSS;
406 proj.gravity = gravityfactor;
409 proj.movetype = MOVETYPE_FLY;
410 proj.think = SUB_Remove;
411 proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
412 W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
413 proj.angles = vectoangles(proj.velocity);
414 if(bulletconstant > 0)
415 proj.dmg_radius = autocvar_g_ballistics_materialconstant / bulletconstant;
416 else if(bulletconstant == 0)
419 proj.dmg_radius = -1;
420 // so: bulletconstant = bullet mass / area of bullet circle
421 setorigin(proj, start);
422 proj.flags = FL_PROJECTILE;
424 proj.touch = W_BallisticBullet_Touch;
426 proj.dmg_force = force;
427 proj.projectiledeathtype = dtype;
429 proj.oldvelocity = proj.velocity;
431 other = proj; MUTATOR_CALLHOOK(EditProjectile);
437 if(tracereffects & EF_RED)
438 eff = particleeffectnum("tr_rifle");
439 else if(tracereffects & EF_BLUE)
440 eff = particleeffectnum("tr_rifle_weak");
442 eff = particleeffectnum("tr_bullet");
444 // NOTE: this may severely throw off weapon balance
445 lag = ANTILAG_LATENCY(self);
448 if(clienttype(self) != CLIENTTYPE_REAL)
450 if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
451 lag = 0; // only do hitscan, but no antilag
456 antilag_takeback(pl, time - lag);
461 savetime = frametime;
466 // DP tracetoss is stupid and always traces in 0.05s
467 // ticks. This makes it trace in 0.05*0.125s ticks
473 self.velocity = self.velocity * 0.125;
474 self.gravity *= 0.125 * 0.125;
476 fireBallisticBullet_trace_callback_ent = self;
477 fireBallisticBullet_trace_callback_eff = eff;
478 WarpZone_TraceToss_ThroughZone(self, self.owner, world, fireBallisticBullet_trace_callback);
482 if(trace_fraction == 1)
484 // won't hit anything anytime soon (DP's
485 // tracetoss does 200 tics of, here,
486 // 0.05*0.125s, that is, 1.25 seconds
489 dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
490 setorigin(self, trace_endpos);
491 self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
493 if(!SUB_OwnerCheck())
495 if(SUB_NoImpactCheck())
499 W_BallisticBullet_Hit();
502 if(proj.dmg_radius < 0) // these NEVER penetrate solid
505 // if we hit "weapclip", bail out
507 // rationale of this check:
509 // any shader that is solid, nodraw AND trans is meant to clip weapon
510 // shots and players, but has no other effect!
512 // if it is not trans, it is caulk and should not have this side effect
515 // common/weapclip (intended)
516 // common/noimpact (is supposed to eat projectiles, but is erased farther above)
517 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
518 if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
519 if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
523 if(!W_BallisticBullet_LeaveSolid((other && (other.solid != SOLID_BSP)) ? eff : -1))
526 W_BallisticBullet_LeaveSolid_think();
528 self.projectiledeathtype |= HITTYPE_BOUNCE;
530 frametime = savetime;
543 if(tracereffects & EF_RED)
544 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
545 else if(tracereffects & EF_BLUE)
546 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
548 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
551 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
555 dir = normalize(dir + randomvec() * spread);
556 end = start + dir * MAX_SHOT_DISTANCE;
557 if(self.antilag_debug)
558 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
560 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
564 if (pointcontents (trace_endpos) != CONTENT_SKY)
566 if not (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
567 Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, trace_ent.species, self);
569 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
574 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
576 float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
577 float is_from_owner = (inflictor == projowner);
578 float is_from_exception = (exception != -1);
580 //dprint(strcat("W_CheckProjectileDamage: from_contents ", ftos(is_from_contents), " : from_owner ", ftos(is_from_owner), " : exception ", strcat(ftos(is_from_exception), " (", ftos(exception), "). \n")));
582 if(autocvar_g_projectiles_damage <= -2)
584 return FALSE; // no damage to projectiles at all, not even with the exceptions
586 else if(autocvar_g_projectiles_damage == -1)
588 if(is_from_exception)
589 return (exception); // if exception is detected, allow it to override
591 return FALSE; // otherwise, no other damage is allowed
593 else if(autocvar_g_projectiles_damage == 0)
595 if(is_from_exception)
596 return (exception); // if exception is detected, allow it to override
597 else if not(is_from_contents)
598 return FALSE; // otherwise, only allow damage from contents
600 else if(autocvar_g_projectiles_damage == 1)
602 if(is_from_exception)
603 return (exception); // if exception is detected, allow it to override
604 else if not(is_from_contents || is_from_owner)
605 return FALSE; // otherwise, only allow self damage and damage from contents
607 else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
609 if(is_from_exception)
610 return (exception); // if exception is detected, allow it to override
613 return TRUE; // if none of these return, then allow damage anyway.
616 void W_PrepareExplosionByDamage(entity attacker, void() explode)
618 self.takedamage = DAMAGE_NO;
619 self.event_damage = func_null;
621 if((attacker.flags & FL_CLIENT) && !autocvar_g_projectiles_keep_owner)
623 self.owner = attacker;
624 self.realowner = attacker;
627 // do not explode NOW but in the NEXT FRAME!
628 // because recursive calls to RadiusDamage are not allowed
629 self.nextthink = time;
630 self.think = explode;