3 #include <common/effects/all.qh>
6 #include "clientkill.qh"
9 #include "miscfunctions.qh"
11 #include "teamplay.qh"
12 #include "weapons/throwing.qh"
13 #include "command/common.qh"
14 #include "../common/state.qh"
15 #include "../common/anim.qh"
16 #include "../common/animdecide.qh"
17 #include "../common/csqcmodel_settings.qh"
18 #include "../common/gamemodes/sv_rules.qh"
19 #include "../common/deathtypes/all.qh"
20 #include "../common/mapobjects/subs.qh"
21 #include "../common/playerstats.qh"
22 #include "../lib/csqcmodel/sv_model.qh"
24 #include "../common/minigames/sv_minigames.qh"
26 #include <common/gamemodes/_mod.qh>
28 #include "../common/physics/player.qh"
29 #include "../common/effects/qc/_mod.qh"
30 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
31 #include "../common/mapobjects/_mod.qh"
32 #include "../common/wepent.qh"
34 #include "weapons/weaponstats.qh"
36 #include "../common/animdecide.qh"
38 void Drop_Special_Items(entity player)
40 // called when the player has become stuck or frozen
41 // so objective items aren't stuck with the player
43 MUTATOR_CALLHOOK(DropSpecialItems, player);
46 void CopyBody_Think(entity this)
48 if(this.CopyBody_nextthink && time > this.CopyBody_nextthink)
50 this.CopyBody_think(this);
53 this.CopyBody_nextthink = this.nextthink;
54 this.CopyBody_think = getthink(this);
55 setthink(this, CopyBody_Think);
57 CSQCMODEL_AUTOUPDATE(this);
58 this.nextthink = time;
60 void CopyBody(entity this, float keepvelocity)
62 if (this.effects & EF_NODRAW)
64 entity clone = new(body);
67 clone.colormap = this.colormap;
68 clone.iscreature = this.iscreature;
69 clone.teleportable = this.teleportable;
70 clone.damagedbycontents = this.damagedbycontents;
71 if(clone.damagedbycontents)
72 IL_PUSH(g_damagedbycontents, clone);
73 clone.angles = this.angles;
74 clone.v_angle = this.v_angle;
75 clone.avelocity = this.avelocity;
76 clone.damageforcescale = this.damageforcescale;
77 clone.effects = this.effects;
78 clone.glowmod = this.glowmod;
79 clone.event_damage = this.event_damage;
80 clone.event_heal = this.event_heal;
81 clone.anim_state = this.anim_state;
82 clone.anim_time = this.anim_time;
83 clone.anim_lower_action = this.anim_lower_action;
84 clone.anim_lower_time = this.anim_lower_time;
85 clone.anim_upper_action = this.anim_upper_action;
86 clone.anim_upper_time = this.anim_upper_time;
87 clone.anim_implicit_state = this.anim_implicit_state;
88 clone.anim_implicit_time = this.anim_implicit_time;
89 clone.anim_lower_implicit_action = this.anim_lower_implicit_action;
90 clone.anim_lower_implicit_time = this.anim_lower_implicit_time;
91 clone.anim_upper_implicit_action = this.anim_upper_implicit_action;
92 clone.anim_upper_implicit_time = this.anim_upper_implicit_time;
93 clone.dphitcontentsmask = this.dphitcontentsmask;
94 clone.death_time = this.death_time;
95 clone.pain_finished = this.pain_finished;
96 SetResourceExplicit(clone, RES_HEALTH, GetResource(this, RES_HEALTH));
97 SetResourceExplicit(clone, RES_ARMOR, GetResource(this, RES_ARMOR));
98 clone.armortype = this.armortype;
99 clone.model = this.model;
100 clone.modelindex = this.modelindex;
101 clone.skin = this.skin;
102 clone.species = this.species;
103 clone.move_qcphysics = false; // don't run gamecode logic on clones, too many
104 set_movetype(clone, this.move_movetype);
105 clone.solid = this.solid;
106 clone.takedamage = this.takedamage;
107 setcefc(clone, getcefc(this));
108 clone.uncustomizeentityforclient = this.uncustomizeentityforclient;
109 clone.uncustomizeentityforclient_set = this.uncustomizeentityforclient_set;
110 if (keepvelocity == 1)
111 clone.velocity = this.velocity;
112 clone.oldvelocity = clone.velocity;
113 clone.alpha = this.alpha;
114 clone.fade_time = this.fade_time;
115 clone.fade_rate = this.fade_rate;
116 //clone.weapon = this.weapon;
117 setorigin(clone, this.origin);
118 setsize(clone, this.mins, this.maxs);
119 clone.reset = SUB_Remove;
120 clone._ps = this._ps;
122 Drag_MoveDrag(this, clone);
124 if(clone.colormap <= maxclients && clone.colormap > 0)
125 clone.colormap = 1024 + this.clientcolors;
127 CSQCMODEL_AUTOINIT(clone);
128 clone.CopyBody_nextthink = this.nextthink;
129 clone.CopyBody_think = getthink(this);
130 clone.nextthink = time;
131 setthink(clone, CopyBody_Think);
132 // "bake" the current animation frame for clones (they don't get clientside animation)
133 animdecide_load_if_needed(clone);
134 animdecide_setframes(clone, false, frame, frame1time, frame2, frame2time);
136 IL_PUSH(g_clones, clone);
138 MUTATOR_CALLHOOK(CopyBody, this, clone, keepvelocity);
141 void player_setupanimsformodel(entity this)
143 // load animation info
144 animdecide_load_if_needed(this);
145 animdecide_setstate(this, 0, false);
148 void player_anim(entity this)
150 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
153 // Decide on which death animation to use.
155 deadbits = ANIMSTATE_DEAD1;
157 deadbits = ANIMSTATE_DEAD2;
160 // Clear a previous death animation.
163 int animbits = deadbits;
164 if(STAT(FROZEN, this))
165 animbits |= ANIMSTATE_FROZEN;
166 if(this.move_movetype == MOVETYPE_FOLLOW)
167 animbits |= ANIMSTATE_FOLLOW;
169 animbits |= ANIMSTATE_DUCK;
170 animdecide_setstate(this, animbits, false);
171 animdecide_setimplicitstate(this, IS_ONGROUND(this));
174 void PlayerCorpseDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
178 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
180 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
184 if(sound_allowed(MSG_BROADCAST, attacker))
187 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
189 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
191 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);
195 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
197 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
199 TakeResource(this, RES_ARMOR, save);
200 TakeResource(this, RES_HEALTH, take);
201 // pause regeneration for 5 seconds
202 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
204 this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
205 this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
206 this.dmg_inflictor = inflictor;
208 if (GetResource(this, RES_HEALTH) <= -autocvar_sv_gibhealth && this.alpha >= 0)
210 // don't use any animations as a gib
212 // view just above the floor
213 this.view_ofs = '0 0 4';
215 Violence_GibSplash(this, 1, 1, attacker);
217 this.solid = SOLID_NOT; // restore later
218 this.takedamage = DAMAGE_NO; // restore later
219 if(this.damagedbycontents)
220 IL_REMOVE(g_damagedbycontents, this);
221 this.damagedbycontents = false;
225 void calculate_player_respawn_time(entity this)
227 if(MUTATOR_CALLHOOK(CalculateRespawnTime, this))
230 float gametype_setting_tmp;
231 float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
232 float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
233 float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
234 float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
235 float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
236 float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
238 float pcount = 1; // Include myself whether or not team is already set right and I'm a "player".
241 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
242 if(it.team == this.team)
245 if (sdelay_small_count == 0)
246 sdelay_small_count = 1;
247 if (sdelay_large_count == 0)
248 sdelay_large_count = 1;
252 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
255 if (sdelay_small_count == 0)
257 if (IS_INDEPENDENT_PLAYER(this))
259 // Players play independently. No point in requiring enemies.
260 sdelay_small_count = 1;
264 // Players play AGAINST each other. Enemies required.
265 sdelay_small_count = 2;
268 if (sdelay_large_count == 0)
270 if (IS_INDEPENDENT_PLAYER(this))
272 // Players play independently. No point in requiring enemies.
273 sdelay_large_count = 1;
277 // Players play AGAINST each other. Enemies required.
278 sdelay_large_count = 2;
285 if (pcount <= sdelay_small_count)
286 sdelay = sdelay_small;
287 else if (pcount >= sdelay_large_count)
288 sdelay = sdelay_large;
289 else // NOTE: this case implies sdelay_large_count > sdelay_small_count.
290 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
293 this.respawn_time = ceil((time + sdelay) / waves) * waves;
295 this.respawn_time = time + sdelay;
297 if(sdelay < sdelay_max)
298 this.respawn_time_max = time + sdelay_max;
300 this.respawn_time_max = this.respawn_time;
302 if((sdelay + waves >= 5.0) && (this.respawn_time - time > 1.75))
303 this.respawn_countdown = 10; // first number to count down from is 10
305 this.respawn_countdown = -1; // do not count down
307 if(autocvar_g_forced_respawn)
308 this.respawn_flags = this.respawn_flags | RESPAWN_FORCE;
311 void PlayerDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
314 float dh = max(GetResource(this, RES_HEALTH), 0);
315 float da = max(GetResource(this, RES_ARMOR), 0);
316 float take = 0, save = 0;
320 if(!DEATH_ISSPECIAL(deathtype))
322 damage *= Handicap_GetTotalHandicap(this);
323 if (this != attacker && IS_PLAYER(attacker))
325 damage /= Handicap_GetTotalHandicap(attacker);
329 if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
330 damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
332 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
338 ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
340 makevectors(this.angles);
341 ear1 += v_right * -10;
342 ear2 += v_right * +10;
343 d = inflictor.origin - this.origin;
345 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
347 f = 0; // Assum ecenter.
348 force = v_right * vlen(force);
349 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), this, attacker);
350 Violence_GibSplash_At(ear2, force, 2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), this, attacker);
359 // force is already good
363 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
365 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
372 // don't reset pushltime for self damage as it may be an attempt to
373 // escape a lava pit or similar
374 //this.pushltime = 0;
377 else if(IS_PLAYER(attacker))
379 this.pusher = attacker;
380 this.pushltime = time + autocvar_g_maxpushtime;
381 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
383 else if(time < this.pushltime)
385 attacker = this.pusher;
386 this.pushltime = max(this.pushltime, time + 0.6);
394 MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, this, force, take, save, deathtype, damage);
395 take = bound(0, M_ARGV(4, float), GetResource(this, RES_HEALTH));
396 save = bound(0, M_ARGV(5, float), GetResource(this, RES_ARMOR));
397 float excess = max(0, damage - take - save);
399 if(sound_allowed(MSG_BROADCAST, attacker))
401 if (save > 10 && (dh - take) > 0) // don't play armor sound if the attack is fatal
402 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
404 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
406 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
410 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
412 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
414 if ((time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
415 && !STAT(FROZEN, this))
417 if (!(this.flags & FL_GODMODE))
419 TakeResource(this, RES_ARMOR, save);
420 TakeResource(this, RES_HEALTH, take);
421 // pause regeneration for 5 seconds
423 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
425 if (time > this.pain_finished) //Don't switch pain sequences like crazy
427 this.pain_finished = time + 0.5; //Supajoe
429 if(autocvar_sv_gentle < 1) {
430 if(this.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
432 if (!this.animstate_override)
435 animdecide_setaction(this, ANIMACTION_PAIN1, true);
437 animdecide_setaction(this, ANIMACTION_PAIN2, true);
440 float myhp = GetResource(this, RES_HEALTH);
442 if(myhp < 25 || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || take > 20 || attacker != this)
443 if(sound_allowed(MSG_BROADCAST, attacker))
444 // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
446 if(deathtype == DEATH_FALL.m_id)
447 PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
449 PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
451 PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
453 PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
455 PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
460 // throw off bot aim temporarily
462 if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
464 shake = damage * 5 / (bound(0,skill,100) + 1);
465 this.v_angle_x = this.v_angle.x + (random() * 2 - 1) * shake;
466 this.v_angle_y = this.v_angle.y + (random() * 2 - 1) * shake;
467 this.v_angle_x = bound(-90, this.v_angle.x, 90);
470 float realdmg = damage - excess;
471 if (this != attacker && realdmg)
472 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
474 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
475 GameRules_scoring_add(attacker, DMG, realdmg);
477 if (IS_PLAYER(this)) {
478 GameRules_scoring_add(this, DMGTAKEN, realdmg);
483 this.max_armorvalue += (save + take);
485 this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
486 this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
487 this.dmg_inflictor = inflictor;
489 bool abot = (IS_BOT_CLIENT(attacker));
490 bool vbot = (IS_BOT_CLIENT(this));
492 bool valid_damage_for_weaponstats = false;
493 Weapon awep = WEP_Null;
495 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
496 if(vbot || IS_REAL_CLIENT(this))
497 if(abot || IS_REAL_CLIENT(attacker))
498 if(attacker && this != attacker)
499 if (DIFF_TEAM(this, attacker) && (!STAT(FROZEN, this) || this.freeze_time > time))
501 if(DEATH_ISSPECIAL(deathtype))
502 awep = attacker.(weaponentity).m_weapon;
504 awep = DEATH_WEAPONOF(deathtype);
505 valid_damage_for_weaponstats = true;
508 dh -= max(GetResource(this, RES_HEALTH), 0); // health difference
509 da -= max(GetResource(this, RES_ARMOR), 0); // armor difference
510 if(valid_damage_for_weaponstats)
512 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
515 MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
517 if (GetResource(this, RES_HEALTH) < 1)
519 float defer_ClientKill_Now_TeamChange;
520 defer_ClientKill_Now_TeamChange = false;
524 PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
528 if(valid_damage_for_weaponstats)
529 WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
531 if(autocvar_sv_gentle < 1)
532 if(sound_allowed(MSG_BROADCAST, attacker))
534 if(deathtype == DEATH_DROWN.m_id)
535 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
537 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
540 // get rid of kill indicator
541 if(this.killindicator)
543 delete(this.killindicator);
544 this.killindicator = NULL;
545 if(this.killindicator_teamchange)
546 defer_ClientKill_Now_TeamChange = true;
548 if(this.classname == "body")
549 if(deathtype == DEATH_KILL.m_id)
551 // for the lemmings fans, a small harmless explosion
552 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
556 // print an obituary message
557 if(this.classname != "body")
558 Obituary(attacker, inflictor, this, deathtype, weaponentity);
560 // increment frag counter for used weapon type
561 Weapon w = DEATH_WEAPONOF(deathtype);
562 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
563 CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
565 this.respawn_time = 0;
566 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
567 damage = M_ARGV(4, float);
568 excess = max(0, damage - take - save);
570 //Weapon wep = this.(weaponentity).m_weapon;
571 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
573 .entity went = weaponentities[slot];
575 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
576 Weapon wep = this.(went).m_weapon;
577 wep.wr_playerdeath(wep, this, went);
580 RemoveGrapplingHooks(this);
582 Portal_ClearAllLater(this);
584 this.fixangle = true;
586 if(defer_ClientKill_Now_TeamChange)
587 ClientKill_Now_TeamChange(this); // can turn player into spectator
589 // player could have been miraculously resuscitated ;)
590 // e.g. players in freezetag get frozen, they don't really die
591 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
594 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
595 calculate_player_respawn_time(this);
597 // when we get here, player actually dies
599 Unfreeze(this, false); // remove any icy remains
602 WaypointSprite_PlayerDead(this);
604 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
606 .entity went = weaponentities[slot];
607 SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
610 // become fully visible
611 this.alpha = default_player_alpha;
612 // make the corpse upright (not tilted)
616 this.avelocity = '0 0 0';
617 // view from the floor
618 this.view_ofs = '0 0 -8';
620 set_movetype(this, MOVETYPE_TOSS);
622 this.solid = SOLID_CORPSE;
623 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
624 // don't stick to the floor
625 UNSET_ONGROUND(this);
627 this.deadflag = DEAD_DYING;
629 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
631 this.death_time = time;
633 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
635 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
637 // set damage function to corpse damage
638 this.event_damage = PlayerCorpseDamage;
639 this.event_heal = func_null;
640 // call the corpse damage function just in case it wants to gib
641 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
643 // set up to fade out later
644 SUB_SetFade (this, time + 6 + random (), 1);
645 // reset body think wrapper broken by SUB_SetFade
646 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
647 this.CopyBody_think = getthink(this);
648 this.CopyBody_nextthink = this.nextthink;
649 setthink(this, CopyBody_Think);
650 this.nextthink = time;
653 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
655 // clones don't run any animation code any more, so we must gib them when they die :(
656 this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
659 // reset fields the weapons may use just in case
660 if(this.classname != "body")
662 FOREACH(Weapons, it != WEP_Null,
664 it.wr_resetplayer(it, this);
665 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
667 ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
671 MUTATOR_CALLHOOK(PlayerDied, this);
675 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
677 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
680 GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);