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