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