]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/client/damage.qc
Rename cl_damageeffect_limit to cl_damageeffect_bones, which is more appropriate now
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / damage.qc
index 1617e4923d8940503b7e0c9d8b3d4e49707ffc81..8faa255a33314975f1afb0d54628d133ca039ddb 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;
@@ -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;
@@ -157,20 +168,13 @@ void Ent_DamageInfo(float isNew)
            {   
              case DEATH_TURRET_EWHEEL:
                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
-                pointparticles(particleeffectnum("electro_impact"), self.origin, w_backoff * 1000, 1);
+                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, CH_SHOTS, "weapons/hagexp1.wav", VOL_BASE, ATTN_NORM);
-                else if (w_random<0.7)
-                    sound(self, CH_SHOTS, "weapons/hagexp2.wav", VOL_BASE, ATTN_NORM);
-                else
-                    sound(self, CH_SHOTS, "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:
@@ -183,7 +187,6 @@ void Ent_DamageInfo(float isNew)
              
              case DEATH_TURRET_MACHINEGUN:
              case DEATH_TURRET_WALKER_GUN:
-                string _snd;
                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
                 pointparticles(particleeffectnum("machinegun_impact"), self.origin, w_backoff * 1000, 1);
@@ -234,3 +237,115 @@ void DamageInfo_Precache()
        for(i = WEP_FIRST; i <= WEP_LAST; ++i)
                (get_weaponinfo(i)).weapon_func(WR_PRECACHE);
 }
+
+.float total_damages;
+void DamageEffect_Think()
+{
+       // if particle distribution is enabled, decrease ticrate by total number of damages
+       if(autocvar_cl_damageeffect_distribute)
+               self.nextthink = time + autocvar_cl_damageeffect_ticrate * self.owner.total_damages;
+       else
+               self.nextthink = time + autocvar_cl_damageeffect_ticrate;
+
+       if(time >= self.cnt || !self.owner || !self.owner.modelindex || !self.owner.drawmask)
+       {
+               // time is up or the player got gibbed / disconnected
+               self.owner.total_damages -= 1;
+               remove(self);
+               return;
+       }
+       if(self.state && !self.owner.csqcmodel_isdead)
+       {
+               // if the player was dead but is now alive, it means he respawned
+               // if so, clear his damage effects, or damages from his dead body will be copied over
+               self.owner.total_damages -= 1;
+               remove(self);
+               return;
+       }
+       self.state = self.owner.csqcmodel_isdead;
+       if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum || self.owner.entnum == spectatee_status) && !autocvar_chase_active)
+               return; // if we aren't using a third person camera, 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);
+}
+
+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, skeletal;
+       string specstr, effectnum;
+       entity e;
+
+       if(autocvar_cl_gentle || autocvar_cl_gentle_damage)
+               return;
+       if(!self || !self.modelindex || !self.drawmask)
+               return;
+
+       // if this is a rigged mesh, the effect will show on the bone where damage was dealt
+       // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
+       // if there's no skeleton, object origin will automatically be selected
+       float closest;
+       FOR_EACH_TAG(self)
+       {
+               // blacklist bones positioned outside the mesh, or the effect will be floating
+               // TODO: Do we have to do it this way? Why do these bones exist at all?
+               if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
+                       continue; // player model bone blacklist
+               if(gettaginfo_name == "")
+                       continue; // skip empty bones
+
+               // now choose the bone closest to impact origin
+               if(!closest || vlen(hitorg - gettaginfo(self, tagnum)) <= vlen(hitorg - gettaginfo(self, closest)))
+               {
+                       closest = tagnum;
+                       skeletal = TRUE;
+               }
+       }
+       gettaginfo(self, closest); // set gettaginfo_name
+
+       // return if we reached our damage effect limit or damages are disabled
+       if(skeletal)
+       {
+               if(autocvar_cl_damageeffect < 1 || self.total_damages >= autocvar_cl_damageeffect_bones)
+                       return; // allow multiple damages on skeletal models
+       }
+       else
+       {
+               if(autocvar_cl_damageeffect < 2 || self.total_damages)
+                       return; // allow a single damage on non-skeletal models
+       }
+
+       life = bound(autocvar_cl_damageeffect_lifetime_min, dmg * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
+       specstr = species_prefix(specnum);
+       type = DEATH_WEAPONOF(type);
+       e = get_weaponinfo(type);
+
+       effectnum = strcat("weapondamage_", e.netname);
+       // if damage was dealt with a bullet weapon, our effect is blood
+       // since blood is species dependent, include the species tag
+       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
+       }
+
+       e = spawn();
+       setmodel(e, "models/null.md3"); // necessary to attach and read origin
+       setattachment(e, self, gettaginfo_name); // attach to the given bone
+       e.classname = "damageeffect";
+       e.owner = self;
+       e.cnt = time + life;
+       e.team = particleeffectnum(effectnum);
+       e.think = DamageEffect_Think;
+       e.nextthink = time;
+       self.total_damages += 1;
+}