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)
108 if(msg_entity != self)
109 if(!msg_entity.railgunhit)
110 if(!(IS_SPEC(msg_entity) && msg_entity.enemy == self)) // we use realclient, so spectators can hear the whoosh too
112 // nearest point on the beam
113 beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
115 f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
119 snd = strcat("weapons/nexwhoosh", ftos(floor(random() * 3) + 1), ".wav");
121 if(!pseudoprojectile)
122 pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
123 soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTEN_NONE);
127 remove(pseudoprojectile);
130 // find all the entities the railgun hit and hurt them
131 ent = findfloat(world, railgunhit, TRUE);
134 // get the details we need to call the damage function
135 hitloc = ent.railgunhitloc;
137 f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
138 ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
140 if(accuracy_isgooddamage(self.realowner, ent))
141 totaldmg += bdamage * f;
145 Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
147 // create a small explosion to throw gibs around (if applicable)
148 //setorigin (explosion, hitloc);
149 //RadiusDamage (explosion, self, 10, 0, 50, world, 300, deathtype);
151 ent.railgunhitloc = '0 0 0';
152 ent.railgunhitsolidbackup = SOLID_NOT;
153 ent.railgunhit = FALSE;
154 ent.railgundistance = 0;
156 // advance to the next entity
157 ent = findfloat(ent, railgunhit, TRUE);
160 // calculate hits and fired shots for hitscan
161 accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
163 trace_endpos = endpoint;
165 trace_dphitq3surfaceflags = endq3surfaceflags;
172 void W_BallisticBullet_Hit (void)
176 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
177 q = 1 + self.dmg_edge / self.dmg;
179 if(other.solid == SOLID_BSP || other.solid == SOLID_SLIDEBOX)
180 Damage_DamageInfo(self.origin, self.dmg * f, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * f, self.projectiledeathtype, other.species, self);
182 if(other && other != self.enemy)
184 endzcurveparticles();
187 railgun_start = self.origin - 2 * frametime * self.velocity;
188 railgun_end = self.origin + 2 * frametime * self.velocity;
189 g = accuracy_isgooddamage(self.realowner, other);
190 Damage(other, self, self.realowner, self.dmg * f, self.projectiledeathtype, self.origin, self.dmg_force * normalize(self.velocity) * f);
192 /*if(yoda && (time > (self.last_yoda + 5)))
194 Send_Notification(NOTIF_ONE, self.realowner, MSG_ANNCE, ANNCE_ACHIEVEMENT_YODA);
195 self.last_yoda = time;
198 // calculate hits for ballistic weapons
201 // do not exceed 100%
202 q = min(self.dmg * q, self.dmg_total + f * self.dmg) - self.dmg_total;
203 self.dmg_total += f * self.dmg;
204 accuracy_add(self.realowner, self.realowner.weapon, 0, q);
208 self.enemy = other; // don't hit the same player twice with the same bullet
211 .void(void) W_BallisticBullet_LeaveSolid_think_save;
212 .float W_BallisticBullet_LeaveSolid_nextthink_save;
213 .vector W_BallisticBullet_LeaveSolid_origin;
214 .vector W_BallisticBullet_LeaveSolid_velocity;
216 void W_BallisticBullet_LeaveSolid_think()
218 setorigin(self, self.W_BallisticBullet_LeaveSolid_origin);
219 self.velocity = self.W_BallisticBullet_LeaveSolid_velocity;
221 self.think = self.W_BallisticBullet_LeaveSolid_think_save;
222 self.nextthink = max(time, self.W_BallisticBullet_LeaveSolid_nextthink_save);
223 self.W_BallisticBullet_LeaveSolid_think_save = func_null;
225 self.flags &= ~FL_ONGROUND;
227 if(self.enemy.solid == SOLID_BSP)
230 f = pow(bound(0, vlen(self.velocity) / vlen(self.oldvelocity), 1), 2); // energy multiplier
231 Damage_DamageInfo(self.origin, 0, 0, 0, max(1, self.dmg_force) * normalize(self.velocity) * -f, self.projectiledeathtype, 0, self);
234 UpdateCSQCProjectile(self);
237 float W_BallisticBullet_LeaveSolid(float eff)
239 // move the entity along its velocity until it's out of solid, then let it resume
240 vector vel = self.velocity;
241 float dt, dst, velfactor, v0, vs;
244 float constant = self.dmg_radius * (other.ballistics_density ? other.ballistics_density : 1);
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)
250 // special case for zero density and zero bullet constant:
252 if(self.dmg_radius == 0)
254 if(other.ballistics_density < 0)
255 constant = 0; // infinite travel distance
257 return 0; // no penetration
261 if(other.ballistics_density < 0)
262 constant = 0; // infinite travel distance
263 else if(other.ballistics_density == 0)
264 constant = self.dmg_radius;
266 constant = self.dmg_radius * other.ballistics_density;
269 // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
272 E0_m = 0.5 * v0 * v0;
276 maxdist = E0_m / constant;
277 // maxdist = 0.5 * v0 * v0 / constant
278 // dprint("max dist = ", ftos(maxdist), "\n");
280 if(maxdist <= autocvar_g_ballistics_mindistance)
285 maxdist = vlen(other.maxs - other.mins) + 1; // any distance, as long as we leave the entity
288 traceline_inverted (self.origin, self.origin + normalize(vel) * maxdist, MOVE_NORMAL, self, TRUE);
289 if(trace_fraction == 1) // 1: we never got out of solid
292 self.W_BallisticBullet_LeaveSolid_origin = trace_endpos;
294 dst = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - self.origin));
295 // E(s) = E0 - constant * s, constant = area of bullet circle * material constant / mass
296 Es_m = E0_m - constant * dst;
299 // roundoff errors got us
305 dt = dst / (0.5 * (v0 + vs));
306 // this is not correct, but the differential equations have no analytic
307 // solution - and these times are very small anyway
308 //print("dt = ", ftos(dt), "\n");
310 self.W_BallisticBullet_LeaveSolid_think_save = self.think;
311 self.W_BallisticBullet_LeaveSolid_nextthink_save = self.nextthink;
312 self.think = W_BallisticBullet_LeaveSolid_think;
313 self.nextthink = time + dt;
315 vel = vel * velfactor;
317 self.velocity = '0 0 0';
318 self.flags |= FL_ONGROUND; // prevent moving
319 self.W_BallisticBullet_LeaveSolid_velocity = vel;
322 if(vlen(trace_endpos - self.origin) > 4)
324 endzcurveparticles();
325 trailparticles(self, eff, self.origin, trace_endpos);
331 void W_BallisticBullet_Touch (void)
335 if(self.think == W_BallisticBullet_LeaveSolid_think) // skip this!
339 W_BallisticBullet_Hit ();
341 if(self.dmg_radius < 0) // these NEVER penetrate solid
347 // if we hit "weapclip", bail out
349 // rationale of this check:
351 // any shader that is solid, nodraw AND trans is meant to clip weapon
352 // shots and players, but has no other effect!
354 // if it is not trans, it is caulk and should not have this side effect
357 // common/weapclip (intended)
358 // common/noimpact (is supposed to eat projectiles, but is erased farther above)
359 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
360 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
361 if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
368 if(!W_BallisticBullet_LeaveSolid(-1))
374 self.projectiledeathtype |= HITTYPE_BOUNCE;
377 void endFireBallisticBullet()
379 endzcurveparticles();
382 entity fireBallisticBullet_trace_callback_ent;
383 float fireBallisticBullet_trace_callback_eff;
384 void fireBallisticBullet_trace_callback(vector start, vector hit, vector end)
386 if(vlen(trace_endpos - fireBallisticBullet_trace_callback_ent.origin) > 16)
387 zcurveparticles_from_tracetoss(fireBallisticBullet_trace_callback_eff, fireBallisticBullet_trace_callback_ent.origin, trace_endpos, fireBallisticBullet_trace_callback_ent.velocity);
388 WarpZone_trace_forent = world;
392 void fireBallisticBullet(vector start, vector dir, float spread, float pSpeed, float lifetime, float damage, float force, float dtype, float tracereffects, float gravityfactor, float bulletconstant)
394 float lag, dt, savetime; //, density;
398 antilagging = (autocvar_g_antilag_bullets && (pSpeed >= autocvar_g_antilag_bullets));
402 proj.classname = "bullet";
403 proj.owner = proj.realowner = self;
404 PROJECTILE_MAKETRIGGER(proj);
405 if(gravityfactor > 0)
407 proj.movetype = MOVETYPE_TOSS;
408 proj.gravity = gravityfactor;
411 proj.movetype = MOVETYPE_FLY;
412 proj.think = SUB_Remove;
413 proj.nextthink = time + lifetime; // min(pLifetime, vlen(world.maxs - world.mins) / pSpeed);
414 W_SetupProjectileVelocityEx(proj, dir, v_up, pSpeed, 0, 0, spread, antilagging);
415 proj.angles = vectoangles(proj.velocity);
416 if(bulletconstant > 0)
417 proj.dmg_radius = autocvar_g_ballistics_materialconstant / bulletconstant;
418 else if(bulletconstant == 0)
421 proj.dmg_radius = -1;
422 // so: bulletconstant = bullet mass / area of bullet circle
423 setorigin(proj, start);
424 proj.flags = FL_PROJECTILE;
426 proj.touch = W_BallisticBullet_Touch;
428 proj.dmg_force = force;
429 proj.projectiledeathtype = dtype;
431 proj.oldvelocity = proj.velocity;
433 other = proj; MUTATOR_CALLHOOK(EditProjectile);
439 if(tracereffects & EF_RED)
440 eff = particleeffectnum("tr_rifle");
441 else if(tracereffects & EF_BLUE)
442 eff = particleeffectnum("tr_rifle_weak");
444 eff = particleeffectnum("tr_bullet");
446 // NOTE: this may severely throw off weapon balance
447 lag = ANTILAG_LATENCY(self);
450 if (!IS_REAL_CLIENT(self))
452 if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
453 lag = 0; // only do hitscan, but no antilag
458 antilag_takeback(pl, time - lag);
463 savetime = frametime;
468 // DP tracetoss is stupid and always traces in 0.05s
469 // ticks. This makes it trace in 0.05*0.125s ticks
475 self.velocity = self.velocity * 0.125;
476 self.gravity *= 0.125 * 0.125;
478 fireBallisticBullet_trace_callback_ent = self;
479 fireBallisticBullet_trace_callback_eff = eff;
480 WarpZone_TraceToss_ThroughZone(self, self.owner, world, fireBallisticBullet_trace_callback);
484 if(trace_fraction == 1)
486 // won't hit anything anytime soon (DP's
487 // tracetoss does 200 tics of, here,
488 // 0.05*0.125s, that is, 1.25 seconds
491 dt = WarpZone_tracetoss_time * 0.125; // this is only approximate!
492 setorigin(self, trace_endpos);
493 self.velocity = WarpZone_tracetoss_velocity * (1 / 0.125);
495 if(!SUB_OwnerCheck())
497 if(SUB_NoImpactCheck())
501 W_BallisticBullet_Hit();
504 if(proj.dmg_radius < 0) // these NEVER penetrate solid
507 // if we hit "weapclip", bail out
509 // rationale of this check:
511 // any shader that is solid, nodraw AND trans is meant to clip weapon
512 // shots and players, but has no other effect!
514 // if it is not trans, it is caulk and should not have this side effect
517 // common/weapclip (intended)
518 // common/noimpact (is supposed to eat projectiles, but is erased farther above)
519 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
520 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
521 if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
525 if(!W_BallisticBullet_LeaveSolid((other && (other.solid != SOLID_BSP)) ? eff : -1))
528 W_BallisticBullet_LeaveSolid_think();
530 self.projectiledeathtype |= HITTYPE_BOUNCE;
532 frametime = savetime;
545 if(tracereffects & EF_RED)
546 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING_TRACER, TRUE);
547 else if(tracereffects & EF_BLUE)
548 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET_GLOWING, TRUE);
550 CSQCProjectile(proj, TRUE, PROJECTILE_BULLET, TRUE);
553 void fireBullet (vector start, vector dir, float spread, float damage, float force, float dtype, float tracer)
557 dir = normalize(dir + randomvec() * spread);
558 end = start + dir * MAX_SHOT_DISTANCE;
559 if(self.antilag_debug)
560 traceline_antilag (self, start, end, FALSE, self, self.antilag_debug);
562 traceline_antilag (self, start, end, FALSE, self, ANTILAG_LATENCY(self));
566 if (pointcontents (trace_endpos) != CONTENT_SKY)
568 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
569 Damage_DamageInfo(trace_endpos, damage, 0, 0, dir * max(1, force), dtype, trace_ent.species, self);
571 Damage (trace_ent, self, self, damage, dtype, trace_endpos, dir * force);
576 float W_CheckProjectileDamage(entity inflictor, entity projowner, float deathtype, float exception)
578 float is_from_contents = (deathtype == DEATH_SLIME || deathtype == DEATH_LAVA);
579 float is_from_owner = (inflictor == projowner);
580 float is_from_exception = (exception != -1);
582 //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")));
584 if(autocvar_g_projectiles_damage <= -2)
586 return FALSE; // no damage to projectiles at all, not even with the exceptions
588 else if(autocvar_g_projectiles_damage == -1)
590 if(is_from_exception)
591 return (exception); // if exception is detected, allow it to override
593 return FALSE; // otherwise, no other damage is allowed
595 else if(autocvar_g_projectiles_damage == 0)
597 if(is_from_exception)
598 return (exception); // if exception is detected, allow it to override
599 else if (!is_from_contents)
600 return FALSE; // otherwise, only allow damage from contents
602 else if(autocvar_g_projectiles_damage == 1)
604 if(is_from_exception)
605 return (exception); // if exception is detected, allow it to override
606 else if (!(is_from_contents || is_from_owner))
607 return FALSE; // otherwise, only allow self damage and damage from contents
609 else if(autocvar_g_projectiles_damage == 2) // allow any damage, but override for exceptions
611 if(is_from_exception)
612 return (exception); // if exception is detected, allow it to override
615 return TRUE; // if none of these return, then allow damage anyway.
618 void W_PrepareExplosionByDamage(entity attacker, void() explode)
620 self.takedamage = DAMAGE_NO;
621 self.event_damage = func_null;
623 if(IS_CLIENT(attacker) && !autocvar_g_projectiles_keep_owner)
625 self.owner = attacker;
626 self.realowner = attacker;
629 // do not explode NOW but in the NEXT FRAME!
630 // because recursive calls to RadiusDamage are not allowed
631 self.nextthink = time;
632 self.think = explode;