1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-15 11:46:56 +02:00

Pathfinding: move getNode into CPathsInfo

This commit is contained in:
ArseniyShestakov 2015-11-02 14:04:26 +03:00
parent 4b64bec711
commit 9c1c7d0caf
2 changed files with 18 additions and 11 deletions

View File

@ -79,7 +79,7 @@ void CPathfinder::calculatePaths()
//logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner; //logGlobal->infoStream() << boost::format("Calculating paths for hero %s (adress %d) of player %d") % hero->name % hero % hero->tempOwner;
//initial tile - set cost on 0 and add to the queue //initial tile - set cost on 0 and add to the queue
CGPathNode &initialNode = *getNode(out.hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND); CGPathNode &initialNode = *out.getNode(out.hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND);
initialNode.turns = 0; initialNode.turns = 0;
initialNode.moveRemains = hero->movement; initialNode.moveRemains = hero->movement;
mq.push_back(&initialNode); mq.push_back(&initialNode);
@ -100,7 +100,7 @@ void CPathfinder::calculatePaths()
addNeighbours(cp->coord); addNeighbours(cp->coord);
for(auto & neighbour : neighbours) for(auto & neighbour : neighbours)
{ {
dp = getNode(neighbour, EPathfindingLayer::LAND); dp = out.getNode(neighbour, EPathfindingLayer::LAND);
dt = &gs->map->getTile(neighbour); dt = &gs->map->getTile(neighbour);
useEmbarkCost = 0; //0 - usual movement; 1 - embark; 2 - disembark useEmbarkCost = 0; //0 - usual movement; 1 - embark; 2 - disembark
@ -143,7 +143,7 @@ void CPathfinder::calculatePaths()
addTeleportExits(); addTeleportExits();
for(auto & neighbour : neighbours) for(auto & neighbour : neighbours)
{ {
dp = getNode(neighbour, EPathfindingLayer::LAND); dp = out.getNode(neighbour, EPathfindingLayer::LAND);
if(isBetterWay(movement, turn)) if(isBetterWay(movement, turn))
{ {
dp->moveRemains = movement; dp->moveRemains = movement;
@ -360,11 +360,6 @@ void CPathfinder::initializeGraph()
} }
} }
CGPathNode *CPathfinder::getNode(const int3 &coord, const EPathfindingLayer &layer)
{
return &out.nodes[coord.x][coord.y][coord.z][layer];
}
CGPathNode::EAccessibility CPathfinder::evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const CGPathNode::EAccessibility CPathfinder::evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const
{ {
CGPathNode::EAccessibility ret = (tinfo->blocked ? CGPathNode::BLOCKED : CGPathNode::ACCESSIBLE); CGPathNode::EAccessibility ret = (tinfo->blocked ? CGPathNode::BLOCKED : CGPathNode::ACCESSIBLE);
@ -530,7 +525,7 @@ const CGPathNode * CPathsInfo::getPathInfo(const int3 &tile, const EPathfindingL
if(tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z || layer >= EPathfindingLayer::NUM_LAYERS) if(tile.x >= sizes.x || tile.y >= sizes.y || tile.z >= sizes.z || layer >= EPathfindingLayer::NUM_LAYERS)
return nullptr; return nullptr;
return &nodes[tile.x][tile.y][tile.z][layer]; return getNode(tile, layer);
} }
bool CPathsInfo::getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer) const bool CPathsInfo::getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer) const
@ -538,7 +533,7 @@ bool CPathsInfo::getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &
boost::unique_lock<boost::mutex> pathLock(pathMx); boost::unique_lock<boost::mutex> pathLock(pathMx);
out.nodes.clear(); out.nodes.clear();
const CGPathNode *curnode = &nodes[dst.x][dst.y][dst.z][layer]; const CGPathNode *curnode = getNode(dst, layer);
if(!curnode->theNodeBefore) if(!curnode->theNodeBefore)
return false; return false;
@ -562,3 +557,15 @@ int CPathsInfo::getDistance(const int3 &tile, const EPathfindingLayer &layer) co
else else
return 255; return 255;
} }
CGPathNode *CPathsInfo::getNode(const int3 &coord, const EPathfindingLayer &layer) const
{
if(layer != EPathfindingLayer::AUTO)
return &nodes[coord.x][coord.y][coord.z][layer];
auto landNode = &nodes[coord.x][coord.y][coord.z][EPathfindingLayer::LAND];
if(landNode->theNodeBefore)
return landNode;
else
return &nodes[coord.x][coord.y][coord.z][EPathfindingLayer::SAIL];
}

View File

@ -65,6 +65,7 @@ struct DLL_LINKAGE CPathsInfo
const CGPathNode * getPathInfo(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const; const CGPathNode * getPathInfo(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
bool getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const; bool getPath(CGPath &out, const int3 &dst, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
int getDistance(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const; int getDistance(const int3 &tile, const EPathfindingLayer &layer = EPathfindingLayer::AUTO) const;
CGPathNode *getNode(const int3 &coord, const EPathfindingLayer &layer) const;
}; };
class CPathfinder : private CGameInfoCallback class CPathfinder : private CGameInfoCallback
@ -114,7 +115,6 @@ private:
void initializeGraph(); void initializeGraph();
CGPathNode *getNode(const int3 &coord, const EPathfindingLayer &layer);
CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const; CGPathNode::EAccessibility evaluateAccessibility(const int3 &pos, const TerrainTile *tinfo) const;
bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility) bool canMoveBetween(const int3 &a, const int3 &b) const; //checks only for visitable objects that may make moving between tiles impossible, not other conditions (like tiles itself accessibility)