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