1 #ifndef DAMAGEEFFECTS_H
2 #define DAMAGEEFFECTS_H
5 #include "../../deathtypes/all.qh"
6 #include "../../physics/movetypes/movetypes.qh"
7 #include "../../../client/mutators/events.qh"
8 #include "../../vehicles/all.qh"
9 #include "../../weapons/all.qh"
16 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
20 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
22 WriteHeader(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
23 WriteShort(MSG_ENTITY, self.projectiledeathtype);
24 WriteCoord(MSG_ENTITY, floor(self.origin.x));
25 WriteCoord(MSG_ENTITY, floor(self.origin.y));
26 WriteCoord(MSG_ENTITY, floor(self.origin.z));
27 WriteByte(MSG_ENTITY, bound(1, self.dmg, 255));
28 WriteByte(MSG_ENTITY, bound(0, self.dmg_radius, 255));
29 WriteByte(MSG_ENTITY, bound(1, self.dmg_edge, 255));
30 WriteShort(MSG_ENTITY, self.oldorigin.x);
31 WriteByte(MSG_ENTITY, self.species);
35 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
37 // TODO maybe call this from non-edgedamage too?
38 // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
42 if(!sound_allowed(MSG_BROADCAST, dmgowner))
48 e.projectiledeathtype = deathtype;
50 e.dmg_edge = edgedamage;
52 e.dmg_force = vlen(force);
54 e.oldorigin_x = compressShortVector(e.velocity);
55 e.species = bloodtype;
57 Net_LinkEntity(e, false, 0.2, Damage_DamageInfo_SendEntity);
64 /** number of effects which currently are attached to a player */
73 void DamageEffect_Think()
75 // if particle distribution is enabled, slow ticrate by total number of damages
76 if(autocvar_cl_damageeffect_distribute)
77 self.nextthink = time + autocvar_cl_damageeffect_ticrate * self.owner.total_damages;
79 self.nextthink = time + autocvar_cl_damageeffect_ticrate;
81 if(time >= self.cnt || !self.owner || !self.owner.modelindex || !self.owner.drawmask)
83 // time is up or the player got gibbed / disconnected
84 self.owner.total_damages = max(0, self.owner.total_damages - 1);
88 if(self.state && !self.owner.csqcmodel_isdead)
90 // if the player was dead but is now alive, it means he respawned
91 // if so, clear his damage effects, or damages from his dead body will be copied back
92 self.owner.total_damages = max(0, self.owner.total_damages - 1);
96 self.state = self.owner.csqcmodel_isdead;
97 if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum) && !autocvar_chase_active)
98 return; // if we aren't using a third person camera, hide our own effects
100 // now generate the particles
102 org = gettaginfo(self, 0); // origin at attached location
103 __pointparticles(self.team, org, '0 0 0', 1);
106 string species_prefix(int specnum)
110 case SPECIES_HUMAN: return "";
111 case SPECIES_ALIEN: return "alien_";
112 case SPECIES_ROBOT_SHINY: return "robot_";
113 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
114 case SPECIES_ROBOT_SOLID: return "robot_"; // use the same effects, only different gibs
115 case SPECIES_ANIMAL: return "animal_";
116 case SPECIES_RESERVED: return "reserved_";
121 void DamageEffect(vector hitorg, float thedamage, int type, int specnum)
123 // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
127 string specstr, effectname;
130 if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
132 if(!self || !self.modelindex || !self.drawmask)
135 // if this is a rigged mesh, the effect will show on the bone where damage was dealt
136 // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
137 // if there's no skeleton, object origin will automatically be selected
141 continue; // skip empty bones
142 // blacklist bones positioned outside the mesh, or the effect will be floating
143 // TODO: Do we have to do it this way? Why do these bones exist at all?
144 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
145 continue; // player model bone blacklist
147 // now choose the bone closest to impact origin
148 if(nearestbone == 0 || vlen(hitorg - gettaginfo(self, tagnum)) <= vlen(hitorg - gettaginfo(self, nearestbone)))
149 nearestbone = tagnum;
151 gettaginfo(self, nearestbone); // set gettaginfo_name
153 // return if we reached our damage effect limit or damages are disabled
154 // TODO: When the limit is reached, it would be better if the oldest damage was removed instead of not adding a new one
157 if(self.total_damages >= autocvar_cl_damageeffect_bones)
158 return; // allow multiple damages on skeletal models
162 if(autocvar_cl_damageeffect < 2 || self.total_damages)
163 return; // allow a single damage on non-skeletal models
166 life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
168 effectname = DEATH_WEAPONOF(type).netname;
170 if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
172 if(self.isplayermodel)
174 specstr = species_prefix(specnum);
175 specstr = substring(specstr, 0, strlen(specstr) - 1);
176 effectname = strreplace("BLOOD", specstr, effectname);
178 else { return; } // objects don't bleed
183 setmodel(e, MDL_Null); // necessary to attach and read origin
184 setattachment(e, self, gettaginfo_name); // attach to the given bone
187 e.team = _particleeffectnum(effectname);
188 e.think = DamageEffect_Think;
190 self.total_damages += 1;
193 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
196 float thedamage, rad, edge, thisdmg;
197 bool hitplayer = false;
198 int species, forcemul;
199 vector force, thisforce;
201 w_deathtype = ReadShort();
202 w_issilent = (w_deathtype & 0x8000);
203 w_deathtype = (w_deathtype & 0x7FFF);
205 w_org.x = ReadCoord();
206 w_org.y = ReadCoord();
207 w_org.z = ReadCoord();
209 thedamage = ReadByte();
212 force = decompressShortVector(ReadShort());
213 species = ReadByte();
228 for(entity e = findradius(w_org, rad + MAX_DAMAGEEXTRARADIUS); e; e = e.chain)
231 // attached ents suck
235 vector nearest = NearestPointOnBox(self, w_org);
238 thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
245 thisdmg = thedamage + (edge - thedamage) * thisdmg;
246 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
251 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
256 if(vlen(nearest - w_org) > bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS))
260 thisforce = forcemul * force;
263 if(self.damageforcescale)
266 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
267 self.move_flags &= ~FL_ONGROUND;
273 if(self.event_damage)
274 self.event_damage(thisdmg, w_deathtype, w_org, thisforce);
276 DamageEffect(w_org, thisdmg, w_deathtype, species);
278 if(self.isplayermodel)
279 hitplayer = true; // this impact damaged a player
283 if(DEATH_ISVEHICLE(w_deathtype))
285 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
286 if(trace_plane_normal != '0 0 0')
287 w_backoff = trace_plane_normal;
289 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
291 setorigin(self, w_org + w_backoff * 2); // for sound() calls
293 switch(DEATH_ENT(w_deathtype))
299 case DEATH_VH_SPID_MINIGUN:
300 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
301 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
303 case DEATH_VH_SPID_ROCKET:
304 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
305 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
307 case DEATH_VH_SPID_DEATH:
308 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
309 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
312 case DEATH_VH_WAKI_GUN:
313 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
314 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
316 case DEATH_VH_WAKI_ROCKET:
317 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
318 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
320 case DEATH_VH_WAKI_DEATH:
321 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
322 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
325 case DEATH_VH_RAPT_CANNON:
326 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
327 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
329 case DEATH_VH_RAPT_FRAGMENT:
332 for(i = 1; i < 4; ++i)
334 vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
335 ang = vectoangles(vel);
336 RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
338 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
339 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
341 case DEATH_VH_RAPT_BOMB:
342 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
343 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
345 case DEATH_VH_RAPT_DEATH:
346 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
347 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
349 case DEATH_VH_BUMB_GUN:
350 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
351 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
357 if(DEATH_ISTURRET(w_deathtype))
359 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
360 if(trace_plane_normal != '0 0 0')
361 w_backoff = trace_plane_normal;
363 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
365 setorigin(self, w_org + w_backoff * 2); // for sound() calls
367 switch(DEATH_ENT(w_deathtype))
369 case DEATH_TURRET_EWHEEL:
370 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
371 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
374 case DEATH_TURRET_FLAC:
375 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
376 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
379 case DEATH_TURRET_MLRS:
380 case DEATH_TURRET_HK:
381 case DEATH_TURRET_WALK_ROCKET:
382 case DEATH_TURRET_HELLION:
383 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
384 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
387 case DEATH_TURRET_MACHINEGUN:
388 case DEATH_TURRET_WALK_GUN:
389 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
390 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
393 case DEATH_TURRET_PLASMA:
394 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
395 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
398 case DEATH_TURRET_WALK_MELEE:
399 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
400 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
403 case DEATH_TURRET_PHASER:
406 case DEATH_TURRET_TESLA:
407 te_smallflash(self.origin);
413 // TODO spawn particle effects and sounds based on w_deathtype
414 if(!DEATH_ISSPECIAL(w_deathtype))
415 if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
417 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
418 w_random = prandom();
420 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
421 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
422 w_backoff = trace_plane_normal;
424 w_backoff = -1 * normalize(force);
425 setorigin(self, w_org + w_backoff * 2); // for sound() calls
427 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
429 if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep))
430 hitwep.wr_impacteffect(hitwep);