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