]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/damageeffects.qc
Merge branch 'terencehill/quickmenu_file_example' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / damageeffects.qc
1 #ifndef DAMAGEEFFECTS_H
2 #define DAMAGEEFFECTS_H
3
4 #ifdef CSQC
5 #include "../../deathtypes/all.qh"
6 #include "../../movetypes/movetypes.qh"
7 #include "../../vehicles/all.qh"
8 #include "../../weapons/all.qh"
9 #endif
10
11 #endif
12
13 #ifdef IMPLEMENTATION
14
15 REGISTER_NET_LINKED(ENT_CLIENT_DAMAGEINFO)
16
17 #ifdef SVQC
18
19 bool Damage_DamageInfo_SendEntity(entity this, entity to, int sf)
20 {
21         WriteHeader(MSG_ENTITY, ENT_CLIENT_DAMAGEINFO);
22         WriteShort(MSG_ENTITY, self.projectiledeathtype);
23         WriteCoord(MSG_ENTITY, floor(self.origin.x));
24         WriteCoord(MSG_ENTITY, floor(self.origin.y));
25         WriteCoord(MSG_ENTITY, floor(self.origin.z));
26         WriteByte(MSG_ENTITY, bound(1, self.dmg, 255));
27         WriteByte(MSG_ENTITY, bound(0, self.dmg_radius, 255));
28         WriteByte(MSG_ENTITY, bound(1, self.dmg_edge, 255));
29         WriteShort(MSG_ENTITY, self.oldorigin.x);
30         WriteByte(MSG_ENTITY, self.species);
31         return true;
32 }
33
34 void Damage_DamageInfo(vector org, float coredamage, float edgedamage, float rad, vector force, int deathtype, float bloodtype, entity dmgowner)
35 {
36         // TODO maybe call this from non-edgedamage too?
37         // TODO maybe make the client do the particle effects for the weapons and the impact sounds using this info?
38
39         entity e;
40
41         if(!sound_allowed(MSG_BROADCAST, dmgowner))
42                 deathtype |= 0x8000;
43
44         e = new(damageinfo);
45         make_pure(e);
46         setorigin(e, org);
47         e.projectiledeathtype = deathtype;
48         e.dmg = coredamage;
49         e.dmg_edge = edgedamage;
50         e.dmg_radius = rad;
51         e.dmg_force = vlen(force);
52         e.velocity = force;
53         e.oldorigin_x = compressShortVector(e.velocity);
54         e.species = bloodtype;
55
56         Net_LinkEntity(e, false, 0.2, Damage_DamageInfo_SendEntity);
57 }
58
59 #endif
60
61 #ifdef CSQC
62
63 /** number of effects which currently are attached to a player */
64 .int total_damages;
65
66 .entity tag_entity;
67
68 .float cnt;
69 .int state;
70 .bool isplayermodel;
71
72 void DamageEffect_Think()
73 {SELFPARAM();
74         // if particle distribution is enabled, slow ticrate by total number of damages
75         if(autocvar_cl_damageeffect_distribute)
76                 self.nextthink = time + autocvar_cl_damageeffect_ticrate * self.owner.total_damages;
77         else
78                 self.nextthink = time + autocvar_cl_damageeffect_ticrate;
79
80         if(time >= self.cnt || !self.owner || !self.owner.modelindex || !self.owner.drawmask)
81         {
82                 // time is up or the player got gibbed / disconnected
83                 self.owner.total_damages = max(0, self.owner.total_damages - 1);
84                 remove(self);
85                 return;
86         }
87         if(self.state && !self.owner.csqcmodel_isdead)
88         {
89                 // if the player was dead but is now alive, it means he respawned
90                 // if so, clear his damage effects, or damages from his dead body will be copied back
91                 self.owner.total_damages = max(0, self.owner.total_damages - 1);
92                 remove(self);
93                 return;
94         }
95         self.state = self.owner.csqcmodel_isdead;
96         if(self.owner.isplayermodel && (self.owner.entnum == player_localentnum) && !autocvar_chase_active)
97                 return; // if we aren't using a third person camera, hide our own effects
98
99         // now generate the particles
100         vector org;
101         org = gettaginfo(self, 0); // origin at attached location
102         __pointparticles(self.team, org, '0 0 0', 1);
103 }
104
105 string species_prefix(int specnum)
106 {
107         switch(specnum)
108         {
109                 case SPECIES_HUMAN:       return "";
110                 case SPECIES_ALIEN:       return "alien_";
111                 case SPECIES_ROBOT_SHINY: return "robot_";
112                 case SPECIES_ROBOT_RUSTY: return "robot_"; // use the same effects, only different gibs
113                 case SPECIES_ROBOT_SOLID: return "robot_"; // use the same effects, only different gibs
114                 case SPECIES_ANIMAL:      return "animal_";
115                 case SPECIES_RESERVED:    return "reserved_";
116                 default:         return "";
117         }
118 }
119
120 void DamageEffect(vector hitorg, float thedamage, int type, int specnum)
121 {SELFPARAM();
122         // particle effects for players and objects damaged by weapons (eg: flames coming out of victims shot with rockets)
123
124         int nearestbone = 0;
125         float life;
126         string specstr, effectname;
127         entity e;
128
129         if(!autocvar_cl_damageeffect || autocvar_cl_gentle || autocvar_cl_gentle_damage)
130                 return;
131         if(!self || !self.modelindex || !self.drawmask)
132                 return;
133
134         // if this is a rigged mesh, the effect will show on the bone where damage was dealt
135         // we do this by choosing the skeletal bone closest to the impact, and attaching our entity to it
136         // if there's no skeleton, object origin will automatically be selected
137         FOR_EACH_TAG(self)
138         {
139                 if(!tagnum)
140                         continue; // skip empty bones
141                 // blacklist bones positioned outside the mesh, or the effect will be floating
142                 // TODO: Do we have to do it this way? Why do these bones exist at all?
143                 if(gettaginfo_name == "master" || gettaginfo_name == "knee_L" || gettaginfo_name == "knee_R" || gettaginfo_name == "leg_L" || gettaginfo_name == "leg_R")
144                         continue; // player model bone blacklist
145
146                 // now choose the bone closest to impact origin
147                 if(nearestbone == 0 || vlen(hitorg - gettaginfo(self, tagnum)) <= vlen(hitorg - gettaginfo(self, nearestbone)))
148                         nearestbone = tagnum;
149         }
150         gettaginfo(self, nearestbone); // set gettaginfo_name
151
152         // return if we reached our damage effect limit or damages are disabled
153         // TODO: When the limit is reached, it would be better if the oldest damage was removed instead of not adding a new one
154         if(nearestbone)
155         {
156                 if(self.total_damages >= autocvar_cl_damageeffect_bones)
157                         return; // allow multiple damages on skeletal models
158         }
159         else
160         {
161                 if(autocvar_cl_damageeffect < 2 || self.total_damages)
162                         return; // allow a single damage on non-skeletal models
163         }
164
165         life = bound(autocvar_cl_damageeffect_lifetime_min, thedamage * autocvar_cl_damageeffect_lifetime, autocvar_cl_damageeffect_lifetime_max);
166
167         effectname = DEATH_WEAPONOF(type).netname;
168
169         if(substring(effectname, strlen(effectname) - 5, 5) == "BLOOD")
170         {
171                 if(self.isplayermodel)
172                 {
173                         specstr = species_prefix(specnum);
174                         specstr = substring(specstr, 0, strlen(specstr) - 1);
175                         effectname = strreplace("BLOOD", specstr, effectname);
176                 }
177                 else { return; } // objects don't bleed
178         }
179
180         e = new(damage);
181         make_pure(e);
182         setmodel(e, MDL_Null); // necessary to attach and read origin
183         setattachment(e, self, gettaginfo_name); // attach to the given bone
184         e.owner = self;
185         e.cnt = time + life;
186         e.team = _particleeffectnum(effectname);
187         e.think = DamageEffect_Think;
188         e.nextthink = time;
189         self.total_damages += 1;
190 }
191
192 NET_HANDLE(ENT_CLIENT_DAMAGEINFO, bool isNew)
193 {
194         make_pure(this);
195         float thedamage, rad, edge, thisdmg;
196         bool hitplayer = false;
197         int species, forcemul;
198         vector force, thisforce;
199
200         w_deathtype = ReadShort();
201         w_issilent = (w_deathtype & 0x8000);
202         w_deathtype = (w_deathtype & 0x7FFF);
203
204         w_org.x = ReadCoord();
205         w_org.y = ReadCoord();
206         w_org.z = ReadCoord();
207
208         thedamage = ReadByte();
209         rad = ReadByte();
210         edge = ReadByte();
211         force = decompressShortVector(ReadShort());
212         species = ReadByte();
213
214         return = true;
215
216         if (!isNew)
217                 return;
218
219         if(rad < 0)
220         {
221                 rad = -rad;
222                 forcemul = -1;
223         }
224         else
225                 forcemul = 1;
226
227         for(entity e = findradius(w_org, rad + MAX_DAMAGEEXTRARADIUS); e; e = e.chain)
228         {
229                 setself(e);
230                 // attached ents suck
231                 if(self.tag_entity)
232                         continue;
233
234                 vector nearest = NearestPointOnBox(self, w_org);
235                 if(rad)
236                 {
237                         thisdmg = ((vlen (nearest - w_org) - bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS)) / rad);
238                         if(thisdmg >= 1)
239                                 continue;
240                         if(thisdmg < 0)
241                                 thisdmg = 0;
242                         if(thedamage)
243                         {
244                                 thisdmg = thedamage + (edge - thedamage) * thisdmg;
245                                 thisforce = forcemul * vlen(force) * (thisdmg / thedamage) * normalize(self.origin - w_org);
246                         }
247                         else
248                         {
249                                 thisdmg = 0;
250                                 thisforce = forcemul * vlen(force) * normalize(self.origin - w_org);
251                         }
252                 }
253                 else
254                 {
255                         if(vlen(nearest - w_org) > bound(MIN_DAMAGEEXTRARADIUS, self.damageextraradius, MAX_DAMAGEEXTRARADIUS))
256                                 continue;
257
258                         thisdmg = thedamage;
259                         thisforce = forcemul * force;
260                 }
261
262                 if(self.damageforcescale)
263                         if(vlen(thisforce))
264                         {
265                                 self.move_velocity = self.move_velocity + damage_explosion_calcpush(self.damageforcescale * thisforce, self.move_velocity, autocvar_g_balance_damagepush_speedfactor);
266                                 self.move_flags &= ~FL_ONGROUND;
267                         }
268
269                 if(w_issilent)
270                         self.silent = 1;
271
272                 if(self.event_damage)
273                         self.event_damage(thisdmg, w_deathtype, w_org, thisforce);
274
275                 DamageEffect(w_org, thisdmg, w_deathtype, species);
276
277                 if(self.isplayermodel)
278                         hitplayer = true; // this impact damaged a player
279         }
280         setself(this);
281
282         if(DEATH_ISVEHICLE(w_deathtype))
283         {
284                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
285                 if(trace_plane_normal != '0 0 0')
286                         w_backoff = trace_plane_normal;
287                 else
288                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
289
290                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
291
292                 switch(DEATH_ENT(w_deathtype))
293                 {
294                         case DEATH_VH_CRUSH:
295                                 break;
296
297                         // spiderbot
298                         case DEATH_VH_SPID_MINIGUN:
299                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
300                                 pointparticles(EFFECT_SPIDERBOT_MINIGUN_IMPACT, self.origin, w_backoff * 1000, 1);
301                                 break;
302                         case DEATH_VH_SPID_ROCKET:
303                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
304                                 pointparticles(EFFECT_SPIDERBOT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
305                                 break;
306                         case DEATH_VH_SPID_DEATH:
307                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
308                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
309                                 break;
310
311                         case DEATH_VH_WAKI_GUN:
312                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
313                                 pointparticles(EFFECT_RACER_IMPACT, self.origin, w_backoff * 1000, 1);
314                                 break;
315                         case DEATH_VH_WAKI_ROCKET:
316                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
317                                 pointparticles(EFFECT_RACER_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
318                                 break;
319                         case DEATH_VH_WAKI_DEATH:
320                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
321                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
322                                 break;
323
324                         case DEATH_VH_RAPT_CANNON:
325                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_NORM);
326                                 pointparticles(EFFECT_RAPTOR_CANNON_IMPACT, self.origin, w_backoff * 1000, 1);
327                                 break;
328                         case DEATH_VH_RAPT_FRAGMENT:
329                                 float i;
330                                 vector ang, vel;
331                                 for(i = 1; i < 4; ++i)
332                                 {
333                                         vel = normalize(w_org - (w_org + normalize(force) * 16)) + randomvec() * 128;
334                                         ang = vectoangles(vel);
335                                         RaptorCBShellfragToss(w_org, vel, ang + '0 0 1' * (120 * i));
336                                 }
337                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
338                                 pointparticles(EFFECT_RAPTOR_BOMB_SPREAD, self.origin, w_backoff * 1000, 1);
339                                 break;
340                         case DEATH_VH_RAPT_BOMB:
341                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_NORM);
342                                 pointparticles(EFFECT_RAPTOR_BOMB_IMPACT, self.origin, w_backoff * 1000, 1);
343                                 break;
344                         case DEATH_VH_RAPT_DEATH:
345                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
346                                 pointparticles(EFFECT_EXPLOSION_BIG, self.origin, w_backoff * 1000, 1);
347                                 break;
348                         case DEATH_VH_BUMB_GUN:
349                                 sound(self, CH_SHOTS, SND_FIREBALL_IMPACT2, VOL_BASE, ATTEN_NORM);
350                                 pointparticles(EFFECT_BIGPLASMA_IMPACT, self.origin, w_backoff * 1000, 1);
351                                 break;
352                 }
353         }
354
355
356         if(DEATH_ISTURRET(w_deathtype))
357         {
358                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
359                 if(trace_plane_normal != '0 0 0')
360                         w_backoff = trace_plane_normal;
361                 else
362                         w_backoff = -1 * normalize(w_org - (w_org + normalize(force) * 16));
363
364                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
365
366                 switch(DEATH_ENT(w_deathtype))
367                 {
368                          case DEATH_TURRET_EWHEEL:
369                                 sound(self, CH_SHOTS, SND_LASERIMPACT, VOL_BASE, ATTEN_MIN);
370                                 pointparticles(EFFECT_BLASTER_IMPACT, self.origin, w_backoff * 1000, 1);
371                                 break;
372
373                          case DEATH_TURRET_FLAC:
374                                 pointparticles(EFFECT_HAGAR_EXPLODE, w_org, '0 0 0', 1);
375                                 sound(self, CH_SHOTS, SND_HAGEXP_RANDOM(), VOL_BASE, ATTEN_NORM);
376                                 break;
377
378                          case DEATH_TURRET_MLRS:
379                          case DEATH_TURRET_HK:
380                          case DEATH_TURRET_WALK_ROCKET:
381                          case DEATH_TURRET_HELLION:
382                                 sound(self, CH_SHOTS, SND_ROCKET_IMPACT, VOL_BASE, ATTEN_MIN);
383                                 pointparticles(EFFECT_ROCKET_EXPLODE, self.origin, w_backoff * 1000, 1);
384                                 break;
385
386                          case DEATH_TURRET_MACHINEGUN:
387                          case DEATH_TURRET_WALK_GUN:
388                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_NORM);
389                                 pointparticles(EFFECT_MACHINEGUN_IMPACT, self.origin, w_backoff * 1000, 1);
390                                 break;
391
392                          case DEATH_TURRET_PLASMA:
393                                 sound(self, CH_SHOTS, SND_ELECTRO_IMPACT, VOL_BASE, ATTEN_MIN);
394                                 pointparticles(EFFECT_ELECTRO_IMPACT, self.origin, w_backoff * 1000, 1);
395                                 break;
396
397                          case DEATH_TURRET_WALK_MELEE:
398                                 sound(self, CH_SHOTS, SND_RIC_RANDOM(), VOL_BASE, ATTEN_MIN);
399                                 pointparticles(EFFECT_TE_SPARK, self.origin, w_backoff * 1000, 1);
400                                 break;
401
402                          case DEATH_TURRET_PHASER:
403                                 break;
404
405                          case DEATH_TURRET_TESLA:
406                                 te_smallflash(self.origin);
407                                 break;
408
409                 }
410         }
411
412         // TODO spawn particle effects and sounds based on w_deathtype
413         if(!DEATH_ISSPECIAL(w_deathtype))
414         if(!hitplayer || rad) // don't show ground impacts for hitscan weapons if a player was hit
415         {
416                 Weapon hitwep = DEATH_WEAPONOF(w_deathtype);
417                 w_random = prandom();
418
419                 traceline(w_org - normalize(force) * 16, w_org + normalize(force) * 16, MOVE_NOMONSTERS, world);
420                 if(trace_fraction < 1 && hitwep != WEP_VORTEX && hitwep != WEP_VAPORIZER)
421                         w_backoff = trace_plane_normal;
422                 else
423                         w_backoff = -1 * normalize(force);
424                 setorigin(self, w_org + w_backoff * 2); // for sound() calls
425
426                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)) {
427                         hitwep.wr_impacteffect(hitwep);
428                 }
429         }
430 }
431
432 #endif
433
434 #endif