3 #include <common/effects/all.qh>
7 #include "clientkill.qh"
10 #include "miscfunctions.qh"
12 #include "teamplay.qh"
13 #include <server/sv_main.qh>
14 #include "weapons/throwing.qh"
15 #include "command/common.qh"
16 #include "command/vote.qh"
17 #include "../common/state.qh"
18 #include "../common/anim.qh"
19 #include "../common/animdecide.qh"
20 #include "../common/csqcmodel_settings.qh"
21 #include "../common/gamemodes/sv_rules.qh"
22 #include "../common/deathtypes/all.qh"
23 #include "../common/mapobjects/subs.qh"
24 #include <common/mapobjects/teleporters.qh>
25 #include "../common/playerstats.qh"
26 #include "../lib/csqcmodel/sv_model.qh"
28 #include "../common/minigames/sv_minigames.qh"
30 #include <common/gamemodes/_mod.qh>
32 #include "../common/physics/player.qh"
33 #include "../common/effects/qc/_mod.qh"
34 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
35 #include "../common/mapobjects/_mod.qh"
36 #include "../common/wepent.qh"
38 #include "weapons/weaponstats.qh"
39 #include <server/weapons/weaponsystem.qh>
41 #include "../common/animdecide.qh"
43 void Drop_Special_Items(entity player)
45 // called when the player has become stuck or frozen
46 // so objective items aren't stuck with the player
48 MUTATOR_CALLHOOK(DropSpecialItems, player);
51 void CopyBody_Think(entity this)
53 if(this.CopyBody_nextthink && time > this.CopyBody_nextthink)
55 this.CopyBody_think(this);
58 this.CopyBody_nextthink = this.nextthink;
59 this.CopyBody_think = getthink(this);
60 setthink(this, CopyBody_Think);
62 CSQCMODEL_AUTOUPDATE(this);
63 this.nextthink = time;
65 void CopyBody(entity this, float keepvelocity)
67 if (this.effects & EF_NODRAW)
69 entity clone = new(body);
72 clone.colormap = this.colormap;
73 clone.iscreature = this.iscreature;
74 clone.teleportable = this.teleportable;
75 clone.damagedbycontents = this.damagedbycontents;
76 if(clone.damagedbycontents)
77 IL_PUSH(g_damagedbycontents, clone);
78 clone.angles = this.angles;
79 clone.v_angle = this.v_angle;
80 clone.avelocity = this.avelocity;
81 clone.damageforcescale = this.damageforcescale;
82 clone.effects = this.effects;
83 clone.glowmod = this.glowmod;
84 clone.event_damage = this.event_damage;
85 clone.event_heal = this.event_heal;
86 clone.anim_state = this.anim_state;
87 clone.anim_time = this.anim_time;
88 clone.anim_lower_action = this.anim_lower_action;
89 clone.anim_lower_time = this.anim_lower_time;
90 clone.anim_upper_action = this.anim_upper_action;
91 clone.anim_upper_time = this.anim_upper_time;
92 clone.anim_implicit_state = this.anim_implicit_state;
93 clone.anim_implicit_time = this.anim_implicit_time;
94 clone.anim_lower_implicit_action = this.anim_lower_implicit_action;
95 clone.anim_lower_implicit_time = this.anim_lower_implicit_time;
96 clone.anim_upper_implicit_action = this.anim_upper_implicit_action;
97 clone.anim_upper_implicit_time = this.anim_upper_implicit_time;
98 clone.dphitcontentsmask = this.dphitcontentsmask;
99 clone.death_time = this.death_time;
100 clone.pain_finished = this.pain_finished;
101 SetResourceExplicit(clone, RES_HEALTH, GetResource(this, RES_HEALTH));
102 SetResourceExplicit(clone, RES_ARMOR, GetResource(this, RES_ARMOR));
103 clone.armortype = this.armortype;
104 clone.model = this.model;
105 clone.modelindex = this.modelindex;
106 clone.skin = this.skin;
107 clone.species = this.species;
108 clone.move_qcphysics = false; // don't run gamecode logic on clones, too many
109 set_movetype(clone, this.move_movetype);
110 clone.solid = this.solid;
111 clone.takedamage = this.takedamage;
112 setcefc(clone, getcefc(this));
113 clone.uncustomizeentityforclient = this.uncustomizeentityforclient;
114 clone.uncustomizeentityforclient_set = this.uncustomizeentityforclient_set;
115 if (keepvelocity == 1)
116 clone.velocity = this.velocity;
117 clone.oldvelocity = clone.velocity;
118 clone.alpha = this.alpha;
119 clone.fade_time = this.fade_time;
120 clone.fade_rate = this.fade_rate;
121 //clone.weapon = this.weapon;
122 setorigin(clone, this.origin);
123 setsize(clone, this.mins, this.maxs);
124 clone.reset = SUB_Remove;
125 clone._ps = this._ps;
127 Drag_MoveDrag(this, clone);
129 if(clone.colormap <= maxclients && clone.colormap > 0)
130 clone.colormap = 1024 + this.clientcolors;
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);
141 IL_PUSH(g_clones, clone);
143 MUTATOR_CALLHOOK(CopyBody, this, clone, keepvelocity);
146 void player_setupanimsformodel(entity this)
148 // load animation info
149 animdecide_load_if_needed(this);
150 animdecide_setstate(this, 0, false);
153 void player_anim(entity this)
155 int deadbits = (this.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
158 // Decide on which death animation to use.
160 deadbits = ANIMSTATE_DEAD1;
162 deadbits = ANIMSTATE_DEAD2;
165 // Clear a previous death animation.
168 int animbits = deadbits;
169 if(STAT(FROZEN, this))
170 animbits |= ANIMSTATE_FROZEN;
171 if(this.move_movetype == MOVETYPE_FOLLOW)
172 animbits |= ANIMSTATE_FOLLOW;
174 animbits |= ANIMSTATE_DUCK;
175 animdecide_setstate(this, animbits, false);
176 animdecide_setimplicitstate(this, IS_ONGROUND(this));
179 void PlayerCorpseDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
183 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
185 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
189 if(sound_allowed(MSG_BROADCAST, attacker))
192 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
194 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
196 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);
200 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
202 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
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);
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;
213 if (GetResource(this, RES_HEALTH) <= -autocvar_sv_gibhealth && this.alpha >= 0)
215 // don't use any animations as a gib
217 // view just above the floor
218 this.view_ofs = '0 0 4';
220 Violence_GibSplash(this, 1, 1, attacker);
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;
230 void PlayerDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, .entity weaponentity, vector hitloc, vector force)
233 float dh = max(GetResource(this, RES_HEALTH), 0);
234 float da = max(GetResource(this, RES_ARMOR), 0);
235 float take = 0, save = 0;
239 if(!DEATH_ISSPECIAL(deathtype))
241 damage *= Handicap_GetTotalHandicap(this);
242 if (this != attacker && IS_PLAYER(attacker))
244 damage /= Handicap_GetTotalHandicap(attacker);
248 if (time < this.spawnshieldtime && autocvar_g_spawnshield_blockdamage < 1)
249 damage *= 1 - max(0, autocvar_g_spawnshield_blockdamage);
251 if(deathtype & HITTYPE_SOUND) // sound based attacks cause bleeding from the ears
257 ear1_z += 0.125 * this.view_ofs.z + 0.875 * this.maxs.z; // 7/8
259 makevectors(this.angles);
260 ear1 += v_right * -10;
261 ear2 += v_right * +10;
262 d = inflictor.origin - this.origin;
264 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
266 f = 0; // Assum ecenter.
267 force = v_right * vlen(force);
268 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), this, attacker);
269 Violence_GibSplash_At(ear2, force, 2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), this, attacker);
278 // force is already good
282 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, this, attacker);
284 v = healtharmor_applydamage(GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, deathtype, damage);
291 // don't reset pushltime for self damage as it may be an attempt to
292 // escape a lava pit or similar
293 //this.pushltime = 0;
296 else if(IS_PLAYER(attacker))
298 this.pusher = attacker;
299 this.pushltime = time + autocvar_g_maxpushtime;
300 this.istypefrag = PHYS_INPUT_BUTTON_CHAT(this);
302 else if(time < this.pushltime)
304 attacker = this.pusher;
305 this.pushltime = max(this.pushltime, time + 0.6);
313 MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, this, force, take, save, deathtype, damage);
314 take = bound(0, M_ARGV(4, float), GetResource(this, RES_HEALTH));
315 save = bound(0, M_ARGV(5, float), GetResource(this, RES_ARMOR));
316 float excess = max(0, damage - take - save);
318 if(sound_allowed(MSG_BROADCAST, attacker))
320 if (save > 10 && (dh - take) > 0) // don't play armor sound if the attack is fatal
321 sound (this, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
323 sound (this, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
325 sound (this, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
329 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, this, attacker);
331 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, this, attacker);
333 if (time >= this.spawnshieldtime || autocvar_g_spawnshield_blockdamage < 1)
335 if (!(this.flags & FL_GODMODE))
337 TakeResource(this, RES_ARMOR, save);
338 TakeResource(this, RES_HEALTH, take);
339 // pause regeneration for 5 seconds
341 this.pauseregen_finished = max(this.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
343 if (time > this.pain_finished) //Don't switch pain sequences like crazy
345 this.pain_finished = time + 0.5; //Supajoe
347 if(autocvar_sv_gentle < 1) {
348 if(this.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
350 if (!this.animstate_override)
353 animdecide_setaction(this, ANIMACTION_PAIN1, true);
355 animdecide_setaction(this, ANIMACTION_PAIN2, true);
358 float myhp = GetResource(this, RES_HEALTH);
360 if(myhp < 25 || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || take > 20 || attacker != this)
361 if(sound_allowed(MSG_BROADCAST, attacker))
362 // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
364 if(deathtype == DEATH_FALL.m_id)
365 PlayerSound(this, playersound_fall, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
367 PlayerSound(this, playersound_pain100, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
369 PlayerSound(this, playersound_pain75, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
371 PlayerSound(this, playersound_pain50, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
373 PlayerSound(this, playersound_pain25, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
378 // throw off bot aim temporarily
380 if(IS_BOT_CLIENT(this) && GetResource(this, RES_HEALTH) >= 1)
382 shake = damage * 5 / (bound(0,skill,100) + 1);
383 this.v_angle_x = this.v_angle.x + (random() * 2 - 1) * shake;
384 this.v_angle_y = this.v_angle.y + (random() * 2 - 1) * shake;
385 this.v_angle_x = bound(-90, this.v_angle.x, 90);
388 float realdmg = damage - excess;
389 if (this != attacker && realdmg)
390 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
392 if (IS_PLAYER(attacker) && DIFF_TEAM(attacker, this)) {
393 GameRules_scoring_add(attacker, DMG, realdmg);
395 if (IS_PLAYER(this)) {
396 GameRules_scoring_add(this, DMGTAKEN, realdmg);
401 this.max_armorvalue += (save + take);
403 this.dmg_save = this.dmg_save + save;//max(save - 10, 0);
404 this.dmg_take = this.dmg_take + take;//max(take - 10, 0);
405 this.dmg_inflictor = inflictor;
407 bool abot = (IS_BOT_CLIENT(attacker));
408 bool vbot = (IS_BOT_CLIENT(this));
410 bool valid_damage_for_weaponstats = false;
411 Weapon awep = WEP_Null;
413 if (!(round_handler_IsActive() && !round_handler_IsRoundStarted()) && time >= game_starttime)
414 if(vbot || IS_REAL_CLIENT(this))
415 if(abot || IS_REAL_CLIENT(attacker))
416 if(attacker && this != attacker)
417 if(DIFF_TEAM(this, attacker))
419 if(DEATH_ISSPECIAL(deathtype))
420 awep = attacker.(weaponentity).m_weapon;
422 awep = DEATH_WEAPONOF(deathtype);
423 valid_damage_for_weaponstats = true;
426 dh = dh - max(GetResource(this, RES_HEALTH), 0);
427 da = da - max(GetResource(this, RES_ARMOR), 0);
428 if(valid_damage_for_weaponstats)
430 WeaponStats_LogDamage(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot, dh + da);
433 MUTATOR_CALLHOOK(PlayerDamaged, attacker, this, dh, da, hitloc, deathtype, damage);
435 if (GetResource(this, RES_HEALTH) < 1)
437 float defer_ClientKill_Now_TeamChange;
438 defer_ClientKill_Now_TeamChange = false;
442 PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
446 if(valid_damage_for_weaponstats)
447 WeaponStats_LogKill(awep.m_id, abot, this.(weaponentity).m_weapon.m_id, vbot);
449 if(autocvar_sv_gentle < 1)
450 if(sound_allowed(MSG_BROADCAST, attacker))
452 if(deathtype == DEATH_DROWN.m_id)
453 PlayerSound(this, playersound_drown, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
455 PlayerSound(this, playersound_death, CH_PAIN, VOL_BASE, VOICETYPE_PLAYERSOUND);
458 // get rid of kill indicator
459 if(this.killindicator)
461 delete(this.killindicator);
462 this.killindicator = NULL;
463 if(this.killindicator_teamchange)
464 defer_ClientKill_Now_TeamChange = true;
466 if(this.classname == "body")
467 if(deathtype == DEATH_KILL.m_id)
469 // for the lemmings fans, a small harmless explosion
470 Send_Effect(EFFECT_ROCKET_EXPLODE, this.origin, '0 0 0', 1);
474 // print an obituary message
475 if(this.classname != "body")
476 Obituary(attacker, inflictor, this, deathtype, weaponentity);
478 // increment frag counter for used weapon type
479 Weapon w = DEATH_WEAPONOF(deathtype);
480 if(w != WEP_Null && accuracy_isgooddamage(attacker, this))
481 CS(attacker).accuracy.(accuracy_frags[w.m_id-1]) += 1;
483 this.respawn_time = 0;
484 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, this, deathtype, damage);
485 damage = M_ARGV(4, float);
486 excess = max(0, damage - take - save);
488 //Weapon wep = this.(weaponentity).m_weapon;
489 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
491 .entity went = weaponentities[slot];
493 continue; // TODO: clones have no weapon, but we don't want to have to check this all the time
494 Weapon wep = this.(went).m_weapon;
495 wep.wr_playerdeath(wep, this, went);
498 RemoveGrapplingHooks(this);
500 Portal_ClearAllLater(this);
502 this.fixangle = true;
504 if(defer_ClientKill_Now_TeamChange)
505 ClientKill_Now_TeamChange(this); // can turn player into spectator
507 // player could have been miraculously resuscitated ;)
508 // e.g. players in freezetag get frozen, they don't really die
509 if(GetResource(this, RES_HEALTH) >= 1 || !(IS_PLAYER(this) || this.classname == "body"))
512 if (!this.respawn_time) // can be set in the mutator hook PlayerDies
513 calculate_player_respawn_time(this);
515 // when we get here, player actually dies
517 Unfreeze(this, false); // remove any icy remains
520 WaypointSprite_PlayerDead(this);
522 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
524 .entity went = weaponentities[slot];
525 SpawnThrownWeapon(this, this.origin + (this.mins + this.maxs) * 0.5, this.(went).m_weapon, went);
528 // become fully visible
529 this.alpha = default_player_alpha;
530 // make the corpse upright (not tilted)
534 this.avelocity = '0 0 0';
535 // view from the floor
536 this.view_ofs = '0 0 -8';
537 if(this.move_movetype == MOVETYPE_NOCLIP)
539 // don't toss the corpse in this case, it can get stuck in solid (causing low fps)
540 // or fall indefinitely into the void if out of the map
541 this.velocity = '0 0 0';
546 set_movetype(this, MOVETYPE_TOSS);
549 this.solid = SOLID_CORPSE;
550 PS(this).ballistics_density = autocvar_g_ballistics_density_corpse;
551 // don't stick to the floor
552 UNSET_ONGROUND(this);
554 this.deadflag = DEAD_DYING;
556 STAT(MOVEVARS_SPECIALCOMMAND, this) = false; // sweet release
558 this.death_time = time;
560 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD1, true);
562 animdecide_setstate(this, this.anim_state | ANIMSTATE_DEAD2, true);
564 // set damage function to corpse damage
565 this.event_damage = PlayerCorpseDamage;
566 this.event_heal = func_null;
567 // call the corpse damage function just in case it wants to gib
568 this.event_damage(this, inflictor, attacker, excess, deathtype, weaponentity, hitloc, force);
570 // set up to fade out later
571 SUB_SetFade (this, time + 6 + random (), 1);
572 // reset body think wrapper broken by SUB_SetFade
573 if(this.classname == "body" && getthink(this) != CopyBody_Think) {
574 this.CopyBody_think = getthink(this);
575 this.CopyBody_nextthink = this.nextthink;
576 setthink(this, CopyBody_Think);
577 this.nextthink = time;
580 if(autocvar_sv_gentle > 0 || autocvar_ekg || this.classname == "body") {
582 // clones don't run any animation code any more, so we must gib them when they die :(
583 this.event_damage(this, inflictor, attacker, autocvar_sv_gibhealth + 1, deathtype, weaponentity, hitloc, force);
586 // reset fields the weapons may use just in case
587 if(this.classname != "body")
589 FOREACH(Weapons, it != WEP_Null,
591 it.wr_resetplayer(it, this);
592 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
594 ATTACK_FINISHED_FOR(this, it.m_id, slot) = 0;
598 MUTATOR_CALLHOOK(PlayerDied, this);
602 bool PlayerHeal(entity targ, entity inflictor, float amount, float limit)
604 if(GetResource(targ, RES_HEALTH) <= 0 || GetResource(targ, RES_HEALTH) >= limit)
607 GiveResourceWithLimit(targ, RES_HEALTH, amount, limit);