1 #ifndef DAMAGEEFFECTS_H
2 #define DAMAGEEFFECTS_H
5 #include "../../deathtypes/all.qh"
6 #include "../../movetypes/movetypes.qh"
7 #include "../../vehicles/all.qh"
8 #include "../../weapons/all.qh"
15 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
19 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
21 WriteHeader(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
22 WriteShort(MSG_ENTITY, self.projectiledeathtype);
23 WriteCoord(MSG_ENTITY, floor(self.origin.x));
24 WriteCoord(MSG_ENTITY, floor(self.origin.y));
25 WriteCoord(MSG_ENTITY, floor(self.origin.z));
26 WriteByte(MSG_ENTITY, bound(1, self.dmg, 255));
27 WriteByte(MSG_ENTITY, bound(0, self.dmg_radius, 255));
28 WriteByte(MSG_ENTITY, bound(1, self.dmg_edge, 255));
29 WriteShort(MSG_ENTITY, self.oldorigin.x);
30 WriteByte(MSG_ENTITY, self.species);
34 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
36 // TODO maybe call this from non-edgedamage too?
37 // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
41 if(!sound_allowed(MSG_BROADCAST, dmgowner))
47 e.projectiledeathtype = deathtype;
49 e.dmg_edge = edgedamage;
51 e.dmg_force = vlen(force);
53 e.oldorigin_x = compressShortVector(e.velocity);
54 e.species = bloodtype;
56 Net_LinkEntity(e, false, 0.2, Damage_DamageInfo_SendEntity);
63 /** number of effects which currently are attached to a player */
72 void DamageEffect_Think()
74 // if particle distribution is enabled, slow ticrate by total number of damages
75 if(autocvar_cl_damageeffect_distribute)
76 self.nextthink = time + autocvar_cl_damageeffect_ticrate * self.owner.total_damages;
78 self.nextthink = time + autocvar_cl_damageeffect_ticrate;
80 if(time >= self.cnt || !self.owner || !self.owner.modelindex || !self.owner.drawmask)
82 // time is up or the player got gibbed / disconnected
83 self.owner.total_damages = max(0, self.owner.total_damages - 1);
87 if(self.state && !self.owner.csqcmodel_isdead)
89 // if the player was dead but is now alive, it means he respawned
90 // if so, clear his damage effects, or damages from his dead body will be copied back
91 self.owner.total_damages = max(0, self.owner.total_damages - 1);
95 self.state = self.owner.csqcmodel_isdead;
96 if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum) && !autocvar_chase_active)
97 return; // if we aren't using a third person camera, hide our own effects
99 // now generate the particles
101 org = gettaginfo(self, 0); // origin at attached location
102 __pointparticles(self.team, org, '0 0 0', 1);
105 string species_prefix(int specnum)
109 case SPECIES_HUMAN: return "";
110 case SPECIES_ALIEN: return "alien_";
111 case SPECIES_ROBOT_SHINY: return "robot_";
112 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
113 case SPECIES_ROBOT_SOLID: return "robot_"; // use the same effects, only different gibs
114 case SPECIES_ANIMAL: return "animal_";
115 case SPECIES_RESERVED: return "reserved_";
120 void DamageEffect(vector hitorg, float thedamage, int type, int specnum)
122 // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
126 string specstr, effectname;
129 if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
131 if(!self || !self.modelindex || !self.drawmask)
134 // if this is a rigged mesh, the effect will show on the bone where damage was dealt
135 // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
136 // if there's no skeleton, object origin will automatically be selected
140 continue; // skip empty bones
141 // blacklist bones positioned outside the mesh, or the effect will be floating
142 // TODO: Do we have to do it this way? Why do these bones exist at all?
143 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
144 continue; // player model bone blacklist
146 // now choose the bone closest to impact origin
147 if(nearestbone == 0 || vlen(hitorg - gettaginfo(self, tagnum)) <= vlen(hitorg - gettaginfo(self, nearestbone)))
148 nearestbone = tagnum;
150 gettaginfo(self, nearestbone); // set gettaginfo_name
152 // return if we reached our damage effect limit or damages are disabled
153 // TODO: When the limit is reached, it would be better if the oldest damage was removed instead of not adding a new one
156 if(self.total_damages >= autocvar_cl_damageeffect_bones)
157 return; // allow multiple damages on skeletal models
161 if(autocvar_cl_damageeffect < 2 || self.total_damages)
162 return; // allow a single damage on non-skeletal models
165 life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
167 effectname = DEATH_WEAPONOF(type).netname;
169 if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
171 if(self.isplayermodel)
173 specstr = species_prefix(specnum);
174 specstr = substring(specstr, 0, strlen(specstr) - 1);
175 effectname = strreplace("BLOOD", specstr, effectname);
177 else { return; } // objects don't bleed
182 setmodel(e, MDL_Null); // necessary to attach and read origin
183 setattachment(e, self, gettaginfo_name); // attach to the given bone
186 e.team = _particleeffectnum(effectname);
187 e.think = DamageEffect_Think;
189 self.total_damages += 1;
192 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
195 float thedamage, rad, edge, thisdmg;
196 bool hitplayer = false;
197 int species, forcemul;
198 vector force, thisforce;
200 w_deathtype = ReadShort();
201 w_issilent = (w_deathtype & 0x8000);
202 w_deathtype = (w_deathtype & 0x7FFF);
204 w_org.x = ReadCoord();
205 w_org.y = ReadCoord();
206 w_org.z = ReadCoord();
208 thedamage = ReadByte();
211 force = decompressShortVector(ReadShort());
212 species = ReadByte();
227 for(entity e = findradius(w_org, rad + MAX_DAMAGEEXTRARADIUS); e; e = e.chain)
230 // attached ents suck
234 vector nearest = NearestPointOnBox(self, w_org);
237 thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
244 thisdmg = thedamage + (edge - thedamage) * thisdmg;
245 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
250 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
255 if(vlen(nearest - w_org) > bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS))
259 thisforce = forcemul * force;
262 if(self.damageforcescale)
265 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
266 self.move_flags &= ~FL_ONGROUND;
272 if(self.event_damage)
273 self.event_damage(thisdmg, w_deathtype, w_org, thisforce);
275 DamageEffect(w_org, thisdmg, w_deathtype, species);
277 if(self.isplayermodel)
278 hitplayer = true; // this impact damaged a player
282 if(DEATH_ISVEHICLE(w_deathtype))
284 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
285 if(trace_plane_normal != '0 0 0')
286 w_backoff = trace_plane_normal;
288 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
290 setorigin(self, w_org + w_backoff * 2); // for sound() calls
292 switch(DEATH_ENT(w_deathtype))
298 case DEATH_VH_SPID_MINIGUN:
299 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
300 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
302 case DEATH_VH_SPID_ROCKET:
303 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
304 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
306 case DEATH_VH_SPID_DEATH:
307 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
308 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
311 case DEATH_VH_WAKI_GUN:
312 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
313 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
315 case DEATH_VH_WAKI_ROCKET:
316 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
317 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
319 case DEATH_VH_WAKI_DEATH:
320 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
321 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
324 case DEATH_VH_RAPT_CANNON:
325 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
326 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
328 case DEATH_VH_RAPT_FRAGMENT:
331 for(i = 1; i < 4; ++i)
333 vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
334 ang = vectoangles(vel);
335 RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
337 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
338 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
340 case DEATH_VH_RAPT_BOMB:
341 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
342 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
344 case DEATH_VH_RAPT_DEATH:
345 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
346 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
348 case DEATH_VH_BUMB_GUN:
349 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
350 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
356 if(DEATH_ISTURRET(w_deathtype))
358 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
359 if(trace_plane_normal != '0 0 0')
360 w_backoff = trace_plane_normal;
362 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
364 setorigin(self, w_org + w_backoff * 2); // for sound() calls
366 switch(DEATH_ENT(w_deathtype))
368 case DEATH_TURRET_EWHEEL:
369 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
370 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
373 case DEATH_TURRET_FLAC:
374 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
375 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
378 case DEATH_TURRET_MLRS:
379 case DEATH_TURRET_HK:
380 case DEATH_TURRET_WALK_ROCKET:
381 case DEATH_TURRET_HELLION:
382 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
383 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
386 case DEATH_TURRET_MACHINEGUN:
387 case DEATH_TURRET_WALK_GUN:
388 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
389 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
392 case DEATH_TURRET_PLASMA:
393 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
394 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
397 case DEATH_TURRET_WALK_MELEE:
398 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
399 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
402 case DEATH_TURRET_PHASER:
405 case DEATH_TURRET_TESLA:
406 te_smallflash(self.origin);
412 // TODO spawn particle effects and sounds based on w_deathtype
413 if(!DEATH_ISSPECIAL(w_deathtype))
414 if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
416 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
417 w_random = prandom();
419 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
420 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
421 w_backoff = trace_plane_normal;
423 w_backoff = -1 * normalize(force);
424 setorigin(self, w_org + w_backoff * 2); // for sound() calls
426 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)) {
427 hitwep.wr_impacteffect(hitwep);