1
0
Fork 0

Store Health as a float (#4073)

* Fix #4024

* Fix clang error

* Add comment

* Fix behaviour

* Save Health as float

* Changed m_Health to float

* Remove redundant static_cast

* Fix casts
This commit is contained in:
Fabian 2017-11-22 14:47:52 +01:00 committed by Alexander Harkness
parent ef091fe24b
commit 0dd172b80f
6 changed files with 35 additions and 16 deletions

View File

@ -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))
@ -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);
}
@ -1782,7 +1782,7 @@ void cEntity::OnFinishedBurning(void)
void cEntity::SetMaxHealth(int a_MaxHealth)
void cEntity::SetMaxHealth(float a_MaxHealth)
{
m_MaxHealth = a_MaxHealth;

View File

@ -368,10 +368,10 @@ public:
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
@ -403,9 +403,9 @@ public:
// 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);
@ -556,8 +556,8 @@ protected:
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;

View File

@ -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
}

View File

@ -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);

View File

@ -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());
}

View File

@ -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;
}