]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/damageeffects.qc
ba60e51bd10cf3c483a24a3c818f52d2ab3ed476
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / damageeffects.qc
1 #ifndef DAMAGEEFFECTS_H
2 #define DAMAGEEFFECTS_H
3
4 #ifdef CSQC
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>
10 #endif
11
12 #endif
13
14 #ifdef IMPLEMENTATION
15
16 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
17
18 #ifdef SVQC
19
20 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
21 {
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);
32         return true;
33 }
34
35 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
36 {
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?
39
40         entity e;
41
42         if(!sound_allowed(MSG_BROADCAST, dmgowner))
43                 deathtype |= 0x8000;
44
45         e = new(damageinfo);
46         setorigin(e, org);
47         e.projectiledeathtype = deathtype;
48         e.dmg = coredamage;
49         e.dmg_edge = edgedamage;
50         e.dmg_radius = rad;
51         e.dmg_force = vlen(force);
52         e.velocity = force;
53         e.oldorigin_x = compressShortVector(e.velocity);
54         e.species = bloodtype;
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 .bool isplayermodel;
71
72 void DamageEffect_Think()
73 {SELFPARAM();
74         // if particle distribution is enabled, slow ticrate by total number of damages
75         if(autocvar_cl_damageeffect_distribute)
76                 self.nextthink = time + autocvar_cl_damageeffect_ticrate * self.owner.total_damages;
77         else
78                 self.nextthink = time + autocvar_cl_damageeffect_ticrate;
79
80         if(time >= self.cnt || !self.owner || !self.owner.modelindex || !self.owner.drawmask)
81         {
82                 // time is up or the player got gibbed / disconnected
83                 self.owner.total_damages = max(0, self.owner.total_damages - 1);
84                 remove(self);
85                 return;
86         }
87         if(self.state && !self.owner.csqcmodel_isdead)
88         {
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                 self.owner.total_damages = max(0, self.owner.total_damages - 1);
92                 remove(self);
93                 return;
94         }
95         self.state = self.owner.csqcmodel_isdead;
96         if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum) && !autocvar_chase_active)
97                 return; // if we aren't using a third person camera, hide our own effects
98
99         // now generate the particles
100         vector org;
101         org = gettaginfo(self, 0); // origin at attached location
102         __pointparticles(self.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(vector hitorg, float thedamage, int type, int specnum)
121 {SELFPARAM();
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 specstr, effectname;
127         entity e;
128
129         if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
130                 return;
131         if(!self || !self.modelindex || !self.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(self)
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(self, tagnum)) <= vlen2(hitorg - gettaginfo(self, nearestbone)))
148                         nearestbone = tagnum;
149         }
150         gettaginfo(self, 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(self.total_damages >= autocvar_cl_damageeffect_bones)
157                         return; // allow multiple damages on skeletal models
158         }
159         else
160         {
161                 if(autocvar_cl_damageeffect < 2 || self.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         effectname = DEATH_WEAPONOF(type).netname;
168
169         if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
170         {
171                 if(self.isplayermodel)
172                 {
173                         specstr = species_prefix(specnum);
174                         specstr = substring(specstr, 0, strlen(specstr) - 1);
175                         effectname = strreplace("BLOOD", 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, self, gettaginfo_name); // attach to the given bone
183         e.owner = self;
184         e.cnt = time + life;
185         e.team = _particleeffectnum(effectname);
186         e.think = DamageEffect_Think;
187         e.nextthink = time;
188         self.total_damages += 1;
189 }
190
191 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
192 {
193         make_pure(this);
194         float thedamage, rad, edge, thisdmg;
195         bool hitplayer = false;
196         int species, forcemul;
197         vector force, thisforce;
198
199         w_deathtype = ReadShort();
200         w_issilent = (w_deathtype & 0x8000);
201         w_deathtype = (w_deathtype & 0x7FFF);
202
203         w_org.x = ReadCoord();
204         w_org.y = ReadCoord();
205         w_org.z = ReadCoord();
206
207         thedamage = ReadByte();
208         rad = ReadByte();
209         edge = ReadByte();
210         force = decompressShortVector(ReadShort());
211         species = ReadByte();
212
213         return = true;
214
215         if (!isNew)
216                 return;
217
218         if(rad < 0)
219         {
220                 rad = -rad;
221                 forcemul = -1;
222         }
223         else
224                 forcemul = 1;
225
226     FOREACH_ENTITY_RADIUS(w_org, rad + MAX_DAMAGEEXTRARADIUS, !it.tag_entity, {
227                 setself(it);
228                 vector nearest = NearestPointOnBox(self, w_org);
229                 if (rad)
230                 {
231                         thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
232                         if(thisdmg >= 1)
233                                 continue;
234                         if(thisdmg < 0)
235                                 thisdmg = 0;
236                         if(thedamage)
237                         {
238                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
239                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
240                         }
241                         else
242                         {
243                                 thisdmg = 0;
244                                 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
245                         }
246                 }
247                 else
248                 {
249                         if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
250                                 continue;
251
252                         thisdmg = thedamage;
253                         thisforce = forcemul * force;
254                 }
255
256                 if(self.damageforcescale)
257                         if(vdist(thisforce, !=, 0))
258                         {
259                                 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
260                                 self.move_flags &= ~FL_ONGROUND;
261                         }
262
263                 if(w_issilent)
264                         self.silent = 1;
265
266                 if(self.event_damage)
267                         self.event_damage(self, thisdmg, w_deathtype, w_org, thisforce);
268
269                 DamageEffect(w_org, thisdmg, w_deathtype, species);
270
271                 if(self.isplayermodel)
272                         hitplayer = true; // this impact damaged a player
273         });
274         setself(this);
275
276         if(DEATH_ISVEHICLE(w_deathtype))
277         {
278                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
279                 if(trace_plane_normal != '0 0 0')
280                         w_backoff = trace_plane_normal;
281                 else
282                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
283
284                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
285
286                 switch(DEATH_ENT(w_deathtype))
287                 {
288                         case DEATH_VH_CRUSH:
289                                 break;
290
291                         // spiderbot
292                         case DEATH_VH_SPID_MINIGUN:
293                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
294                                 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
295                                 break;
296                         case DEATH_VH_SPID_ROCKET:
297                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
298                                 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
299                                 break;
300                         case DEATH_VH_SPID_DEATH:
301                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
302                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
303                                 break;
304
305                         case DEATH_VH_WAKI_GUN:
306                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
307                                 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
308                                 break;
309                         case DEATH_VH_WAKI_ROCKET:
310                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
311                                 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
312                                 break;
313                         case DEATH_VH_WAKI_DEATH:
314                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
315                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
316                                 break;
317
318                         case DEATH_VH_RAPT_CANNON:
319                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
320                                 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
321                                 break;
322                         case DEATH_VH_RAPT_FRAGMENT:
323                                 float i;
324                                 vector ang, vel;
325                                 for(i = 1; i < 4; ++i)
326                                 {
327                                         vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
328                                         ang = vectoangles(vel);
329                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
330                                 }
331                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
332                                 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
333                                 break;
334                         case DEATH_VH_RAPT_BOMB:
335                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
336                                 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
337                                 break;
338                         case DEATH_VH_RAPT_DEATH:
339                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
340                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
341                                 break;
342                         case DEATH_VH_BUMB_GUN:
343                                 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
344                                 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
345                                 break;
346                 }
347         }
348
349
350         if(DEATH_ISTURRET(w_deathtype))
351         {
352                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
353                 if(trace_plane_normal != '0 0 0')
354                         w_backoff = trace_plane_normal;
355                 else
356                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
357
358                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
359
360                 switch(DEATH_ENT(w_deathtype))
361                 {
362                          case DEATH_TURRET_EWHEEL:
363                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
364                                 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
365                                 break;
366
367                          case DEATH_TURRET_FLAC:
368                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
369                                 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
370                                 break;
371
372                          case DEATH_TURRET_MLRS:
373                          case DEATH_TURRET_HK:
374                          case DEATH_TURRET_WALK_ROCKET:
375                          case DEATH_TURRET_HELLION:
376                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
377                                 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
378                                 break;
379
380                          case DEATH_TURRET_MACHINEGUN:
381                          case DEATH_TURRET_WALK_GUN:
382                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
383                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
384                                 break;
385
386                          case DEATH_TURRET_PLASMA:
387                                 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
388                                 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
389                                 break;
390
391                          case DEATH_TURRET_WALK_MELEE:
392                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
393                                 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
394                                 break;
395
396                          case DEATH_TURRET_PHASER:
397                                 break;
398
399                          case DEATH_TURRET_TESLA:
400                                 te_smallflash(self.origin);
401                                 break;
402
403                 }
404         }
405
406         // TODO spawn particle effects and sounds based on w_deathtype
407         if(!DEATH_ISSPECIAL(w_deathtype))
408         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
409         {
410                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
411                 w_random = prandom();
412
413                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
414                 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
415                         w_backoff = trace_plane_normal;
416                 else
417                         w_backoff = -1 * normalize(force);
418                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
419
420                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
421                 {
422                         if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep))
423                                 hitwep.wr_impacteffect(hitwep);
424                 }
425         }
426 }
427
428 #endif
429
430 #endif