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