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 (STAT(FROZEN, this))
252 if (!ITEM_DAMAGE_NEEDKILL(deathtype))
255 else if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
256 damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
258 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
264 ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
266 makevectors(this.angles);
267 ear1 += v_right * -10;
268 ear2 += v_right * +10;
269 d = inflictor.origin - this.origin;
271 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
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);
285 // force is already good
289 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
291 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
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;
303 else if(IS_PLAYER(attacker))
305 this.pusher = attacker;
306 this.pushltime = time + autocvar_g_maxpushtime;
307 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
309 else if(time < this.pushltime)
311 attacker = this.pusher;
312 this.pushltime = max(this.pushltime, time + 0.6);
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);
325 if(sound_allowed(MSG_BROADCAST, attacker))
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);
330 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
332 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
336 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
338 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
340 if (time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
342 if (!(this.flags & FL_GODMODE))
344 TakeResource(this, RES_ARMOR, save);
345 TakeResource(this, RES_HEALTH, take);
346 // pause regeneration for 5 seconds
348 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
350 if (time > this.pain_finished && !STAT(FROZEN, this)) // Don't switch pain sequences like crazy
352 this.pain_finished = time + 0.5; //Supajoe
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
357 if (!this.animstate_override)
360 animdecide_setaction(this, ANIMACTION_PAIN1, true);
362 animdecide_setaction(this, ANIMACTION_PAIN2, true);
365 float myhp = GetResource(this, RES_HEALTH);
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
371 if(deathtype == DEATH_FALL.m_id)
372 PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
374 PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
376 PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
378 PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
380 PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
385 // throw off bot aim temporarily
387 if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
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);
395 float realdmg = damage - excess;
396 if (this != attacker && realdmg && !STAT(FROZEN, this)
397 && (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime))
399 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
400 GameRules_scoring_add(attacker, DMG, realdmg);
402 if (IS_PLAYER(this)) {
403 GameRules_scoring_add(this, DMGTAKEN, realdmg);
408 this.max_armorvalue += (save + take);
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;
414 bool abot = (IS_BOT_CLIENT(attacker));
415 bool vbot = (IS_BOT_CLIENT(this));
417 bool valid_damage_for_weaponstats = false;
418 Weapon awep = WEP_Null;
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))
426 if(DEATH_ISSPECIAL(deathtype))
427 awep = attacker.(weaponentity).m_weapon;
429 awep = DEATH_WEAPONOF(deathtype);
430 valid_damage_for_weaponstats = true;
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)
437 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
440 MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
442 if (GetResource(this, RES_HEALTH) < 1)
444 float defer_ClientKill_Now_TeamChange;
445 defer_ClientKill_Now_TeamChange = false;
449 PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
453 if(valid_damage_for_weaponstats)
454 WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
456 if(autocvar_sv_gentle < 1)
457 if(sound_allowed(MSG_BROADCAST, attacker))
459 if(deathtype == DEATH_DROWN.m_id)
460 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
462 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
465 // get rid of kill indicator
466 if(this.killindicator)
468 delete(this.killindicator);
469 this.killindicator = NULL;
470 if(this.killindicator_teamchange)
471 defer_ClientKill_Now_TeamChange = true;
473 if(this.classname == "body")
474 if(deathtype == DEATH_KILL.m_id)
476 // for the lemmings fans, a small harmless explosion
477 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
481 // print an obituary message
482 if(this.classname != "body")
483 Obituary(attacker, inflictor, this, deathtype, weaponentity);
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;
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);
495 //Weapon wep = this.(weaponentity).m_weapon;
496 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
498 .entity went = weaponentities[slot];
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);
505 RemoveGrapplingHooks(this);
507 Portal_ClearAllLater(this);
509 this.fixangle = true;
511 if(defer_ClientKill_Now_TeamChange)
512 ClientKill_Now_TeamChange(this); // can turn player into spectator
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"))
519 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
520 calculate_player_respawn_time(this);
522 // when we get here, player actually dies
524 Unfreeze(this, false); // remove any icy remains
527 WaypointSprite_PlayerDead(this);
529 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
531 .entity went = weaponentities[slot];
532 SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
535 // become fully visible
536 this.alpha = default_player_alpha;
537 // make the corpse upright (not tilted)
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)
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';
553 set_movetype(this, MOVETYPE_TOSS);
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);
561 this.deadflag = DEAD_DYING;
563 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
565 this.death_time = time;
567 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
569 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
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);
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;
587 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
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);
593 // reset fields the weapons may use just in case
594 if(this.classname != "body")
596 FOREACH(Weapons, it != WEP_Null,
598 it.wr_resetplayer(it, this);
599 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
601 ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
605 MUTATOR_CALLHOOK(PlayerDied, this);
609 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
611 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
614 GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);
618 void precache_playermodel(string m)
620 int globhandle, i, n;
623 // remove :<skinnumber> suffix
624 int j = strstrofs(m, ":", 0);
626 m = substring(m, 0, j);
628 if(substring(m, -9, 5) == "_lod1")
630 if(substring(m, -9, 5) == "_lod2")
633 f = strcat(substring(m, 0, -5), "_lod1", substring(m, -4, -1));
636 f = strcat(substring(m, 0, -5), "_lod2", substring(m, -4, -1));
640 globhandle = search_begin(strcat(m, "_*.sounds"), true, false);
643 n = search_getsize(globhandle);
644 for (i = 0; i < n; ++i)
646 //print(search_getfilename(globhandle, i), "\n");
647 f = search_getfilename(globhandle, i);
648 PrecachePlayerSounds(f);
650 search_end(globhandle);
652 void precache_all_playermodels(string pattern)
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)
659 string s = search_getfilename(globhandle, i);
660 precache_playermodel(s);
662 search_end(globhandle);
665 void precache_playermodels(string s)
667 FOREACH_WORD(s, true, { precache_playermodel(it); });
670 PRECACHE(PlayerModels)
672 // Precache all player models if desired
673 if (autocvar_sv_precacheplayermodels)
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");
683 if (autocvar_sv_defaultcharacter)
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);