]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/damageeffects.qc
Merge branch 'master' into Mario/hagar_notfixed
[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                 this.nextthink = time + autocvar_cl_damageeffect_ticrate * this.owner.total_damages;
77         else
78                 this.nextthink = time + autocvar_cl_damageeffect_ticrate;
79
80         if(time >= this.cnt || !this.owner || !this.owner.modelindex || !this.owner.drawmask)
81         {
82                 // time is up or the player got gibbed / disconnected
83                 this.owner.total_damages = max(0, this.owner.total_damages - 1);
84                 remove(this);
85                 return;
86         }
87         if(this.state && !this.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                 this.owner.total_damages = max(0, this.owner.total_damages - 1);
92                 remove(this);
93                 return;
94         }
95         this.state = this.owner.csqcmodel_isdead;
96         if(this.owner.isplayermodel && (this.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(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 specstr, effectname;
127         entity e;
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         effectname = DEATH_WEAPONOF(type).netname;
168
169         if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
170         {
171                 if(this.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, this, gettaginfo_name); // attach to the given bone
183         e.owner = this;
184         e.cnt = time + life;
185         e.team = _particleeffectnum(effectname);
186         e.think = DamageEffect_Think;
187         e.nextthink = time;
188         this.total_damages += 1;
189 }
190
191 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
192 {
193         const float ATTEN_LOW = 0.2;
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                 vector nearest = NearestPointOnBox(it, w_org);
228                 if (rad)
229                 {
230                         thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
231                         if(thisdmg >= 1)
232                                 continue;
233                         if(thisdmg < 0)
234                                 thisdmg = 0;
235                         if(thedamage)
236                         {
237                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
238                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(it.origin - w_org);
239                         }
240                         else
241                         {
242                                 thisdmg = 0;
243                                 thisforce = forcemul * vlen(force) * normalize(it.origin - w_org);
244                         }
245                 }
246                 else
247                 {
248                         if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
249                                 continue;
250
251                         thisdmg = thedamage;
252                         thisforce = forcemul * force;
253                 }
254
255                 if(it.damageforcescale)
256                         if(vdist(thisforce, !=, 0))
257                         {
258                                 it.move_velocity = it.move_velocity + damage_explosion_calcpush(it.damageforcescale * thisforce, it.move_velocity, autocvar_g_balance_damagepush_speedfactor);
259                                 it.move_flags &= ~FL_ONGROUND;
260                         }
261
262                 if(w_issilent)
263                         it.silent = 1;
264
265                 if(it.event_damage)
266                         it.event_damage(it, thisdmg, w_deathtype, w_org, thisforce);
267
268                 DamageEffect(it, w_org, thisdmg, w_deathtype, species);
269
270                 if(it.isplayermodel)
271                         hitplayer = true; // this impact damaged a player
272         });
273
274         if(DEATH_ISVEHICLE(w_deathtype))
275         {
276                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
277                 if(trace_plane_normal != '0 0 0')
278                         w_backoff = trace_plane_normal;
279                 else
280                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
281
282                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
283
284                 switch(DEATH_ENT(w_deathtype))
285                 {
286                         case DEATH_VH_CRUSH:
287                                 break;
288
289                         // spiderbot
290                         case DEATH_VH_SPID_MINIGUN:
291                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
292                                 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, this.origin, w_backoff * 1000, 1);
293                                 break;
294                         case DEATH_VH_SPID_ROCKET:
295                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
296                                 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
297                                 break;
298                         case DEATH_VH_SPID_DEATH:
299                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
300                                 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
301                                 break;
302
303                         case DEATH_VH_WAKI_GUN:
304                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
305                                 pointparticles(EFFECT_RACER_IMPACT, this.origin, w_backoff * 1000, 1);
306                                 break;
307                         case DEATH_VH_WAKI_ROCKET:
308                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
309                                 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
310                                 break;
311                         case DEATH_VH_WAKI_DEATH:
312                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
313                                 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
314                                 break;
315
316                         case DEATH_VH_RAPT_CANNON:
317                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
318                                 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, this.origin, w_backoff * 1000, 1);
319                                 break;
320                         case DEATH_VH_RAPT_FRAGMENT:
321                                 float i;
322                                 vector ang, vel;
323                                 for(i = 1; i < 4; ++i)
324                                 {
325                                         vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
326                                         ang = vectoangles(vel);
327                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
328                                 }
329                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
330                                 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, this.origin, w_backoff * 1000, 1);
331                                 break;
332                         case DEATH_VH_RAPT_BOMB:
333                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
334                                 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, this.origin, w_backoff * 1000, 1);
335                                 break;
336                         case DEATH_VH_RAPT_DEATH:
337                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
338                                 pointparticles(EFFECT_EXPLOSION_BIG, this.origin, w_backoff * 1000, 1);
339                                 break;
340                         case DEATH_VH_BUMB_GUN:
341                                 sound(this, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
342                                 pointparticles(EFFECT_BIGPLASMA_IMPACT, this.origin, w_backoff * 1000, 1);
343                                 break;
344                 }
345         }
346
347
348         if(DEATH_ISTURRET(w_deathtype))
349         {
350                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
351                 if(trace_plane_normal != '0 0 0')
352                         w_backoff = trace_plane_normal;
353                 else
354                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
355
356                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
357
358                 switch(DEATH_ENT(w_deathtype))
359                 {
360                          case DEATH_TURRET_EWHEEL:
361                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
362                                 pointparticles(EFFECT_BLASTER_IMPACT, this.origin, w_backoff * 1000, 1);
363                                 break;
364
365                          case DEATH_TURRET_FLAC:
366                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
367                                 sound(this, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
368                                 break;
369
370                          case DEATH_TURRET_MLRS:
371                          case DEATH_TURRET_HK:
372                          case DEATH_TURRET_WALK_ROCKET:
373                          case DEATH_TURRET_HELLION:
374                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
375                                 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
376                                 break;
377
378                          case DEATH_TURRET_MACHINEGUN:
379                          case DEATH_TURRET_WALK_GUN:
380                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
381                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, this.origin, w_backoff * 1000, 1);
382                                 break;
383
384                          case DEATH_TURRET_PLASMA:
385                                 sound(this, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_LOW);
386                                 pointparticles(EFFECT_ELECTRO_IMPACT, this.origin, w_backoff * 1000, 1);
387                                 break;
388
389                          case DEATH_TURRET_WALK_MELEE:
390                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_LOW);
391                                 pointparticles(EFFECT_TE_SPARK, this.origin, w_backoff * 1000, 1);
392                                 break;
393
394                          case DEATH_TURRET_PHASER:
395                                 break;
396
397                          case DEATH_TURRET_TESLA:
398                                 te_smallflash(this.origin);
399                                 break;
400
401                 }
402         }
403
404         // TODO spawn particle effects and sounds based on w_deathtype
405         if(!DEATH_ISSPECIAL(w_deathtype))
406         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
407         {
408                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
409                 w_random = prandom();
410
411                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
412                 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
413                         w_backoff = trace_plane_normal;
414                 else
415                         w_backoff = -1 * normalize(force);
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))
421                                 hitwep.wr_impacteffect(hitwep);
422                 }
423         }
424 }
425
426 #endif
427
428 #endif