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