3 #include <common/effects/all.qh>
7 #include "clientkill.qh"
10 #include "miscfunctions.qh"
12 #include "teamplay.qh"
13 #include "weapons/throwing.qh"
14 #include "command/common.qh"
15 #include "../common/state.qh"
16 #include "../common/anim.qh"
17 #include "../common/animdecide.qh"
18 #include "../common/csqcmodel_settings.qh"
19 #include "../common/gamemodes/sv_rules.qh"
20 #include "../common/deathtypes/all.qh"
21 #include "../common/mapobjects/subs.qh"
22 #include "../common/playerstats.qh"
23 #include "../lib/csqcmodel/sv_model.qh"
25 #include "../common/minigames/sv_minigames.qh"
27 #include <common/gamemodes/_mod.qh>
29 #include "../common/physics/player.qh"
30 #include "../common/effects/qc/_mod.qh"
31 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
32 #include "../common/mapobjects/_mod.qh"
33 #include "../common/wepent.qh"
35 #include "weapons/weaponstats.qh"
36 #include <server/weapons/weaponsystem.qh>
38 #include "../common/animdecide.qh"
40 void Drop_Special_Items(entity player)
42 // called when the player has become stuck or frozen
43 // so objective items aren't stuck with the player
45 MUTATOR_CALLHOOK(DropSpecialItems, player);
48 void CopyBody_Think(entity this)
50 if(this.CopyBody_nextthink && time > this.CopyBody_nextthink)
52 this.CopyBody_think(this);
55 this.CopyBody_nextthink = this.nextthink;
56 this.CopyBody_think = getthink(this);
57 setthink(this, CopyBody_Think);
59 CSQCMODEL_AUTOUPDATE(this);
60 this.nextthink = time;
62 void CopyBody(entity this, float keepvelocity)
64 if (this.effects & EF_NODRAW)
66 entity clone = new(body);
69 clone.colormap = this.colormap;
70 clone.iscreature = this.iscreature;
71 clone.teleportable = this.teleportable;
72 clone.damagedbycontents = this.damagedbycontents;
73 if(clone.damagedbycontents)
74 IL_PUSH(g_damagedbycontents, clone);
75 clone.angles = this.angles;
76 clone.v_angle = this.v_angle;
77 clone.avelocity = this.avelocity;
78 clone.damageforcescale = this.damageforcescale;
79 clone.effects = this.effects;
80 clone.glowmod = this.glowmod;
81 clone.event_damage = this.event_damage;
82 clone.event_heal = this.event_heal;
83 clone.anim_state = this.anim_state;
84 clone.anim_time = this.anim_time;
85 clone.anim_lower_action = this.anim_lower_action;
86 clone.anim_lower_time = this.anim_lower_time;
87 clone.anim_upper_action = this.anim_upper_action;
88 clone.anim_upper_time = this.anim_upper_time;
89 clone.anim_implicit_state = this.anim_implicit_state;
90 clone.anim_implicit_time = this.anim_implicit_time;
91 clone.anim_lower_implicit_action = this.anim_lower_implicit_action;
92 clone.anim_lower_implicit_time = this.anim_lower_implicit_time;
93 clone.anim_upper_implicit_action = this.anim_upper_implicit_action;
94 clone.anim_upper_implicit_time = this.anim_upper_implicit_time;
95 clone.dphitcontentsmask = this.dphitcontentsmask;
96 clone.death_time = this.death_time;
97 clone.pain_finished = this.pain_finished;
98 SetResourceExplicit(clone, RES_HEALTH, GetResource(this, RES_HEALTH));
99 SetResourceExplicit(clone, RES_ARMOR, GetResource(this, RES_ARMOR));
100 clone.armortype = this.armortype;
101 clone.model = this.model;
102 clone.modelindex = this.modelindex;
103 clone.skin = this.skin;
104 clone.species = this.species;
105 clone.move_qcphysics = false; // don't run gamecode logic on clones, too many
106 set_movetype(clone, this.move_movetype);
107 clone.solid = this.solid;
108 clone.takedamage = this.takedamage;
109 setcefc(clone, getcefc(this));
110 clone.uncustomizeentityforclient = this.uncustomizeentityforclient;
111 clone.uncustomizeentityforclient_set = this.uncustomizeentityforclient_set;
112 if (keepvelocity == 1)
113 clone.velocity = this.velocity;
114 clone.oldvelocity = clone.velocity;
115 clone.alpha = this.alpha;
116 clone.fade_time = this.fade_time;
117 clone.fade_rate = this.fade_rate;
118 //clone.weapon = this.weapon;
119 setorigin(clone, this.origin);
120 setsize(clone, this.mins, this.maxs);
121 clone.reset = SUB_Remove;
122 clone._ps = this._ps;
124 Drag_MoveDrag(this, clone);
126 if(clone.colormap <= maxclients && clone.colormap > 0)
127 clone.colormap = 1024 + this.clientcolors;
129 CSQCMODEL_AUTOINIT(clone);
130 clone.CopyBody_nextthink = this.nextthink;
131 clone.CopyBody_think = getthink(this);
132 clone.nextthink = time;
133 setthink(clone, CopyBody_Think);
134 // "bake" the current animation frame for clones (they don't get clientside animation)
135 animdecide_load_if_needed(clone);
136 animdecide_setframes(clone, false, frame, frame1time, frame2, frame2time);
138 IL_PUSH(g_clones, clone);
140 MUTATOR_CALLHOOK(CopyBody, this, clone, keepvelocity);
143 void player_setupanimsformodel(entity this)
145 // load animation info
146 animdecide_load_if_needed(this);
147 animdecide_setstate(this, 0, false);
150 void player_anim(entity this)
152 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
155 // Decide on which death animation to use.
157 deadbits = ANIMSTATE_DEAD1;
159 deadbits = ANIMSTATE_DEAD2;
162 // Clear a previous death animation.
165 int animbits = deadbits;
166 if(STAT(FROZEN, this))
167 animbits |= ANIMSTATE_FROZEN;
168 if(this.move_movetype == MOVETYPE_FOLLOW)
169 animbits |= ANIMSTATE_FOLLOW;
171 animbits |= ANIMSTATE_DUCK;
172 animdecide_setstate(this, animbits, false);
173 animdecide_setimplicitstate(this, IS_ONGROUND(this));
176 void PlayerCorpseDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
180 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
182 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
186 if(sound_allowed(MSG_BROADCAST, attacker))
189 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
191 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
193 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);
197 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
199 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
201 TakeResource(this, RES_ARMOR, save);
202 TakeResource(this, RES_HEALTH, take);
203 // pause regeneration for 5 seconds
204 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
206 this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
207 this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
208 this.dmg_inflictor = inflictor;
210 if (GetResource(this, RES_HEALTH) <= -autocvar_sv_gibhealth && this.alpha >= 0)
212 // don't use any animations as a gib
214 // view just above the floor
215 this.view_ofs = '0 0 4';
217 Violence_GibSplash(this, 1, 1, attacker);
219 this.solid = SOLID_NOT; // restore later
220 this.takedamage = DAMAGE_NO; // restore later
221 if(this.damagedbycontents)
222 IL_REMOVE(g_damagedbycontents, this);
223 this.damagedbycontents = false;
227 void PlayerDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
230 float dh = max(GetResource(this, RES_HEALTH), 0);
231 float da = max(GetResource(this, RES_ARMOR), 0);
232 float take = 0, save = 0;
236 if(!DEATH_ISSPECIAL(deathtype))
238 damage *= Handicap_GetTotalHandicap(this);
239 if (this != attacker && IS_PLAYER(attacker))
241 damage /= Handicap_GetTotalHandicap(attacker);
245 if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
246 damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
248 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
254 ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
256 makevectors(this.angles);
257 ear1 += v_right * -10;
258 ear2 += v_right * +10;
259 d = inflictor.origin - this.origin;
261 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
263 f = 0; // Assum ecenter.
264 force = v_right * vlen(force);
265 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), this, attacker);
266 Violence_GibSplash_At(ear2, force, 2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), this, attacker);
275 // force is already good
279 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
281 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
288 // don't reset pushltime for self damage as it may be an attempt to
289 // escape a lava pit or similar
290 //this.pushltime = 0;
293 else if(IS_PLAYER(attacker))
295 this.pusher = attacker;
296 this.pushltime = time + autocvar_g_maxpushtime;
297 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
299 else if(time < this.pushltime)
301 attacker = this.pusher;
302 this.pushltime = max(this.pushltime, time + 0.6);
310 MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, this, force, take, save, deathtype, damage);
311 take = bound(0, M_ARGV(4, float), GetResource(this, RES_HEALTH));
312 save = bound(0, M_ARGV(5, float), GetResource(this, RES_ARMOR));
313 float excess = max(0, damage - take - save);
315 if(sound_allowed(MSG_BROADCAST, attacker))
317 if (save > 10 && (dh - take) > 0) // don't play armor sound if the attack is fatal
318 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
320 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
322 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
326 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
328 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
330 if (time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
332 if (!(this.flags & FL_GODMODE))
334 TakeResource(this, RES_ARMOR, save);
335 TakeResource(this, RES_HEALTH, take);
336 // pause regeneration for 5 seconds
338 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
340 if (time > this.pain_finished) //Don't switch pain sequences like crazy
342 this.pain_finished = time + 0.5; //Supajoe
344 if(autocvar_sv_gentle < 1) {
345 if(this.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
347 if (!this.animstate_override)
350 animdecide_setaction(this, ANIMACTION_PAIN1, true);
352 animdecide_setaction(this, ANIMACTION_PAIN2, true);
355 float myhp = GetResource(this, RES_HEALTH);
357 if(myhp < 25 || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || take > 20 || attacker != this)
358 if(sound_allowed(MSG_BROADCAST, attacker))
359 // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
361 if(deathtype == DEATH_FALL.m_id)
362 PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
364 PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
366 PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
368 PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
370 PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
375 // throw off bot aim temporarily
377 if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
379 shake = damage * 5 / (bound(0,skill,100) + 1);
380 this.v_angle_x = this.v_angle.x + (random() * 2 - 1) * shake;
381 this.v_angle_y = this.v_angle.y + (random() * 2 - 1) * shake;
382 this.v_angle_x = bound(-90, this.v_angle.x, 90);
385 float realdmg = damage - excess;
386 if (this != attacker && realdmg)
387 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
389 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
390 GameRules_scoring_add(attacker, DMG, realdmg);
392 if (IS_PLAYER(this)) {
393 GameRules_scoring_add(this, DMGTAKEN, realdmg);
398 this.max_armorvalue += (save + take);
400 this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
401 this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
402 this.dmg_inflictor = inflictor;
404 bool abot = (IS_BOT_CLIENT(attacker));
405 bool vbot = (IS_BOT_CLIENT(this));
407 bool valid_damage_for_weaponstats = false;
408 Weapon awep = WEP_Null;
410 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
411 if(vbot || IS_REAL_CLIENT(this))
412 if(abot || IS_REAL_CLIENT(attacker))
413 if(attacker && this != attacker)
414 if(DIFF_TEAM(this, attacker))
416 if(DEATH_ISSPECIAL(deathtype))
417 awep = attacker.(weaponentity).m_weapon;
419 awep = DEATH_WEAPONOF(deathtype);
420 valid_damage_for_weaponstats = true;
423 dh = dh - max(GetResource(this, RES_HEALTH), 0);
424 da = da - max(GetResource(this, RES_ARMOR), 0);
425 if(valid_damage_for_weaponstats)
427 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
430 MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
432 if (GetResource(this, RES_HEALTH) < 1)
434 float defer_ClientKill_Now_TeamChange;
435 defer_ClientKill_Now_TeamChange = false;
439 PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
443 if(valid_damage_for_weaponstats)
444 WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
446 if(autocvar_sv_gentle < 1)
447 if(sound_allowed(MSG_BROADCAST, attacker))
449 if(deathtype == DEATH_DROWN.m_id)
450 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
452 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
455 // get rid of kill indicator
456 if(this.killindicator)
458 delete(this.killindicator);
459 this.killindicator = NULL;
460 if(this.killindicator_teamchange)
461 defer_ClientKill_Now_TeamChange = true;
463 if(this.classname == "body")
464 if(deathtype == DEATH_KILL.m_id)
466 // for the lemmings fans, a small harmless explosion
467 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
471 // print an obituary message
472 if(this.classname != "body")
473 Obituary(attacker, inflictor, this, deathtype, weaponentity);
475 // increment frag counter for used weapon type
476 Weapon w = DEATH_WEAPONOF(deathtype);
477 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
478 CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
480 this.respawn_time = 0;
481 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
482 damage = M_ARGV(4, float);
483 excess = max(0, damage - take - save);
485 //Weapon wep = this.(weaponentity).m_weapon;
486 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
488 .entity went = weaponentities[slot];
490 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
491 Weapon wep = this.(went).m_weapon;
492 wep.wr_playerdeath(wep, this, went);
495 RemoveGrapplingHooks(this);
497 Portal_ClearAllLater(this);
499 this.fixangle = true;
501 if(defer_ClientKill_Now_TeamChange)
502 ClientKill_Now_TeamChange(this); // can turn player into spectator
504 // player could have been miraculously resuscitated ;)
505 // e.g. players in freezetag get frozen, they don't really die
506 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
509 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
510 calculate_player_respawn_time(this);
512 // when we get here, player actually dies
514 Unfreeze(this, false); // remove any icy remains
517 WaypointSprite_PlayerDead(this);
519 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
521 .entity went = weaponentities[slot];
522 SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
525 // become fully visible
526 this.alpha = default_player_alpha;
527 // make the corpse upright (not tilted)
531 this.avelocity = '0 0 0';
532 // view from the floor
533 this.view_ofs = '0 0 -8';
534 if(this.move_movetype == MOVETYPE_NOCLIP)
536 // don't toss the corpse in this case, it can get stuck in solid (causing low fps)
537 // or fall indefinitely into the void if out of the map
538 this.velocity = '0 0 0';
543 set_movetype(this, MOVETYPE_TOSS);
546 this.solid = SOLID_CORPSE;
547 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
548 // don't stick to the floor
549 UNSET_ONGROUND(this);
551 this.deadflag = DEAD_DYING;
553 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
555 this.death_time = time;
557 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
559 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
561 // set damage function to corpse damage
562 this.event_damage = PlayerCorpseDamage;
563 this.event_heal = func_null;
564 // call the corpse damage function just in case it wants to gib
565 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
567 // set up to fade out later
568 SUB_SetFade (this, time + 6 + random (), 1);
569 // reset body think wrapper broken by SUB_SetFade
570 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
571 this.CopyBody_think = getthink(this);
572 this.CopyBody_nextthink = this.nextthink;
573 setthink(this, CopyBody_Think);
574 this.nextthink = time;
577 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
579 // clones don't run any animation code any more, so we must gib them when they die :(
580 this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
583 // reset fields the weapons may use just in case
584 if(this.classname != "body")
586 FOREACH(Weapons, it != WEP_Null,
588 it.wr_resetplayer(it, this);
589 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
591 ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
595 MUTATOR_CALLHOOK(PlayerDied, this);
599 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
601 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
604 GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);