1 #include "damageeffects.qh"
5 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
9 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
11 WriteHeader(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
12 WriteShort(MSG_ENTITY, this.projectiledeathtype);
13 WriteCoord(MSG_ENTITY, floor(this.origin.x));
14 WriteCoord(MSG_ENTITY, floor(this.origin.y));
15 WriteCoord(MSG_ENTITY, floor(this.origin.z));
16 WriteByte(MSG_ENTITY, bound(1, this.dmg, 255));
17 WriteByte(MSG_ENTITY, bound(0, this.dmg_radius, 255));
18 WriteByte(MSG_ENTITY, bound(1, this.dmg_edge, 255));
19 WriteShort(MSG_ENTITY, this.oldorigin.x);
20 WriteByte(MSG_ENTITY, this.species);
24 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
26 // TODO maybe call this from non-edgedamage too?
27 // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
31 if(!sound_allowed(MSG_BROADCAST, dmgowner))
36 e.projectiledeathtype = deathtype;
38 e.dmg_edge = edgedamage;
40 e.dmg_force = vlen(force);
42 e.oldorigin_x = compressShortVector(e.velocity);
43 e.species = bloodtype;
45 Net_LinkEntity(e, false, 0.2, Damage_DamageInfo_SendEntity);
52 /** number of effects which currently are attached to a player */
61 void DamageEffect_Think(entity this)
63 // if particle distribution is enabled, slow ticrate by total number of damages
64 if(autocvar_cl_damageeffect_distribute)
65 this.nextthink = time + autocvar_cl_damageeffect_ticrate * this.owner.total_damages;
67 this.nextthink = time + autocvar_cl_damageeffect_ticrate;
69 if(time >= this.cnt || !this.owner || !this.owner.modelindex || !this.owner.drawmask)
71 // time is up or the player got gibbed / disconnected
72 this.owner.total_damages = max(0, this.owner.total_damages - 1);
76 if(this.state && !this.owner.csqcmodel_isdead)
78 // if the player was dead but is now alive, it means he respawned
79 // if so, clear his damage effects, or damages from his dead body will be copied back
80 this.owner.total_damages = max(0, this.owner.total_damages - 1);
84 this.state = this.owner.csqcmodel_isdead;
85 if(this.owner.isplayermodel && (this.owner.entnum == player_localentnum) && !autocvar_chase_active)
86 return; // if we aren't using a third person camera, hide our own effects
88 // now generate the particles
90 org = gettaginfo(this, 0); // origin at attached location
91 __pointparticles(this.team, org, '0 0 0', 1);
94 string species_prefix(int specnum)
98 case SPECIES_HUMAN: return "";
99 case SPECIES_ALIEN: return "alien_";
100 case SPECIES_ROBOT_SHINY: return "robot_";
101 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
102 case SPECIES_ROBOT_SOLID: return "robot_"; // use the same effects, only different gibs
103 case SPECIES_ANIMAL: return "animal_";
104 case SPECIES_RESERVED: return "reserved_";
109 void DamageEffect(entity this, vector hitorg, float thedamage, int type, int specnum)
111 // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
115 string specstr, effectname;
118 if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
120 if(!this || !this.modelindex || !this.drawmask)
123 // if this is a rigged mesh, the effect will show on the bone where damage was dealt
124 // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
125 // if there's no skeleton, object origin will automatically be selected
129 continue; // skip empty bones
130 // blacklist bones positioned outside the mesh, or the effect will be floating
131 // TODO: Do we have to do it this way? Why do these bones exist at all?
132 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
133 continue; // player model bone blacklist
135 // now choose the bone closest to impact origin
136 if(nearestbone == 0 || vlen2(hitorg - gettaginfo(this, tagnum)) <= vlen2(hitorg - gettaginfo(this, nearestbone)))
137 nearestbone = tagnum;
139 gettaginfo(this, nearestbone); // set gettaginfo_name
141 // return if we reached our damage effect limit or damages are disabled
142 // TODO: When the limit is reached, it would be better if the oldest damage was removed instead of not adding a new one
145 if(this.total_damages >= autocvar_cl_damageeffect_bones)
146 return; // allow multiple damages on skeletal models
150 if(autocvar_cl_damageeffect < 2 || this.total_damages)
151 return; // allow a single damage on non-skeletal models
154 life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
156 effectname = DEATH_WEAPONOF(type).netname;
158 if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
160 if(this.isplayermodel)
162 specstr = species_prefix(specnum);
163 specstr = substring(specstr, 0, strlen(specstr) - 1);
164 effectname = strreplace("BLOOD", specstr, effectname);
166 else { return; } // objects don't bleed
170 setmodel(e, MDL_Null); // necessary to attach and read origin
171 setattachment(e, this, gettaginfo_name); // attach to the given bone
174 e.team = _particleeffectnum(effectname);
175 setthink(e, DamageEffect_Think);
177 this.total_damages += 1;
180 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
182 const float ATTEN_LOW = 0.2;
183 float thedamage, rad, edge, thisdmg;
184 bool hitplayer = false;
185 int species, forcemul;
186 vector force, thisforce;
188 w_deathtype = ReadShort();
189 w_issilent = (w_deathtype & 0x8000);
190 w_deathtype = (w_deathtype & 0x7FFF);
192 w_org.x = ReadCoord();
193 w_org.y = ReadCoord();
194 w_org.z = ReadCoord();
196 thedamage = ReadByte();
199 force = decompressShortVector(ReadShort());
200 species = ReadByte();
215 FOREACH_ENTITY_RADIUS(w_org, rad + MAX_DAMAGEEXTRARADIUS, !it.tag_entity, {
216 vector nearest = NearestPointOnBox(it, w_org);
219 thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
226 thisdmg = thedamage + (edge - thedamage) * thisdmg;
227 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(it.origin - w_org);
232 thisforce = forcemul * vlen(force) * normalize(it.origin - w_org);
237 if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
241 thisforce = forcemul * force;
244 if(it.damageforcescale)
245 if(vdist(thisforce, !=, 0))
247 it.velocity = it.velocity + damage_explosion_calcpush(it.damageforcescale * thisforce, it.velocity, damagepush_speedfactor);
255 it.event_damage(it, thisdmg, w_deathtype, w_org, thisforce);
257 DamageEffect(it, w_org, thisdmg, w_deathtype, species);
260 hitplayer = true; // this impact damaged a player
263 if(DEATH_ISVEHICLE(w_deathtype))
265 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, NULL);
266 if(trace_plane_normal != '0 0 0')
267 w_backoff = trace_plane_normal;
269 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
271 setorigin(this, w_org + w_backoff * 2); // for sound() calls
273 switch(DEATH_ENT(w_deathtype))
279 case DEATH_VH_SPID_MINIGUN:
280 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
281 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, this.origin, w_backoff * 1000, 1);
283 case DEATH_VH_SPID_ROCKET:
284 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
285 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
287 case DEATH_VH_SPID_DEATH:
288 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
289 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
292 case DEATH_VH_WAKI_GUN:
293 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
294 pointparticles(EFFECT_RACER_IMPACT, this.origin, w_backoff * 1000, 1);
296 case DEATH_VH_WAKI_ROCKET:
297 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
298 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
300 case DEATH_VH_WAKI_DEATH:
301 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
302 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
305 case DEATH_VH_RAPT_CANNON:
306 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
307 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, this.origin, w_backoff * 1000, 1);
309 case DEATH_VH_RAPT_FRAGMENT:
312 for(i = 1; i < 4; ++i)
314 vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
315 ang = vectoangles(vel);
316 RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
318 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
319 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, this.origin, w_backoff * 1000, 1);
321 case DEATH_VH_RAPT_BOMB:
322 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
323 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, this.origin, w_backoff * 1000, 1);
325 case DEATH_VH_RAPT_DEATH:
326 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
327 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
329 case DEATH_VH_BUMB_GUN:
330 sound(this, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
331 pointparticles(EFFECT_BIGPLASMA_IMPACT, this.origin, w_backoff * 1000, 1);
337 if(DEATH_ISTURRET(w_deathtype))
339 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, NULL);
340 if(trace_plane_normal != '0 0 0')
341 w_backoff = trace_plane_normal;
343 w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
345 setorigin(this, w_org + w_backoff * 2); // for sound() calls
347 switch(DEATH_ENT(w_deathtype))
349 case DEATH_TURRET_EWHEEL:
350 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
351 pointparticles(EFFECT_BLASTER_IMPACT, this.origin, w_backoff * 1000, 1);
354 case DEATH_TURRET_FLAC:
355 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
356 sound(this, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
359 case DEATH_TURRET_MLRS:
360 case DEATH_TURRET_HK:
361 case DEATH_TURRET_WALK_ROCKET:
362 case DEATH_TURRET_HELLION:
363 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
364 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
367 case DEATH_TURRET_MACHINEGUN:
368 case DEATH_TURRET_WALK_GUN:
369 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
370 pointparticles(EFFECT_MACHINEGUN_IMPACT, this.origin, w_backoff * 1000, 1);
373 case DEATH_TURRET_PLASMA:
374 sound(this, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_LOW);
375 pointparticles(EFFECT_ELECTRO_IMPACT, this.origin, w_backoff * 1000, 1);
378 case DEATH_TURRET_WALK_MELEE:
379 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_LOW);
380 pointparticles(EFFECT_TE_SPARK, this.origin, w_backoff * 1000, 1);
383 case DEATH_TURRET_PHASER:
386 case DEATH_TURRET_TESLA:
387 te_smallflash(this.origin);
393 MUTATOR_CALLHOOK(DamageInfo, this, w_deathtype, w_org);
395 // TODO spawn particle effects and sounds based on w_deathtype
396 if(!DEATH_ISSPECIAL(w_deathtype))
397 if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
399 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
400 w_random = prandom();
402 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, NULL);
403 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
404 w_backoff = trace_plane_normal;
406 w_backoff = -1 * normalize(force);
407 setorigin(this, w_org + w_backoff * 2); // for sound() calls
409 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
411 if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep, this))
412 hitwep.wr_impacteffect(hitwep, this);