3 #include <common/effects/all.qh>
7 #include "clientkill.qh"
9 #include <server/mutators/_mod.qh>
11 #include "handicap.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"
30 #include "../common/minigames/sv_minigames.qh"
32 #include <common/gamemodes/_mod.qh>
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"
40 #include "weapons/weaponstats.qh"
41 #include <server/weapons/weaponsystem.qh>
43 #include "../common/animdecide.qh"
45 void Drop_Special_Items(entity player)
47 // called when the player has become stuck or frozen
48 // so objective items aren't stuck with the player
50 MUTATOR_CALLHOOK(DropSpecialItems, player);
53 void CopyBody_Think(entity this)
55 if(this.CopyBody_nextthink && time > this.CopyBody_nextthink)
57 this.CopyBody_think(this);
60 this.CopyBody_nextthink = this.nextthink;
61 this.CopyBody_think = getthink(this);
62 setthink(this, CopyBody_Think);
64 CSQCMODEL_AUTOUPDATE(this);
65 this.nextthink = time;
67 void CopyBody(entity this, float keepvelocity)
69 if (this.effects & EF_NODRAW)
71 entity clone = new(body);
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;
129 Drag_MoveDrag(this, clone);
131 if(clone.colormap <= maxclients && clone.colormap > 0)
132 clone.colormap = 1024 + this.clientcolors;
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);
143 IL_PUSH(g_clones, clone);
145 MUTATOR_CALLHOOK(CopyBody, this, clone, keepvelocity);
148 void player_setupanimsformodel(entity this)
150 // load animation info
151 animdecide_load_if_needed(this);
152 animdecide_setstate(this, 0, false);
155 void player_anim(entity this)
157 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
160 // Decide on which death animation to use.
162 deadbits = ANIMSTATE_DEAD1;
164 deadbits = ANIMSTATE_DEAD2;
167 // Clear a previous death animation.
170 int animbits = deadbits;
171 if(STAT(FROZEN, this))
172 animbits |= ANIMSTATE_FROZEN;
173 if(this.move_movetype == MOVETYPE_FOLLOW)
174 animbits |= ANIMSTATE_FOLLOW;
176 animbits |= ANIMSTATE_DUCK;
177 animdecide_setstate(this, animbits, false);
178 animdecide_setimplicitstate(this, IS_ONGROUND(this));
181 void PlayerCorpseDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
185 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
187 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
191 if(sound_allowed(MSG_BROADCAST, attacker))
194 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
196 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
198 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);
202 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
204 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
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);
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;
215 if (GetResource(this, RES_HEALTH) <= -autocvar_sv_gibhealth && this.alpha >= 0)
217 // don't use any animations as a gib
219 // view just above the floor
220 this.view_ofs = '0 0 4';
222 Violence_GibSplash(this, 1, 1, attacker);
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;
232 void PlayerDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
235 float dh = max(GetResource(this, RES_HEALTH), 0);
236 float da = max(GetResource(this, RES_ARMOR), 0);
237 float take = 0, save = 0;
241 if(!DEATH_ISSPECIAL(deathtype))
243 damage *= Handicap_GetTotalHandicap(this);
244 if (this != attacker && IS_PLAYER(attacker))
246 damage /= Handicap_GetTotalHandicap(attacker);
250 if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
251 damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
253 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
259 ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
261 makevectors(this.angles);
262 ear1 += v_right * -10;
263 ear2 += v_right * +10;
264 d = inflictor.origin - this.origin;
266 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
268 f = 0; // Assum ecenter.
269 force = v_right * vlen(force);
270 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), this, attacker);
271 Violence_GibSplash_At(ear2, force, 2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), this, attacker);
280 // force is already good
284 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
286 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
293 // don't reset pushltime for self damage as it may be an attempt to
294 // escape a lava pit or similar
295 //this.pushltime = 0;
298 else if(IS_PLAYER(attacker))
300 this.pusher = attacker;
301 this.pushltime = time + autocvar_g_maxpushtime;
302 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
304 else if(time < this.pushltime)
306 attacker = this.pusher;
307 this.pushltime = max(this.pushltime, time + 0.6);
315 MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, this, force, take, save, deathtype, damage);
316 take = bound(0, M_ARGV(4, float), GetResource(this, RES_HEALTH));
317 save = bound(0, M_ARGV(5, float), GetResource(this, RES_ARMOR));
318 float excess = max(0, damage - take - save);
320 if(sound_allowed(MSG_BROADCAST, attacker))
322 if (save > 10 && (dh - take) > 0) // don't play armor sound if the attack is fatal
323 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
325 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
327 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
331 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
333 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
335 if (time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
337 if (!(this.flags & FL_GODMODE))
339 TakeResource(this, RES_ARMOR, save);
340 TakeResource(this, RES_HEALTH, take);
341 // pause regeneration for 5 seconds
343 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
345 if (time > this.pain_finished) //Don't switch pain sequences like crazy
347 this.pain_finished = time + 0.5; //Supajoe
349 if(autocvar_sv_gentle < 1) {
350 if(this.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
352 if (!this.animstate_override)
355 animdecide_setaction(this, ANIMACTION_PAIN1, true);
357 animdecide_setaction(this, ANIMACTION_PAIN2, true);
360 float myhp = GetResource(this, RES_HEALTH);
362 if(myhp < 25 || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || take > 20 || attacker != this)
363 if(sound_allowed(MSG_BROADCAST, attacker))
364 // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
366 if(deathtype == DEATH_FALL.m_id)
367 PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
369 PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
371 PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
373 PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
375 PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
380 // throw off bot aim temporarily
382 if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
384 shake = damage * 5 / (bound(0,skill,100) + 1);
385 this.v_angle_x = this.v_angle.x + (random() * 2 - 1) * shake;
386 this.v_angle_y = this.v_angle.y + (random() * 2 - 1) * shake;
387 this.v_angle_x = bound(-90, this.v_angle.x, 90);
390 float realdmg = damage - excess;
391 if (this != attacker && realdmg)
392 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
394 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
395 GameRules_scoring_add(attacker, DMG, realdmg);
397 if (IS_PLAYER(this)) {
398 GameRules_scoring_add(this, DMGTAKEN, realdmg);
403 this.max_armorvalue += (save + take);
405 this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
406 this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
407 this.dmg_inflictor = inflictor;
409 bool abot = (IS_BOT_CLIENT(attacker));
410 bool vbot = (IS_BOT_CLIENT(this));
412 bool valid_damage_for_weaponstats = false;
413 Weapon awep = WEP_Null;
415 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
416 if(vbot || IS_REAL_CLIENT(this))
417 if(abot || IS_REAL_CLIENT(attacker))
418 if(attacker && this != attacker)
419 if(DIFF_TEAM(this, attacker))
421 if(DEATH_ISSPECIAL(deathtype))
422 awep = attacker.(weaponentity).m_weapon;
424 awep = DEATH_WEAPONOF(deathtype);
425 valid_damage_for_weaponstats = true;
428 dh = dh - max(GetResource(this, RES_HEALTH), 0);
429 da = da - max(GetResource(this, RES_ARMOR), 0);
430 if(valid_damage_for_weaponstats)
432 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
435 MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
437 if (GetResource(this, RES_HEALTH) < 1)
439 float defer_ClientKill_Now_TeamChange;
440 defer_ClientKill_Now_TeamChange = false;
444 PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
448 if(valid_damage_for_weaponstats)
449 WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
451 if(autocvar_sv_gentle < 1)
452 if(sound_allowed(MSG_BROADCAST, attacker))
454 if(deathtype == DEATH_DROWN.m_id)
455 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
457 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
460 // get rid of kill indicator
461 if(this.killindicator)
463 delete(this.killindicator);
464 this.killindicator = NULL;
465 if(this.killindicator_teamchange)
466 defer_ClientKill_Now_TeamChange = true;
468 if(this.classname == "body")
469 if(deathtype == DEATH_KILL.m_id)
471 // for the lemmings fans, a small harmless explosion
472 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
476 // print an obituary message
477 if(this.classname != "body")
478 Obituary(attacker, inflictor, this, deathtype, weaponentity);
480 // increment frag counter for used weapon type
481 Weapon w = DEATH_WEAPONOF(deathtype);
482 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
483 CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
485 this.respawn_time = 0;
486 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
487 damage = M_ARGV(4, float);
488 excess = max(0, damage - take - save);
490 //Weapon wep = this.(weaponentity).m_weapon;
491 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
493 .entity went = weaponentities[slot];
495 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
496 Weapon wep = this.(went).m_weapon;
497 wep.wr_playerdeath(wep, this, went);
500 RemoveGrapplingHooks(this);
502 Portal_ClearAllLater(this);
504 this.fixangle = true;
506 if(defer_ClientKill_Now_TeamChange)
507 ClientKill_Now_TeamChange(this); // can turn player into spectator
509 // player could have been miraculously resuscitated ;)
510 // e.g. players in freezetag get frozen, they don't really die
511 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
514 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
515 calculate_player_respawn_time(this);
517 // when we get here, player actually dies
519 Unfreeze(this, false); // remove any icy remains
522 WaypointSprite_PlayerDead(this);
524 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
526 .entity went = weaponentities[slot];
527 SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
530 // become fully visible
531 this.alpha = default_player_alpha;
532 // make the corpse upright (not tilted)
536 this.avelocity = '0 0 0';
537 // view from the floor
538 this.view_ofs = '0 0 -8';
539 if(this.move_movetype == MOVETYPE_NOCLIP)
541 // don't toss the corpse in this case, it can get stuck in solid (causing low fps)
542 // or fall indefinitely into the void if out of the map
543 this.velocity = '0 0 0';
548 set_movetype(this, MOVETYPE_TOSS);
551 this.solid = SOLID_CORPSE;
552 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
553 // don't stick to the floor
554 UNSET_ONGROUND(this);
556 this.deadflag = DEAD_DYING;
558 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
560 this.death_time = time;
562 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
564 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
566 // set damage function to corpse damage
567 this.event_damage = PlayerCorpseDamage;
568 this.event_heal = func_null;
569 // call the corpse damage function just in case it wants to gib
570 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
572 // set up to fade out later
573 SUB_SetFade (this, time + 6 + random (), 1);
574 // reset body think wrapper broken by SUB_SetFade
575 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
576 this.CopyBody_think = getthink(this);
577 this.CopyBody_nextthink = this.nextthink;
578 setthink(this, CopyBody_Think);
579 this.nextthink = time;
582 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
584 // clones don't run any animation code any more, so we must gib them when they die :(
585 this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
588 // reset fields the weapons may use just in case
589 if(this.classname != "body")
591 FOREACH(Weapons, it != WEP_Null,
593 it.wr_resetplayer(it, this);
594 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
596 ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
600 MUTATOR_CALLHOOK(PlayerDied, this);
604 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
606 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
609 GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);
613 void precache_playermodel(string m)
615 int globhandle, i, n;
618 // remove :<skinnumber> suffix
619 int j = strstrofs(m, ":", 0);
621 m = substring(m, 0, j);
623 if(substring(m, -9, 5) == "_lod1")
625 if(substring(m, -9, 5) == "_lod2")
628 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
631 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
635 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
638 n = search_getsize(globhandle);
639 for (i = 0; i < n; ++i)
641 //print(search_getfilename(globhandle, i), "\n");
642 f = search_getfilename(globhandle, i);
643 PrecachePlayerSounds(f);
645 search_end(globhandle);
647 void precache_all_playermodels(string pattern)
649 int globhandle = search_begin(pattern, true, false);
650 if (globhandle < 0) return;
651 int n = search_getsize(globhandle);
652 for (int i = 0; i < n; ++i)
654 string s = search_getfilename(globhandle, i);
655 precache_playermodel(s);
657 search_end(globhandle);
660 void precache_playermodels(string s)
662 FOREACH_WORD(s, true, { precache_playermodel(it); });
665 PRECACHE(PlayerModels)
667 // Precache all player models if desired
668 if (autocvar_sv_precacheplayermodels)
670 PrecachePlayerSounds("sound/player/default.sounds");
671 precache_all_playermodels("models/player/*.zym");
672 precache_all_playermodels("models/player/*.dpm");
673 precache_all_playermodels("models/player/*.md3");
674 precache_all_playermodels("models/player/*.psk");
675 precache_all_playermodels("models/player/*.iqm");
678 if (autocvar_sv_defaultcharacter)
680 precache_playermodels(autocvar_sv_defaultplayermodel_red);
681 precache_playermodels(autocvar_sv_defaultplayermodel_blue);
682 precache_playermodels(autocvar_sv_defaultplayermodel_yellow);
683 precache_playermodels(autocvar_sv_defaultplayermodel_pink);
684 precache_playermodels(autocvar_sv_defaultplayermodel);