Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature for search closest path #7

Open
wants to merge 5 commits into
base: wip
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions jps.hh
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ enum JPS_Flags_

// Don't check whether end position is walkable.
JPS_Flag_NoEndCheck = 0x08,

// Find closest path
JPS_Flag_Closest = 0x16,
};

enum JPS_Result
Expand Down Expand Up @@ -896,6 +899,8 @@ public:
private:

const GRID& grid;
Position closestPosition;
ScoreType closestBestDistance;

Node *getNode(const Position& pos);
bool identifySuccessors(const Node& n);
Expand Down Expand Up @@ -1291,9 +1296,18 @@ template <typename GRID> template<typename PV> bool Searcher<GRID>::findPath(PV&
case JPS_EMPTY_PATH:
JPS_ASSERT(false); // can't happen
// fall through
case JPS_NO_PATH:
case JPS_OUT_OF_MEMORY:
return false;
case JPS_NO_PATH:
if(flags & JPS_Flag_Closest)
{
if (start != closestPosition)
{
endNodeIdx = storage.getindex(getNode(closestPosition));
return findPathFinish(path, step) == JPS_FOUND_PATH;
}
}
return false;
}
}
}
Expand All @@ -1305,6 +1319,8 @@ template <typename GRID> JPS_Result Searcher<GRID>::findPathInit(Position start,

this->flags = flags;
endPos = end;
closestBestDistance = UINT_MAX;
closestPosition = start;

// FIXME: check this
if(start == end && !(flags & (JPS_Flag_NoStartCheck|JPS_Flag_NoEndCheck)))
Expand All @@ -1318,11 +1334,15 @@ template <typename GRID> JPS_Result Searcher<GRID>::findPathInit(Position start,
if(!grid(start.x, start.y))
return JPS_NO_PATH;

if(!(flags & JPS_Flag_NoEndCheck))
if(!(flags & (JPS_Flag_NoEndCheck | JPS_Flag_Closest)))
if(!grid(end.x, end.y))
return JPS_NO_PATH;

Node *endNode = getNode(end); // this might realloc the internal storage...
Node *endNode = NULL;
if(flags & JPS_Flag_NoEndCheck)
endNode = nodemap(end.x, end.y);
else
endNode = getNode(end); // this might realloc the internal storage...
if(!endNode)
return JPS_OUT_OF_MEMORY;
endNodeIdx = storage.getindex(endNode); // .. so we keep this for later
Expand Down Expand Up @@ -1353,6 +1373,23 @@ template <typename GRID> JPS_Result Searcher<GRID>::findPathStep(int limit)
return JPS_NO_PATH;
Node& n = open.popNode();
n.setClosed();
if(flags & JPS_Flag_Closest)
{
if(closestBestDistance == M_MAX_UNSIGNED)
{
closestPosition = n.pos;
closestBestDistance = Heuristic::Manhattan(n.pos, endPos);
}
else
{
unsigned h = Heuristic::Manhattan(n.pos, endPos);
if(closestBestDistance > h)
{
closestBestDistance = h;
closestPosition = n.pos;
}
}
}
if(n.pos == endPos)
return JPS_FOUND_PATH;
if(!identifySuccessors(n))
Expand Down