2 void W_GiveWeapon (entity e, float wep)
9 e.weapons |= WepSet_FromWeapon(wep);
15 { Send_Notification(NOTIF_ONE, other, MSG_MULTI, ITEM_WEAPON_GOT, wep); }
20 .float railgundistance;
22 void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, float deathtype)
24 vector hitloc, force, endpoint, dir;
26 float endq3surfaceflags;
33 entity pseudoprojectile;
36 pseudoprojectile = world;
38 railgun_start = start;
41 dir = normalize(end - start);
42 length = vlen(end - start);
45 // go a little bit into the wall because we need to hit this wall later
50 // trace multiple times until we hit a wall, each obstacle will be made
51 // non-solid so we can hit the next, while doing this we spawn effects and
52 // note down which entities were hit so we can damage them later
56 if(self.antilag_debug)
57 WarpZone_traceline_antilag (self, start, end, FALSE, o, self.antilag_debug);
59 WarpZone_traceline_antilag (self, start, end, FALSE, o, ANTILAG_LATENCY(self));
60 if(o && WarpZone_trace_firstzone)
66 if(trace_ent.solid == SOLID_BSP || trace_ent.solid == SOLID_SLIDEBOX)
67 Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, self);
69 // if it is world we can't hurt it so stop now
70 if (trace_ent == world || trace_fraction == 1)
73 // make the entity non-solid so we can hit the next one
74 trace_ent.railgunhit = TRUE;
75 trace_ent.railgunhitloc = end;
76 trace_ent.railgunhitsolidbackup = trace_ent.solid;
77 trace_ent.railgundistance = vlen(WarpZone_UnTransformOrigin(WarpZone_trace_transform, trace_endpos) - start);
78 trace_ent.railgunforce = WarpZone_TransformVelocity(WarpZone_trace_transform, force);
80 // stop if this is a wall
81 if (trace_ent.solid == SOLID_BSP)
84 // make the entity non-solid
85 trace_ent.solid = SOLID_NOT;
88 endpoint = trace_endpos;
90 endq3surfaceflags = trace_dphitq3surfaceflags;
92 // find all the entities the railgun hit and restore their solid state
93 ent = findfloat(world, railgunhit, TRUE);
96 // restore their solid type
97 ent.solid = ent.railgunhitsolidbackup;
98 ent = findfloat(ent, railgunhit, TRUE);
101 // spawn a temporary explosion entity for RadiusDamage calls
102 //explosion = spawn();
104 // Find all non-hit players the beam passed close by
105 if(deathtype == WEP_MINSTANEX || deathtype == WEP_NEX)
107 FOR_EACH_REALCLIENT(msg_entity) if(msg_entity != self) if(!msg_entity.railgunhit) if not(IS_SPEC(msg_entity) && msg_entity.enemy == self) // we use realclient, so spectators can hear the whoosh too
109 // nearest point on the beam
110 beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
112 f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
116 snd = strcat("weapons/nexwhoosh", ftos(floor(random() * 3) + 1), ".wav");
118 if(!pseudoprojectile)
119 pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
120 soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTEN_NONE);
124 remove(pseudoprojectile);
127 // find all the entities the railgun hit and hurt them
128 ent = findfloat(world, railgunhit, TRUE);
131 // get the details we need to call the damage function
132 hitloc = ent.railgunhitloc;
134 f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
135 ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
137 if(accuracy_isgooddamage(self.realowner, ent))
138 totaldmg += bdamage * f;
142 Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
144 // create a small explosion to throw gibs around (if applicable)
145 //setorigin (explosion, hitloc);
146 //RadiusDamage (explosion, self, 10, 0, 50, world, 300, deathtype);
148 ent.railgunhitloc = '0 0 0';
149 ent.railgunhitsolidbackup = SOLID_NOT;
150 ent.railgunhit = FALSE;
151 ent.railgundistance = 0;
153 // advance to the next entity
154 ent = findfloat(ent, railgunhit, TRUE);
157 // calculate hits and fired shots for hitscan
158 accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
160 trace_endpos = endpoint;
162 trace_dphitq3surfaceflags = endq3surfaceflags;
169 void W_BallisticBullet_Hit (void)
173 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
174 q = 1 + self.dmg_edge / self.dmg;
176 if(other.solid == SOLID_BSP || other.solid == SOLID_SLIDEBOX)
177 Damage_DamageInfo(self.origin, self.dmg * f, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * f, self.projectiledeathtype, other.species, self);
179 if(other && other != self.enemy)
181 endzcurveparticles();
184 railgun_start = self.origin - 2 * frametime * self.velocity;
185 railgun_end = self.origin + 2 * frametime * self.velocity;
186 g = accuracy_isgooddamage(self.realowner, other);
187 Damage(other, self, self.realowner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
189 /*if(yoda && (time > (self.last_yoda + 5)))
191 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_YODA);
192 self.last_yoda = time;
195 // calculate hits for ballistic weapons
198 // do not exceed 100%
199 q = min(self.dmg * q, self.dmg_total + f * self.dmg) - self.dmg_total;
200 self.dmg_total += f * self.dmg;
201 accuracy_add(self.realowner, self.realowner.weapon, 0, q);
205 self.enemy = other; // don't hit the same player twice with the same bullet
208 .void(void) W_BallisticBullet_LeaveSolid_think_save;
209 .float W_BallisticBullet_LeaveSolid_nextthink_save;
210 .vector W_BallisticBullet_LeaveSolid_origin;
211 .vector W_BallisticBullet_LeaveSolid_velocity;
213 void W_BallisticBullet_LeaveSolid_think()
215 setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
216 self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
218 self.think = self.W_BallisticBullet_LeaveSolid_think_save;
219 self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save);
220 self.W_BallisticBullet_LeaveSolid_think_save = func_null;
222 self.flags &= ~FL_ONGROUND;
224 if(self.enemy.solid == SOLID_BSP)
227 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
228 Damage_DamageInfo(self.origin, 0, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * -f, self.projectiledeathtype, 0, self);
231 UpdateCSQCProjectile(self);
234 float W_BallisticBullet_LeaveSolid(float eff)
236 // move the entity along its velocity until it's out of solid, then let it resume
237 vector vel = self.velocity;
238 float dt, dst, velfactor, v0, vs;
241 float constant = self.dmg_radius * (other.ballistics_density ? other.ballistics_density : 1);
243 // outside the world? forget it
244 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)
247 // special case for zero density and zero bullet constant:
249 if(self.dmg_radius == 0)
251 if(other.ballistics_density < 0)
252 constant = 0; // infinite travel distance
254 return 0; // no penetration
258 if(other.ballistics_density < 0)
259 constant = 0; // infinite travel distance
260 else if(other.ballistics_density == 0)
261 constant = self.dmg_radius;
263 constant = self.dmg_radius * other.ballistics_density;
266 // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
269 E0_m = 0.5 * v0 * v0;
273 maxdist = E0_m / constant;
274 // maxdist = 0.5 * v0 * v0 / constant
275 // dprint("max dist = ", ftos(maxdist), "\n");
277 if(maxdist <= autocvar_g_ballistics_mindistance)
282 maxdist = vlen(other.maxs - other.mins) + 1; // any distance, as long as we leave the entity
285 traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self, TRUE);
286 if(trace_fraction == 1) // 1: we never got out of solid
289 self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
291 dst = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - self.origin));
292 // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
293 Es_m = E0_m - constant * dst;
296 // roundoff errors got us
302 dt = dst / (0.5 * (v0 + vs));
303 // this is not correct, but the differential equations have no analytic
304 // solution - and these times are very small anyway
305 //print("dt = ", ftos(dt), "\n");
307 self.W_BallisticBullet_LeaveSolid_think_save = self.think;
308 self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
309 self.think = W_BallisticBullet_LeaveSolid_think;
310 self.nextthink = time + dt;
312 vel = vel * velfactor;
314 self.velocity = '0 0 0';
315 self.flags |= FL_ONGROUND; // prevent moving
316 self.W_BallisticBullet_LeaveSolid_velocity = vel;
319 if(vlen(trace_endpos - self.origin) > 4)
321 endzcurveparticles();
322 trailparticles(self, eff, self.origin, trace_endpos);
328 void W_BallisticBullet_Touch (void)
332 if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
336 W_BallisticBullet_Hit ();
338 if(self.dmg_radius < 0) // these NEVER penetrate solid
344 // if we hit "weapclip", bail out
346 // rationale of this check:
348 // any shader that is solid, nodraw AND trans is meant to clip weapon
349 // shots and players, but has no other effect!
351 // if it is not trans, it is caulk and should not have this side effect
354 // common/weapclip (intended)
355 // common/noimpact (is supposed to eat projectiles, but is erased farther above)
356 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
357 if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
358 if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
365 if(!W_BallisticBullet_LeaveSolid(-1))
371 self.projectiledeathtype |= HITTYPE_BOUNCE;
374 void endFireBallisticBullet()
376 endzcurveparticles();
379 entity fireBallisticBullet_trace_callback_ent;
380 float fireBallisticBullet_trace_callback_eff;
381 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
383 if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
384 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
385 WarpZone_trace_forent = world;
389 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float force, float dtype, float tracereffects, float gravityfactor, float bulletconstant)
391 float lag, dt, savetime; //, density;
395 antilagging = (autocvar_g_antilag_bullets && (pSpeed >= autocvar_g_antilag_bullets));
399 proj.classname = "bullet";
400 proj.owner = proj.realowner = self;
401 PROJECTILE_MAKETRIGGER(proj);
402 if(gravityfactor > 0)
404 proj.movetype = MOVETYPE_TOSS;
405 proj.gravity = gravityfactor;
408 proj.movetype = MOVETYPE_FLY;
409 proj.think = SUB_Remove;
410 proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
411 W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
412 proj.angles = vectoangles(proj.velocity);
413 if(bulletconstant > 0)
414 proj.dmg_radius = autocvar_g_ballistics_materialconstant / bulletconstant;
415 else if(bulletconstant == 0)
418 proj.dmg_radius = -1;
419 // so: bulletconstant = bullet mass / area of bullet circle
420 setorigin(proj, start);
421 proj.flags = FL_PROJECTILE;
423 proj.touch = W_BallisticBullet_Touch;
425 proj.dmg_force = force;
426 proj.projectiledeathtype = dtype;
428 proj.oldvelocity = proj.velocity;
430 other = proj; MUTATOR_CALLHOOK(EditProjectile);
436 if(tracereffects & EF_RED)
437 eff = particleeffectnum("tr_rifle");
438 else if(tracereffects & EF_BLUE)
439 eff = particleeffectnum("tr_rifle_weak");
441 eff = particleeffectnum("tr_bullet");
443 // NOTE: this may severely throw off weapon balance
444 lag = ANTILAG_LATENCY(self);
447 if not(IS_REAL_CLIENT(self))
449 if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
450 lag = 0; // only do hitscan, but no antilag
455 antilag_takeback(pl, time - lag);
460 savetime = frametime;
465 // DP tracetoss is stupid and always traces in 0.05s
466 // ticks. This makes it trace in 0.05*0.125s ticks
472 self.velocity = self.velocity * 0.125;
473 self.gravity *= 0.125 * 0.125;
475 fireBallisticBullet_trace_callback_ent = self;
476 fireBallisticBullet_trace_callback_eff = eff;
477 WarpZone_TraceToss_ThroughZone(self, self.owner, world, fireBallisticBullet_trace_callback);
481 if(trace_fraction == 1)
483 // won't hit anything anytime soon (DP's
484 // tracetoss does 200 tics of, here,
485 // 0.05*0.125s, that is, 1.25 seconds
488 dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
489 setorigin(self, trace_endpos);
490 self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
492 if(!SUB_OwnerCheck())
494 if(SUB_NoImpactCheck())
498 W_BallisticBullet_Hit();
501 if(proj.dmg_radius < 0) // these NEVER penetrate solid
504 // if we hit "weapclip", bail out
506 // rationale of this check:
508 // any shader that is solid, nodraw AND trans is meant to clip weapon
509 // shots and players, but has no other effect!
511 // if it is not trans, it is caulk and should not have this side effect
514 // common/weapclip (intended)
515 // common/noimpact (is supposed to eat projectiles, but is erased farther above)
516 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
517 if not(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID)
518 if not(trace_dphitcontents & DPCONTENTS_OPAQUE)
522 if(!W_BallisticBullet_LeaveSolid((other && (other.solid != SOLID_BSP)) ? eff : -1))
525 W_BallisticBullet_LeaveSolid_think();
527 self.projectiledeathtype |= HITTYPE_BOUNCE;
529 frametime = savetime;
542 if(tracereffects & EF_RED)
543 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
544 else if(tracereffects & EF_BLUE)
545 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
547 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
550 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
554 dir = normalize(dir + randomvec() * spread);
555 end = start + dir * MAX_SHOT_DISTANCE;
556 if(self.antilag_debug)
557 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
559 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
563 if (pointcontents (trace_endpos) != CONTENT_SKY)
565 if not (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
566 Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, trace_ent.species, self);
568 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
573 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
575 float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
576 float is_from_owner = (inflictor == projowner);
577 float is_from_exception = (exception != -1);
579 //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")));
581 if(autocvar_g_projectiles_damage <= -2)
583 return FALSE; // no damage to projectiles at all, not even with the exceptions
585 else if(autocvar_g_projectiles_damage == -1)
587 if(is_from_exception)
588 return (exception); // if exception is detected, allow it to override
590 return FALSE; // otherwise, no other damage is allowed
592 else if(autocvar_g_projectiles_damage == 0)
594 if(is_from_exception)
595 return (exception); // if exception is detected, allow it to override
596 else if not(is_from_contents)
597 return FALSE; // otherwise, only allow damage from contents
599 else if(autocvar_g_projectiles_damage == 1)
601 if(is_from_exception)
602 return (exception); // if exception is detected, allow it to override
603 else if not(is_from_contents || is_from_owner)
604 return FALSE; // otherwise, only allow self damage and damage from contents
606 else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
608 if(is_from_exception)
609 return (exception); // if exception is detected, allow it to override
612 return TRUE; // if none of these return, then allow damage anyway.
615 void W_PrepareExplosionByDamage(entity attacker, void() explode)
617 self.takedamage = DAMAGE_NO;
618 self.event_damage = func_null;
620 if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
622 self.owner = attacker;
623 self.realowner = attacker;
626 // do not explode NOW but in the NEXT FRAME!
627 // because recursive calls to RadiusDamage are not allowed
628 self.nextthink = time;
629 self.think = explode;