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