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