]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/client/damage.qc
Merge branch 'master' into mirceakitsune/damage_effects
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / damage.qc
index 6ff989e4a6ed598d55e0da35ad71d535f33bfa2d..2ac4a01c138f4c7c1511a2985465ab7472350998 100644 (file)
@@ -1,6 +1,7 @@
+void DamageEffect(vector hitorg, float dmg, float type, float specnum1);
 void Ent_DamageInfo(float isNew)
 {
-       float dmg, rad, edge, thisdmg, forcemul;
+       float dmg, rad, edge, thisdmg, forcemul, species;
        vector force, thisforce;
        entity oldself;
 
@@ -18,6 +19,7 @@ void Ent_DamageInfo(float isNew)
        rad = ReadByte();
        edge = ReadByte();
        force = decompressShortVector(ReadShort());
+       species = ReadByte();
 
        if not(isNew)
                return;
@@ -30,13 +32,16 @@ void Ent_DamageInfo(float isNew)
        else
                forcemul = 1;
        
-       for(self = findradius(w_org, rad); self; self = self.chain)
+       for(self = findradius(w_org, rad + MAX_DAMAGEEXTRARADIUS); self; self = self.chain)
        {
+               vector nearest = NearestPointOnBox(self, w_org);
                if(rad)
                {
-                       thisdmg = vlen(self.origin - w_org) / rad;
+                       thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
                        if(thisdmg >= 1)
                                continue;
+                       if(thisdmg < 0)
+                               thisdmg = 0;
                        if(dmg)
                        {
                                thisdmg = dmg + (edge - dmg) * thisdmg;
@@ -50,6 +55,9 @@ void Ent_DamageInfo(float isNew)
                }
                else
                {
+                       if(vlen(nearest - w_org) > bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS))
+                               continue;
+
                        thisdmg = dmg;
                        thisforce = forcemul * force;
                }
@@ -66,6 +74,8 @@ void Ent_DamageInfo(float isNew)
 
                if(self.event_damage)
                        self.event_damage(thisdmg, w_deathtype, w_org, thisforce);
+
+               DamageEffect(w_org, thisdmg, w_deathtype, species);
        }
 
        self = oldself;
@@ -88,33 +98,33 @@ void Ent_DamageInfo(float isNew)
             case DEATH_SBMINIGUN:
                 string _snd;
                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
-                sound(self, CHAN_PROJECTILE, _snd, VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("spiderbot_minigun_impact"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_SBROCKET:
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("spiderbot_rocket_explode"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_SBBLOWUP:
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
                 break;
                 
             case DEATH_WAKIGUN:
-                sound(self, CHAN_PROJECTILE, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("wakizashi_gun_impact"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_WAKIROCKET:
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("wakizashi_rocket_explode"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_WAKIBLOWUP:
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
                 break;
                 
             case DEATH_RAPTOR_CANNON:
-                sound(self, CHAN_PROJECTILE, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("raptor_cannon_impact"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_RAPTOR_BOMB_SPLIT:
@@ -128,15 +138,15 @@ void Ent_DamageInfo(float isNew)
                 }
                     
                 
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("raptor_bomb_spread"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_RAPTOR_BOMB:
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("raptor_bomb_impact"), self.origin, w_backoff * 1000, 1);
                 break;
             case DEATH_RAPTOR_DEATH:
-                sound(self, CHAN_PROJECTILE, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
+                sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
                 break;
            }
@@ -145,6 +155,7 @@ void Ent_DamageInfo(float isNew)
        
        if(DEATH_ISTURRET(w_deathtype))
        {           
+           string _snd;
            traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
            if(trace_plane_normal != '0 0 0')       
             w_backoff = trace_plane_normal;
@@ -156,46 +167,38 @@ void Ent_DamageInfo(float isNew)
            switch(w_deathtype)
            {   
              case DEATH_TURRET_EWHEEL:
-                sound(self, CHAN_PROJECTILE, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
-                pointparticles(particleeffectnum("electro_impact"), self.origin, w_backoff * 1000, 1);
+                sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
+                pointparticles(particleeffectnum("laser_impact"), self.origin, w_backoff * 1000, 1);
                 break;
              
              case DEATH_TURRET_FLAC:
-                vector org2;
-                org2 = w_org + w_backoff * 6;
-                pointparticles(particleeffectnum("hagar_explode"), org2, '0 0 0', 1);
-                if (w_random<0.15)
-                    sound(self, CHAN_PROJECTILE, "weapons/hagexp1.wav", VOL_BASE, ATTN_NORM);
-                else if (w_random<0.7)
-                    sound(self, CHAN_PROJECTILE, "weapons/hagexp2.wav", VOL_BASE, ATTN_NORM);
-                else
-                    sound(self, CHAN_PROJECTILE, "weapons/hagexp3.wav", VOL_BASE, ATTN_NORM);
-                
+                pointparticles(particleeffectnum("hagar_explode"), w_org, '0 0 0', 1);
+                _snd = strcat("weapons/hagexp", ftos(1 + rint(random() * 2)), ".waw");
+                sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);                
                 break;
                 
              case DEATH_TURRET_MLRS:
              case DEATH_TURRET_HK:
              case DEATH_TURRET_WALKER_ROCKET:
              case DEATH_TURRET_HELLION:
-                sound(self, CHAN_PROJECTILE, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
+                sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
                 pointparticles(particleeffectnum("rocket_explode"), self.origin, w_backoff * 1000, 1);
                 break;
              
              case DEATH_TURRET_MACHINEGUN:
              case DEATH_TURRET_WALKER_GUN:
-                string _snd;
                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
-                sound(self, CHAN_PROJECTILE, _snd, VOL_BASE, ATTN_NORM);
+                sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("machinegun_impact"), self.origin, w_backoff * 1000, 1);
                 break;
                           
              case DEATH_TURRET_PLASMA:
-                sound(self, CHAN_PROJECTILE, "weapons/electro_impact.wav", VOL_BASE, ATTN_MIN);
+                sound(self, CH_SHOTS, "weapons/electro_impact.wav", VOL_BASE, ATTN_MIN);
                 pointparticles(particleeffectnum("electro_impact"), self.origin, w_backoff * 1000, 1);
                 break;
                           
              case DEATH_TURRET_WALKER_MEELE:
-                sound(self, CHAN_PROJECTILE, "weapons/ric1.wav", VOL_BASE, ATTN_MIN);
+                sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_MIN);
                 pointparticles(particleeffectnum("TE_SPARK"), self.origin, w_backoff * 1000, 1);
                 break;
 
@@ -234,3 +237,89 @@ void DamageInfo_Precache()
        for(i = WEP_FIRST; i <= WEP_LAST; ++i)
                (get_weaponinfo(i)).weapon_func(WR_PRECACHE);
 }
+
+void DamageEffect_Think()
+{
+       if(time >= self.cnt || self.owner == world || self.owner.model == "" || !self.owner.drawmask)
+       {
+               // time is up or the player got gibbed / disconnected
+               remove(self);
+               return;
+       }
+       if(self.owner.entnum == player_localentnum && !autocvar_chase_active)
+               return; // if we aren't using a third person view, hide our own effects
+
+       // now generate the particles
+       vector org;
+       org = gettaginfo(self, 0); // origin at attached location
+       pointparticles(self.team, org, '0 0 0', 1);
+       self.nextthink = time + autocvar_cl_damageeffect_ticrate;
+}
+
+void DamageEffect(vector hitorg, float dmg, float type, float specnum)
+{
+       // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
+
+       float life, i;
+       string specstr, effectnum;
+       entity e;
+
+       if(autocvar_cl_gentle || autocvar_cl_gentle_damage)
+               return;
+       if(self == world || self.model == "" || !self.drawmask)
+               return;
+
+       // return if we reached our damage effect limit
+       for(e = world; (e = find(e, classname, "damageeffect")); )
+               if(e.owner.entnum == self.entnum)
+                       i += 1;
+       if(autocvar_cl_damageeffect < 1 || (self.isplayermodel && i >= autocvar_cl_damageeffect_limit))
+               return; // allow multiple damage effects on players
+       if(autocvar_cl_damageeffect < 2 || (!self.isplayermodel && i))
+               return; // allow a single damage effect on objects
+
+       specstr = species_prefix(specnum);
+       life = bound(autocvar_cl_damageeffect_lifetime_min, dmg * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
+       type = DEATH_WEAPONOF(type);
+       e = get_weaponinfo(type);
+
+       effectnum = strcat("weapondamage_", e.netname);
+       // if the weapon is a bullet weapon, its damage effect is blood
+       // since blood is species dependent, use the species tag for this effect
+       if(type == WEP_SHOTGUN || type == WEP_UZI || type == WEP_RIFLE)
+       {
+               if(self.isplayermodel)
+               {
+                       effectnum = strcat(effectnum, "_", specstr);
+                       effectnum = substring(effectnum, 0, strlen(effectnum) - 1); // remove the _ symbol at the end of the species tag
+               }
+               else
+                       return; // objects don't bleed
+       }
+
+       // if this is a player, damage effects will show on the limb where damage was dealt
+       // we do this by choosing the skeletal bone closest to the impact, and attaching the effect there
+       if(self.isplayermodel)
+       {
+               float closest;
+               FOR_EACH_TAG(self)
+               {
+                       // go through all skeletal bones on the player, and choose the one closest to the damage origin
+                       if(!closest || vlen(hitorg - gettaginfo(self, tagnum)) <= vlen(hitorg - gettaginfo(self, closest)))
+                               closest = tagnum;
+               }
+               gettaginfo(self, closest); // set gettaginfo_name to our bone
+       }
+       else
+               gettaginfo(self, 0); // set gettaginfo_name to entity origin
+
+       e = spawn();
+       setmodel(e, "models/null.md3"); // necessary to attach and read origin
+       setattachment(e, self, gettaginfo_name); // attach to the given bone
+       e.owner = self;
+       e.cnt = time + life;
+       e.classname = "damageeffect";
+       e.team = particleeffectnum(effectnum);
+       e.think = DamageEffect_Think;
+       e.nextthink = time;
+}