Merge branch 'master' into mirceakitsune/damage_effects
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / damage.qc
1 void Ent_DamageInfo(float isNew)
2 {
3         float dmg, rad, edge, thisdmg, forcemul;
4         vector force, thisforce;
5         entity oldself;
6
7         oldself = self;
8
9         w_deathtype = ReadShort();
10         w_issilent = (w_deathtype & 0x8000);
11         w_deathtype = (w_deathtype & 0x7FFF);
12
13         w_org_x = ReadCoord();
14         w_org_y = ReadCoord();
15         w_org_z = ReadCoord();
16
17         dmg = ReadByte();
18         rad = ReadByte();
19         edge = ReadByte();
20         force = decompressShortVector(ReadShort());
21
22         if not(isNew)
23                 return;
24
25         if(rad < 0)
26         {
27                 rad = -rad;
28                 forcemul = -1;
29         }
30         else
31                 forcemul = 1;
32         
33         for(self = findradius(w_org, rad); self; self = self.chain)
34         {
35                 if(rad)
36                 {
37                         thisdmg = vlen(self.origin - w_org) / rad;
38                         if(thisdmg >= 1)
39                                 continue;
40                         if(dmg)
41                         {
42                                 thisdmg = dmg + (edge - dmg) * thisdmg;
43                                 thisforce = forcemul * vlen(force) * (thisdmg / dmg) * normalize(self.origin - w_org);
44                         }
45                         else
46                         {
47                                 thisdmg = 0;
48                                 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
49                         }
50                 }
51                 else
52                 {
53                         thisdmg = dmg;
54                         thisforce = forcemul * force;
55                 }
56
57                 if(self.damageforcescale)
58                         if(vlen(thisforce))
59                         {
60                                 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
61                                 self.move_flags &~= FL_ONGROUND;
62                         }
63
64                 if(w_issilent)
65                         self.silent = 1;
66
67                 if(self.event_damage)
68                         self.event_damage(thisdmg, w_deathtype, w_org, thisforce);
69         }
70
71         self = oldself;
72         
73         if(DEATH_ISVEHICLE(w_deathtype))
74         {
75             traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
76             if(trace_plane_normal != '0 0 0')       
77             w_backoff = trace_plane_normal;
78         else
79             w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
80             
81             setorigin(self, w_org + w_backoff * 2); // for sound() calls
82             
83             switch(w_deathtype)
84             {            
85             case DEATH_VHCRUSH:
86                 break;
87                 
88             case DEATH_SBMINIGUN:
89                 string _snd;
90                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
91                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
92                 pointparticles(particleeffectnum("spiderbot_minigun_impact"), self.origin, w_backoff * 1000, 1);
93                 break;
94             case DEATH_SBROCKET:
95                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
96                 pointparticles(particleeffectnum("spiderbot_rocket_explode"), self.origin, w_backoff * 1000, 1);
97                 break;
98             case DEATH_SBBLOWUP:
99                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
100                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
101                 break;
102                 
103             case DEATH_WAKIGUN:
104                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
105                 pointparticles(particleeffectnum("wakizashi_gun_impact"), self.origin, w_backoff * 1000, 1);
106                 break;
107             case DEATH_WAKIROCKET:
108                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
109                 pointparticles(particleeffectnum("wakizashi_rocket_explode"), self.origin, w_backoff * 1000, 1);
110                 break;
111             case DEATH_WAKIBLOWUP:
112                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
113                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
114                 break;
115                 
116             case DEATH_RAPTOR_CANNON:
117                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_NORM);
118                 pointparticles(particleeffectnum("raptor_cannon_impact"), self.origin, w_backoff * 1000, 1);
119                 break;
120             case DEATH_RAPTOR_BOMB_SPLIT:
121                 float i;
122                 vector ang, vel;
123                 for(i = 1; i < 4; ++i)
124                 {
125                     vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
126                     ang = vectoangles(vel);
127                     RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
128                 }
129                     
130                 
131                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
132                 pointparticles(particleeffectnum("raptor_bomb_spread"), self.origin, w_backoff * 1000, 1);
133                 break;
134             case DEATH_RAPTOR_BOMB:
135                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_NORM);
136                 pointparticles(particleeffectnum("raptor_bomb_impact"), self.origin, w_backoff * 1000, 1);
137                 break;
138             case DEATH_RAPTOR_DEATH:
139                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
140                 pointparticles(particleeffectnum("explosion_big"), self.origin, w_backoff * 1000, 1);
141                 break;
142             }
143         }
144         
145         
146         if(DEATH_ISTURRET(w_deathtype))
147         {           
148             string _snd;
149             traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
150             if(trace_plane_normal != '0 0 0')       
151             w_backoff = trace_plane_normal;
152         else
153             w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
154             
155             setorigin(self, w_org + w_backoff * 2); // for sound() calls
156             
157             switch(w_deathtype)
158             {   
159              case DEATH_TURRET_EWHEEL:
160                 sound(self, CH_SHOTS, "weapons/laserimpact.wav", VOL_BASE, ATTN_MIN);
161                 pointparticles(particleeffectnum("laser_impact"), self.origin, w_backoff * 1000, 1);
162                 break;
163              
164              case DEATH_TURRET_FLAC:
165                 pointparticles(particleeffectnum("hagar_explode"), w_org, '0 0 0', 1);
166                 _snd = strcat("weapons/hagexp", ftos(1 + rint(random() * 2)), ".waw");
167                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);                
168                 break;
169                 
170              case DEATH_TURRET_MLRS:
171              case DEATH_TURRET_HK:
172              case DEATH_TURRET_WALKER_ROCKET:
173              case DEATH_TURRET_HELLION:
174                 sound(self, CH_SHOTS, "weapons/rocket_impact.wav", VOL_BASE, ATTN_MIN);
175                 pointparticles(particleeffectnum("rocket_explode"), self.origin, w_backoff * 1000, 1);
176                 break;
177              
178              case DEATH_TURRET_MACHINEGUN:
179              case DEATH_TURRET_WALKER_GUN:
180                 _snd = strcat("weapons/ric", ftos(1 + rint(random() * 2)), ".waw");
181                 sound(self, CH_SHOTS, _snd, VOL_BASE, ATTN_NORM);
182                 pointparticles(particleeffectnum("machinegun_impact"), self.origin, w_backoff * 1000, 1);
183                 break;
184                           
185              case DEATH_TURRET_PLASMA:
186                 sound(self, CH_SHOTS, "weapons/electro_impact.wav", VOL_BASE, ATTN_MIN);
187                 pointparticles(particleeffectnum("electro_impact"), self.origin, w_backoff * 1000, 1);
188                 break;
189                           
190              case DEATH_TURRET_WALKER_MEELE:
191                 sound(self, CH_SHOTS, "weapons/ric1.wav", VOL_BASE, ATTN_MIN);
192                 pointparticles(particleeffectnum("TE_SPARK"), self.origin, w_backoff * 1000, 1);
193                 break;
194
195              case DEATH_TURRET_PHASER:
196                 break;
197                 
198              case DEATH_TURRET_TESLA:
199                 te_smallflash(self.origin);
200                 break;
201
202         }        
203         }
204         
205         // TODO spawn particle effects and sounds based on w_deathtype
206         if(!DEATH_ISSPECIAL(w_deathtype))
207         {
208                 float hitwep;
209
210                 hitwep = DEATH_WEAPONOFWEAPONDEATH(w_deathtype);
211                 w_random = prandom();
212
213                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
214                 if(trace_fraction < 1 && hitwep != WEP_NEX && hitwep != WEP_MINSTANEX)
215                         w_backoff = trace_plane_normal;
216                 else
217                         w_backoff = -1 * normalize(force);
218                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
219
220                 (get_weaponinfo(hitwep)).weapon_func(WR_IMPACTEFFECT);
221         }
222 }
223
224 void DamageInfo_Precache()
225 {
226         float i;
227         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
228                 (get_weaponinfo(i)).weapon_func(WR_PRECACHE);
229 }
230
231 // damage effect
232
233 .entity dmgent;
234 .float dmgpartnum, dmgtime;
235 .float lifetime;
236
237 void Ent_DamageEffect_Think()
238 {
239         self.nextthink = time;
240
241         float foundgib;
242         vector org;
243
244         if(time >= self.lifetime)
245         {
246                 remove(self);
247                 self = world;
248                 return;
249         }
250         if(self.dmgtime > time)
251                 return;
252         org = getplayerorigin(self.team);
253         if(org == GETPLAYERORIGIN_ERROR)
254                 return;
255
256         // Scan the owner of all gibs in the world. If a gib owner is the same as the player we're applying
257         // the effect to, it means our player is gibbed. Therefore, apply particles to the gibs instead.
258         entity head;
259         for(head = world; (head = find(head, classname, "gib")); )
260         {
261                 if(head.team == self.team)
262                 {
263                         if(autocvar_cl_damageeffect_gibs)
264                         {
265                                 if(autocvar_cl_damageeffect_gibs_randomize >= random())
266                                         pointparticles(self.dmgpartnum, head.origin, '0 0 0', 1);
267                                 self.dmgtime = time + autocvar_cl_damageeffect_gibs;
268                         }
269                         foundgib = TRUE;
270                 }
271         }
272
273         if(foundgib || !autocvar_cl_damageeffect_player)
274                 return; // don't show effects on the invisible dead body if gibs exist
275         if(self.team == player_localentnum - 1 && !autocvar_chase_active)
276                 return; // if we aren't in third person mode, hide own damage effect
277
278         // Now apply the effect to actual players
279         pointparticles(self.dmgpartnum, org, '0 0 0', 1);
280         self.dmgtime = time + autocvar_cl_damageeffect_player;
281 }
282
283 void Ent_DamageEffect()
284 {
285         float dmg, type, specnum1, specnum2, entnumber, life;
286         vector org;
287         string specstr, effectnum;
288         entity e;
289
290         dmg = ReadByte(); // damage amount
291         type = ReadByte(); // damage weapon
292         specnum1 = ReadByte(); // player species
293         entnumber = ReadByte(); // player entnum
294
295         if(!autocvar_cl_damageeffect_player && !autocvar_cl_damageeffect_gibs)
296                 return;
297         if(autocvar_cl_gentle || autocvar_cl_gentle_damage)
298                 return;
299
300         specnum2 = (specnum1 & 0x78) / 8; // blood type: using four bits (0..7, bit indexes 3,4,5)
301         specstr = species_prefix(specnum2);
302         life = bound(0, dmg * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
303
304         e = get_weaponinfo(type);
305         effectnum = strcat("weapondamage_", e.netname);
306         // If the weapon is a bullet weapon, its damage effect is blood.
307         // Since blood is species dependent, we make this effect per-species.
308         if(type == WEP_SHOTGUN || type == WEP_UZI || type == WEP_RIFLE)
309         if(specstr != "")
310         {
311                 effectnum = strcat(effectnum, "_", specstr);
312                 effectnum = substring(effectnum, 0, strlen(effectnum) - 1); // remove the _ symbol at the end of the species name
313         }
314
315         // if the player already has a damage effect, update it instead of spawning a new one
316         entity head;
317         for(head = world; (head = find(head, classname, "damageeffect")); )
318         {
319                 if(head.team == entnumber - 1)
320                 {
321                         head.dmgpartnum = particleeffectnum(effectnum);
322                         head.lifetime += life;
323                         return;
324                 }
325         }
326
327         entity e;
328         e = spawn();
329         e.classname = "damageeffect";
330         e.team = entnumber - 1;
331         e.dmgpartnum = particleeffectnum(effectnum);
332         e.lifetime = time + life;
333         e.think = Ent_DamageEffect_Think;
334         e.nextthink = time;
335 }