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