1 #ifndef DAMAGEEFFECTS_H
2 #define DAMAGEEFFECTS_H
5 #include <common/deathtypes/all.qh>
6 #include <common/physics/movetypes/movetypes.qh>
7 #include <client/mutators/events.qh>
8 #include <common/vehicles/all.qh>
9 #include <common/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))
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 || vlen2(hitorg - gettaginfo(self, tagnum)) <= vlen2(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
181 setmodel(e, MDL_Null); // necessary to attach and read origin
182 setattachment(e, self, gettaginfo_name); // attach to the given bone
185 e.team = _particleeffectnum(effectname);
186 e.think = DamageEffect_Think;
188 self.total_damages += 1;
191 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
194 float thedamage, rad, edge, thisdmg;
195 bool hitplayer = false;
196 int species, forcemul;
197 vector force, thisforce;
199 w_deathtype = ReadShort();
200 w_issilent = (w_deathtype & 0x8000);
201 w_deathtype = (w_deathtype & 0x7FFF);
203 w_org.x = ReadCoord();
204 w_org.y = ReadCoord();
205 w_org.z = ReadCoord();
207 thedamage = ReadByte();
210 force = decompressShortVector(ReadShort());
211 species = ReadByte();
226 FOREACH_ENTITY_RADIUS(w_org, rad + MAX_DAMAGEEXTRARADIUS, !it.tag_entity, {
228 vector nearest = NearestPointOnBox(self, w_org);
231 thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
238 thisdmg = thedamage + (edge - thedamage) * thisdmg;
239 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
244 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
249 if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
253 thisforce = forcemul * force;
256 if(self.damageforcescale)
257 if(vdist(thisforce, !=, 0))
259 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
260 self.move_flags &= ~FL_ONGROUND;
266 if(self.event_damage)
267 self.event_damage(self, thisdmg, w_deathtype, w_org, thisforce);
269 DamageEffect(w_org, thisdmg, w_deathtype, species);
271 if(self.isplayermodel)
272 hitplayer = true; // this impact damaged a player
276 if(DEATH_ISVEHICLE(w_deathtype))
278 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
279 if(trace_plane_normal != '0 0 0')
280 w_backoff = trace_plane_normal;
282 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
284 setorigin(self, w_org + w_backoff * 2); // for sound() calls
286 switch(DEATH_ENT(w_deathtype))
292 case DEATH_VH_SPID_MINIGUN:
293 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
294 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
296 case DEATH_VH_SPID_ROCKET:
297 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
298 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
300 case DEATH_VH_SPID_DEATH:
301 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
302 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
305 case DEATH_VH_WAKI_GUN:
306 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
307 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
309 case DEATH_VH_WAKI_ROCKET:
310 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
311 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
313 case DEATH_VH_WAKI_DEATH:
314 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
315 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
318 case DEATH_VH_RAPT_CANNON:
319 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
320 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
322 case DEATH_VH_RAPT_FRAGMENT:
325 for(i = 1; i < 4; ++i)
327 vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
328 ang = vectoangles(vel);
329 RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
331 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
332 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
334 case DEATH_VH_RAPT_BOMB:
335 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
336 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
338 case DEATH_VH_RAPT_DEATH:
339 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
340 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
342 case DEATH_VH_BUMB_GUN:
343 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
344 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
350 if(DEATH_ISTURRET(w_deathtype))
352 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
353 if(trace_plane_normal != '0 0 0')
354 w_backoff = trace_plane_normal;
356 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
358 setorigin(self, w_org + w_backoff * 2); // for sound() calls
360 switch(DEATH_ENT(w_deathtype))
362 case DEATH_TURRET_EWHEEL:
363 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
364 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
367 case DEATH_TURRET_FLAC:
368 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
369 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
372 case DEATH_TURRET_MLRS:
373 case DEATH_TURRET_HK:
374 case DEATH_TURRET_WALK_ROCKET:
375 case DEATH_TURRET_HELLION:
376 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
377 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
380 case DEATH_TURRET_MACHINEGUN:
381 case DEATH_TURRET_WALK_GUN:
382 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
383 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
386 case DEATH_TURRET_PLASMA:
387 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
388 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
391 case DEATH_TURRET_WALK_MELEE:
392 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
393 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
396 case DEATH_TURRET_PHASER:
399 case DEATH_TURRET_TESLA:
400 te_smallflash(self.origin);
406 // TODO spawn particle effects and sounds based on w_deathtype
407 if(!DEATH_ISSPECIAL(w_deathtype))
408 if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
410 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
411 w_random = prandom();
413 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
414 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
415 w_backoff = trace_plane_normal;
417 w_backoff = -1 * normalize(force);
418 setorigin(self, w_org + w_backoff * 2); // for sound() calls
420 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
422 if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep))
423 hitwep.wr_impacteffect(hitwep);