1
0

Add entity invulnerable

This commit is contained in:
Howaner 2014-04-26 00:32:30 +02:00
parent 43cca14763
commit 7e76f030aa
22 changed files with 111 additions and 50 deletions

View File

@ -33,9 +33,12 @@ void cBoat::SpawnOn(cClientHandle & a_ClientHandle)
void cBoat::DoTakeDamage(TakeDamageInfo & TDI) bool cBoat::DoTakeDamage(TakeDamageInfo & TDI)
{ {
super::DoTakeDamage(TDI); if (!super::DoTakeDamage(TDI))
{
return false;
}
if (GetHealth() == 0) if (GetHealth() == 0)
{ {
@ -50,6 +53,7 @@ void cBoat::DoTakeDamage(TakeDamageInfo & TDI)
} }
Destroy(true); Destroy(true);
} }
return true;
} }

View File

@ -26,7 +26,7 @@ public:
// cEntity overrides: // cEntity overrides:
virtual void SpawnOn(cClientHandle & a_ClientHandle) override; virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
virtual void OnRightClicked(cPlayer & a_Player) override; virtual void OnRightClicked(cPlayer & a_Player) override;
virtual void DoTakeDamage(TakeDamageInfo & TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & TDI) override;
virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override; virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override;

View File

@ -60,6 +60,7 @@ cEntity::cEntity(eEntityType a_EntityType, double a_X, double a_Y, double a_Z, d
, m_Mass (0.001) // Default 1g , m_Mass (0.001) // Default 1g
, m_Width(a_Width) , m_Width(a_Width)
, m_Height(a_Height) , m_Height(a_Height)
, m_InvulnerableTicks(20)
{ {
cCSLock Lock(m_CSCount); cCSLock Lock(m_CSCount);
m_EntityCount++; m_EntityCount++;
@ -294,17 +295,23 @@ void cEntity::SetPitchFromSpeed(void)
void cEntity::DoTakeDamage(TakeDamageInfo & a_TDI) bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
if (cRoot::Get()->GetPluginManager()->CallHookTakeDamage(*this, a_TDI)) if (cRoot::Get()->GetPluginManager()->CallHookTakeDamage(*this, a_TDI))
{ {
return; return false;
} }
if (m_Health <= 0) if (m_Health <= 0)
{ {
// Can't take damage if already dead // Can't take damage if already dead
return; return false;
}
if (m_InvulnerableTicks > 0)
{
// Entity is invulnerable
return false;
} }
if ((a_TDI.Attacker != NULL) && (a_TDI.Attacker->IsPlayer())) if ((a_TDI.Attacker != NULL) && (a_TDI.Attacker->IsPlayer()))
@ -333,10 +340,13 @@ void cEntity::DoTakeDamage(TakeDamageInfo & a_TDI)
m_World->BroadcastEntityStatus(*this, esGenericHurt); m_World->BroadcastEntityStatus(*this, esGenericHurt);
m_InvulnerableTicks = 10;
if (m_Health <= 0) if (m_Health <= 0)
{ {
KilledBy(a_TDI.Attacker); KilledBy(a_TDI.Attacker);
} }
return true;
} }
@ -511,6 +521,11 @@ void cEntity::SetHealth(int a_Health)
void cEntity::Tick(float a_Dt, cChunk & a_Chunk) void cEntity::Tick(float a_Dt, cChunk & a_Chunk)
{ {
if (m_InvulnerableTicks > 0)
{
m_InvulnerableTicks--;
}
if (m_AttachedTo != NULL) if (m_AttachedTo != NULL)
{ {
if ((m_Pos - m_AttachedTo->GetPosition()).Length() > 0.5) if ((m_Pos - m_AttachedTo->GetPosition()).Length() > 0.5)

View File

@ -262,8 +262,8 @@ public:
// tolua_end // tolua_end
/// Makes this entity take damage specified in the a_TDI. The TDI is sent through plugins first, then applied /** Makes this entity take damage specified in the a_TDI. The TDI is sent through plugins first, then applied. If it returns false, the entity hasn't become any damage. */
virtual void DoTakeDamage(TakeDamageInfo & a_TDI); virtual bool DoTakeDamage(TakeDamageInfo & a_TDI);
// tolua_begin // tolua_begin
@ -391,6 +391,12 @@ public:
virtual bool IsSubmerged(void) const{ return m_IsSubmerged; } virtual bool IsSubmerged(void) const{ return m_IsSubmerged; }
/** Gets remaining air of a monster */ /** Gets remaining air of a monster */
int GetAirLevel(void) const { return m_AirLevel; } int GetAirLevel(void) const { return m_AirLevel; }
/** Gets the invulnerable ticks from the entity */
int GetInvulnerableTicks(void) const { return m_InvulnerableTicks; }
/** Set the invulnerable ticks from the entity */
void SetInvulnerableTicks(int a_InvulnerableTicks) { m_InvulnerableTicks = a_InvulnerableTicks; }
// tolua_end // tolua_end
@ -493,11 +499,14 @@ private:
// Measured in Kilograms (Kg) // Measured in Kilograms (Kg)
double m_Mass; double m_Mass;
/// Width of the entity, in the XZ plane. Since entities are represented as cylinders, this is more of a diameter. // Width of the entity, in the XZ plane. Since entities are represented as cylinders, this is more of a diameter.
double m_Width; double m_Width;
/// Height of the entity (Y axis) // Height of the entity (Y axis)
double m_Height; double m_Height;
// If a player hunt a entity, the entity become a invulnerable of 20 ticks
int m_InvulnerableTicks;
} ; // tolua_export } ; // tolua_export
typedef std::list<cEntity *> cEntityList; typedef std::list<cEntity *> cEntityList;

View File

@ -902,18 +902,21 @@ bool cMinecart::TestEntityCollision(NIBBLETYPE a_RailMeta)
void cMinecart::DoTakeDamage(TakeDamageInfo & TDI) bool cMinecart::DoTakeDamage(TakeDamageInfo & TDI)
{ {
if ((TDI.Attacker != NULL) && TDI.Attacker->IsPlayer() && ((cPlayer *)TDI.Attacker)->IsGameModeCreative()) if ((TDI.Attacker != NULL) && TDI.Attacker->IsPlayer() && ((cPlayer *)TDI.Attacker)->IsGameModeCreative())
{ {
Destroy(); Destroy();
TDI.FinalDamage = GetMaxHealth(); // Instant hit for creative TDI.FinalDamage = GetMaxHealth(); // Instant hit for creative
super::DoTakeDamage(TDI); SetInvulnerableTicks(0);
return; // No drops for creative return super::DoTakeDamage(TDI); // No drops for creative
} }
m_LastDamage = TDI.FinalDamage; m_LastDamage = TDI.FinalDamage;
super::DoTakeDamage(TDI); if (!super::DoTakeDamage(TDI))
{
return false;
}
m_World->BroadcastEntityMetadata(*this); m_World->BroadcastEntityMetadata(*this);
@ -952,12 +955,13 @@ void cMinecart::DoTakeDamage(TakeDamageInfo & TDI)
default: default:
{ {
ASSERT(!"Unhandled minecart type when spawning pickup!"); ASSERT(!"Unhandled minecart type when spawning pickup!");
return; return true;
} }
} }
m_World->SpawnItemPickups(Drops, GetPosX(), GetPosY(), GetPosZ()); m_World->SpawnItemPickups(Drops, GetPosX(), GetPosY(), GetPosZ());
} }
return true;
} }

View File

@ -36,7 +36,7 @@ public:
// cEntity overrides: // cEntity overrides:
virtual void SpawnOn(cClientHandle & a_ClientHandle) override; virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
virtual void HandlePhysics(float a_Dt, cChunk & a_Chunk) override; virtual void HandlePhysics(float a_Dt, cChunk & a_Chunk) override;
virtual void DoTakeDamage(TakeDamageInfo & TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & TDI) override;
virtual void Destroyed() override; virtual void Destroyed() override;
int LastDamage(void) const { return m_LastDamage; } int LastDamage(void) const { return m_LastDamage; }

View File

@ -808,14 +808,14 @@ void cPlayer::SetFlying(bool a_IsFlying)
void cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI) bool cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
if ((a_TDI.DamageType != dtInVoid) && (a_TDI.DamageType != dtPlugin)) if ((a_TDI.DamageType != dtInVoid) && (a_TDI.DamageType != dtPlugin))
{ {
if (IsGameModeCreative()) if (IsGameModeCreative())
{ {
// No damage / health in creative mode if not void or plugin damage // No damage / health in creative mode if not void or plugin damage
return; return false;
} }
} }
@ -828,17 +828,19 @@ void cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI)
if (!m_Team->AllowsFriendlyFire()) if (!m_Team->AllowsFriendlyFire())
{ {
// Friendly fire is disabled // Friendly fire is disabled
return; return false;
} }
} }
} }
super::DoTakeDamage(a_TDI); if (super::DoTakeDamage(a_TDI))
{
// Any kind of damage adds food exhaustion // Any kind of damage adds food exhaustion
AddFoodExhaustion(0.3f); AddFoodExhaustion(0.3f);
SendHealth();
SendHealth(); return true;
}
return false;
} }
@ -897,6 +899,7 @@ void cPlayer::KilledBy(cEntity * a_Killer)
void cPlayer::Respawn(void) void cPlayer::Respawn(void)
{ {
m_Health = GetMaxHealth(); m_Health = GetMaxHealth();
SetInvulnerableTicks(20);
// Reset food level: // Reset food level:
m_FoodLevel = MAX_FOOD_LEVEL; m_FoodLevel = MAX_FOOD_LEVEL;

View File

@ -498,7 +498,7 @@ protected:
virtual void Destroyed(void); virtual void Destroyed(void);
/** Filters out damage for creative mode/friendly fire */ /** Filters out damage for creative mode/friendly fire */
virtual void DoTakeDamage(TakeDamageInfo & TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & TDI) override;
/** Stops players from burning in creative mode */ /** Stops players from burning in creative mode */
virtual void TickBurning(cChunk & a_Chunk) override; virtual void TickBurning(cChunk & a_Chunk) override;

View File

@ -75,9 +75,12 @@ void cCreeper::GetDrops(cItems & a_Drops, cEntity * a_Killer)
void cCreeper::DoTakeDamage(TakeDamageInfo & a_TDI) bool cCreeper::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
super::DoTakeDamage(a_TDI); if (!super::DoTakeDamage(a_TDI))
{
return false;
}
if (a_TDI.DamageType == dtLightning) if (a_TDI.DamageType == dtLightning)
{ {
@ -85,6 +88,7 @@ void cCreeper::DoTakeDamage(TakeDamageInfo & a_TDI)
} }
m_World->BroadcastEntityMetadata(*this); m_World->BroadcastEntityMetadata(*this);
return true;
} }

View File

@ -18,7 +18,7 @@ public:
CLASS_PROTODEF(cCreeper); CLASS_PROTODEF(cCreeper);
virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = NULL) override; virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = NULL) override;
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
virtual void Attack(float a_Dt) override; virtual void Attack(float a_Dt) override;
virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
virtual void OnRightClicked(cPlayer & a_Player) override; virtual void OnRightClicked(cPlayer & a_Player) override;

View File

@ -457,9 +457,12 @@ int cMonster::FindFirstNonAirBlockPosition(double a_PosX, double a_PosZ)
void cMonster::DoTakeDamage(TakeDamageInfo & a_TDI) bool cMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
super::DoTakeDamage(a_TDI); if (!super::DoTakeDamage(a_TDI))
{
return false;
}
if((m_SoundHurt != "") && (m_Health > 0)) if((m_SoundHurt != "") && (m_Health > 0))
m_World->BroadcastSoundEffect(m_SoundHurt, (int)(GetPosX() * 8), (int)(GetPosY() * 8), (int)(GetPosZ() * 8), 1.0f, 0.8f); m_World->BroadcastSoundEffect(m_SoundHurt, (int)(GetPosX() * 8), (int)(GetPosY() * 8), (int)(GetPosZ() * 8), 1.0f, 0.8f);
@ -468,6 +471,7 @@ void cMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
m_Target = a_TDI.Attacker; m_Target = a_TDI.Attacker;
} }
return true;
} }

View File

@ -87,7 +87,7 @@ public:
virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
virtual void KilledBy(cEntity * a_Killer) override; virtual void KilledBy(cEntity * a_Killer) override;

View File

@ -19,9 +19,12 @@ cPassiveAggressiveMonster::cPassiveAggressiveMonster(const AString & a_ConfigNam
void cPassiveAggressiveMonster::DoTakeDamage(TakeDamageInfo & a_TDI) bool cPassiveAggressiveMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
super::DoTakeDamage(a_TDI); if (!super::DoTakeDamage(a_TDI))
{
return false;
}
if ((m_Target != NULL) && (m_Target->IsPlayer())) if ((m_Target != NULL) && (m_Target->IsPlayer()))
{ {
@ -30,6 +33,7 @@ void cPassiveAggressiveMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
m_EMState = CHASING; m_EMState = CHASING;
} }
} }
return true;
} }

View File

@ -15,7 +15,7 @@ class cPassiveAggressiveMonster :
public: public:
cPassiveAggressiveMonster(const AString & a_ConfigName, eType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, double a_Width, double a_Height); cPassiveAggressiveMonster(const AString & a_ConfigName, eType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, double a_Width, double a_Height);
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
} ; } ;

View File

@ -18,13 +18,17 @@ cPassiveMonster::cPassiveMonster(const AString & a_ConfigName, eType a_MobType,
void cPassiveMonster::DoTakeDamage(TakeDamageInfo & a_TDI) bool cPassiveMonster::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
super::DoTakeDamage(a_TDI); if (!super::DoTakeDamage(a_TDI))
{
return false;
}
if ((a_TDI.Attacker != this) && (a_TDI.Attacker != NULL)) if ((a_TDI.Attacker != this) && (a_TDI.Attacker != NULL))
{ {
m_EMState = ESCAPING; m_EMState = ESCAPING;
} }
return true;
} }

View File

@ -18,7 +18,7 @@ public:
virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
/// When hit by someone, run away /// When hit by someone, run away
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
/** Returns the item that the animal of this class follows when a player holds it in hand /** Returns the item that the animal of this class follows when a player holds it in hand
Return an empty item not to follow (default). */ Return an empty item not to follow (default). */
virtual const cItem GetFollowedItem(void) const { return cItem(); } virtual const cItem GetFollowedItem(void) const { return cItem(); }

View File

@ -23,9 +23,13 @@ cVillager::cVillager(eVillagerType VillagerType) :
void cVillager::DoTakeDamage(TakeDamageInfo & a_TDI) bool cVillager::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
super::DoTakeDamage(a_TDI); if (!super::DoTakeDamage(a_TDI))
{
return false;
}
if ((a_TDI.Attacker != NULL) && a_TDI.Attacker->IsPlayer()) if ((a_TDI.Attacker != NULL) && a_TDI.Attacker->IsPlayer())
{ {
if (m_World->GetTickRandomNumber(5) == 3) if (m_World->GetTickRandomNumber(5) == 3)
@ -33,6 +37,7 @@ void cVillager::DoTakeDamage(TakeDamageInfo & a_TDI)
m_World->BroadcastEntityStatus(*this, esVillagerAngry); m_World->BroadcastEntityStatus(*this, esVillagerAngry);
} }
} }
return true;
} }

View File

@ -30,7 +30,7 @@ public:
CLASS_PROTODEF(cVillager); CLASS_PROTODEF(cVillager);
// cEntity overrides // cEntity overrides
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
virtual void Tick (float a_Dt, cChunk & a_Chunk) override; virtual void Tick (float a_Dt, cChunk & a_Chunk) override;
// cVillager functions // cVillager functions

View File

@ -40,24 +40,24 @@ bool cWither::Initialize(cWorld * a_World)
void cWither::DoTakeDamage(TakeDamageInfo & a_TDI) bool cWither::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
if (a_TDI.DamageType == dtDrowning) if (a_TDI.DamageType == dtDrowning)
{ {
return; return false;
} }
if (m_InvulnerableTicks > 0) if (m_InvulnerableTicks > 0)
{ {
return; return false;
} }
if (IsArmored() && (a_TDI.DamageType == dtRangedAttack)) if (IsArmored() && (a_TDI.DamageType == dtRangedAttack))
{ {
return; return false;
} }
super::DoTakeDamage(a_TDI); return super::DoTakeDamage(a_TDI);
} }

View File

@ -27,7 +27,7 @@ public:
// cEntity overrides // cEntity overrides
virtual bool Initialize(cWorld * a_World) override; virtual bool Initialize(cWorld * a_World) override;
virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = NULL) override; virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = NULL) override;
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
private: private:

View File

@ -25,14 +25,19 @@ cWolf::cWolf(void) :
void cWolf::DoTakeDamage(TakeDamageInfo & a_TDI) bool cWolf::DoTakeDamage(TakeDamageInfo & a_TDI)
{ {
super::DoTakeDamage(a_TDI); if (super::DoTakeDamage(a_TDI))
{
return false;
}
if (!m_IsTame) if (!m_IsTame)
{ {
m_IsAngry = true; m_IsAngry = true;
} }
m_World->BroadcastEntityMetadata(*this); // Broadcast health and possibly angry face m_World->BroadcastEntityMetadata(*this); // Broadcast health and possibly angry face
return true;
} }

View File

@ -18,7 +18,7 @@ public:
CLASS_PROTODEF(cWolf); CLASS_PROTODEF(cWolf);
virtual void DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
virtual void OnRightClicked(cPlayer & a_Player) override; virtual void OnRightClicked(cPlayer & a_Player) override;
virtual void Tick(float a_Dt, cChunk & a_Chunk) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
virtual void TickFollowPlayer(); virtual void TickFollowPlayer();