Skip to content

Commit

Permalink
Improved road routing so they can go under any passable object.
Browse files Browse the repository at this point in the history
  • Loading branch information
DjWarmonger authored and DjWarmonger committed Dec 21, 2016
1 parent f78b524 commit cc452bd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ CCallback::~CCallback()

bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
{
//TODO: merge with Pathfinder::canMoveBetween
return gs->checkForVisitableDir(a, b) && gs->checkForVisitableDir(b, a);
//bidirectional
return gs->map->canMoveBetween(a, b);
}

const CPathsInfo * CCallback::getPathsInfo(const CGHeroInstance *h)
Expand Down
6 changes: 6 additions & 0 deletions lib/mapping/CMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ bool CMap::isWaterTile(const int3 &pos) const
{
return isInTheMap(pos) && getTile(pos).isWater();
}
bool CMap::canMoveBetween(const int3 &src, const int3 &dst) const
{
const TerrainTile * dstTile = &getTile(dst);
const TerrainTile * srcTile = &getTile(src);
return checkForVisitableDir(src, dstTile, dst) && checkForVisitableDir(dst, srcTile, src);
}

bool CMap::checkForVisitableDir(const int3 & src, const TerrainTile *pom, const int3 & dst ) const
{
Expand Down
1 change: 1 addition & 0 deletions lib/mapping/CMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class DLL_LINKAGE CMap : public CMapHeader
bool isInTheMap(const int3 & pos) const;
bool isWaterTile(const int3 & pos) const;

bool canMoveBetween(const int3 &src, const int3 &dst) const;
bool checkForVisitableDir( const int3 & src, const TerrainTile *pom, const int3 & dst ) const;
int3 guardingCreaturePosition (int3 pos) const;

Expand Down
2 changes: 1 addition & 1 deletion lib/rmg/CMapGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void CMapGenerator::fillZones()
it.second->createObstacles2(this);
}

#define PRINT_MAP_BEFORE_ROADS true
#define PRINT_MAP_BEFORE_ROADS false
if (PRINT_MAP_BEFORE_ROADS) //enable to debug
{
std::ofstream out("road debug");
Expand Down
15 changes: 9 additions & 6 deletions lib/rmg/CRmgTemplateZone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ bool CRmgTemplateZone::createRoad(CMapGenerator* gen, const int3& src, const int
pq.pop(); //remove top element
int3 currentNode = node.first;
closed.insert (currentNode);
auto currentTile = &gen->map->getTile(currentNode);

if (currentNode == dst || gen->isRoad(currentNode))
{
Expand All @@ -798,7 +799,7 @@ bool CRmgTemplateZone::createRoad(CMapGenerator* gen, const int3& src, const int
bool directNeighbourFound = false;
float movementCost = 1;

auto foo = [gen, this, &pq, &distances, &closed, &cameFrom, &currentNode, &node, &dst, &directNeighbourFound, &movementCost](int3& pos) -> void
auto foo = [gen, this, &pq, &distances, &closed, &cameFrom, &currentNode, &currentTile, &node, &dst, &directNeighbourFound, &movementCost](int3& pos) -> void
{
if (vstd::contains(closed, pos)) //we already visited that node
return;
Expand All @@ -810,18 +811,20 @@ bool CRmgTemplateZone::createRoad(CMapGenerator* gen, const int3& src, const int

if (distance < bestDistanceSoFar)
{
auto obj = gen->map->getTile(pos).topVisitableObj();
//FIXME: make road go through any empty or visitable tile
//if (gen->map->checkForVisitableDir(currentNode, &gen->map->getTile(pos), pos)) //TODO: why it has no effect?
if (gen->isFree(pos) || (obj && obj->ID == Obj::MONSTER) || pos == dst)
auto tile = &gen->map->getTile(pos);
auto obj = tile->topVisitableObj();
bool canMoveBetween = gen->map->canMoveBetween(currentNode, pos);

if (gen->isFree(pos) && gen->isFree(currentNode) //empty path
|| ((tile->visitable || currentTile->visitable) && canMoveBetween) //moving from or to visitable object
|| pos == dst) //we already compledted the path
{
if (gen->getZoneID(pos) == id || pos == dst) //otherwise guard position may appear already connected to other zone.
{
cameFrom[pos] = currentNode;
distances[pos] = distance;
pq.push(std::make_pair(pos, distance));
directNeighbourFound = true;
//logGlobal->traceStream() << boost::format("Found connection between node %s and %s, current distance %d") % currentNode % pos % distance;
}
}
}
Expand Down

0 comments on commit cc452bd

Please sign in to comment.