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 this.nextthink = time + autocvar_cl_damageeffect_ticrate * this.owner.total_damages;
78 this.nextthink = time + autocvar_cl_damageeffect_ticrate;
80 if(time >= this.cnt || !this.owner || !this.owner.modelindex || !this.owner.drawmask)
82 // time is up or the player got gibbed / disconnected
83 this.owner.total_damages = max(0, this.owner.total_damages - 1);
87 if(this.state && !this.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 this.owner.total_damages = max(0, this.owner.total_damages - 1);
95 this.state = this.owner.csqcmodel_isdead;
96 if(this.owner.isplayermodel && (this.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(this, 0); // origin at attached location
102 __pointparticles(this.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(entity this, 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(!this || !this.modelindex || !this.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(this, tagnum)) <= vlen2(hitorg - gettaginfo(this, nearestbone)))
148 nearestbone = tagnum;
150 gettaginfo(this, 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(this.total_damages >= autocvar_cl_damageeffect_bones)
157 return; // allow multiple damages on skeletal models
161 if(autocvar_cl_damageeffect < 2 || this.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(this.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, this, gettaginfo_name); // attach to the given bone
185 e.team = _particleeffectnum(effectname);
186 e.think = DamageEffect_Think;
188 this.total_damages += 1;
191 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
193 const float ATTEN_LOW = 0.2;
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, {
227 vector nearest = NearestPointOnBox(it, w_org);
230 thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
237 thisdmg = thedamage + (edge - thedamage) * thisdmg;
238 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(it.origin - w_org);
243 thisforce = forcemul * vlen(force) * normalize(it.origin - w_org);
248 if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
252 thisforce = forcemul * force;
255 if(it.damageforcescale)
256 if(vdist(thisforce, !=, 0))
258 it.move_velocity = it.move_velocity + damage_explosion_calcpush(it.damageforcescale * thisforce, it.move_velocity, autocvar_g_balance_damagepush_speedfactor);
259 it.move_flags &= ~FL_ONGROUND;
266 it.event_damage(it, thisdmg, w_deathtype, w_org, thisforce);
268 DamageEffect(it, w_org, thisdmg, w_deathtype, species);
271 hitplayer = true; // this impact damaged a player
274 if(DEATH_ISVEHICLE(w_deathtype))
276 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
277 if(trace_plane_normal != '0 0 0')
278 w_backoff = trace_plane_normal;
280 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
282 setorigin(this, w_org + w_backoff * 2); // for sound() calls
284 switch(DEATH_ENT(w_deathtype))
290 case DEATH_VH_SPID_MINIGUN:
291 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
292 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, this.origin, w_backoff * 1000, 1);
294 case DEATH_VH_SPID_ROCKET:
295 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
296 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
298 case DEATH_VH_SPID_DEATH:
299 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
300 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
303 case DEATH_VH_WAKI_GUN:
304 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
305 pointparticles(EFFECT_RACER_IMPACT, this.origin, w_backoff * 1000, 1);
307 case DEATH_VH_WAKI_ROCKET:
308 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
309 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
311 case DEATH_VH_WAKI_DEATH:
312 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
313 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
316 case DEATH_VH_RAPT_CANNON:
317 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
318 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, this.origin, w_backoff * 1000, 1);
320 case DEATH_VH_RAPT_FRAGMENT:
323 for(i = 1; i < 4; ++i)
325 vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
326 ang = vectoangles(vel);
327 RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
329 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
330 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, this.origin, w_backoff * 1000, 1);
332 case DEATH_VH_RAPT_BOMB:
333 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
334 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, this.origin, w_backoff * 1000, 1);
336 case DEATH_VH_RAPT_DEATH:
337 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
338 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
340 case DEATH_VH_BUMB_GUN:
341 sound(this, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
342 pointparticles(EFFECT_BIGPLASMA_IMPACT, this.origin, w_backoff * 1000, 1);
348 if(DEATH_ISTURRET(w_deathtype))
350 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
351 if(trace_plane_normal != '0 0 0')
352 w_backoff = trace_plane_normal;
354 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
356 setorigin(this, w_org + w_backoff * 2); // for sound() calls
358 switch(DEATH_ENT(w_deathtype))
360 case DEATH_TURRET_EWHEEL:
361 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
362 pointparticles(EFFECT_BLASTER_IMPACT, this.origin, w_backoff * 1000, 1);
365 case DEATH_TURRET_FLAC:
366 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
367 sound(this, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
370 case DEATH_TURRET_MLRS:
371 case DEATH_TURRET_HK:
372 case DEATH_TURRET_WALK_ROCKET:
373 case DEATH_TURRET_HELLION:
374 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
375 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
378 case DEATH_TURRET_MACHINEGUN:
379 case DEATH_TURRET_WALK_GUN:
380 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
381 pointparticles(EFFECT_MACHINEGUN_IMPACT, this.origin, w_backoff * 1000, 1);
384 case DEATH_TURRET_PLASMA:
385 sound(this, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_LOW);
386 pointparticles(EFFECT_ELECTRO_IMPACT, this.origin, w_backoff * 1000, 1);
389 case DEATH_TURRET_WALK_MELEE:
390 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_LOW);
391 pointparticles(EFFECT_TE_SPARK, this.origin, w_backoff * 1000, 1);
394 case DEATH_TURRET_PHASER:
397 case DEATH_TURRET_TESLA:
398 te_smallflash(this.origin);
404 // TODO spawn particle effects and sounds based on w_deathtype
405 if(!DEATH_ISSPECIAL(w_deathtype))
406 if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
408 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
409 w_random = prandom();
411 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
412 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
413 w_backoff = trace_plane_normal;
415 w_backoff = -1 * normalize(force);
416 setorigin(this, w_org + w_backoff * 2); // for sound() calls
418 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
420 if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep))
421 hitwep.wr_impacteffect(hitwep);