]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/player.qc
Phase out miscfunctions.qc from the server codebase, preferring more fitting location...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / player.qc
1 #include "player.qh"
2
3 #include <common/effects/all.qh>
4 #include "bot/api.qh"
5 #include "cheats.qh"
6 #include "client.qh"
7 #include "clientkill.qh"
8 #include "damage.qh"
9 #include <server/mutators/_mod.qh>
10 #include "world.qh"
11 #include "handicap.qh"
12 #include "portals.qh"
13 #include "teamplay.qh"
14 #include <server/main.qh>
15 #include "weapons/common.qh"
16 #include "weapons/throwing.qh"
17 #include "command/common.qh"
18 #include "command/vote.qh"
19 #include "../common/state.qh"
20 #include "../common/anim.qh"
21 #include "../common/animdecide.qh"
22 #include "../common/csqcmodel_settings.qh"
23 #include "../common/gamemodes/sv_rules.qh"
24 #include "../common/deathtypes/all.qh"
25 #include "../common/mapobjects/subs.qh"
26 #include <common/mapobjects/teleporters.qh>
27 #include "../common/playerstats.qh"
28 #include "../lib/csqcmodel/sv_model.qh"
29
30 #include "../common/minigames/sv_minigames.qh"
31
32 #include <common/gamemodes/_mod.qh>
33
34 #include "../common/physics/player.qh"
35 #include "../common/effects/qc/_mod.qh"
36 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
37 #include "../common/mapobjects/_mod.qh"
38 #include "../common/wepent.qh"
39
40 #include "weapons/weaponstats.qh"
41 #include <server/weapons/weaponsystem.qh>
42
43 #include "../common/animdecide.qh"
44
45 void Drop_Special_Items(entity player)
46 {
47         // called when the player has become stuck or frozen
48         // so objective items aren't stuck with the player
49
50         MUTATOR_CALLHOOK(DropSpecialItems, player);
51 }
52
53 void CopyBody_Think(entity this)
54 {
55         if(this.CopyBody_nextthink && time > this.CopyBody_nextthink)
56         {
57                 this.CopyBody_think(this);
58                 if(wasfreed(this))
59                         return;
60                 this.CopyBody_nextthink = this.nextthink;
61                 this.CopyBody_think = getthink(this);
62                 setthink(this, CopyBody_Think);
63         }
64         CSQCMODEL_AUTOUPDATE(this);
65         this.nextthink = time;
66 }
67 void CopyBody(entity this, float keepvelocity)
68 {
69         if (this.effects & EF_NODRAW)
70                 return;
71         entity clone = new(body);
72         clone.enemy = this;
73         clone.lip = this.lip;
74         clone.colormap = this.colormap;
75         clone.iscreature = this.iscreature;
76         clone.teleportable = this.teleportable;
77         clone.damagedbycontents = this.damagedbycontents;
78         if(clone.damagedbycontents)
79                 IL_PUSH(g_damagedbycontents, clone);
80         clone.angles = this.angles;
81         clone.v_angle = this.v_angle;
82         clone.avelocity = this.avelocity;
83         clone.damageforcescale = this.damageforcescale;
84         clone.effects = this.effects;
85         clone.glowmod = this.glowmod;
86         clone.event_damage = this.event_damage;
87         clone.event_heal = this.event_heal;
88         clone.anim_state = this.anim_state;
89         clone.anim_time = this.anim_time;
90         clone.anim_lower_action = this.anim_lower_action;
91         clone.anim_lower_time = this.anim_lower_time;
92         clone.anim_upper_action = this.anim_upper_action;
93         clone.anim_upper_time = this.anim_upper_time;
94         clone.anim_implicit_state = this.anim_implicit_state;
95         clone.anim_implicit_time = this.anim_implicit_time;
96         clone.anim_lower_implicit_action = this.anim_lower_implicit_action;
97         clone.anim_lower_implicit_time = this.anim_lower_implicit_time;
98         clone.anim_upper_implicit_action = this.anim_upper_implicit_action;
99         clone.anim_upper_implicit_time = this.anim_upper_implicit_time;
100         clone.dphitcontentsmask = this.dphitcontentsmask;
101         clone.death_time = this.death_time;
102         clone.pain_finished = this.pain_finished;
103         SetResourceExplicit(clone, RES_HEALTH, GetResource(this, RES_HEALTH));
104         SetResourceExplicit(clone, RES_ARMOR, GetResource(this, RES_ARMOR));
105         clone.armortype = this.armortype;
106         clone.model = this.model;
107         clone.modelindex = this.modelindex;
108         clone.skin = this.skin;
109         clone.species = this.species;
110         clone.move_qcphysics = false; // don't run gamecode logic on clones, too many
111         set_movetype(clone, this.move_movetype);
112         clone.solid = this.solid;
113         clone.takedamage = this.takedamage;
114         setcefc(clone, getcefc(this));
115         clone.uncustomizeentityforclient = this.uncustomizeentityforclient;
116         clone.uncustomizeentityforclient_set = this.uncustomizeentityforclient_set;
117         if (keepvelocity == 1)
118                 clone.velocity = this.velocity;
119         clone.oldvelocity = clone.velocity;
120         clone.alpha = this.alpha;
121         clone.fade_time = this.fade_time;
122         clone.fade_rate = this.fade_rate;
123         //clone.weapon = this.weapon;
124         setorigin(clone, this.origin);
125         setsize(clone, this.mins, this.maxs);
126         clone.reset = SUB_Remove;
127         clone._ps = this._ps;
128
129         Drag_MoveDrag(this, clone);
130
131         if(clone.colormap <= maxclients && clone.colormap > 0)
132                 clone.colormap = 1024 + this.clientcolors;
133
134         CSQCMODEL_AUTOINIT(clone);
135         clone.CopyBody_nextthink = this.nextthink;
136         clone.CopyBody_think = getthink(this);
137         clone.nextthink = time;
138         setthink(clone, CopyBody_Think);
139         // "bake" the current animation frame for clones (they don't get clientside animation)
140         animdecide_load_if_needed(clone);
141         animdecide_setframes(clone, false, frame, frame1time, frame2, frame2time);
142
143         IL_PUSH(g_clones, clone);
144
145         MUTATOR_CALLHOOK(CopyBody, this, clone, keepvelocity);
146 }
147
148 void player_setupanimsformodel(entity this)
149 {
150         // load animation info
151         animdecide_load_if_needed(this);
152         animdecide_setstate(this, 0, false);
153 }
154
155 void player_anim(entity this)
156 {
157         int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
158         if(IS_DEAD(this)) {
159                 if (!deadbits) {
160                         // Decide on which death animation to use.
161                         if(random() < 0.5)
162                                 deadbits = ANIMSTATE_DEAD1;
163                         else
164                                 deadbits = ANIMSTATE_DEAD2;
165                 }
166         } else {
167                 // Clear a previous death animation.
168                 deadbits = 0;
169         }
170         int animbits = deadbits;
171         if(STAT(FROZEN, this))
172                 animbits |= ANIMSTATE_FROZEN;
173         if(this.move_movetype == MOVETYPE_FOLLOW)
174                 animbits |= ANIMSTATE_FOLLOW;
175         if(IS_DUCKED(this))
176                 animbits |= ANIMSTATE_DUCK;
177         animdecide_setstate(this, animbits, false);
178         animdecide_setimplicitstate(this, IS_ONGROUND(this));
179 }
180
181 void PlayerCorpseDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
182 {
183         float take, save;
184         vector v;
185         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
186
187         v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
188         take = v.x;
189         save = v.y;
190
191         if(sound_allowed(MSG_BROADCAST, attacker))
192         {
193                 if (save > 10)
194                         sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
195                 else if (take > 30)
196                         sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
197                 else if (take > 10)
198                         sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);
199         }
200
201         if (take > 50)
202                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
203         if (take > 100)
204                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
205
206         TakeResource(this, RES_ARMOR, save);
207         TakeResource(this, RES_HEALTH, take);
208         // pause regeneration for 5 seconds
209         this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
210
211         this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
212         this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
213         this.dmg_inflictor = inflictor;
214
215         if (GetResource(this, RES_HEALTH) <= -autocvar_sv_gibhealth && this.alpha >= 0)
216         {
217                 // don't use any animations as a gib
218                 this.frame = 0;
219                 // view just above the floor
220                 this.view_ofs = '0 0 4';
221
222                 Violence_GibSplash(this, 1, 1, attacker);
223                 this.alpha = -1;
224                 this.solid = SOLID_NOT; // restore later
225                 this.takedamage = DAMAGE_NO; // restore later
226                 if(this.damagedbycontents)
227                         IL_REMOVE(g_damagedbycontents, this);
228                 this.damagedbycontents = false;
229         }
230 }
231
232 void PlayerDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
233 {
234         vector v;
235         float dh = max(GetResource(this, RES_HEALTH), 0);
236         float da = max(GetResource(this, RES_ARMOR), 0);
237         float take = 0, save = 0;
238
239         if (damage)
240         {
241                 if(!DEATH_ISSPECIAL(deathtype))
242                 {
243                         damage *= Handicap_GetTotalHandicap(this);
244                         if (this != attacker && IS_PLAYER(attacker))
245                         {
246                                 damage /= Handicap_GetTotalHandicap(attacker);
247                         }
248                 }
249
250                 if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
251                         damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
252
253                 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
254                 {
255                         vector ear1, ear2;
256                         vector d;
257                         float f;
258                         ear1 = this.origin;
259                         ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
260                         ear2 = ear1;
261                         makevectors(this.angles);
262                         ear1 += v_right * -10;
263                         ear2 += v_right * +10;
264                         d = inflictor.origin - this.origin;
265                         if (d)
266                                 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
267                         else
268                                 f = 0;  // Assum ecenter.
269                         force = v_right * vlen(force);
270                         Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), this, attacker);
271                         Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), this, attacker);
272                         if(f > 0)
273                         {
274                                 hitloc = ear1;
275                                 force = force * -1;
276                         }
277                         else
278                         {
279                                 hitloc = ear2;
280                                 // force is already good
281                         }
282                 }
283                 else
284                         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
285
286                 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
287                 take = v.x;
288                 save = v.y;
289         }
290
291         if(attacker == this)
292         {
293                 // don't reset pushltime for self damage as it may be an attempt to
294                 // escape a lava pit or similar
295                 //this.pushltime = 0;
296                 this.istypefrag = 0;
297         }
298         else if(IS_PLAYER(attacker))
299         {
300                 this.pusher = attacker;
301                 this.pushltime = time + autocvar_g_maxpushtime;
302                 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
303         }
304         else if(time < this.pushltime)
305         {
306                 attacker = this.pusher;
307                 this.pushltime = max(this.pushltime, time + 0.6);
308         }
309         else
310         {
311                 this.pushltime = 0;
312                 this.istypefrag = 0;
313         }
314
315         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, this, force, take, save, deathtype, damage);
316         take = bound(0, M_ARGV(4, float), GetResource(this, RES_HEALTH));
317         save = bound(0, M_ARGV(5, float), GetResource(this, RES_ARMOR));
318         float excess = max(0, damage - take - save);
319
320         if(sound_allowed(MSG_BROADCAST, attacker))
321         {
322                 if (save > 10 && (dh - take) > 0) // don't play armor sound if the attack is fatal
323                         sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
324                 else if (take > 30)
325                         sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
326                 else if (take > 10)
327                         sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
328         }
329
330         if (take > 50)
331                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
332         if (take > 100)
333                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
334
335         if (time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
336         {
337                 if (!(this.flags & FL_GODMODE))
338                 {
339                         TakeResource(this, RES_ARMOR, save);
340                         TakeResource(this, RES_HEALTH, take);
341                         // pause regeneration for 5 seconds
342                         if(take)
343                                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
344
345                         if (time > this.pain_finished)          //Don't switch pain sequences like crazy
346                         {
347                                 this.pain_finished = time + 0.5;        //Supajoe
348
349                                 if(autocvar_sv_gentle < 1) {
350                                         if(this.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
351                                         {
352                                                 if (!this.animstate_override)
353                                                 {
354                                                         if (random() > 0.5)
355                                                                 animdecide_setaction(this, ANIMACTION_PAIN1, true);
356                                                         else
357                                                                 animdecide_setaction(this, ANIMACTION_PAIN2, true);
358                                                 }
359                                         }
360                                         float myhp = GetResource(this, RES_HEALTH);
361                                         if(myhp > 1)
362                                         if(myhp < 25 || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || take > 20 || attacker != this)
363                                         if(sound_allowed(MSG_BROADCAST, attacker))
364                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
365                                         {
366                                                 if(deathtype == DEATH_FALL.m_id)
367                                                         PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
368                                                 else if(myhp > 75)
369                                                         PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
370                                                 else if(myhp > 50)
371                                                         PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
372                                                 else if(myhp > 25)
373                                                         PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
374                                                 else
375                                                         PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
376                                         }
377                                 }
378                         }
379
380                         // throw off bot aim temporarily
381                         float shake;
382                         if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
383                         {
384                                 shake = damage * 5 / (bound(0,skill,100) + 1);
385                                 this.v_angle_x = this.v_angle.x + (random() * 2 - 1) * shake;
386                                 this.v_angle_y = this.v_angle.y + (random() * 2 - 1) * shake;
387                                 this.v_angle_x = bound(-90, this.v_angle.x, 90);
388                         }
389
390                         float realdmg = damage - excess;
391                         if (this != attacker && realdmg)
392                         if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
393                         {
394                                 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
395                                         GameRules_scoring_add(attacker, DMG, realdmg);
396                                 }
397                                 if (IS_PLAYER(this)) {
398                                         GameRules_scoring_add(this, DMGTAKEN, realdmg);
399                                 }
400                         }
401                 }
402                 else
403                         this.max_armorvalue += (save + take);
404         }
405         this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
406         this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
407         this.dmg_inflictor = inflictor;
408
409         bool abot = (IS_BOT_CLIENT(attacker));
410         bool vbot = (IS_BOT_CLIENT(this));
411
412         bool valid_damage_for_weaponstats = false;
413         Weapon awep = WEP_Null;
414
415         if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
416         if(vbot || IS_REAL_CLIENT(this))
417         if(abot || IS_REAL_CLIENT(attacker))
418         if(attacker && this != attacker)
419         if(DIFF_TEAM(this, attacker))
420         {
421                 if(DEATH_ISSPECIAL(deathtype))
422                         awep = attacker.(weaponentity).m_weapon;
423                 else
424                         awep = DEATH_WEAPONOF(deathtype);
425                 valid_damage_for_weaponstats = true;
426         }
427
428         dh = dh - max(GetResource(this, RES_HEALTH), 0);
429         da = da - max(GetResource(this, RES_ARMOR), 0);
430         if(valid_damage_for_weaponstats)
431         {
432                 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
433         }
434
435         MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
436
437         if (GetResource(this, RES_HEALTH) < 1)
438         {
439                 float defer_ClientKill_Now_TeamChange;
440                 defer_ClientKill_Now_TeamChange = false;
441
442                 if(this.alivetime)
443                 {
444                         PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
445                         this.alivetime = 0;
446                 }
447
448                 if(valid_damage_for_weaponstats)
449                         WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
450
451                 if(autocvar_sv_gentle < 1)
452                 if(sound_allowed(MSG_BROADCAST, attacker))
453                 {
454                         if(deathtype == DEATH_DROWN.m_id)
455                                 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
456                         else
457                                 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
458                 }
459
460                 // get rid of kill indicator
461                 if(this.killindicator)
462                 {
463                         delete(this.killindicator);
464                         this.killindicator = NULL;
465                         if(this.killindicator_teamchange)
466                                 defer_ClientKill_Now_TeamChange = true;
467
468                         if(this.classname == "body")
469                         if(deathtype == DEATH_KILL.m_id)
470                         {
471                                 // for the lemmings fans, a small harmless explosion
472                                 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
473                         }
474                 }
475
476                 // print an obituary message
477                 if(this.classname != "body")
478                         Obituary(attacker, inflictor, this, deathtype, weaponentity);
479
480                 // increment frag counter for used weapon type
481                 Weapon w = DEATH_WEAPONOF(deathtype);
482                 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
483                         CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
484
485                 this.respawn_time = 0;
486                 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
487                 damage = M_ARGV(4, float);
488                 excess = max(0, damage - take - save);
489
490                 //Weapon wep = this.(weaponentity).m_weapon;
491                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
492                 {
493                         .entity went = weaponentities[slot];
494                         if(!this.(went))
495                                 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
496                         Weapon wep = this.(went).m_weapon;
497                         wep.wr_playerdeath(wep, this, went);
498                 }
499
500                 RemoveGrapplingHooks(this);
501
502                 Portal_ClearAllLater(this);
503
504                 this.fixangle = true;
505
506                 if(defer_ClientKill_Now_TeamChange)
507                         ClientKill_Now_TeamChange(this); // can turn player into spectator
508
509                 // player could have been miraculously resuscitated ;)
510                 // e.g. players in freezetag get frozen, they don't really die
511                 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
512                         return;
513
514                 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
515                         calculate_player_respawn_time(this);
516
517                 // when we get here, player actually dies
518
519                 Unfreeze(this, false); // remove any icy remains
520
521                 // clear waypoints
522                 WaypointSprite_PlayerDead(this);
523                 // throw a weapon
524                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
525                 {
526                         .entity went = weaponentities[slot];
527                         SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
528                 }
529
530                 // become fully visible
531                 this.alpha = default_player_alpha;
532                 // make the corpse upright (not tilted)
533                 this.angles_x = 0;
534                 this.angles_z = 0;
535                 // don't spin
536                 this.avelocity = '0 0 0';
537                 // view from the floor
538                 this.view_ofs = '0 0 -8';
539                 if(this.move_movetype == MOVETYPE_NOCLIP)
540                 {
541                         // don't toss the corpse in this case, it can get stuck in solid (causing low fps)
542                         // or fall indefinitely into the void if out of the map
543                         this.velocity = '0 0 0';
544                 }
545                 else
546                 {
547                         // toss the corpse
548                         set_movetype(this, MOVETYPE_TOSS);
549                 }
550                 // shootable corpse
551                 this.solid = SOLID_CORPSE;
552                 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
553                 // don't stick to the floor
554                 UNSET_ONGROUND(this);
555                 // dying animation
556                 this.deadflag = DEAD_DYING;
557
558                 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
559
560                 this.death_time = time;
561                 if (random() < 0.5)
562                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
563                 else
564                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
565
566                 // set damage function to corpse damage
567                 this.event_damage = PlayerCorpseDamage;
568                 this.event_heal = func_null;
569                 // call the corpse damage function just in case it wants to gib
570                 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
571
572                 // set up to fade out later
573                 SUB_SetFade (this, time + 6 + random (), 1);
574                 // reset body think wrapper broken by SUB_SetFade
575                 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
576                         this.CopyBody_think = getthink(this);
577                         this.CopyBody_nextthink = this.nextthink;
578                         setthink(this, CopyBody_Think);
579                         this.nextthink = time;
580                 }
581
582                 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
583                         // remove corpse
584                         // clones don't run any animation code any more, so we must gib them when they die :(
585                         this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
586                 }
587
588                 // reset fields the weapons may use just in case
589                 if(this.classname != "body")
590                 {
591                         FOREACH(Weapons, it != WEP_Null,
592                         {
593                                 it.wr_resetplayer(it, this);
594                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
595                                 {
596                                         ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
597                                 }
598                         });
599                 }
600                 MUTATOR_CALLHOOK(PlayerDied, this);
601         }
602 }
603
604 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
605 {
606         if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
607                 return false;
608
609         GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);
610         return true;
611 }
612
613 void precache_playermodel(string m)
614 {
615         int globhandle, i, n;
616         string f;
617
618         // remove :<skinnumber> suffix
619         int j = strstrofs(m, ":", 0);
620         if(j >= 0)
621                 m = substring(m, 0, j);
622
623         if(substring(m, -9, 5) == "_lod1")
624                 return;
625         if(substring(m, -9, 5) == "_lod2")
626                 return;
627         precache_model(m);
628         f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
629         if(fexists(f))
630                 precache_model(f);
631         f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
632         if(fexists(f))
633                 precache_model(f);
634
635         globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
636         if (globhandle < 0)
637                 return;
638         n = search_getsize(globhandle);
639         for (i = 0; i < n; ++i)
640         {
641                 //print(search_getfilename(globhandle, i), "\n");
642                 f = search_getfilename(globhandle, i);
643                 PrecachePlayerSounds(f);
644         }
645         search_end(globhandle);
646 }
647 void precache_all_playermodels(string pattern)
648 {
649         int globhandle = search_begin(pattern, true, false);
650         if (globhandle < 0) return;
651         int n = search_getsize(globhandle);
652         for (int i = 0; i < n; ++i)
653         {
654                 string s = search_getfilename(globhandle, i);
655                 precache_playermodel(s);
656         }
657         search_end(globhandle);
658 }
659
660 void precache_playermodels(string s)
661 {
662         FOREACH_WORD(s, true, { precache_playermodel(it); });
663 }
664
665 PRECACHE(PlayerModels)
666 {
667     // Precache all player models if desired
668     if (autocvar_sv_precacheplayermodels)
669     {
670         PrecachePlayerSounds("sound/player/default.sounds");
671         precache_all_playermodels("models/player/*.zym");
672         precache_all_playermodels("models/player/*.dpm");
673         precache_all_playermodels("models/player/*.md3");
674         precache_all_playermodels("models/player/*.psk");
675         precache_all_playermodels("models/player/*.iqm");
676     }
677
678     if (autocvar_sv_defaultcharacter)
679     {
680                 precache_playermodels(autocvar_sv_defaultplayermodel_red);
681                 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
682                 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
683                 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
684                 precache_playermodels(autocvar_sv_defaultplayermodel);
685     }
686 }