]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/damage.qc
Simplify the bone detection code for damage effects
[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 -= 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 -= 1;
21                 remove(self);
22                 return;
23         }
24         self.state = self.owner.csqcmodel_isdead;
25         if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum || self.owner.entnum == spectatee_status) && !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;
39         string specstr, effectnum;
40         entity e;
41
42         if(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                 // blacklist bones positioned outside the mesh, or the effect will be floating
53                 // TODO: Do we have to do it this way? Why do these bones exist at all?
54                 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
55                         continue; // player model bone blacklist
56                 if(gettaginfo_name == "")
57                         continue; // skip empty bones
58
59                 // now choose the bone closest to impact origin
60                 if(!nearestbone || 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         if(nearestbone)
67         {
68                 if(autocvar_cl_damageeffect < 1 || self.total_damages >= autocvar_cl_damageeffect_bones)
69                         return; // allow multiple damages on skeletal models
70         }
71         else
72         {
73                 if(autocvar_cl_damageeffect < 2 || self.total_damages)
74                         return; // allow a single damage on non-skeletal models
75         }
76
77         life = bound(autocvar_cl_damageeffect_lifetime_min, dmg * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
78         specstr = species_prefix(specnum);
79         type = DEATH_WEAPONOF(type);
80         e = get_weaponinfo(type);
81
82         effectnum = strcat("damage_", e.netname);
83         
84         // if damage was dealt with a bullet weapon, our effect is blood
85         // since blood is species dependent, include the species tag
86         if(type == WEP_SHOTGUN || type == WEP_UZI || type == WEP_RIFLE)
87         {
88                 if(self.isplayermodel)
89                 {
90                         effectnum = strcat(effectnum, "_", specstr);
91                         effectnum = substring(effectnum, 0, strlen(effectnum) - 1); // remove the _ symbol at the end of the species tag
92                 }
93                 else
94                         return; // objects don't bleed
95         }
96
97         e = spawn();
98         setmodel(e, "null"); // necessary to attach and read origin // samual: FIXME: this is weird, is there some better way to do this?
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(effectnum);
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;
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 not(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                 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
188         self = oldself;
189         
190         if(DEATH_ISVEHICLE(w_deathtype))
191         {
192                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
193                 if(trace_plane_normal != '0 0 0')
194                         w_backoff = trace_plane_normal;
195                 else
196                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
197                 
198                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
199                 
200                 switch(w_deathtype)
201                 {
202                         case DEATH_VHCRUSH:
203                                 break;
204                                 
205                         // spiderbot
206                         case DEATH_SBMINIGUN:
207                                 string _snd;
208                                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
209                                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
210                                 pointparticles(particleeffectnum("spiderbot_minigun_impact"), self.origin, w_backoff * 1000, 1);
211                                 break;
212                         case DEATH_SBROCKET:
213                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
214                                 pointparticles(particleeffectnum("spiderbot_rocket_explode"), self.origin, w_backoff * 1000, 1);
215                                 break;
216                         case DEATH_SBBLOWUP:
217                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
218                                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
219                                 break;
220                                 
221                         case DEATH_WAKIGUN:
222                                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
223                                 pointparticles(particleeffectnum("wakizashi_gun_impact"), self.origin, w_backoff * 1000, 1);
224                                 break;
225                         case DEATH_WAKIROCKET:
226                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
227                                 pointparticles(particleeffectnum("wakizashi_rocket_explode"), self.origin, w_backoff * 1000, 1);
228                                 break;
229                         case DEATH_WAKIBLOWUP:
230                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
231                                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
232                                 break;
233                                 
234                         case DEATH_RAPTOR_CANNON:
235                                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
236                                 pointparticles(particleeffectnum("raptor_cannon_impact"), self.origin, w_backoff * 1000, 1);
237                                 break;
238                         case DEATH_RAPTOR_BOMB_SPLIT:
239                                 float i;
240                                 vector ang, vel;
241                                 for(i = 1; i < 4; ++i)
242                                 {
243                                         vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
244                                         ang = vectoangles(vel);
245                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
246                                 }
247                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
248                                 pointparticles(particleeffectnum("raptor_bomb_spread"), self.origin, w_backoff * 1000, 1);
249                                 break;
250                         case DEATH_RAPTOR_BOMB:
251                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
252                                 pointparticles(particleeffectnum("raptor_bomb_impact"), self.origin, w_backoff * 1000, 1);
253                                 break;
254                         case DEATH_RAPTOR_DEATH:
255                                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
256                                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
257                                 break;
258                 }
259         }
260         
261         
262         if(DEATH_ISTURRET(w_deathtype))
263         {
264                 string _snd;
265                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
266                 if(trace_plane_normal != '0 0 0')
267                         w_backoff = trace_plane_normal;
268                 else
269                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
270                 
271                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
272                 
273                 switch(w_deathtype)
274                 {   
275                          case DEATH_TURRET_EWHEEL:
276                                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
277                                 pointparticles(particleeffectnum("laser_impact"), self.origin, w_backoff * 1000, 1);
278                                 break;
279                          
280                          case DEATH_TURRET_FLAC:
281                                 pointparticles(particleeffectnum("hagar_explode"), w_org, '0 0 0', 1);
282                                 _snd = strcat("weapons/hagexp", ftos(1 + rint(random() * 2)), ".waw");
283                                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
284                                 break;
285                                 
286                          case DEATH_TURRET_MLRS:
287                          case DEATH_TURRET_HK:
288                          case DEATH_TURRET_WALKER_ROCKET:
289                          case DEATH_TURRET_HELLION:
290                                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
291                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, w_backoff * 1000, 1);
292                                 break;
293                          
294                          case DEATH_TURRET_MACHINEGUN:
295                          case DEATH_TURRET_WALKER_GUN:
296                                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
297                                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
298                                 pointparticles(particleeffectnum("machinegun_impact"), self.origin, w_backoff * 1000, 1);
299                                 break;
300                                                   
301                          case DEATH_TURRET_PLASMA:
302                                 sound(self, CH_SHOTS, "weapons/electro_impact.wav", VOL_BASE, ATTN_MIN);
303                                 pointparticles(particleeffectnum("electro_impact"), self.origin, w_backoff * 1000, 1);
304                                 break;
305                                                   
306                          case DEATH_TURRET_WALKER_MEELE:
307                                 sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_MIN);
308                                 pointparticles(particleeffectnum("TE_SPARK"), self.origin, w_backoff * 1000, 1);
309                                 break;
310
311                          case DEATH_TURRET_PHASER:
312                                 break;
313                                 
314                          case DEATH_TURRET_TESLA:
315                                 te_smallflash(self.origin);
316                                 break;
317
318                 }
319         }
320         
321         // TODO spawn particle effects and sounds based on w_deathtype
322         if(!DEATH_ISSPECIAL(w_deathtype))
323         {
324                 float hitwep;
325
326                 hitwep = DEATH_WEAPONOFWEAPONDEATH(w_deathtype);
327                 w_random = prandom();
328
329                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
330                 if(trace_fraction < 1 && hitwep != WEP_NEX && hitwep != WEP_MINSTANEX)
331                         w_backoff = trace_plane_normal;
332                 else
333                         w_backoff = -1 * normalize(force);
334                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
335
336                 (get_weaponinfo(hitwep)).weapon_func(WR_IMPACTEFFECT);
337         }
338 }
339
340 void DamageInfo_Precache()
341 {
342         float i;
343         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
344                 (get_weaponinfo(i)).weapon_func(WR_PRECACHE);
345 }