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