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