Skip to content

Commit

Permalink
PlayerSAO: Run on_player_hpchange raw change values (#10478)
Browse files Browse the repository at this point in the history
The callback is only run when a change in HP is to be expected.
Following cases will not trigger the callback:
 * Dead player damaged further
 * Healing full-health player
 * Change of 0 HP
  • Loading branch information
SmallJoker committed Nov 12, 2020
1 parent be8d1d2 commit adffef2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/server/player_sao.cpp
Expand Up @@ -458,20 +458,25 @@ u16 PlayerSAO::punch(v3f dir,

void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
{
s32 oldhp = m_hp;
if (hp == (s32)m_hp)
return; // Nothing to do

hp = rangelim(hp, 0, m_prop.hp_max);
if (m_hp <= 0 && hp < (s32)m_hp)
return; // Cannot take more damage

if (oldhp != hp) {
s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp, reason);
{
s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - m_hp, reason);
if (hp_change == 0)
return;

hp = rangelim(oldhp + hp_change, 0, m_prop.hp_max);
hp = m_hp + hp_change;
}

s32 oldhp = m_hp;
hp = rangelim(hp, 0, m_prop.hp_max);

if (hp < oldhp && isImmortal())
return;
return; // Do not allow immortal players to be damaged

m_hp = hp;

Expand Down

0 comments on commit adffef2

Please sign in to comment.