]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/damageeffects.qc
Transifex autosync
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / damageeffects.qc
1 #include "damageeffects.qh"
2
3 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
4
5 #ifdef SVQC
6
7 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
8 {
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         // we can't send the force vector compressed with compressShortVector as it's too inaccurate
17         // it would break decals when hit angle on a surface is small
18         // (the traceline performed by the client to spawn a decal wouldn't hit the surface at all)
19         WriteShort(MSG_ENTITY, floor(this.velocity.x / 4));
20         WriteShort(MSG_ENTITY, floor(this.velocity.y / 4));
21         WriteShort(MSG_ENTITY, floor(this.velocity.z / 4));
22         WriteByte(MSG_ENTITY, this.species);
23         return true;
24 }
25
26 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
27 {
28         // TODO maybe call this from non-edgedamage too?
29         // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
30
31         if(!sound_allowed(MSG_BROADCAST, dmgowner))
32                 deathtype |= 0x8000;
33
34         entity e = new_pure(damageinfo);
35         // origin is just data to be sent
36         //setorigin(e, org);
37         e.origin = org;
38         e.projectiledeathtype = deathtype;
39         e.dmg = coredamage;
40         e.dmg_edge = edgedamage;
41         bool rad_negative = false;
42         if(rad < 0)
43         {
44                 // make it positive (unsigned) so it can be sent as byte
45                 rad_negative = true;
46                 rad = -rad;
47         }
48         e.dmg_radius = rad;
49         e.dmg_force = vlen(force);
50         e.velocity = force;
51
52         e.species = bloodtype & BITS(4); // it only uses bits from 0 to 3, see SPECIES_* constants
53         if(rad_negative)
54                 e.species |= BIT(7);
55
56         Net_LinkEntity(e, false, 0.2, Damage_DamageInfo_SendEntity);
57 }
58
59 #endif
60
61 #ifdef CSQC
62
63 /** number of effects which currently are attached to a player */
64 .int total_damages;
65
66 .entity tag_entity;
67
68 .float cnt;
69 .int state;
70
71 .bool silent;
72
73 void DamageEffect_Think(entity this)
74 {
75         // if particle distribution is enabled, slow ticrate by total number of damages
76         if(autocvar_cl_damageeffect_distribute)
77                 this.nextthink = time + autocvar_cl_damageeffect_ticrate * this.owner.total_damages;
78         else
79                 this.nextthink = time + autocvar_cl_damageeffect_ticrate;
80
81         if(time >= this.cnt || !this.owner || !this.owner.modelindex || !this.owner.drawmask)
82         {
83                 // time is up or the player got gibbed / disconnected
84                 this.owner.total_damages = max(0, this.owner.total_damages - 1);
85                 delete(this);
86                 return;
87         }
88         if(this.state && !this.owner.csqcmodel_isdead)
89         {
90                 // if the player was dead but is now alive, it means they respawned
91                 // if so, clear their damage effects, or damages from their dead body will be copied back
92                 this.owner.total_damages = max(0, this.owner.total_damages - 1);
93                 delete(this);
94                 return;
95         }
96         this.state = this.owner.csqcmodel_isdead;
97         if((this.owner.isplayermodel & ISPLAYER_LOCAL) && !autocvar_chase_active)
98                 return; // if we aren't using a third person camera, hide our own effects
99
100         // now generate the particles
101         vector org = gettaginfo(this, 0); // origin at attached location
102         __pointparticles(this.team, org, '0 0 0', 1);
103 }
104
105 string species_prefix(int specnum)
106 {
107         switch(specnum)
108         {
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_";
116                 default:         return "";
117         }
118 }
119
120 void DamageEffect(entity this, vector hitorg, float thedamage, int type, int specnum)
121 {
122         // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
123
124         int nearestbone = 0;
125         float life;
126         string effectname;
127         entity e, wep;
128
129         if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
130                 return;
131         if(!this || !this.modelindex || !this.drawmask)
132                 return;
133
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
137         FOR_EACH_TAG(this)
138         {
139                 if(!tagnum)
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
145
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;
149         }
150         gettaginfo(this, nearestbone); // set gettaginfo_name
151
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
154         if(nearestbone)
155         {
156                 if(this.total_damages >= autocvar_cl_damageeffect_bones)
157                         return; // allow multiple damages on skeletal models
158         }
159         else
160         {
161                 if(autocvar_cl_damageeffect < 2 || this.total_damages)
162                         return; // allow a single damage on non-skeletal models
163         }
164
165         life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
166
167         wep = DEATH_WEAPONOF(type);
168         effectname = strcat("damage_", wep.netname);
169         if((wep.spawnflags & WEP_FLAG_BLEED))
170         {
171                 // if this weapon induces bleeding, use the effect name with the proper species prefix (blood type)
172                 if((this.isplayermodel & ISPLAYER_MODEL))
173                 {
174                         string specstr = species_prefix(specnum);
175                         effectname = strcat(specstr, effectname);
176                 }
177                 else { return; } // objects don't bleed
178         }
179
180         e = new(damage);
181         setmodel(e, MDL_Null); // necessary to attach and read origin
182         setattachment(e, this, gettaginfo_name); // attach to the given bone
183         e.owner = this;
184         e.cnt = time + life;
185         e.team = _particleeffectnum(effectname);
186         setthink(e, DamageEffect_Think);
187         e.nextthink = time;
188         this.total_damages += 1;
189 }
190
191 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
192 {
193         float thedamage, rad, edge, thisdmg;
194         bool hitplayer = false;
195         int species, forcemul;
196         vector force, thisforce;
197
198         w_deathtype = ReadShort();
199         w_issilent = (w_deathtype & 0x8000);
200         w_deathtype = (w_deathtype & 0x7FFF);
201
202         w_org = ReadVector();
203
204         thedamage = ReadByte();
205         rad = ReadByte();
206         edge = ReadByte();
207         force.x = ReadShort() * 4 + 2;
208         force.y = ReadShort() * 4 + 2;
209         force.z = ReadShort() * 4 + 2;
210
211         species = ReadByte();
212         bool rad_negative = (species & BIT(7));
213         species = (species & BITS(4));
214
215         return = true;
216
217         if (!isNew)
218                 return;
219
220         forcemul = (rad_negative ? -1 : 1);
221
222         FOREACH_ENTITY_RADIUS(w_org, rad + MAX_DAMAGEEXTRARADIUS, !it.tag_entity && !is_pure(it), {
223                 vector nearest = NearestPointOnBox(it, w_org);
224                 if (rad)
225                 {
226                         thisdmg = ((vlen(nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
227                         if(thisdmg >= 1)
228                                 continue;
229                         if(thisdmg < 0)
230                                 thisdmg = 0;
231                         if(thedamage)
232                         {
233                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
234                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(it.origin - w_org);
235                         }
236                         else
237                         {
238                                 thisdmg = 0;
239                                 thisforce = forcemul * vlen(force) * normalize(it.origin - w_org);
240                         }
241                 }
242                 else
243                 {
244                         if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
245                                 continue;
246
247                         thisdmg = thedamage;
248                         thisforce = forcemul * force;
249                 }
250
251                 if(it.damageforcescale)
252                         if(vdist(thisforce, !=, 0))
253                         {
254                                 it.velocity = it.velocity + damage_explosion_calcpush(it.damageforcescale * thisforce, it.velocity, damagepush_speedfactor);
255                                 UNSET_ONGROUND(it);
256                         }
257
258                 if(w_issilent)
259                         it.silent = 1;
260
261                 if(it.event_damage)
262                         it.event_damage(it, thisdmg, w_deathtype, w_org, thisforce);
263
264                 DamageEffect(it, w_org, thisdmg, w_deathtype, species);
265
266                 if(it != csqcplayer && (it.isplayermodel & ISPLAYER_MODEL))
267                         hitplayer = true; // this impact damaged another player
268         });
269
270         if(DEATH_ISVEHICLE(w_deathtype))
271         {
272                 vector force_dir = normalize(force);
273                 traceline(w_org - force_dir * 16, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
274                 if(trace_plane_normal != '0 0 0')
275                         w_backoff = trace_plane_normal;
276                 else
277                         w_backoff = -1 * normalize(w_org - (w_org + force_dir * 16));
278
279                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
280
281                 switch(DEATH_ENT(w_deathtype))
282                 {
283                         case DEATH_VH_CRUSH:
284                                 break;
285
286                         // spiderbot
287                         case DEATH_VH_SPID_MINIGUN:
288                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
289                                 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, this.origin, w_backoff * 1000, 1);
290                                 break;
291                         case DEATH_VH_SPID_ROCKET:
292                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
293                                 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
294                                 break;
295                         case DEATH_VH_SPID_DEATH:
296                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
297                                 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
298                                 break;
299
300                         case DEATH_VH_WAKI_GUN:
301                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
302                                 pointparticles(EFFECT_RACER_IMPACT, this.origin, w_backoff * 1000, 1);
303                                 break;
304                         case DEATH_VH_WAKI_ROCKET:
305                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
306                                 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
307                                 break;
308                         case DEATH_VH_WAKI_DEATH:
309                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
310                                 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
311                                 break;
312
313                         case DEATH_VH_RAPT_CANNON:
314                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
315                                 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, this.origin, w_backoff * 1000, 1);
316                                 break;
317                         case DEATH_VH_RAPT_FRAGMENT:
318                                 float i;
319                                 vector ang, vel;
320                                 for(i = 1; i < 4; ++i)
321                                 {
322                                         vel = normalize(w_org - (w_org + force_dir * 16)) + randomvec() * 128;
323                                         ang = vectoangles(vel);
324                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
325                                 }
326                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
327                                 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, this.origin, w_backoff * 1000, 1);
328                                 break;
329                         case DEATH_VH_RAPT_BOMB:
330                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
331                                 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, this.origin, w_backoff * 1000, 1);
332                                 break;
333                         case DEATH_VH_RAPT_DEATH:
334                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
335                                 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
336                                 break;
337                         case DEATH_VH_BUMB_GUN:
338                                 sound(this, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
339                                 pointparticles(EFFECT_BIGPLASMA_IMPACT, this.origin, w_backoff * 1000, 1);
340                                 break;
341                 }
342         }
343
344
345         if(DEATH_ISTURRET(w_deathtype))
346         {
347                 vector force_dir = normalize(force);
348                 traceline(w_org - force_dir * 16, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
349                 if(trace_plane_normal != '0 0 0')
350                         w_backoff = trace_plane_normal;
351                 else
352                         w_backoff = -1 * normalize(w_org - (w_org + force_dir * 16));
353
354                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
355
356                 switch(DEATH_ENT(w_deathtype))
357                 {
358                         case DEATH_TURRET_EWHEEL:
359                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
360                                 pointparticles(EFFECT_BLASTER_IMPACT, this.origin, w_backoff * 1000, 1);
361                                 break;
362
363                         case DEATH_TURRET_FLAC:
364                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
365                                 sound(this, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
366                                 break;
367
368                         case DEATH_TURRET_MLRS:
369                         case DEATH_TURRET_HK:
370                         case DEATH_TURRET_WALK_ROCKET:
371                         case DEATH_TURRET_HELLION:
372                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
373                                 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
374                                 break;
375
376                         case DEATH_TURRET_MACHINEGUN:
377                         case DEATH_TURRET_WALK_GUN:
378                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
379                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, this.origin, w_backoff * 1000, 1);
380                                 break;
381
382                         case DEATH_TURRET_PLASMA:
383                                 sound(this, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_LOW);
384                                 pointparticles(EFFECT_ELECTRO_IMPACT, this.origin, w_backoff * 1000, 1);
385                                 break;
386
387                         case DEATH_TURRET_WALK_MELEE:
388                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_LOW);
389                                 pointparticles(EFFECT_TE_SPARK, this.origin, w_backoff * 1000, 1);
390                                 break;
391
392                         case DEATH_TURRET_PHASER:
393                                 break;
394
395                         case DEATH_TURRET_TESLA:
396                                 te_smallflash(this.origin);
397                                 break;
398                 }
399         }
400
401         MUTATOR_CALLHOOK(DamageInfo, this, w_deathtype, w_org);
402
403         // TODO spawn particle effects and sounds based on w_deathtype
404         if(!DEATH_ISSPECIAL(w_deathtype))
405         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
406         {
407                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
408                 w_random = prandom();
409
410                 vector force_dir = normalize(force);
411                 // this traceline usually starts in solid when a hitscan shot hits a surface with a very small angle
412                 // if so, try another traceline starting further back (may still start in solid but only with extremely small angles)
413                 traceline(w_org - force_dir * 16, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
414                 if(trace_startsolid)
415                         traceline(w_org - force_dir * 40, w_org + force_dir * 16, MOVE_NOMONSTERS, NULL);
416                 if(trace_fraction < 1)
417                         w_backoff = trace_plane_normal;
418                 else
419                         w_backoff = -force_dir;
420                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
421
422                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
423                 {
424                         if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep, this))
425                                 hitwep.wr_impacteffect(hitwep, this);
426                 }
427         }
428 }
429
430 #endif