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