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