]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/player.qc
1be6b62a3f2b8903775047ab2af91d2fdb758d2e
[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 (STAT(FROZEN, this))
251                 {
252                         if (!ITEM_DAMAGE_NEEDKILL(deathtype))
253                                 damage = 0;
254                 }
255                 else if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
256                         damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
257
258                 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
259                 {
260                         vector ear1, ear2;
261                         vector d;
262                         float f;
263                         ear1 = this.origin;
264                         ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
265                         ear2 = ear1;
266                         makevectors(this.angles);
267                         ear1 += v_right * -10;
268                         ear2 += v_right * +10;
269                         d = inflictor.origin - this.origin;
270                         if (d)
271                                 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
272                         else
273                                 f = 0;  // Assum ecenter.
274                         force = v_right * vlen(force);
275                         Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), this, attacker);
276                         Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), this, attacker);
277                         if(f > 0)
278                         {
279                                 hitloc = ear1;
280                                 force = force * -1;
281                         }
282                         else
283                         {
284                                 hitloc = ear2;
285                                 // force is already good
286                         }
287                 }
288                 else
289                         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
290
291                 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
292                 take = v.x;
293                 save = v.y;
294         }
295
296         if(attacker == this)
297         {
298                 // don't reset pushltime for self damage as it may be an attempt to
299                 // escape a lava pit or similar
300                 //this.pushltime = 0;
301                 this.istypefrag = 0;
302         }
303         else if(IS_PLAYER(attacker))
304         {
305                 this.pusher = attacker;
306                 this.pushltime = time + autocvar_g_maxpushtime;
307                 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
308         }
309         else if(time < this.pushltime)
310         {
311                 attacker = this.pusher;
312                 this.pushltime = max(this.pushltime, time + 0.6);
313         }
314         else
315         {
316                 this.pushltime = 0;
317                 this.istypefrag = 0;
318         }
319
320         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, this, force, take, save, deathtype, damage);
321         take = bound(0, M_ARGV(4, float), GetResource(this, RES_HEALTH));
322         save = bound(0, M_ARGV(5, float), GetResource(this, RES_ARMOR));
323         float excess = max(0, damage - take - save);
324
325         if(sound_allowed(MSG_BROADCAST, attacker))
326         {
327                 if (save > 10 && (dh - take) > 0) // don't play armor sound if the attack is fatal
328                         sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
329                 else if (take > 30)
330                         sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
331                 else if (take > 10)
332                         sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
333         }
334
335         if (take > 50)
336                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
337         if (take > 100)
338                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
339
340         if (time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
341         {
342                 if (!(this.flags & FL_GODMODE))
343                 {
344                         TakeResource(this, RES_ARMOR, save);
345                         TakeResource(this, RES_HEALTH, take);
346                         // pause regeneration for 5 seconds
347                         if(take)
348                                 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
349
350                         if (time > this.pain_finished && !STAT(FROZEN, this)) // Don't switch pain sequences like crazy
351                         {
352                                 this.pain_finished = time + 0.5;        //Supajoe
353
354                                 if(autocvar_sv_gentle < 1) {
355                                         if(this.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
356                                         {
357                                                 if (!this.animstate_override)
358                                                 {
359                                                         if (random() > 0.5)
360                                                                 animdecide_setaction(this, ANIMACTION_PAIN1, true);
361                                                         else
362                                                                 animdecide_setaction(this, ANIMACTION_PAIN2, true);
363                                                 }
364                                         }
365                                         float myhp = GetResource(this, RES_HEALTH);
366                                         if(myhp > 1)
367                                         if(myhp < 25 || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || take > 20 || attacker != this)
368                                         if(sound_allowed(MSG_BROADCAST, attacker))
369                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
370                                         {
371                                                 if(deathtype == DEATH_FALL.m_id)
372                                                         PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
373                                                 else if(myhp > 75)
374                                                         PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
375                                                 else if(myhp > 50)
376                                                         PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
377                                                 else if(myhp > 25)
378                                                         PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
379                                                 else
380                                                         PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
381                                         }
382                                 }
383                         }
384
385                         // throw off bot aim temporarily
386                         float shake;
387                         if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
388                         {
389                                 shake = damage * 5 / (bound(0,skill,100) + 1);
390                                 this.v_angle_x = this.v_angle.x + (random() * 2 - 1) * shake;
391                                 this.v_angle_y = this.v_angle.y + (random() * 2 - 1) * shake;
392                                 this.v_angle_x = bound(-90, this.v_angle.x, 90);
393                         }
394
395                         float realdmg = damage - excess;
396                         if (this != attacker && realdmg && !STAT(FROZEN, this)
397                                 && (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime))
398                         {
399                                 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
400                                         GameRules_scoring_add(attacker, DMG, realdmg);
401                                 }
402                                 if (IS_PLAYER(this)) {
403                                         GameRules_scoring_add(this, DMGTAKEN, realdmg);
404                                 }
405                         }
406                 }
407                 else
408                         this.max_armorvalue += (save + take);
409         }
410         this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
411         this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
412         this.dmg_inflictor = inflictor;
413
414         bool abot = (IS_BOT_CLIENT(attacker));
415         bool vbot = (IS_BOT_CLIENT(this));
416
417         bool valid_damage_for_weaponstats = false;
418         Weapon awep = WEP_Null;
419
420         if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
421         if(vbot || IS_REAL_CLIENT(this))
422         if(abot || IS_REAL_CLIENT(attacker))
423         if(attacker && this != attacker)
424         if (DIFF_TEAM(this, attacker) && (!STAT(FROZEN, this) || this.freeze_time > time))
425         {
426                 if(DEATH_ISSPECIAL(deathtype))
427                         awep = attacker.(weaponentity).m_weapon;
428                 else
429                         awep = DEATH_WEAPONOF(deathtype);
430                 valid_damage_for_weaponstats = true;
431         }
432
433         dh -= max(GetResource(this, RES_HEALTH), 0); // health difference
434         da -= max(GetResource(this, RES_ARMOR), 0); // armor difference
435         if(valid_damage_for_weaponstats)
436         {
437                 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
438         }
439
440         MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
441
442         if (GetResource(this, RES_HEALTH) < 1)
443         {
444                 float defer_ClientKill_Now_TeamChange;
445                 defer_ClientKill_Now_TeamChange = false;
446
447                 if(this.alivetime)
448                 {
449                         PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
450                         this.alivetime = 0;
451                 }
452
453                 if(valid_damage_for_weaponstats)
454                         WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
455
456                 if(autocvar_sv_gentle < 1)
457                 if(sound_allowed(MSG_BROADCAST, attacker))
458                 {
459                         if(deathtype == DEATH_DROWN.m_id)
460                                 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
461                         else
462                                 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
463                 }
464
465                 // get rid of kill indicator
466                 if(this.killindicator)
467                 {
468                         delete(this.killindicator);
469                         this.killindicator = NULL;
470                         if(this.killindicator_teamchange)
471                                 defer_ClientKill_Now_TeamChange = true;
472
473                         if(this.classname == "body")
474                         if(deathtype == DEATH_KILL.m_id)
475                         {
476                                 // for the lemmings fans, a small harmless explosion
477                                 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
478                         }
479                 }
480
481                 // print an obituary message
482                 if(this.classname != "body")
483                         Obituary(attacker, inflictor, this, deathtype, weaponentity);
484
485                 // increment frag counter for used weapon type
486                 Weapon w = DEATH_WEAPONOF(deathtype);
487                 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
488                         CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
489
490                 this.respawn_time = 0;
491                 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
492                 damage = M_ARGV(4, float);
493                 excess = max(0, damage - take - save);
494
495                 //Weapon wep = this.(weaponentity).m_weapon;
496                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
497                 {
498                         .entity went = weaponentities[slot];
499                         if(!this.(went))
500                                 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
501                         Weapon wep = this.(went).m_weapon;
502                         wep.wr_playerdeath(wep, this, went);
503                 }
504
505                 RemoveGrapplingHooks(this);
506
507                 Portal_ClearAllLater(this);
508
509                 this.fixangle = true;
510
511                 if(defer_ClientKill_Now_TeamChange)
512                         ClientKill_Now_TeamChange(this); // can turn player into spectator
513
514                 // player could have been miraculously resuscitated ;)
515                 // e.g. players in freezetag get frozen, they don't really die
516                 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
517                         return;
518
519                 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
520                         calculate_player_respawn_time(this);
521
522                 // when we get here, player actually dies
523
524                 Unfreeze(this, false); // remove any icy remains
525
526                 // clear waypoints
527                 WaypointSprite_PlayerDead(this);
528                 // throw a weapon
529                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
530                 {
531                         .entity went = weaponentities[slot];
532                         SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
533                 }
534
535                 // become fully visible
536                 this.alpha = default_player_alpha;
537                 // make the corpse upright (not tilted)
538                 this.angles_x = 0;
539                 this.angles_z = 0;
540                 // don't spin
541                 this.avelocity = '0 0 0';
542                 // view from the floor
543                 this.view_ofs = '0 0 -8';
544                 if(this.move_movetype == MOVETYPE_NOCLIP)
545                 {
546                         // don't toss the corpse in this case, it can get stuck in solid (causing low fps)
547                         // or fall indefinitely into the void if out of the map
548                         this.velocity = '0 0 0';
549                 }
550                 else
551                 {
552                         // toss the corpse
553                         set_movetype(this, MOVETYPE_TOSS);
554                 }
555                 // shootable corpse
556                 this.solid = SOLID_CORPSE;
557                 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
558                 // don't stick to the floor
559                 UNSET_ONGROUND(this);
560                 // dying animation
561                 this.deadflag = DEAD_DYING;
562
563                 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
564
565                 this.death_time = time;
566                 if (random() < 0.5)
567                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
568                 else
569                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
570
571                 // set damage function to corpse damage
572                 this.event_damage = PlayerCorpseDamage;
573                 this.event_heal = func_null;
574                 // call the corpse damage function just in case it wants to gib
575                 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
576
577                 // set up to fade out later
578                 SUB_SetFade (this, time + 6 + random (), 1);
579                 // reset body think wrapper broken by SUB_SetFade
580                 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
581                         this.CopyBody_think = getthink(this);
582                         this.CopyBody_nextthink = this.nextthink;
583                         setthink(this, CopyBody_Think);
584                         this.nextthink = time;
585                 }
586
587                 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
588                         // remove corpse
589                         // clones don't run any animation code any more, so we must gib them when they die :(
590                         this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
591                 }
592
593                 // reset fields the weapons may use just in case
594                 if(this.classname != "body")
595                 {
596                         FOREACH(Weapons, it != WEP_Null,
597                         {
598                                 it.wr_resetplayer(it, this);
599                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
600                                 {
601                                         ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
602                                 }
603                         });
604                 }
605                 MUTATOR_CALLHOOK(PlayerDied, this);
606         }
607 }
608
609 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
610 {
611         if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
612                 return false;
613
614         GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);
615         return true;
616 }
617
618 void precache_playermodel(string m)
619 {
620         int globhandle, i, n;
621         string f;
622
623         // remove :<skinnumber> suffix
624         int j = strstrofs(m, ":", 0);
625         if(j >= 0)
626                 m = substring(m, 0, j);
627
628         if(substring(m, -9, 5) == "_lod1")
629                 return;
630         if(substring(m, -9, 5) == "_lod2")
631                 return;
632         precache_model(m);
633         f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
634         if(fexists(f))
635                 precache_model(f);
636         f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
637         if(fexists(f))
638                 precache_model(f);
639
640         globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
641         if (globhandle < 0)
642                 return;
643         n = search_getsize(globhandle);
644         for (i = 0; i < n; ++i)
645         {
646                 //print(search_getfilename(globhandle, i), "\n");
647                 f = search_getfilename(globhandle, i);
648                 PrecachePlayerSounds(f);
649         }
650         search_end(globhandle);
651 }
652 void precache_all_playermodels(string pattern)
653 {
654         int globhandle = search_begin(pattern, true, false);
655         if (globhandle < 0) return;
656         int n = search_getsize(globhandle);
657         for (int i = 0; i < n; ++i)
658         {
659                 string s = search_getfilename(globhandle, i);
660                 precache_playermodel(s);
661         }
662         search_end(globhandle);
663 }
664
665 void precache_playermodels(string s)
666 {
667         FOREACH_WORD(s, true, { precache_playermodel(it); });
668 }
669
670 PRECACHE(PlayerModels)
671 {
672     // Precache all player models if desired
673     if (autocvar_sv_precacheplayermodels)
674     {
675         PrecachePlayerSounds("sound/player/default.sounds");
676         precache_all_playermodels("models/player/*.zym");
677         precache_all_playermodels("models/player/*.dpm");
678         precache_all_playermodels("models/player/*.md3");
679         precache_all_playermodels("models/player/*.psk");
680         precache_all_playermodels("models/player/*.iqm");
681     }
682
683     if (autocvar_sv_defaultcharacter)
684     {
685                 precache_playermodels(autocvar_sv_defaultplayermodel_red);
686                 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
687                 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
688                 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
689                 precache_playermodels(autocvar_sv_defaultplayermodel);
690     }
691 }