diff options
author | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2015-09-28 10:13:58 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@hotmail.co.uk> | 2015-09-28 10:13:58 +0200 |
commit | 9652f89a6d0dee41481fcb652d7ae78a0079568d (patch) | |
tree | 8e3609c909c24a167071721b849984c66baa67fc /src/Mobs/Monster.cpp | |
parent | Merge pull request #2505 from cuberite/SettableWeather (diff) | |
parent | Partial revert of #2446 (diff) | |
download | cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.tar cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.tar.gz cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.tar.bz2 cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.tar.lz cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.tar.xz cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.tar.zst cuberite-9652f89a6d0dee41481fcb652d7ae78a0079568d.zip |
Diffstat (limited to 'src/Mobs/Monster.cpp')
-rw-r--r-- | src/Mobs/Monster.cpp | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index b28e94ec1..4d9b9ca06 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -78,6 +78,7 @@ cMonster::cMonster(const AString & a_ConfigName, eMonsterType a_MobType, const A , m_IsFollowingPath(false) , m_PathfinderActivated(false) , m_GiveUpCounter(0) + , m_TicksSinceLastPathReset(1000) , m_LastGroundHeight(POSY_TOINT) , m_JumpCoolDown(0) , m_IdleInterval(0) @@ -128,6 +129,11 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) { return false; } + if (m_TicksSinceLastPathReset < 1000) + { + // No need to count beyond 1000. 1000 is arbitary here. + ++m_TicksSinceLastPathReset; + } if (ReachedFinalDestination()) { @@ -135,6 +141,26 @@ 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)) @@ -143,16 +169,21 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) return false; } m_GiveUpCounter = 40; - m_Path = new cPath(a_Chunk, GetPosition(), m_FinalDestination, 20, GetWidth(), GetHeight()); + m_NoPathToTarget = false; + m_NoMoreWayPoints = false; + m_PathFinderDestination = m_FinalDestination; + m_Path = new cPath(a_Chunk, GetPosition(), m_PathFinderDestination, 20, GetWidth(), GetHeight()); } switch (m_Path->Step(a_Chunk)) { case ePathFinderStatus::NEARBY_FOUND: { - m_FinalDestination = m_Path->AcceptNearbyPath(); + m_NoPathToTarget = true; + m_PathFinderDestination = m_Path->AcceptNearbyPath(); break; } + case ePathFinderStatus::PATH_NOT_FOUND: { StopMovingToPosition(); // Try to calculate a path again. @@ -166,10 +197,9 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk) } case ePathFinderStatus::PATH_FOUND: { - if ((--m_GiveUpCounter) == 0) + if (m_NoMoreWayPoints || (--m_GiveUpCounter == 0)) { - // Failed to reach a waypoint - that's a failure condition whichever point we're at - if (m_EMState == CHASING) + if (m_EMState == ATTACKING) { ResetPathFinding(); // Try to calculate a path again. // This results in mobs hanging around an unreachable target (player). @@ -188,6 +218,10 @@ 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; @@ -379,6 +413,7 @@ void cMonster::StopMovingToPosition() void cMonster::ResetPathFinding(void) { + m_TicksSinceLastPathReset = 0; m_IsFollowingPath = false; if (m_Path != nullptr) { |