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