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