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

Use WalkingDistance as cost/heuristic #7521

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
21 changes: 7 additions & 14 deletions Source/engine/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,21 @@ uint16_t PopActiveStep()
/**
* @brief Returns the distance between 2 adjacent nodes.
*
* The distance is 2 for nodes in the same row or column,
* and 3 for diagonally adjacent nodes.
*
* This approximates that diagonal movement on a square grid should have a cost
* of sqrt(2). That's approximately 1.5, so they multiply all step costs by 2,
* except diagonal steps which are times 3
* The distance is the chevsbyshev/walking distance between the nodes, as
* they're neighbours this is a value of 1.
*/
int GetDistance(Point startPosition, Point destinationPosition)
uint8_t GetDistance(Point startPosition, Point destinationPosition)
{
if (startPosition.x == destinationPosition.x || startPosition.y == destinationPosition.y)
return 2;

return 3;
assert(startPosition.WalkingDistance(destinationPosition) == 1 && "GetDistance called with non-neighbours");
return 1;
}

/**
* @brief heuristic, estimated cost from startPosition to destinationPosition.
*/
int GetHeuristicCost(Point startPosition, Point destinationPosition)
uint8_t GetHeuristicCost(Point startPosition, Point destinationPosition)
{
// see GetDistance for why this is times 2
return 2 * startPosition.ManhattanDistance(destinationPosition);
return startPosition.WalkingDistance(destinationPosition);
}

/**
Expand Down
Loading