]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/damageeffects.qc
Merge branch 'master' into terencehill/hud_updates
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / damageeffects.qc
1 #include "damageeffects.qh"
2
3 #ifdef IMPLEMENTATION
4
5 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
6
7 #ifdef SVQC
8
9 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
10 {
11         WriteHeader(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
12         WriteShort(MSG_ENTITY, this.projectiledeathtype);
13         WriteCoord(MSG_ENTITY, floor(this.origin.x));
14         WriteCoord(MSG_ENTITY, floor(this.origin.y));
15         WriteCoord(MSG_ENTITY, floor(this.origin.z));
16         WriteByte(MSG_ENTITY, bound(1, this.dmg, 255));
17         WriteByte(MSG_ENTITY, bound(0, this.dmg_radius, 255));
18         WriteByte(MSG_ENTITY, bound(1, this.dmg_edge, 255));
19         WriteShort(MSG_ENTITY, this.oldorigin.x);
20         WriteByte(MSG_ENTITY, this.species);
21         return true;
22 }
23
24 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
25 {
26         // TODO maybe call this from non-edgedamage too?
27         // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
28
29         entity e;
30
31         if(!sound_allowed(MSG_BROADCAST, dmgowner))
32                 deathtype |= 0x8000;
33
34         e = new(damageinfo);
35         setorigin(e, org);
36         e.projectiledeathtype = deathtype;
37         e.dmg = coredamage;
38         e.dmg_edge = edgedamage;
39         e.dmg_radius = rad;
40         e.dmg_force = vlen(force);
41         e.velocity = force;
42         e.oldorigin_x = compressShortVector(e.velocity);
43         e.species = bloodtype;
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 .bool isplayermodel;
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 && (this.owner.entnum == player_localentnum) && !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;
90         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 specstr, effectname;
116         entity e;
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         effectname = DEATH_WEAPONOF(type).netname;
157
158         if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
159         {
160                 if(this.isplayermodel)
161                 {
162                         specstr = species_prefix(specnum);
163                         specstr = substring(specstr, 0, strlen(specstr) - 1);
164                         effectname = strreplace("BLOOD", 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         const float ATTEN_LOW = 0.2;
183         float thedamage, rad, edge, thisdmg;
184         bool hitplayer = false;
185         int species, forcemul;
186         vector force, thisforce;
187
188         w_deathtype = ReadShort();
189         w_issilent = (w_deathtype & 0x8000);
190         w_deathtype = (w_deathtype & 0x7FFF);
191
192         w_org.x = ReadCoord();
193         w_org.y = ReadCoord();
194         w_org.z = ReadCoord();
195
196         thedamage = ReadByte();
197         rad = ReadByte();
198         edge = ReadByte();
199         force = decompressShortVector(ReadShort());
200         species = ReadByte();
201
202         return = true;
203
204         if (!isNew)
205                 return;
206
207         if(rad < 0)
208         {
209                 rad = -rad;
210                 forcemul = -1;
211         }
212         else
213                 forcemul = 1;
214
215     FOREACH_ENTITY_RADIUS(w_org, rad + MAX_DAMAGEEXTRARADIUS, !it.tag_entity, {
216                 vector nearest = NearestPointOnBox(it, w_org);
217                 if (rad)
218                 {
219                         thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
220                         if(thisdmg >= 1)
221                                 continue;
222                         if(thisdmg < 0)
223                                 thisdmg = 0;
224                         if(thedamage)
225                         {
226                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
227                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(it.origin - w_org);
228                         }
229                         else
230                         {
231                                 thisdmg = 0;
232                                 thisforce = forcemul * vlen(force) * normalize(it.origin - w_org);
233                         }
234                 }
235                 else
236                 {
237                         if(vdist((nearest - w_org), >, bound(MIN_DAMAGEEXTRARADIUS, it.damageextraradius, MAX_DAMAGEEXTRARADIUS)))
238                                 continue;
239
240                         thisdmg = thedamage;
241                         thisforce = forcemul * force;
242                 }
243
244                 if(it.damageforcescale)
245                         if(vdist(thisforce, !=, 0))
246                         {
247                                 it.velocity = it.velocity + damage_explosion_calcpush(it.damageforcescale * thisforce, it.velocity, autocvar_g_balance_damagepush_speedfactor);
248                                 UNSET_ONGROUND(it);
249                         }
250
251                 if(w_issilent)
252                         it.silent = 1;
253
254                 if(it.event_damage)
255                         it.event_damage(it, thisdmg, w_deathtype, w_org, thisforce);
256
257                 DamageEffect(it, w_org, thisdmg, w_deathtype, species);
258
259                 if(it.isplayermodel)
260                         hitplayer = true; // this impact damaged a player
261         });
262
263         if(DEATH_ISVEHICLE(w_deathtype))
264         {
265                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 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 + normalize(force) * 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 + normalize(force) * 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                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, NULL);
340                 if(trace_plane_normal != '0 0 0')
341                         w_backoff = trace_plane_normal;
342                 else
343                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
344
345                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
346
347                 switch(DEATH_ENT(w_deathtype))
348                 {
349                          case DEATH_TURRET_EWHEEL:
350                                 sound(this, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_LOW);
351                                 pointparticles(EFFECT_BLASTER_IMPACT, this.origin, w_backoff * 1000, 1);
352                                 break;
353
354                          case DEATH_TURRET_FLAC:
355                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
356                                 sound(this, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
357                                 break;
358
359                          case DEATH_TURRET_MLRS:
360                          case DEATH_TURRET_HK:
361                          case DEATH_TURRET_WALK_ROCKET:
362                          case DEATH_TURRET_HELLION:
363                                 sound(this, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_LOW);
364                                 pointparticles(EFFECT_ROCKET_EXPLODE, this.origin, w_backoff * 1000, 1);
365                                 break;
366
367                          case DEATH_TURRET_MACHINEGUN:
368                          case DEATH_TURRET_WALK_GUN:
369                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
370                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, this.origin, w_backoff * 1000, 1);
371                                 break;
372
373                          case DEATH_TURRET_PLASMA:
374                                 sound(this, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_LOW);
375                                 pointparticles(EFFECT_ELECTRO_IMPACT, this.origin, w_backoff * 1000, 1);
376                                 break;
377
378                          case DEATH_TURRET_WALK_MELEE:
379                                 sound(this, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_LOW);
380                                 pointparticles(EFFECT_TE_SPARK, this.origin, w_backoff * 1000, 1);
381                                 break;
382
383                          case DEATH_TURRET_PHASER:
384                                 break;
385
386                          case DEATH_TURRET_TESLA:
387                                 te_smallflash(this.origin);
388                                 break;
389
390                 }
391         }
392
393         // TODO spawn particle effects and sounds based on w_deathtype
394         if(!DEATH_ISSPECIAL(w_deathtype))
395         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
396         {
397                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
398                 w_random = prandom();
399
400                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, NULL);
401                 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
402                         w_backoff = trace_plane_normal;
403                 else
404                         w_backoff = -1 * normalize(force);
405                 setorigin(this, w_org + w_backoff * 2); // for sound() calls
406
407                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
408                 {
409                         if(!MUTATOR_CALLHOOK(Weapon_ImpactEffect, hitwep, this))
410                                 hitwep.wr_impacteffect(hitwep, this);
411                 }
412         }
413 }
414
415 #endif
416
417 #endif