diff options
author | LogicParrot <LogicParrot@users.noreply.github.com> | 2017-09-02 13:47:56 +0200 |
---|---|---|
committer | LogicParrot <LogicParrot@users.noreply.github.com> | 2017-09-02 13:47:56 +0200 |
commit | 0710995922546a904d5d018d2d5505017e24c0fc (patch) | |
tree | 101db7e8a701edfe965d9ee8a25cb9a3191cdcda /src | |
parent | Lambda revert fix (diff) | |
download | cuberite-0710995922546a904d5d018d2d5505017e24c0fc.tar cuberite-0710995922546a904d5d018d2d5505017e24c0fc.tar.gz cuberite-0710995922546a904d5d018d2d5505017e24c0fc.tar.bz2 cuberite-0710995922546a904d5d018d2d5505017e24c0fc.tar.lz cuberite-0710995922546a904d5d018d2d5505017e24c0fc.tar.xz cuberite-0710995922546a904d5d018d2d5505017e24c0fc.tar.zst cuberite-0710995922546a904d5d018d2d5505017e24c0fc.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Entities/Entity.cpp | 10 | ||||
-rw-r--r-- | src/Entities/Entity.h | 2 | ||||
-rw-r--r-- | src/Mobs/Behaviors/BehaviorAttacker.cpp | 41 | ||||
-rw-r--r-- | src/Mobs/Behaviors/BehaviorAttacker.h | 4 |
4 files changed, 43 insertions, 14 deletions
diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 5f6b81497..9ac8d2e3c 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -406,6 +406,16 @@ void cEntity::SetPitchFromSpeed(void) +const Vector3i cEntity::GetHeadPosition(void) +{ + int HeadHeight = CeilC(GetPosY() + GetHeight()) - 1; + return Vector3i(POSX_TOINT, HeadHeight, POSZ_TOINT); +} + + + + + bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI) { if (m_Health <= 0) diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index f39039183..3a4898757 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -303,6 +303,8 @@ public: /** Exported in ManualBindings */ const Vector3d & GetPosition(void) const { return m_Position; } + const Vector3i GetHeadPosition(void); + /** Exported in ManualBindings */ const Vector3d & GetSpeed(void) const { return m_Speed; } diff --git a/src/Mobs/Behaviors/BehaviorAttacker.cpp b/src/Mobs/Behaviors/BehaviorAttacker.cpp index 280914f09..2365ee3a3 100644 --- a/src/Mobs/Behaviors/BehaviorAttacker.cpp +++ b/src/Mobs/Behaviors/BehaviorAttacker.cpp @@ -55,6 +55,12 @@ void cBehaviorAttacker::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) UNUSED(a_Dt); UNUSED(a_Chunk); + if (m_Parent->GetHealth() <= 0.0) + { + // mobTodo put this elsewhere? + return; + } + if (m_IsStriking) { if (StrikeTarget(++m_StrikeTickCnt)) @@ -91,13 +97,13 @@ void cBehaviorAttacker::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) if (GetTarget() != nullptr) { m_Parent->SetLookingAt(m_Target); - if (TargetOutOfSight()) + if (TargetTooFar()) { SetTarget(nullptr); } else { - if (TargetIsInStrikeRadiusAndLineOfSight()) + if (TargetIsInStrikeRadius() && TargetIsInLineOfSight()) { m_Parent->StopMovingToPosition(); StrikeTargetIfReady(); @@ -216,6 +222,7 @@ void cBehaviorAttacker::StrikeTarget() bool cBehaviorAttacker::TargetIsInStrikeRadius(void) { ASSERT(GetTarget() != nullptr); + ASSERT(m_Parent != nullptr); return ((GetTarget()->GetPosition() - m_Parent->GetPosition()).SqrLength() < (m_AttackRange * m_AttackRange)); } @@ -223,34 +230,44 @@ bool cBehaviorAttacker::TargetIsInStrikeRadius(void) -bool cBehaviorAttacker::TargetIsInStrikeRadiusAndLineOfSight() +bool cBehaviorAttacker::TargetIsInLineOfSight() { - ASSERT(m_Target != nullptr); + ASSERT(GetTarget() != nullptr); ASSERT(m_Parent != nullptr); + // mobtodo make this smarter + cTracer LineOfSight(m_Parent->GetWorld()); - Vector3d MyHeadPosition = m_Parent->GetPosition() + Vector3d(0, m_Parent->GetHeight(), 0); - Vector3d AttackDirection(GetTarget()->GetPosition() + Vector3d(0, GetTarget()->GetHeight(), 0) - MyHeadPosition); + Vector3d MyHeadPosition = m_Parent->GetHeadPosition(); + Vector3d TargetHeadPosition = GetTarget()->GetHeadPosition(); + Vector3d AttackDirection1(TargetHeadPosition - MyHeadPosition); - if (TargetIsInStrikeRadius()/* && - !LineOfSight.Trace(MyHeadPosition, AttackDirection, static_cast<int>(AttackDirection.Length()))*/ && - (m_Parent->GetHealth() > 0.0) + if (LineOfSight.Trace(MyHeadPosition, AttackDirection1, + static_cast<int>(AttackDirection1.Length())) ) { - return true; + return false; } - else + + Vector3d MyFeetPosition = m_Parent->GetPosition() + Vector3d(0, 0.5, 0); + Vector3d TargetFeetPosition = GetTarget()->GetPosition() + Vector3d(0, 0.5, 0); + Vector3d AttackDirection2(TargetFeetPosition - MyFeetPosition); + if (LineOfSight.Trace(MyFeetPosition, AttackDirection2, + static_cast<int>(AttackDirection2.Length())) + ) { return false; } + + return true; } -bool cBehaviorAttacker::TargetOutOfSight() +bool cBehaviorAttacker::TargetTooFar() { ASSERT(m_Target != nullptr); if ((GetTarget()->GetPosition() - m_Parent->GetPosition()).Length() > m_Parent->GetSightDistance()) diff --git a/src/Mobs/Behaviors/BehaviorAttacker.h b/src/Mobs/Behaviors/BehaviorAttacker.h index c047cde50..fe3805c66 100644 --- a/src/Mobs/Behaviors/BehaviorAttacker.h +++ b/src/Mobs/Behaviors/BehaviorAttacker.h @@ -52,8 +52,8 @@ protected: // Target related methods bool TargetIsInStrikeRadius(); - bool TargetIsInStrikeRadiusAndLineOfSight(); - bool TargetOutOfSight(); + bool TargetIsInLineOfSight(); + bool TargetTooFar(); void StrikeTargetIfReady(std::chrono::milliseconds a_Dt, cChunk & a_Chunk); // Cooldown stuff |