]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/player.qc
Merge branch 'Mario/less_stats' into 'master'
[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 - bound(0, autocvar_g_spawnshield_blockdamage, 1);
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                 else
392                         this.max_armorvalue += (save + take);
393         }
394         this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
395         this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
396         this.dmg_inflictor = inflictor;
397
398         bool abot = (IS_BOT_CLIENT(attacker));
399         bool vbot = (IS_BOT_CLIENT(this));
400
401         bool valid_damage_for_weaponstats = false;
402         Weapon awep = WEP_Null;
403
404         if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
405         if(vbot || IS_REAL_CLIENT(this))
406         if(abot || IS_REAL_CLIENT(attacker))
407         if(attacker && this != attacker)
408         if (DIFF_TEAM(this, attacker) && (!STAT(FROZEN, this) || this.freeze_time > time))
409         {
410                 if(DEATH_ISSPECIAL(deathtype))
411                         awep = attacker.(weaponentity).m_weapon;
412                 else
413                         awep = DEATH_WEAPONOF(deathtype);
414                 valid_damage_for_weaponstats = true;
415         }
416
417         float dh = initial_health - max(GetResource(this, RES_HEALTH), 0); // health difference
418         float da = initial_armor - max(GetResource(this, RES_ARMOR), 0); // armor difference
419         if(valid_damage_for_weaponstats)
420         {
421                 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
422         }
423
424         bool forbid_logging_damage = MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
425
426         if ((dh || da) && !forbid_logging_damage)
427         {
428                 float realdmg = damage - excess;
429                 if ((this != attacker || deathtype == DEATH_KILL.m_id) && realdmg && !STAT(FROZEN, this)
430                         && (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime))
431                 {
432                         if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this) && deathtype != DEATH_KILL.m_id)
433                                 GameRules_scoring_add(attacker, DMG, realdmg);
434                         if (IS_PLAYER(this))
435                                 GameRules_scoring_add(this, DMGTAKEN, realdmg);
436                 }
437         }
438
439         if (GetResource(this, RES_HEALTH) < 1)
440         {
441                 bool defer_ClientKill_Now_TeamChange = false;
442
443                 if(this.alivetime)
444                 {
445                         PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
446                         this.alivetime = 0;
447                 }
448
449                 if(valid_damage_for_weaponstats)
450                         WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
451
452                 if(autocvar_sv_gentle < 1)
453                 if(sound_allowed(MSG_BROADCAST, attacker))
454                 {
455                         if(deathtype == DEATH_DROWN.m_id)
456                                 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
457                         else
458                                 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
459                 }
460
461                 // get rid of kill indicator
462                 if(this.killindicator)
463                 {
464                         delete(this.killindicator);
465                         this.killindicator = NULL;
466                         if(this.killindicator_teamchange)
467                                 defer_ClientKill_Now_TeamChange = true;
468
469                         if(this.classname == "body")
470                         if(deathtype == DEATH_KILL.m_id)
471                         {
472                                 // for the lemmings fans, a small harmless explosion
473                                 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
474                         }
475                 }
476
477                 // print an obituary message
478                 if(this.classname != "body")
479                         Obituary(attacker, inflictor, this, deathtype, weaponentity);
480
481                 // increment frag counter for used weapon type
482                 Weapon w = DEATH_WEAPONOF(deathtype);
483                 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
484                         CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
485
486                 this.respawn_time = 0;
487                 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
488                 damage = M_ARGV(4, float);
489                 excess = max(0, damage - take - save);
490
491                 //Weapon wep = this.(weaponentity).m_weapon;
492                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
493                 {
494                         .entity went = weaponentities[slot];
495                         if(!this.(went))
496                                 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
497                         Weapon wep = this.(went).m_weapon;
498                         wep.wr_playerdeath(wep, this, went);
499                 }
500
501                 RemoveGrapplingHooks(this);
502
503                 Portal_ClearAllLater(this);
504
505                 this.fixangle = true;
506
507                 if(defer_ClientKill_Now_TeamChange)
508                         ClientKill_Now_TeamChange(this); // can turn player into spectator
509
510                 // player could have been miraculously resuscitated ;)
511                 // e.g. players in freezetag get frozen, they don't really die
512                 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
513                         return;
514
515                 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
516                         calculate_player_respawn_time(this);
517
518                 // when we get here, player actually dies
519
520                 Unfreeze(this, false); // remove any icy remains
521
522                 // clear waypoints
523                 WaypointSprite_PlayerDead(this);
524                 // throw a weapon
525                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
526                 {
527                         .entity went = weaponentities[slot];
528                         SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
529                 }
530
531                 // become fully visible
532                 this.alpha = default_player_alpha;
533                 // make the corpse upright (not tilted)
534                 this.angles_x = 0;
535                 this.angles_z = 0;
536                 // don't spin
537                 this.avelocity = '0 0 0';
538                 // view from the floor
539                 this.view_ofs = '0 0 -8';
540                 if(this.move_movetype == MOVETYPE_NOCLIP)
541                 {
542                         // don't toss the corpse in this case, it can get stuck in solid (causing low fps)
543                         // or fall indefinitely into the void if out of the map
544                         this.velocity = '0 0 0';
545                 }
546                 else
547                 {
548                         // toss the corpse
549                         set_movetype(this, MOVETYPE_TOSS);
550                 }
551                 // shootable corpse
552                 this.solid = SOLID_CORPSE;
553                 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
554                 // don't stick to the floor
555                 UNSET_ONGROUND(this);
556                 // dying animation
557                 this.deadflag = DEAD_DYING;
558                 // don't play teleportation sounds
559                 this.teleportable = TELEPORT_SIMPLE;
560
561                 STAT(STRENGTH_FINISHED, this) = 0;
562                 STAT(INVINCIBLE_FINISHED, this) = 0;
563                 STAT(SUPERWEAPONS_FINISHED, this) = 0;
564                 STAT(AIR_FINISHED, this) = 0;
565
566                 this.death_time = time;
567                 if (random() < 0.5)
568                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
569                 else
570                         animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
571
572                 // set damage function to corpse damage
573                 this.event_damage = PlayerCorpseDamage;
574                 this.event_heal = func_null;
575                 // call the corpse damage function just in case it wants to gib
576                 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
577
578                 // set up to fade out later
579                 SUB_SetFade (this, time + 6 + random (), 1);
580                 // reset body think wrapper broken by SUB_SetFade
581                 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
582                         this.CopyBody_think = getthink(this);
583                         this.CopyBody_nextthink = this.nextthink;
584                         setthink(this, CopyBody_Think);
585                         this.nextthink = time;
586                 }
587
588                 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
589                         // remove corpse
590                         // clones don't run any animation code any more, so we must gib them when they die :(
591                         this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
592                 }
593
594                 // reset fields the weapons may use just in case
595                 if(this.classname != "body")
596                 {
597                         FOREACH(Weapons, it != WEP_Null,
598                         {
599                                 it.wr_resetplayer(it, this);
600                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
601                                 {
602                                         ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
603                                 }
604                         });
605                 }
606                 MUTATOR_CALLHOOK(PlayerDied, this);
607         }
608 }
609
610 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
611 {
612         if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
613                 return false;
614
615         GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);
616         return true;
617 }
618
619 void precache_playermodel(string m)
620 {
621         int globhandle, i, n;
622         string f;
623
624         // remove :<skinnumber> suffix
625         int j = strstrofs(m, ":", 0);
626         if(j >= 0)
627                 m = substring(m, 0, j);
628
629         if(substring(m, -9, 5) == "_lod1")
630                 return;
631         if(substring(m, -9, 5) == "_lod2")
632                 return;
633         precache_model(m);
634         f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
635         if(fexists(f))
636                 precache_model(f);
637         f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
638         if(fexists(f))
639                 precache_model(f);
640
641         globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
642         if (globhandle < 0)
643                 return;
644         n = search_getsize(globhandle);
645         for (i = 0; i < n; ++i)
646         {
647                 //print(search_getfilename(globhandle, i), "\n");
648                 f = search_getfilename(globhandle, i);
649                 PrecachePlayerSounds(f);
650         }
651         search_end(globhandle);
652 }
653 void precache_all_playermodels(string pattern)
654 {
655         int globhandle = search_begin(pattern, true, false);
656         if (globhandle < 0) return;
657         int n = search_getsize(globhandle);
658         for (int i = 0; i < n; ++i)
659         {
660                 string s = search_getfilename(globhandle, i);
661                 precache_playermodel(s);
662         }
663         search_end(globhandle);
664 }
665
666 void precache_playermodels(string s)
667 {
668         FOREACH_WORD(s, true, { precache_playermodel(it); });
669 }
670
671 PRECACHE(PlayerModels)
672 {
673     // Precache all player models if desired
674     if (autocvar_sv_precacheplayermodels)
675     {
676         PrecachePlayerSounds("sound/player/default.sounds");
677         precache_all_playermodels("models/player/*.zym");
678         precache_all_playermodels("models/player/*.dpm");
679         precache_all_playermodels("models/player/*.md3");
680         precache_all_playermodels("models/player/*.psk");
681         precache_all_playermodels("models/player/*.iqm");
682     }
683
684     if (autocvar_sv_defaultcharacter)
685     {
686                 precache_playermodels(autocvar_sv_defaultplayermodel_red);
687                 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
688                 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
689                 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
690                 precache_playermodels(autocvar_sv_defaultplayermodel);
691     }
692 }