From baf3a0780489590d9433d115ee51c02a2ecf3e57 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 11 Jul 2015 20:40:03 +0100 Subject: Fixed a position bug in the pathfinder --- src/Mobs/Monster.cpp | 58 +++++++++++++--------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) (limited to 'src/Mobs/Monster.cpp') diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index d97999e0f..fa86f28ae 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -76,8 +76,8 @@ cMonster::cMonster(const AString & a_ConfigName, eMonsterType a_MobType, const A , m_Target(nullptr) , m_Path(nullptr) , m_IsFollowingPath(false) + , m_PathfinderActivated(false) , m_GiveUpCounter(0) - , m_TicksSinceLastPathReset(1000) , m_LastGroundHeight(POSY_TOINT) , m_JumpCoolDown(0) , m_IdleInterval(0) @@ -124,15 +124,10 @@ void cMonster::SpawnOn(cClientHandle & a_Client) bool cMonster::TickPathFinding(cChunk & a_Chunk) { - if (!m_IsFollowingPath) + if (!m_PathfinderActivated) { return false; } - if (m_TicksSinceLastPathReset < 1000) - { - // No need to count beyond 1000. 1000 is arbitary here. - ++m_TicksSinceLastPathReset; - } if (ReachedFinalDestination()) { @@ -140,26 +135,6 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) return false; } - if ((m_FinalDestination - m_PathFinderDestination).Length() > 0.25) // if the distance between where we're going and where we should go is too big. - { - /* If we reached the last path waypoint, - Or if we haven't re-calculated for too long. - Interval is proportional to distance squared, and its minimum is 10. - (Recalculate lots when close, calculate rarely when far) */ - if ( - ((GetPosition() - m_PathFinderDestination).Length() < 0.25) || - ((m_TicksSinceLastPathReset > 10) && (m_TicksSinceLastPathReset > (0.4 * (m_FinalDestination - GetPosition()).SqrLength()))) - ) - { - /* Re-calculating is expensive when there's no path to target, and it results in mobs freezing very often as a result of always recalculating. - This is a workaround till we get better path recalculation. */ - if (!m_NoPathToTarget) - { - ResetPathFinding(); - } - } - } - if (m_Path == nullptr) { if (!EnsureProperDestination(a_Chunk)) @@ -167,21 +142,17 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) StopMovingToPosition(); // Invalid chunks, probably world is loading or something, cancel movement. return false; } - m_NoPathToTarget = false; - m_NoMoreWayPoints = false; - m_PathFinderDestination = m_FinalDestination; - m_Path = new cPath(a_Chunk, GetPosition(), m_PathFinderDestination, 20, GetWidth(), GetHeight()); + m_GiveUpCounter = 40; + m_Path = new cPath(a_Chunk, GetPosition(), m_FinalDestination, 20, GetWidth(), GetHeight()); } switch (m_Path->Step(a_Chunk)) { case ePathFinderStatus::NEARBY_FOUND: { - m_NoPathToTarget = true; - m_PathFinderDestination = m_Path->AcceptNearbyPath(); + m_FinalDestination = m_Path->AcceptNearbyPath(); break; } - case ePathFinderStatus::PATH_NOT_FOUND: { StopMovingToPosition(); // Try to calculate a path again. @@ -195,9 +166,10 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) } case ePathFinderStatus::PATH_FOUND: { - if (m_NoMoreWayPoints || (--m_GiveUpCounter == 0)) + if ((--m_GiveUpCounter) == 0) { - if (m_EMState == ATTACKING) + // Failed to reach a waypoint - that's a failure condition whichever point we're at + if (m_EMState == CHASING) { ResetPathFinding(); // Try to calculate a path again. // This results in mobs hanging around an unreachable target (player). @@ -216,10 +188,8 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) m_GiveUpCounter = 40; // Give up after 40 ticks (2 seconds) if failed to reach m_NextWayPointPosition. } } - else - { - m_NoMoreWayPoints = true; - } + + m_IsFollowingPath = true; return true; } } @@ -389,8 +359,8 @@ bool cMonster::EnsureProperDestination(cChunk & a_Chunk) void cMonster::MoveToPosition(const Vector3d & a_Position) { - m_FinalDestination = a_Position; - m_IsFollowingPath = true; + m_FinalDestination = a_Position; + m_PathfinderActivated = true; } @@ -399,7 +369,7 @@ void cMonster::MoveToPosition(const Vector3d & a_Position) void cMonster::StopMovingToPosition() { - m_IsFollowingPath = false; + m_PathfinderActivated = false; ResetPathFinding(); } @@ -409,7 +379,7 @@ void cMonster::StopMovingToPosition() void cMonster::ResetPathFinding(void) { - m_TicksSinceLastPathReset = 0; + m_IsFollowingPath = false; if (m_Path != nullptr) { delete m_Path; -- cgit v1.2.3