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