Skip to content

Commit

Permalink
fix PE heuristic
Browse files Browse the repository at this point in the history
  • Loading branch information
rtri committed Dec 3, 2016
1 parent 9fe8876 commit ae0ef2f
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rts/Sim/Path/Default/IPathFinder.cpp
Expand Up @@ -189,7 +189,7 @@ IPath::SearchResult IPathFinder::InitSearch(const MoveDef& moveDef, const CPathF

// mark starting point as best found position
mGoalBlockIdx = mStartBlockIdx;
mGoalHeuristic = pfDef.Heuristic(square.x, square.y);
mGoalHeuristic = pfDef.Heuristic(square.x, square.y, BLOCK_SIZE);

// perform the search
const IPath::SearchResult result = DoSearch(moveDef, pfDef, owner);
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Path/Default/PathDataTypes.h
Expand Up @@ -35,7 +35,7 @@ struct PathNode {
/// functor to define node priority
struct lessCost: public std::binary_function<PathNode*, PathNode*, bool> {
inline bool operator() (const PathNode* x, const PathNode* y) const {
return (x->fCost == y->fCost) ? (x->gCost < y->gCost) : (x->fCost > y->fCost);
return (x->fCost == y->fCost) ? (x->gCost < y->gCost) : (x->fCost > y->fCost);
}
};

Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Path/Default/PathEstimator.cpp
Expand Up @@ -696,7 +696,7 @@ bool CPathEstimator::TestBlock(
const float nodeCost = vertexCosts[vertexIdx] + extraCost;

const float gCost = parentOpenBlock->gCost + nodeCost;
const float hCost = peDef.Heuristic(square.x, square.y);
const float hCost = peDef.Heuristic(square.x, square.y, BLOCK_SIZE);
const float fCost = gCost + hCost;

// already in the open set?
Expand Down
6 changes: 3 additions & 3 deletions rts/Sim/Path/Default/PathFinder.cpp
Expand Up @@ -258,9 +258,9 @@ bool CPathFinder::TestBlock(
const float dirMoveCost = (1.0f + heatCost) * PF_DIRECTION_COSTS[pathOptDir];
const float nodeCost = (dirMoveCost / speedMod) + extraCost;

const float gCost = parentSquare->gCost + nodeCost; // g
const float hCost = pfDef.Heuristic(square.x, square.y); // h
const float fCost = gCost + hCost; // f
const float gCost = parentSquare->gCost + nodeCost; // g
const float hCost = pfDef.Heuristic(square.x, square.y, BLOCK_SIZE); // h
const float fCost = gCost + hCost; // f

if (blockStates.nodeMask[sqrIdx] & PATHOPT_OPEN) {
// already in the open set, look for a cost-improvement
Expand Down
10 changes: 5 additions & 5 deletions rts/Sim/Path/Default/PathFinderDef.cpp
Expand Up @@ -5,7 +5,6 @@
#include "PathFinderDef.h"
#include "Sim/MoveTypes/MoveDefHandler.h"


CPathFinderDef::CPathFinderDef(const float3& goalCenter, float goalRadius, float sqGoalDistance)
: goal(goalCenter)
, sqGoalRadius(goalRadius * goalRadius)
Expand All @@ -30,12 +29,13 @@ bool CPathFinderDef::IsGoal(unsigned int xSquare, unsigned int zSquare) const {
}

// returns distance to goal center in heightmap-squares
float CPathFinderDef::Heuristic(unsigned int xSquare, unsigned int zSquare) const
float CPathFinderDef::Heuristic(unsigned int xSquare, unsigned int zSquare, unsigned int blockSize) const
{
const float dx = std::abs(int(xSquare) - int(goalSquareX));
const float dz = std::abs(int(zSquare) - int(goalSquareZ));
const float s = 1.0f / blockSize;
const float dx = std::abs(int(xSquare) - int(goalSquareX)) * s;
const float dz = std::abs(int(zSquare) - int(goalSquareZ)) * s;

// grid is 8-connected, so use octile distance
// grid is 8-connected, so use octile distance (normalized by block-size)
constexpr const float C1 = 1.0f;
constexpr const float C2 = 1.4142f - (2.0f * C1);
return ((dx + dz) * C1 + std::min(dx, dz) * C2);
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Path/Default/PathFinderDef.h
Expand Up @@ -21,7 +21,7 @@ class CPathFinderDef {
void DisableConstraint(bool b) { constraintDisabled = b; }

bool IsGoal(unsigned int xSquare, unsigned int zSquare) const;
float Heuristic(unsigned int xSquare, unsigned int zSquare) const;
float Heuristic(unsigned int xSquare, unsigned int zSquare, unsigned int blockSize) const;
bool IsGoalBlocked(const MoveDef& moveDef, const CMoveMath::BlockType& blockMask, const CSolidObject* owner) const;
int2 GoalSquareOffset(unsigned int blockSize) const;

Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Path/Default/PathManager.cpp
Expand Up @@ -116,7 +116,7 @@ IPath::SearchResult CPathManager::ArrangePath(
// NOTE: this distance can be far smaller than the actual path length!
// NOTE: take height difference into consideration for "special" cases
// (unit at top of cliff, goal at bottom or vv.)
const float heurGoalDist2D = pfDef->Heuristic(startPos.x / SQUARE_SIZE, startPos.z / SQUARE_SIZE) + math::fabs(goalPos.y - startPos.y) / SQUARE_SIZE;
const float heurGoalDist2D = pfDef->Heuristic(startPos.x / SQUARE_SIZE, startPos.z / SQUARE_SIZE, 1) + math::fabs(goalPos.y - startPos.y) / SQUARE_SIZE;
const float searchDistances[] = {std::numeric_limits<float>::max(), MEDRES_SEARCH_DISTANCE, MAXRES_SEARCH_DISTANCE};

// MAX_SEARCHED_NODES_PF is 65536, MAXRES_SEARCH_DISTANCE is 50 squares
Expand Down

0 comments on commit ae0ef2f

Please sign in to comment.