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