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