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