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