]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/weapons/tracing.qc
Merge branch 'master' into martin-t/bullet-trails
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / tracing.qc
index 3f57a3bb39a7ae54368cb6609db387700949d38a..570d976db1d664505d01f6484fdaab7db7fa518d 100644 (file)
@@ -1,37 +1,43 @@
 #include "tracing.qh"
 
+#include <common/effects/all.qh>
+
 #include "accuracy.qh"
 #include "common.qh"
 #include "hitplot.qh"
 #include "weaponsystem.qh"
 
 #include "../g_damage.qh"
-#include "../g_subs.qh"
 #include "../antilag.qh"
 
-#include "../../common/constants.qh"
-#include "../../common/util.qh"
+#include <common/constants.qh>
+#include <common/net_linked.qh>
+#include <common/util.qh>
 
-#include "../../common/weapons/all.qh"
+#include <common/weapons/_all.qh>
+#include <common/wepent.qh>
+#include <common/state.qh>
 
-#include "../../lib/warpzone/common.qh"
+#include <lib/warpzone/common.qh>
 
 // this function calculates w_shotorg and w_shotdir based on the weapon model
 // offset, trueaim and antilag, and won't put w_shotorg inside a wall.
 // make sure you call makevectors first (FIXME?)
-void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector mi, vector ma, float antilag, float recoil, string snd, float chan, float maxdamage, float range)
+void W_SetupShot_Dir_ProjectileSize_Range(entity ent, .entity weaponentity, vector s_forward, vector mi, vector ma, float antilag, float recoil, Sound snd, float chan, float maxdamage, float range, int deathtype)
 {
+       TC(Sound, snd);
        float nudge = 1; // added to traceline target and subtracted from result  TOOD(divVerent): do we still need this? Doesn't the engine do this now for us?
-       float oldsolid;
-       vector vecs, dv;
-       oldsolid = ent.dphitcontentsmask;
-       if(ent.weapon == WEP_RIFLE.m_id)
+       float oldsolid = ent.dphitcontentsmask;
+       Weapon wep = DEATH_WEAPONOF(deathtype);
+       if(!IS_CLIENT(ent))
+               antilag = false; // no antilag for non-clients!
+       if (IS_PLAYER(ent) && (wep.spawnflags & WEP_FLAG_PENETRATEWALLS))
                ent.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_CORPSE;
        else
                ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
        if(antilag)
-               WarpZone_traceline_antilag(world, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
-               // passing world, because we do NOT want it to touch dphitcontentsmask
+               WarpZone_traceline_antilag(NULL, ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
+               // passing NULL, because we do NOT want it to touch dphitcontentsmask
        else
                WarpZone_TraceLine(ent.origin + ent.view_ofs, ent.origin + ent.view_ofs + s_forward * range, MOVE_NOMONSTERS, ent);
        ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
@@ -46,29 +52,27 @@ void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector m
        v_up = vu;
 
        // un-adjust trueaim if shotend is too close
-       if(vlen(w_shotend - (ent.origin + ent.view_ofs)) < autocvar_g_trueaim_minrange)
+       if(vdist(w_shotend - (ent.origin + ent.view_ofs), <, autocvar_g_trueaim_minrange))
                w_shotend = ent.origin + ent.view_ofs + s_forward * autocvar_g_trueaim_minrange;
 
        // track max damage
-       if(accuracy_canbegooddamage(ent))
-               accuracy_add(ent, ent.weapon, maxdamage, 0);
+       if (IS_PLAYER(ent) && accuracy_canbegooddamage(ent))
+               accuracy_add(ent, wep.m_id, maxdamage, 0);
 
-       W_HitPlotAnalysis(ent, v_forward, v_right, v_up);
+       if(IS_PLAYER(ent))
+               W_HitPlotAnalysis(ent, wep, v_forward, v_right, v_up);
 
-       int slot = 0; // TODO: unhardcode
-       if(ent.weaponentity[slot].movedir.x > 0)
-               vecs = ent.weaponentity[slot].movedir;
-       else
-               vecs = '0 0 0';
+       vector md = ent.(weaponentity).movedir;
+       vector vecs = ((md.x > 0) ? md : '0 0 0');
 
-       dv = v_right * -vecs.y + v_up * vecs.z;
+       vector dv = v_right * -vecs.y + v_up * vecs.z;
        w_shotorg = ent.origin + ent.view_ofs + dv;
 
        // now move the shotorg forward as much as requested if possible
        if(antilag)
        {
-               if(ent.antilag_debug)
-                       tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs.x + nudge), MOVE_NORMAL, ent, ent.antilag_debug);
+               if(CS(ent).antilag_debug)
+                       tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs.x + nudge), MOVE_NORMAL, ent, CS(ent).antilag_debug);
                else
                        tracebox_antilag(ent, w_shotorg, mi, ma, w_shotorg + v_forward * (vecs.x + nudge), MOVE_NORMAL, ent, ANTILAG_LATENCY(ent));
        }
@@ -76,14 +80,17 @@ void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector m
                tracebox(w_shotorg, mi, ma, w_shotorg + v_forward * (vecs.x + nudge), MOVE_NORMAL, ent);
        w_shotorg = trace_endpos - v_forward * nudge;
        // calculate the shotdir from the chosen shotorg
-       w_shotdir = normalize(w_shotend - w_shotorg);
+       if(W_DualWielding(ent))
+               w_shotdir = s_forward;
+       else
+               w_shotdir = normalize(w_shotend - w_shotorg);
 
        //vector prevdir = w_shotdir;
        //vector prevorg = w_shotorg;
        //vector prevend = w_shotend;
 
        if (antilag)
-       if (!ent.cvar_cl_noantilag)
+       if (!CS(ent).cvar_cl_noantilag)
        {
                if (autocvar_g_antilag == 1) // switch to "ghost" if not hitting original
                {
@@ -104,10 +111,10 @@ void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector m
                else if(autocvar_g_antilag == 3) // client side hitscan
                {
                        // this part MUST use prydon cursor
-                       if (ent.cursor_trace_ent)                 // client was aiming at someone
-                       if (ent.cursor_trace_ent != ent)         // just to make sure
-                       if (ent.cursor_trace_ent.takedamage)      // and that person is killable
-                       if (IS_PLAYER(ent.cursor_trace_ent)) // and actually a player
+                       if (CS(ent).cursor_trace_ent)                 // client was aiming at someone
+                       if (CS(ent).cursor_trace_ent != ent)         // just to make sure
+                       if (CS(ent).cursor_trace_ent.takedamage)      // and that person is killable
+                       if (IS_PLAYER(CS(ent).cursor_trace_ent)) // and actually a player
                        {
                                // verify that the shot would miss without antilag
                                // (avoids an issue where guns would always shoot at their origin)
@@ -115,11 +122,11 @@ void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector m
                                if (!trace_ent.takedamage)
                                {
                                        // verify that the shot would hit if altered
-                                       traceline(w_shotorg, ent.cursor_trace_ent.origin, MOVE_NORMAL, ent);
-                                       if (trace_ent == ent.cursor_trace_ent)
-                                               w_shotdir = normalize(ent.cursor_trace_ent.origin - w_shotorg);
+                                       traceline(w_shotorg, CS(ent).cursor_trace_ent.origin, MOVE_NORMAL, ent);
+                                       if (trace_ent == CS(ent).cursor_trace_ent)
+                                               w_shotdir = normalize(CS(ent).cursor_trace_ent.origin - w_shotorg);
                                        else
-                                               LOG_INFO("antilag fail\n");
+                                               LOG_INFO("antilag fail");
                                }
                        }
                }
@@ -130,9 +137,8 @@ void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector m
        if (!autocvar_g_norecoil)
                ent.punchangle_x = recoil * -1;
 
-       if (snd != "")
-       {
-               _sound (ent, chan, snd, VOL_BASE, ATTN_NORM);
+       if (snd != SND_Null) {
+               sound(ent, chan, snd, (W_DualWielding(ent) ? VOL_BASE * 0.7 : VOL_BASE), ATTN_NORM);
                W_PlayStrengthSound(ent);
        }
 
@@ -143,13 +149,13 @@ void W_SetupShot_Dir_ProjectileSize_Range(entity ent, vector s_forward, vector m
        //if(w_shotdir != prevdir) { printf("SERVER: shotDIR differs: %s - %s\n", vtos(w_shotdir), vtos(prevdir)); }
 }
 
-vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity, float forceAbsolute)
+vector W_CalculateProjectileVelocity(entity actor, vector pvelocity, vector mvelocity, float forceAbsolute)
 {
        vector mdirection;
        float mspeed;
        vector outvelocity;
 
-       mvelocity = mvelocity * W_WeaponSpeedFactor();
+       mvelocity = mvelocity * W_WeaponSpeedFactor(actor);
 
        mdirection = normalize(mvelocity);
        mspeed = vlen(mvelocity);
@@ -161,7 +167,7 @@ vector W_CalculateProjectileVelocity(vector pvelocity, vector mvelocity, float f
 
 void W_SetupProjVelocity_Explicit(entity proj, vector dir, vector upDir, float pSpeed, float pUpSpeed, float pZSpeed, float spread, float forceAbsolute)
 {
-       if(proj.owner == world)
+       if(proj.owner == NULL)
                error("Unowned missile");
 
        dir = dir + upDir * (pUpSpeed / pSpeed);
@@ -183,10 +189,10 @@ void W_SetupProjVelocity_Explicit(entity proj, vector dir, vector upDir, float p
        #if 0
        mspercallsum += gettime(GETTIME_HIRES);
        mspercallcount += 1;
-       LOG_INFO("avg: ", ftos(mspercallcount / mspercallsum), " per sec\n");
+       LOG_INFO("avg: ", ftos(mspercallcount / mspercallsum), " per sec");
        #endif
 
-       proj.velocity = W_CalculateProjectileVelocity(proj.owner.velocity, pSpeed * dir, forceAbsolute);
+       proj.velocity = W_CalculateProjectileVelocity(proj.owner, proj.owner.velocity, pSpeed * dir, forceAbsolute);
 }
 
 
@@ -194,55 +200,44 @@ void W_SetupProjVelocity_Explicit(entity proj, vector dir, vector upDir, float p
 //  Ballistics Tracing
 // ====================
 
-void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, int deathtype)
-{SELFPARAM();
-       vector hitloc, force, endpoint, dir;
-       entity ent, endent;
-       float endq3surfaceflags;
-       float totaldmg;
-       entity o;
-
-       float length;
-       vector beampos;
-       string snd;
-       entity pseudoprojectile;
-       float f, ffs;
-
-       pseudoprojectile = world;
+void FireRailgunBullet (entity this, .entity weaponentity, vector start, vector end, float bdamage, float bforce, float mindist, float maxdist, float halflifedist, float forcehalflifedist, int deathtype)
+{
+       entity pseudoprojectile = NULL;
 
-       dir = normalize(end - start);
-       length = vlen(end - start);
-       force = dir * bforce;
+       vector dir = normalize(end - start);
+       float length = vlen(end - start);
+       vector force = dir * bforce;
 
        // go a little bit into the wall because we need to hit this wall later
        end = end + dir;
 
-       totaldmg = 0;
+       float totaldmg = 0;
 
        // trace multiple times until we hit a wall, each obstacle will be made
        // non-solid so we can hit the next, while doing this we spawn effects and
        // note down which entities were hit so we can damage them later
-       o = self;
+       entity o = this;
        while (1)
        {
-               if(self.antilag_debug)
-                       WarpZone_traceline_antilag (self, start, end, false, o, self.antilag_debug);
+               if(CS(this).antilag_debug)
+                       WarpZone_traceline_antilag (this, start, end, false, o, CS(this).antilag_debug);
                else
-                       WarpZone_traceline_antilag (self, start, end, false, o, ANTILAG_LATENCY(self));
+                       WarpZone_traceline_antilag (this, start, end, false, o, ANTILAG_LATENCY(this));
                if(o && WarpZone_trace_firstzone)
                {
-                       o = world;
+                       o = NULL;
                        continue;
                }
 
                if(trace_ent.solid == SOLID_BSP || trace_ent.solid == SOLID_SLIDEBOX)
-                       Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, self);
+                       Damage_DamageInfo(trace_endpos, bdamage, 0, 0, force, deathtype, trace_ent.species, this);
 
-               // if it is world we can't hurt it so stop now
-               if (trace_ent == world || trace_fraction == 1)
+               // if it is NULL we can't hurt it so stop now
+               if (trace_ent == NULL || trace_fraction == 1)
                        break;
 
                // make the entity non-solid so we can hit the next one
+               IL_PUSH(g_railgunhit, trace_ent);
                trace_ent.railgunhit = true;
                trace_ent.railgunhitloc = end;
                trace_ent.railgunhitsolidbackup = trace_ent.solid;
@@ -257,18 +252,15 @@ void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, f
                trace_ent.solid = SOLID_NOT;
        }
 
-       endpoint = trace_endpos;
-       endent = trace_ent;
-       endq3surfaceflags = trace_dphitq3surfaceflags;
+       vector endpoint = trace_endpos;
+       entity endent = trace_ent;
+       float endq3surfaceflags = trace_dphitq3surfaceflags;
 
        // find all the entities the railgun hit and restore their solid state
-       ent = findfloat(world, railgunhit, true);
-       while (ent)
+       IL_EACH(g_railgunhit, it.railgunhit,
        {
-               // restore their solid type
-               ent.solid = ent.railgunhitsolidbackup;
-               ent = findfloat(ent, railgunhit, true);
-       }
+               it.solid = it.railgunhitsolidbackup;
+       });
 
        // spawn a temporary explosion entity for RadiusDamage calls
        //explosion = spawn();
@@ -276,61 +268,60 @@ void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, f
        // Find all non-hit players the beam passed close by
        if(deathtype == WEP_VAPORIZER.m_id || deathtype == WEP_VORTEX.m_id)
        {
-               FOR_EACH_REALCLIENT(msg_entity)
-               if(msg_entity != self)
-               if(!msg_entity.railgunhit)
-               if(!(IS_SPEC(msg_entity) && msg_entity.enemy == self)) // we use realclient, so spectators can hear the whoosh too
-               {
-                       // nearest point on the beam
-                       beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
-
-                       f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
-                       if(f <= 0)
-                               continue;
+               FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != this, {
+                       if(!it.railgunhit)
+                       if(!(IS_SPEC(it) && it.enemy == this))
+                       {
+                               msg_entity = it;
+                               // nearest point on the beam
+                               vector beampos = start + dir * bound(0, (msg_entity.origin - start) * dir, length);
 
-                       snd = SND(NEXWHOOSH_RANDOM());
+                               float f = bound(0, 1 - vlen(beampos - msg_entity.origin) / 512, 1);
+                               if(f <= 0)
+                                       continue;
 
-                       if(!pseudoprojectile)
-                               pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
-                       soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, snd, VOL_BASE * f, ATTEN_NONE);
-               }
+                               if(!pseudoprojectile)
+                                       pseudoprojectile = spawn(); // we need this so the sound uses the "entchannel4" volume
+                               soundtoat(MSG_ONE, pseudoprojectile, beampos, CH_SHOTS, SND(NEXWHOOSH_RANDOM()), VOL_BASE * f, ATTEN_NONE);
+                       }
+               });
 
                if(pseudoprojectile)
-                       remove(pseudoprojectile);
+                       delete(pseudoprojectile);
        }
 
        // find all the entities the railgun hit and hurt them
-       ent = findfloat(world, railgunhit, true);
-       while (ent)
+       IL_EACH(g_railgunhit, it.railgunhit,
        {
+               // removal from the list is handled below
+
                // get the details we need to call the damage function
-               hitloc = ent.railgunhitloc;
+               vector hitloc = it.railgunhitloc;
 
-               f = ExponentialFalloff(mindist, maxdist, halflifedist, ent.railgundistance);
-               ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, ent.railgundistance);
+               float foff = ExponentialFalloff(mindist, maxdist, halflifedist, it.railgundistance);
+               float ffs = ExponentialFalloff(mindist, maxdist, forcehalflifedist, it.railgundistance);
 
-               if(accuracy_isgooddamage(self, ent))
-                       totaldmg += bdamage * f;
+               if(accuracy_isgooddamage(this, it))
+                       totaldmg += bdamage * foff;
 
                // apply the damage
-               if (ent.takedamage)
-                       Damage (ent, self, self, bdamage * f, deathtype, hitloc, ent.railgunforce * ffs);
+               if (it.takedamage)
+                       Damage (it, this, this, bdamage * foff, deathtype, weaponentity, hitloc, it.railgunforce * ffs);
 
                // create a small explosion to throw gibs around (if applicable)
-               //setorigin (explosion, hitloc);
-               //RadiusDamage (explosion, self, 10, 0, 50, world, world, 300, deathtype);
+               //setorigin(explosion, hitloc);
+               //RadiusDamage (explosion, this, 10, 0, 50, NULL, NULL, 300, deathtype);
 
-               ent.railgunhitloc = '0 0 0';
-               ent.railgunhitsolidbackup = SOLID_NOT;
-               ent.railgunhit = false;
-               ent.railgundistance = 0;
+               it.railgunhitloc = '0 0 0';
+               it.railgunhitsolidbackup = SOLID_NOT;
+               it.railgunhit = false;
+               it.railgundistance = 0;
+       });
 
-               // advance to the next entity
-               ent = findfloat(ent, railgunhit, true);
-       }
+       IL_CLEAR(g_railgunhit);
 
        // calculate hits and fired shots for hitscan
-       accuracy_add(self, self.weapon, 0, min(bdamage, totaldmg));
+       accuracy_add(this, this.(weaponentity).m_weapon.m_id, 0, min(bdamage, totaldmg));
 
        trace_endpos = endpoint;
        trace_ent = endent;
@@ -339,65 +330,62 @@ void FireRailgunBullet (vector start, vector end, float bdamage, float bforce, f
 
 void fireBullet_trace_callback(vector start, vector hit, vector end)
 {
-       if(vlen(hit - start) > 16)
-               trailparticles(world, fireBullet_trace_callback_eff, start, hit);
-       WarpZone_trace_forent = world;
-       fireBullet_last_hit = world;
+       if(vdist(hit - start, >, 16))
+               trailparticles(NULL, fireBullet_trace_callback_eff, start, hit);
+       WarpZone_trace_forent = NULL;
+       fireBullet_last_hit = NULL;
 }
 
-void fireBullet(vector start, vector dir, float spread, float max_solid_penetration, float damage, float force, float dtype, int tracereffects)
-{SELFPARAM();
+void fireBullet(entity this, .entity weaponentity, vector start, vector dir, float spread, float max_solid_penetration, float damage, float force, float dtype, entity tracer_effect)
+{
        vector  end;
 
        dir = normalize(dir + randomvec() * spread);
-       end = start + dir * MAX_SHOT_DISTANCE;
+       end = start + dir * max_shot_distance;
+
+       fireBullet_last_hit = NULL;
+       fireBullet_trace_callback_eff = tracer_effect;
 
-       entity pl;
-       fireBullet_last_hit = world;
        float solid_penetration_left = 1;
        float total_damage = 0;
 
-       if(tracereffects & EF_RED)
-               fireBullet_trace_callback_eff = EFFECT_RIFLE;
-       else if(tracereffects & EF_BLUE)
-               fireBullet_trace_callback_eff = EFFECT_RIFLE_WEAK;
-       else
-               fireBullet_trace_callback_eff = EFFECT_BULLET;
-
-       float lag = ANTILAG_LATENCY(self);
+       float lag = ((IS_REAL_CLIENT(this)) ? ANTILAG_LATENCY(this) : 0);
        if(lag < 0.001)
                lag = 0;
-       if (!IS_REAL_CLIENT(self))
-               lag = 0;
-       if(autocvar_g_antilag == 0 || self.cvar_cl_noantilag)
+       bool noantilag = ((IS_CLIENT(this)) ? CS(this).cvar_cl_noantilag : false);
+       if(autocvar_g_antilag == 0 || noantilag)
                lag = 0; // only do hitscan, but no antilag
        if(lag)
-       {
-               FOR_EACH_PLAYER(pl)
-                       if(pl != self)
-                               antilag_takeback(pl, time - lag);
-               FOR_EACH_MONSTER(pl)
-                       antilag_takeback(pl, time - lag);
-       }
+               antilag_takeback_all(this, lag);
 
-       WarpZone_trace_forent = self;
+       // change shooter to SOLID_BBOX so the shot can hit corpses
+       int oldsolid = this.dphitcontentsmask;
+       if(this)
+               this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
+
+       WarpZone_trace_forent = this;
 
        for (;;)
        {
                // TODO also show effect while tracing
-               WarpZone_TraceBox_ThroughZone(start, '0 0 0', '0 0 0', end, false, WarpZone_trace_forent, world, fireBullet_trace_callback);
+               WarpZone_TraceBox_ThroughZone(start, '0 0 0', '0 0 0', end, false, WarpZone_trace_forent, NULL, fireBullet_trace_callback);
                dir = WarpZone_TransformVelocity(WarpZone_trace_transform, dir);
                end = WarpZone_TransformOrigin(WarpZone_trace_transform, end);
                start = trace_endpos;
                entity hit = trace_ent;
 
-               // When hitting sky, stop.
-               if (pointcontents(start) == CONTENT_SKY)
+               // traced up to max_shot_distance and didn't hit anything at all
+               if (trace_fraction == 1.0)
                        break;
 
-               if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
+               // When hitting sky, stop.
+               if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
                        break;
 
+               // can't use noimpact, as we need to pass through walls
+               //if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
+                       //break;
+
                // if we hit "weapclip", bail out
                //
                // rationale of this check:
@@ -410,34 +398,34 @@ void fireBullet(vector start, vector dir, float spread, float max_solid_penetrat
                // matching shaders:
                //   common/weapclip (intended)
                //   common/noimpact (is supposed to eat projectiles, but is erased anyway)
-               float is_weapclip = 0;
+               bool is_weapclip = false;
                if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NODRAW)
                if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NONSOLID))
                if (!(trace_dphitcontents & DPCONTENTS_OPAQUE))
-                       is_weapclip = 1;
+                       is_weapclip = true;
 
                if(!hit || hit.solid == SOLID_BSP || hit.solid == SOLID_SLIDEBOX)
-                       Damage_DamageInfo(start, damage * solid_penetration_left, 0, 0, max(1, force) * dir * solid_penetration_left, dtype, hit.species, self);
+                       Damage_DamageInfo(start, damage * solid_penetration_left, 0, 0, max(1, force) * dir * solid_penetration_left, dtype, hit.species, this);
 
                if (hit && hit != WarpZone_trace_forent && hit != fireBullet_last_hit)  // Avoid self-damage (except after going through a warp); avoid hitting the same entity twice (engine bug).
                {
                        fireBullet_last_hit = hit;
                        yoda = 0;
-                       MUTATOR_CALLHOOK(FireBullet_Hit, self, hit, start, end, damage);
-                       damage = frag_damage;
-                       float g = accuracy_isgooddamage(self, hit);
-                       Damage(hit, self, self, damage * solid_penetration_left, dtype, start, force * dir * solid_penetration_left);
+                       MUTATOR_CALLHOOK(FireBullet_Hit, this, hit, start, end, damage, this.(weaponentity));
+                       damage = M_ARGV(4, float);
+                       bool gooddamage = accuracy_isgooddamage(this, hit);
+                       Damage(hit, this, this, damage * solid_penetration_left, dtype, weaponentity, start, force * dir * solid_penetration_left);
                        // calculate hits for ballistic weapons
-                       if(g)
+                       if(gooddamage)
                        {
                                // do not exceed 100%
                                float added_damage = min(damage - total_damage, damage * solid_penetration_left);
                                total_damage += damage * solid_penetration_left;
-                               accuracy_add(self, self.weapon, 0, added_damage);
+                               accuracy_add(this, this.(weaponentity).m_weapon.m_id, 0, added_damage);
                        }
                }
 
-               if (is_weapclip)
+               if (is_weapclip && !autocvar_g_ballistics_penetrate_clips)
                        break;
 
                // go through solid!
@@ -446,16 +434,17 @@ void fireBullet(vector start, vector dir, float spread, float max_solid_penetrat
                        break;
 
                float maxdist;
+               entity hitstore = IS_PLAYER(hit) ? PS(hit) : hit;
                if(max_solid_penetration < 0)
                        break;
-               else if(hit.ballistics_density < -1)
+               else if(hitstore.ballistics_density < -1)
                        break; // -2: no solid penetration, ever
-               else if(hit.ballistics_density < 0)
+               else if(hitstore.ballistics_density < 0)
                        maxdist = vlen(hit.maxs - hit.mins) + 1; // -1: infinite travel distance
-               else if(hit.ballistics_density == 0)
+               else if(hitstore.ballistics_density == 0)
                        maxdist = max_solid_penetration * solid_penetration_left;
                else
-                       maxdist = max_solid_penetration * solid_penetration_left * hit.ballistics_density;
+                       maxdist = max_solid_penetration * solid_penetration_left * hitstore.ballistics_density;
 
                if(maxdist <= autocvar_g_ballistics_mindistance)
                        break;
@@ -467,25 +456,26 @@ void fireBullet(vector start, vector dir, float spread, float max_solid_penetrat
                        break;
 
                float dist_taken = max(autocvar_g_ballistics_mindistance, vlen(trace_endpos - start));
-               solid_penetration_left *= (dist_taken / maxdist);
+               // fraction_used_of_what_is_left = dist_taken / maxdist
+               // solid_penetration_left = solid_penetration_left - solid_penetration_left * fraction_used_of_what_is_left
+               solid_penetration_left *= 1 - dist_taken / maxdist;
+               solid_penetration_left = max(solid_penetration_left, 0);
 
                // Only show effect when going through a player (invisible otherwise)
                if (hit && (hit.solid != SOLID_BSP))
-                       if(vlen(trace_endpos - start) > 4)
-                               trailparticles(self, fireBullet_trace_callback_eff, start, trace_endpos);
+                       if(vdist(trace_endpos - start, >, 4))
+                               trailparticles(this, fireBullet_trace_callback_eff, start, trace_endpos);
 
                start = trace_endpos;
 
                if(hit.solid == SOLID_BSP)
-                       Damage_DamageInfo(start, 0, 0, 0, max(1, force) * normalize(dir) * -solid_penetration_left, dtype, 0, self);
+                       Damage_DamageInfo(start, 0, 0, 0, max(1, force) * normalize(dir) * -solid_penetration_left, dtype, 0, this);
        }
 
        if(lag)
-       {
-               FOR_EACH_PLAYER(pl)
-                       if(pl != self)
-                               antilag_restore(pl);
-               FOR_EACH_MONSTER(pl)
-                       antilag_restore(pl);
-       }
+               antilag_restore_all(this);
+
+       // restore shooter solid type
+       if(this)
+               this.dphitcontentsmask = oldsolid;
 }