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