1
0

Merge pull request #2073 from SafwatHalaby/rand

Path recalculation improvements
This commit is contained in:
worktycho 2015-05-20 22:40:25 +01:00
commit 1bdd205eb8
3 changed files with 17 additions and 10 deletions

View File

@ -177,13 +177,14 @@ bool cMonster::TickPathFinding(cChunk & a_Chunk)
case ePathFinderStatus::NEARBY_FOUND: case ePathFinderStatus::NEARBY_FOUND:
{ {
m_NoPathToTarget = true; m_NoPathToTarget = true;
m_Path->AcceptNearbyPath(); m_PathFinderDestination = m_Path->AcceptNearbyPath();
break; break;
} }
case ePathFinderStatus::PATH_NOT_FOUND: case ePathFinderStatus::PATH_NOT_FOUND:
{ {
StopMovingToPosition(); // Give up pathfinding to that destination. ResetPathFinding(); // Try to calculate a path again.
// Note that the next time may succeed, e.g. if a player breaks a barrier.
break; break;
} }
case ePathFinderStatus::CALCULATING: case ePathFinderStatus::CALCULATING:

View File

@ -6,6 +6,7 @@
#include "Path.h" #include "Path.h"
#include "../Chunk.h" #include "../Chunk.h"
#define DISTANCE_MANHATTAN 0 // 1: More speed, a bit less accuracy 0: Max accuracy, less speed. #define DISTANCE_MANHATTAN 0 // 1: More speed, a bit less accuracy 0: Max accuracy, less speed.
#define HEURISTICS_ONLY 0 // 1: Much more speed, much less accurate. #define HEURISTICS_ONLY 0 // 1: Much more speed, much less accurate.
#define CALCULATIONS_PER_STEP 10 // Higher means more CPU load but faster path calculations. #define CALCULATIONS_PER_STEP 10 // Higher means more CPU load but faster path calculations.
@ -178,11 +179,18 @@ bool cPath::Step_Internal()
// Calculation not finished yet. // Calculation not finished yet.
// Check if we have a new NearestPoint. // Check if we have a new NearestPoint.
if (CurrentCell->m_H < m_NearestPointToTarget->m_H)
if ((m_Destination - CurrentCell->m_Location).Length() < 5)
{
if (m_Rand.NextInt(4) == 0)
{
m_NearestPointToTarget = CurrentCell;
}
}
else if (CurrentCell->m_H < m_NearestPointToTarget->m_H)
{ {
m_NearestPointToTarget = CurrentCell; m_NearestPointToTarget = CurrentCell;
} }
// process a currentCell by inspecting all neighbors. // process a currentCell by inspecting all neighbors.
// Check North, South, East, West on all 3 different heights. // Check North, South, East, West on all 3 different heights.

View File

@ -1,16 +1,13 @@
#pragma once #pragma once
/* Wanna use the pathfinder? Put this in your header file: /*
// Needed Fwds: cPath
// Fwd: cPath
enum class ePathFinderStatus; enum class ePathFinderStatus;
class cPath; class cPath;
Put this in your .cpp:
#include "...Path.h"
*/ */
#include "../FastRandom.h"
#ifdef COMPILING_PATHFIND_DEBUGGER #ifdef COMPILING_PATHFIND_DEBUGGER
/* Note: the COMPILING_PATHFIND_DEBUGGER flag is used by Native / WiseOldMan95 to debug /* Note: the COMPILING_PATHFIND_DEBUGGER flag is used by Native / WiseOldMan95 to debug
this class outside of MCServer. This preprocessor flag is never set when compiling MCServer. */ this class outside of MCServer. This preprocessor flag is never set when compiling MCServer. */
@ -166,6 +163,7 @@ private:
Vector3i m_Source; Vector3i m_Source;
int m_StepsLeft; int m_StepsLeft;
cPathCell * m_NearestPointToTarget; cPathCell * m_NearestPointToTarget;
cFastRandom m_Rand;
/* Control fields */ /* Control fields */
ePathFinderStatus m_Status; ePathFinderStatus m_Status;