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