1 #include "damageeffects.qh"
3 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
7 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
9 vector org = vec3(floor(this.origin.x), floor(this.origin.y), floor(this.origin.z));
10 WriteHeader(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
11 WriteShort(MSG_ENTITY, this.projectiledeathtype);
12 WriteVector(MSG_ENTITY, org);
13 WriteByte(MSG_ENTITY, bound(1, this.dmg, 255));
14 WriteByte(MSG_ENTITY, bound(0, this.dmg_radius, 255));
15 WriteByte(MSG_ENTITY, bound(1, this.dmg_edge, 255));
16 WriteVector(MSG_ENTITY, this.velocity);
17 WriteByte(MSG_ENTITY, this.species);
21 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
23 // TODO maybe call this from non-edgedamage too?
24 // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
28 if(!sound_allowed(MSG_BROADCAST, dmgowner))
31 e = new_pure(damageinfo);
32 // origin is just data to be sent
35 e.projectiledeathtype = deathtype;
37 e.dmg_edge = edgedamage;
39 e.dmg_force = vlen(force);
41 e.species = bloodtype;
43 Net_LinkEntity(e, false, 0.2, Damage_DamageInfo_SendEntity);
50 /** number of effects which currently are attached to a player */
60 void DamageEffect_Think(entity this)
62 // if particle distribution is enabled, slow ticrate by total number of damages
63 if(autocvar_cl_damageeffect_distribute)
64 this.nextthink = time + autocvar_cl_damageeffect_ticrate * this.owner.total_damages;
66 this.nextthink = time + autocvar_cl_damageeffect_ticrate;
68 if(time >= this.cnt || !this.owner || !this.owner.modelindex || !this.owner.drawmask)
70 // time is up or the player got gibbed / disconnected
71 this.owner.total_damages = max(0, this.owner.total_damages - 1);
75 if(this.state && !this.owner.csqcmodel_isdead)
77 // if the player was dead but is now alive, it means he respawned
78 // if so, clear his damage effects, or damages from his dead body will be copied back
79 this.owner.total_damages = max(0, this.owner.total_damages - 1);
83 this.state = this.owner.csqcmodel_isdead;
84 if((this.owner.isplayermodel & ISPLAYER_LOCAL) && !autocvar_chase_active)
85 return; // if we aren't using a third person camera, hide our own effects
87 // now generate the particles
88 vector org = gettaginfo(this, 0); // origin at attached location
89 __pointparticles(this.team, org, '0 0 0', 1);
92 string species_prefix(int specnum)
96 case SPECIES_HUMAN: return "";
97 case SPECIES_ALIEN: return "alien_";
98 case SPECIES_ROBOT_SHINY: return "robot_";
99 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
100 case SPECIES_ROBOT_SOLID: return "robot_"; // use the same effects, only different gibs
101 case SPECIES_ANIMAL: return "animal_";
102 case SPECIES_RESERVED: return "reserved_";
107 void DamageEffect(entity this, vector hitorg, float thedamage, int type, int specnum)
109 // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
116 if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
118 if(!this || !this.modelindex || !this.drawmask)
121 // if this is a rigged mesh, the effect will show on the bone where damage was dealt
122 // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
123 // if there's no skeleton, object origin will automatically be selected
127 continue; // skip empty bones
128 // blacklist bones positioned outside the mesh, or the effect will be floating
129 // TODO: Do we have to do it this way? Why do these bones exist at all?
130 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
131 continue; // player model bone blacklist
133 // now choose the bone closest to impact origin
134 if(nearestbone == 0 || vlen2(hitorg - gettaginfo(this, tagnum)) <= vlen2(hitorg - gettaginfo(this, nearestbone)))
135 nearestbone = tagnum;
137 gettaginfo(this, nearestbone); // set gettaginfo_name
139 // return if we reached our damage effect limit or damages are disabled
140 // TODO: When the limit is reached, it would be better if the oldest damage was removed instead of not adding a new one
143 if(this.total_damages >= autocvar_cl_damageeffect_bones)
144 return; // allow multiple damages on skeletal models
148 if(autocvar_cl_damageeffect < 2 || this.total_damages)
149 return; // allow a single damage on non-skeletal models
152 life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
154 wep = DEATH_WEAPONOF(type);
155 effectname = strcat("damage_", wep.netname);
156 if((wep.spawnflags & WEP_FLAG_BLEED))
158 // if this weapon induces bleeding, use the effect name with the proper species prefix (blood type)
159 if((this.isplayermodel & ISPLAYER_MODEL))
161 string specstr = species_prefix(specnum);
162 effectname = strcat(specstr, effectname);
164 else { return; } // objects don't bleed
168 setmodel(e, MDL_Null); // necessary to attach and read origin
169 setattachment(e, this, gettaginfo_name); // attach to the given bone
172 e.team = _particleeffectnum(effectname);
173 setthink(e, DamageEffect_Think);
175 this.total_damages += 1;
178 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
180 float thedamage, rad, edge, thisdmg;
181 bool hitplayer = false;
182 int species, forcemul;
183 vector force, thisforce;
185 w_deathtype = ReadShort();
186 w_issilent = (w_deathtype & 0x8000);
187 w_deathtype = (w_deathtype & 0x7FFF);
189 w_org = ReadVector();
191 thedamage = ReadByte();
194 force = ReadVector();
195 species = ReadByte();
210 FOREACH_ENTITY_RADIUS(w_org, rad + MAX_DAMAGEEXTRARADIUS, !it.tag_entity, {
211 vector nearest = NearestPointOnBox(it, w_org);
214 thisdmg = ((vlen(nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
221 thisdmg = thedamage + (edge - thedamage) * thisdmg;
222 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(it.origin - w_org);
227 thisforce = forcemul * vlen(force) * normalize(it.origin - w_org);
232 if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
236 thisforce = forcemul * force;
239 if(it.damageforcescale)
240 if(vdist(thisforce, !=, 0))
242 it.velocity = it.velocity + damage_explosion_calcpush(it.damageforcescale * thisforce, it.velocity, damagepush_speedfactor);
250 it.event_damage(it, thisdmg, w_deathtype, w_org, thisforce);
252 DamageEffect(it, w_org, thisdmg, w_deathtype, species);
254 if((it.isplayermodel & ISPLAYER_MODEL))
255 hitplayer = true; // this impact damaged a player
258 if(DEATH_ISVEHICLE(w_deathtype))
260 vector force_dir = normalize(force);
261 traceline(w_org - force_dir * 16, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
262 if(trace_plane_normal != '0 0 0')
263 w_backoff = trace_plane_normal;
265 w_backoff = -1 * normalize(w_org - (w_org + force_dir * 16));
267 setorigin(this, w_org + w_backoff * 2); // for sound() calls
269 switch(DEATH_ENT(w_deathtype))
275 case DEATH_VH_SPID_MINIGUN:
276 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
277 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, this.origin, w_backoff * 1000, 1);
279 case DEATH_VH_SPID_ROCKET:
280 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
281 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
283 case DEATH_VH_SPID_DEATH:
284 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
285 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
288 case DEATH_VH_WAKI_GUN:
289 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
290 pointparticles(EFFECT_RACER_IMPACT, this.origin, w_backoff * 1000, 1);
292 case DEATH_VH_WAKI_ROCKET:
293 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
294 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
296 case DEATH_VH_WAKI_DEATH:
297 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
298 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
301 case DEATH_VH_RAPT_CANNON:
302 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
303 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, this.origin, w_backoff * 1000, 1);
305 case DEATH_VH_RAPT_FRAGMENT:
308 for(i = 1; i < 4; ++i)
310 vel = normalize(w_org - (w_org + force_dir * 16)) + randomvec() * 128;
311 ang = vectoangles(vel);
312 RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
314 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
315 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, this.origin, w_backoff * 1000, 1);
317 case DEATH_VH_RAPT_BOMB:
318 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
319 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, this.origin, w_backoff * 1000, 1);
321 case DEATH_VH_RAPT_DEATH:
322 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
323 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
325 case DEATH_VH_BUMB_GUN:
326 sound(this, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
327 pointparticles(EFFECT_BIGPLASMA_IMPACT, this.origin, w_backoff * 1000, 1);
333 if(DEATH_ISTURRET(w_deathtype))
335 vector force_dir = normalize(force);
336 traceline(w_org - force_dir * 16, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
337 if(trace_plane_normal != '0 0 0')
338 w_backoff = trace_plane_normal;
340 w_backoff = -1 * normalize(w_org - (w_org + force_dir * 16));
342 setorigin(this, w_org + w_backoff * 2); // for sound() calls
344 switch(DEATH_ENT(w_deathtype))
346 case DEATH_TURRET_EWHEEL:
347 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
348 pointparticles(EFFECT_BLASTER_IMPACT, this.origin, w_backoff * 1000, 1);
351 case DEATH_TURRET_FLAC:
352 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
353 sound(this, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
356 case DEATH_TURRET_MLRS:
357 case DEATH_TURRET_HK:
358 case DEATH_TURRET_WALK_ROCKET:
359 case DEATH_TURRET_HELLION:
360 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
361 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
364 case DEATH_TURRET_MACHINEGUN:
365 case DEATH_TURRET_WALK_GUN:
366 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
367 pointparticles(EFFECT_MACHINEGUN_IMPACT, this.origin, w_backoff * 1000, 1);
370 case DEATH_TURRET_PLASMA:
371 sound(this, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_LOW);
372 pointparticles(EFFECT_ELECTRO_IMPACT, this.origin, w_backoff * 1000, 1);
375 case DEATH_TURRET_WALK_MELEE:
376 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_LOW);
377 pointparticles(EFFECT_TE_SPARK, this.origin, w_backoff * 1000, 1);
380 case DEATH_TURRET_PHASER:
383 case DEATH_TURRET_TESLA:
384 te_smallflash(this.origin);
389 MUTATOR_CALLHOOK(DamageInfo, this, w_deathtype, w_org);
391 // TODO spawn particle effects and sounds based on w_deathtype
392 if(!DEATH_ISSPECIAL(w_deathtype))
393 if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
395 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
396 w_random = prandom();
398 vector force_dir = normalize(force);
399 traceline(w_org - force_dir * 16, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
400 if(trace_fraction < 1 && !(hitwep.spawnflags & WEP_TYPE_HITSCAN))
401 w_backoff = trace_plane_normal;
403 w_backoff = -force_dir;
404 setorigin(this, w_org + w_backoff * 2); // for sound() calls
406 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
408 if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep, this))
409 hitwep.wr_impacteffect(hitwep, this);