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