Skip to content

Commit

Permalink
Store Health as a float (#4073)
Browse files Browse the repository at this point in the history
* Fix #4024

* Fix clang error

* Add comment

* Fix behaviour

* Save Health as float

* Changed m_Health to float

* Remove redundant static_cast

* Fix casts
  • Loading branch information
stone3311 authored and bearbin committed Nov 22, 2017
1 parent ef091fe commit 0dd172b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/Entities/Entity.cpp
Expand Up @@ -528,9 +528,9 @@ bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI)
Player->GetStatManager().AddValue(statDamageDealt, static_cast<StatValue>(floor(a_TDI.FinalDamage * 10 + 0.5)));
}

m_Health -= static_cast<short>(a_TDI.FinalDamage);
m_Health -= static_cast<float>(a_TDI.FinalDamage);

m_Health = std::max(m_Health, 0);
m_Health = std::max(m_Health, 0.0f);

// Add knockback:
if ((IsMob() || IsPlayer()) && (a_TDI.Attacker != nullptr))
Expand Down Expand Up @@ -810,9 +810,9 @@ void cEntity::Heal(int a_HitPoints)



void cEntity::SetHealth(int a_Health)
void cEntity::SetHealth(float a_Health)
{
m_Health = Clamp(a_Health, 0, m_MaxHealth);
m_Health = Clamp(a_Health, 0.0f, m_MaxHealth);
}


Expand Down Expand Up @@ -1782,7 +1782,7 @@ void cEntity::OnFinishedBurning(void)



void cEntity::SetMaxHealth(int a_MaxHealth)
void cEntity::SetMaxHealth(float a_MaxHealth)
{
m_MaxHealth = a_MaxHealth;

Expand Down
12 changes: 6 additions & 6 deletions src/Entities/Entity.h
Expand Up @@ -368,10 +368,10 @@ class cEntity
virtual void Heal(int a_HitPoints);

/** Returns the health of this entity */
int GetHealth(void) const { return m_Health; }
float GetHealth(void) const { return m_Health; }

/** Sets the health of this entity; doesn't broadcast any hurt animation */
void SetHealth(int a_Health);
void SetHealth(float a_Health);

// tolua_end

Expand Down Expand Up @@ -403,9 +403,9 @@ class cEntity
// tolua_begin

/** Sets the maximum value for the health */
void SetMaxHealth(int a_MaxHealth);
void SetMaxHealth(float a_MaxHealth);

int GetMaxHealth(void) const { return m_MaxHealth; }
float GetMaxHealth(void) const { return m_MaxHealth; }

/** Sets whether the entity is fireproof */
void SetIsFireproof(bool a_IsFireproof);
Expand Down Expand Up @@ -556,8 +556,8 @@ class cEntity
Note that the UniqueID is not persisted through storage. */
UInt32 m_UniqueID;

int m_Health;
int m_MaxHealth;
float m_Health;
float m_MaxHealth;

/** The entity to which this entity is attached (vehicle), nullptr if none */
cEntity * m_AttachedTo;
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Minecart.cpp
Expand Up @@ -1035,7 +1035,7 @@ bool cMinecart::DoTakeDamage(TakeDamageInfo & TDI)
if ((TDI.Attacker != nullptr) && TDI.Attacker->IsPlayer() && static_cast<cPlayer *>(TDI.Attacker)->IsGameModeCreative())
{
Destroy();
TDI.FinalDamage = GetMaxHealth(); // Instant hit for creative
TDI.FinalDamage = static_cast<int>(GetMaxHealth()); // Instant hit for creative
SetInvulnerableTicks(0);
return super::DoTakeDamage(TDI); // No drops for creative
}
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Protocol_1_8.cpp
Expand Up @@ -3310,7 +3310,7 @@ void cProtocol_1_8_0::WriteEntityMetadata(cPacketizer & a_Pkt, const cEntity & a
Health: 1 | 3 - (1 - 3) = 5
*/
auto & Minecart = reinterpret_cast<const cMinecart &>(a_Entity);
a_Pkt.WriteBEInt32((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4);
a_Pkt.WriteBEInt32(static_cast<Int32>((((a_Entity.GetMaxHealth() / 2) - (a_Entity.GetHealth() - (a_Entity.GetMaxHealth() / 2))) * Minecart.LastDamage()) * 4));
a_Pkt.WriteBEUInt8(0x52);
a_Pkt.WriteBEInt32(1); // Shaking direction, doesn't seem to affect anything
a_Pkt.WriteBEUInt8(0x73);
Expand Down
2 changes: 1 addition & 1 deletion src/WorldStorage/NBTChunkSerializer.cpp
Expand Up @@ -431,7 +431,7 @@ void cNBTChunkSerializer::AddBasicEntity(cEntity * a_Entity, const AString & a_C
m_Writer.AddDouble("", a_Entity->GetYaw());
m_Writer.AddDouble("", a_Entity->GetPitch());
m_Writer.EndList();
m_Writer.AddShort("Health", static_cast<Int16>(a_Entity->GetHealth()));
m_Writer.AddFloat("Health", a_Entity->GetHealth());
}


Expand Down
23 changes: 21 additions & 2 deletions src/WorldStorage/WSSAnvil.cpp
Expand Up @@ -3154,9 +3154,28 @@ bool cWSSAnvil::LoadEntityBaseFromNBT(cEntity & a_Entity, const cParsedNBT & a_N
a_Entity.SetYaw(Rotation[0]);
a_Entity.SetRoll(Rotation[1]);

// Load health:
// Depending on the Minecraft version, the entity's health is
// stored either as a float Health tag (HealF prior to 1.9) or
// as a short Health tag. The float tags should be preferred.
int Health = a_NBT.FindChildByName(a_TagIdx, "Health");
a_Entity.SetHealth(Health > 0 ? a_NBT.GetShort(Health) : a_Entity.GetMaxHealth());
int HealF = a_NBT.FindChildByName(a_TagIdx, "HealF");

if (Health > 0 && a_NBT.GetType(Health) == TAG_Float)
{
a_Entity.SetHealth(a_NBT.GetFloat(Health));
}
else if (HealF > 0)
{
a_Entity.SetHealth(a_NBT.GetFloat(HealF));
}
else if (Health > 0)
{
a_Entity.SetHealth(static_cast<float>(a_NBT.GetShort(Health)));
}
else
{
a_Entity.SetHealth(a_Entity.GetMaxHealth());
}

return true;
}
Expand Down

0 comments on commit 0dd172b

Please sign in to comment.