]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/damageeffects.qc
06f8ea464eaaa8cf6cf878ce14985e63efa9a283
[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         for(entity e = findradius(w_org, rad + MAX_DAMAGEEXTRARADIUS); e; e = e.chain)
227         {
228                 setself(e);
229                 // attached ents suck
230                 if(self.tag_entity)
231                         continue;
232
233                 vector nearest = NearestPointOnBox(self, w_org);
234                 if(rad)
235                 {
236                         thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
237                         if(thisdmg >= 1)
238                                 continue;
239                         if(thisdmg < 0)
240                                 thisdmg = 0;
241                         if(thedamage)
242                         {
243                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
244                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
245                         }
246                         else
247                         {
248                                 thisdmg = 0;
249                                 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
250                         }
251                 }
252                 else
253                 {
254                         if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
255                                 continue;
256
257                         thisdmg = thedamage;
258                         thisforce = forcemul * force;
259                 }
260
261                 if(self.damageforcescale)
262                         if(vdist(thisforce, !=, 0))
263                         {
264                                 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
265                                 self.move_flags &= ~FL_ONGROUND;
266                         }
267
268                 if(w_issilent)
269                         self.silent = 1;
270
271                 if(self.event_damage)
272                         self.event_damage(self, thisdmg, w_deathtype, w_org, thisforce);
273
274                 DamageEffect(w_org, thisdmg, w_deathtype, species);
275
276                 if(self.isplayermodel)
277                         hitplayer = true; // this impact damaged a player
278         }
279         setself(this);
280
281         if(DEATH_ISVEHICLE(w_deathtype))
282         {
283                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
284                 if(trace_plane_normal != '0 0 0')
285                         w_backoff = trace_plane_normal;
286                 else
287                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
288
289                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
290
291                 switch(DEATH_ENT(w_deathtype))
292                 {
293                         case DEATH_VH_CRUSH:
294                                 break;
295
296                         // spiderbot
297                         case DEATH_VH_SPID_MINIGUN:
298                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
299                                 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
300                                 break;
301                         case DEATH_VH_SPID_ROCKET:
302                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
303                                 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
304                                 break;
305                         case DEATH_VH_SPID_DEATH:
306                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
307                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
308                                 break;
309
310                         case DEATH_VH_WAKI_GUN:
311                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
312                                 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
313                                 break;
314                         case DEATH_VH_WAKI_ROCKET:
315                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
316                                 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
317                                 break;
318                         case DEATH_VH_WAKI_DEATH:
319                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
320                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
321                                 break;
322
323                         case DEATH_VH_RAPT_CANNON:
324                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
325                                 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
326                                 break;
327                         case DEATH_VH_RAPT_FRAGMENT:
328                                 float i;
329                                 vector ang, vel;
330                                 for(i = 1; i < 4; ++i)
331                                 {
332                                         vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
333                                         ang = vectoangles(vel);
334                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
335                                 }
336                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
337                                 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
338                                 break;
339                         case DEATH_VH_RAPT_BOMB:
340                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
341                                 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
342                                 break;
343                         case DEATH_VH_RAPT_DEATH:
344                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
345                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
346                                 break;
347                         case DEATH_VH_BUMB_GUN:
348                                 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
349                                 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
350                                 break;
351                 }
352         }
353
354
355         if(DEATH_ISTURRET(w_deathtype))
356         {
357                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
358                 if(trace_plane_normal != '0 0 0')
359                         w_backoff = trace_plane_normal;
360                 else
361                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
362
363                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
364
365                 switch(DEATH_ENT(w_deathtype))
366                 {
367                          case DEATH_TURRET_EWHEEL:
368                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
369                                 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
370                                 break;
371
372                          case DEATH_TURRET_FLAC:
373                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
374                                 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
375                                 break;
376
377                          case DEATH_TURRET_MLRS:
378                          case DEATH_TURRET_HK:
379                          case DEATH_TURRET_WALK_ROCKET:
380                          case DEATH_TURRET_HELLION:
381                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
382                                 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
383                                 break;
384
385                          case DEATH_TURRET_MACHINEGUN:
386                          case DEATH_TURRET_WALK_GUN:
387                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
388                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
389                                 break;
390
391                          case DEATH_TURRET_PLASMA:
392                                 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
393                                 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
394                                 break;
395
396                          case DEATH_TURRET_WALK_MELEE:
397                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
398                                 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
399                                 break;
400
401                          case DEATH_TURRET_PHASER:
402                                 break;
403
404                          case DEATH_TURRET_TESLA:
405                                 te_smallflash(self.origin);
406                                 break;
407
408                 }
409         }
410
411         // TODO spawn particle effects and sounds based on w_deathtype
412         if(!DEATH_ISSPECIAL(w_deathtype))
413         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
414         {
415                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
416                 w_random = prandom();
417
418                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
419                 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
420                         w_backoff = trace_plane_normal;
421                 else
422                         w_backoff = -1 * normalize(force);
423                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
424
425                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
426                 {
427                         if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep))
428                                 hitwep.wr_impacteffect(hitwep);
429                 }
430         }
431 }
432
433 #endif
434
435 #endif