1151f2336887a81ef8e0ba5d4e1c935f7522d4df
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / damage.qc
1 #include "damage.qh"
2
3 #include "gibs.qh"
4 #include "../common/deathtypes/all.qh"
5 #include "../common/movetypes/movetypes.qh"
6 #include "../common/vehicles/all.qh"
7 #include "../common/weapons/all.qh"
8
9 .entity tag_entity;
10
11 .float cnt;
12 .int state;
13 .bool isplayermodel;
14
15 void DamageEffect_Think()
16 {SELFPARAM();
17         // if particle distribution is enabled, slow ticrate by total number of damages
18         if(autocvar_cl_damageeffect_distribute)
19                 self.nextthink = time + autocvar_cl_damageeffect_ticrate * self.owner.total_damages;
20         else
21                 self.nextthink = time + autocvar_cl_damageeffect_ticrate;
22
23         if(time >= self.cnt || !self.owner || !self.owner.modelindex || !self.owner.drawmask)
24         {
25                 // time is up or the player got gibbed / disconnected
26                 self.owner.total_damages = max(0, self.owner.total_damages - 1);
27                 remove(self);
28                 return;
29         }
30         if(self.state && !self.owner.csqcmodel_isdead)
31         {
32                 // if the player was dead but is now alive, it means he respawned
33                 // if so, clear his damage effects, or damages from his dead body will be copied back
34                 self.owner.total_damages = max(0, self.owner.total_damages - 1);
35                 remove(self);
36                 return;
37         }
38         self.state = self.owner.csqcmodel_isdead;
39         if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum) && !autocvar_chase_active)
40                 return; // if we aren't using a third person camera, hide our own effects
41
42         // now generate the particles
43         vector org;
44         org = gettaginfo(self, 0); // origin at attached location
45         __pointparticles(self.team, org, '0 0 0', 1);
46 }
47
48 void DamageEffect(vector hitorg, float thedamage, int type, int specnum)
49 {SELFPARAM();
50         // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
51
52         int nearestbone = 0;
53         float life;
54         string specstr, effectname;
55         entity e;
56
57         if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
58                 return;
59         if(!self || !self.modelindex || !self.drawmask)
60                 return;
61
62         // if this is a rigged mesh, the effect will show on the bone where damage was dealt
63         // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
64         // if there's no skeleton, object origin will automatically be selected
65         FOR_EACH_TAG(self)
66         {
67                 if(!tagnum)
68                         continue; // skip empty bones
69                 // blacklist bones positioned outside the mesh, or the effect will be floating
70                 // TODO: Do we have to do it this way? Why do these bones exist at all?
71                 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
72                         continue; // player model bone blacklist
73
74                 // now choose the bone closest to impact origin
75                 if(nearestbone == 0 || vlen(hitorg - gettaginfo(self, tagnum)) <= vlen(hitorg - gettaginfo(self, nearestbone)))
76                         nearestbone = tagnum;
77         }
78         gettaginfo(self, nearestbone); // set gettaginfo_name
79
80         // return if we reached our damage effect limit or damages are disabled
81         // TODO: When the limit is reached, it would be better if the oldest damage was removed instead of not adding a new one
82         if(nearestbone)
83         {
84                 if(self.total_damages >= autocvar_cl_damageeffect_bones)
85                         return; // allow multiple damages on skeletal models
86         }
87         else
88         {
89                 if(autocvar_cl_damageeffect < 2 || self.total_damages)
90                         return; // allow a single damage on non-skeletal models
91         }
92
93         life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
94
95         effectname = DEATH_WEAPONOF(type).netname;
96
97         if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
98         {
99                 if(self.isplayermodel)
100                 {
101                         specstr = species_prefix(specnum);
102                         specstr = substring(specstr, 0, strlen(specstr) - 1);
103                         effectname = strreplace("BLOOD", specstr, effectname);
104                 }
105                 else { return; } // objects don't bleed
106         }
107
108         e = new(damage);
109         make_pure(e);
110         setmodel(e, MDL_Null); // necessary to attach and read origin
111         setattachment(e, self, gettaginfo_name); // attach to the given bone
112         e.owner = self;
113         e.cnt = time + life;
114         e.team = _particleeffectnum(effectname);
115         e.think = DamageEffect_Think;
116         e.nextthink = time;
117         self.total_damages += 1;
118 }
119
120 void Ent_DamageInfo(float isNew)
121 {SELFPARAM();
122         make_pure(this);
123         float thedamage, rad, edge, thisdmg;
124         bool hitplayer = false;
125         int species, forcemul;
126         vector force, thisforce;
127
128         w_deathtype = ReadShort();
129         w_issilent = (w_deathtype & 0x8000);
130         w_deathtype = (w_deathtype & 0x7FFF);
131
132         w_org.x = ReadCoord();
133         w_org.y = ReadCoord();
134         w_org.z = ReadCoord();
135
136         thedamage = ReadByte();
137         rad = ReadByte();
138         edge = ReadByte();
139         force = decompressShortVector(ReadShort());
140         species = ReadByte();
141
142         if (!isNew)
143                 return;
144
145         if(rad < 0)
146         {
147                 rad = -rad;
148                 forcemul = -1;
149         }
150         else
151                 forcemul = 1;
152
153         for(entity e = findradius(w_org, rad + MAX_DAMAGEEXTRARADIUS); e; e = e.chain)
154         {
155                 setself(e);
156                 // attached ents suck
157                 if(self.tag_entity)
158                         continue;
159
160                 vector nearest = NearestPointOnBox(self, w_org);
161                 if(rad)
162                 {
163                         thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
164                         if(thisdmg >= 1)
165                                 continue;
166                         if(thisdmg < 0)
167                                 thisdmg = 0;
168                         if(thedamage)
169                         {
170                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
171                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
172                         }
173                         else
174                         {
175                                 thisdmg = 0;
176                                 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
177                         }
178                 }
179                 else
180                 {
181                         if(vlen(nearest - w_org) > bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS))
182                                 continue;
183
184                         thisdmg = thedamage;
185                         thisforce = forcemul * force;
186                 }
187
188                 if(self.damageforcescale)
189                         if(vlen(thisforce))
190                         {
191                                 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
192                                 self.move_flags &= ~FL_ONGROUND;
193                         }
194
195                 if(w_issilent)
196                         self.silent = 1;
197
198                 if(self.event_damage)
199                         self.event_damage(thisdmg, w_deathtype, w_org, thisforce);
200
201                 DamageEffect(w_org, thisdmg, w_deathtype, species);
202
203                 if(self.isplayermodel)
204                         hitplayer = true; // this impact damaged a player
205         }
206         setself(this);
207
208         if(DEATH_ISVEHICLE(w_deathtype))
209         {
210                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
211                 if(trace_plane_normal != '0 0 0')
212                         w_backoff = trace_plane_normal;
213                 else
214                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
215
216                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
217
218                 switch(DEATH_ENT(w_deathtype))
219                 {
220                         case DEATH_VH_CRUSH:
221                                 break;
222
223                         // spiderbot
224                         case DEATH_VH_SPID_MINIGUN:
225                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
226                                 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
227                                 break;
228                         case DEATH_VH_SPID_ROCKET:
229                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
230                                 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
231                                 break;
232                         case DEATH_VH_SPID_DEATH:
233                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
234                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
235                                 break;
236
237                         case DEATH_VH_WAKI_GUN:
238                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
239                                 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
240                                 break;
241                         case DEATH_VH_WAKI_ROCKET:
242                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
243                                 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
244                                 break;
245                         case DEATH_VH_WAKI_DEATH:
246                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
247                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
248                                 break;
249
250                         case DEATH_VH_RAPT_CANNON:
251                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
252                                 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
253                                 break;
254                         case DEATH_VH_RAPT_FRAGMENT:
255                                 float i;
256                                 vector ang, vel;
257                                 for(i = 1; i < 4; ++i)
258                                 {
259                                         vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
260                                         ang = vectoangles(vel);
261                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
262                                 }
263                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
264                                 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
265                                 break;
266                         case DEATH_VH_RAPT_BOMB:
267                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
268                                 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
269                                 break;
270                         case DEATH_VH_RAPT_DEATH:
271                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
272                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
273                                 break;
274                         case DEATH_VH_BUMB_GUN:
275                                 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
276                                 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
277                                 break;
278                 }
279         }
280
281
282         if(DEATH_ISTURRET(w_deathtype))
283         {
284                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
285                 if(trace_plane_normal != '0 0 0')
286                         w_backoff = trace_plane_normal;
287                 else
288                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
289
290                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
291
292                 switch(DEATH_ENT(w_deathtype))
293                 {
294                          case DEATH_TURRET_EWHEEL:
295                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
296                                 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
297                                 break;
298
299                          case DEATH_TURRET_FLAC:
300                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
301                                 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
302                                 break;
303
304                          case DEATH_TURRET_MLRS:
305                          case DEATH_TURRET_HK:
306                          case DEATH_TURRET_WALK_ROCKET:
307                          case DEATH_TURRET_HELLION:
308                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
309                                 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
310                                 break;
311
312                          case DEATH_TURRET_MACHINEGUN:
313                          case DEATH_TURRET_WALK_GUN:
314                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
315                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
316                                 break;
317
318                          case DEATH_TURRET_PLASMA:
319                                 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
320                                 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
321                                 break;
322
323                          case DEATH_TURRET_WALK_MELEE:
324                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
325                                 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
326                                 break;
327
328                          case DEATH_TURRET_PHASER:
329                                 break;
330
331                          case DEATH_TURRET_TESLA:
332                                 te_smallflash(self.origin);
333                                 break;
334
335                 }
336         }
337
338         // TODO spawn particle effects and sounds based on w_deathtype
339         if(!DEATH_ISSPECIAL(w_deathtype))
340         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
341         {
342                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
343                 w_random = prandom();
344
345                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
346                 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
347                         w_backoff = trace_plane_normal;
348                 else
349                         w_backoff = -1 * normalize(force);
350                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
351
352                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)) {
353                         hitwep.wr_impacteffect(hitwep);
354                 }
355         }
356 }