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