|
|
@@ -535,11 +535,11 @@ void CRmgTemplateZone::fractalize(CMapGenerator* gen)
|
|
|
}
|
|
|
|
|
|
//connect with all the paths
|
|
|
- crunchPath(gen, node, findClosestTile(freePaths, node), &freePaths);
|
|
|
+ crunchPath(gen, node, findClosestTile(freePaths, node), true, &freePaths);
|
|
|
//connect with nearby nodes
|
|
|
for (auto nearbyNode : nearbyNodes)
|
|
|
{
|
|
|
- crunchPath(gen, node, nearbyNode, &freePaths);
|
|
|
+ crunchPath(gen, node, nearbyNode, true, &freePaths);
|
|
|
}
|
|
|
}
|
|
|
for (auto node : nodes)
|
|
|
@@ -608,7 +608,7 @@ void CRmgTemplateZone::fractalize(CMapGenerator* gen)
|
|
|
//logGlobal->infoStream() << boost::format ("Zone %d subdivided fractally") %id;
|
|
|
}
|
|
|
|
|
|
-bool CRmgTemplateZone::crunchPath(CMapGenerator* gen, const int3 &src, const int3 &dst, std::set<int3>* clearedTiles)
|
|
|
+bool CRmgTemplateZone::crunchPath(CMapGenerator* gen, const int3 &src, const int3 &dst, bool onlyStraight, std::set<int3>* clearedTiles)
|
|
|
{
|
|
|
/*
|
|
|
make shortest path with free tiles, reachning dst or closest already free tile. Avoid blocks.
|
|
|
@@ -664,7 +664,10 @@ do not leave zone border
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- gen->foreach_neighbour (currentPos,processNeighbours);
|
|
|
+ if (onlyStraight)
|
|
|
+ gen->foreachDirectNeighbour (currentPos, processNeighbours);
|
|
|
+ else
|
|
|
+ gen->foreach_neighbour (currentPos,processNeighbours);
|
|
|
|
|
|
int3 anotherPos(-1, -1, -1);
|
|
|
|
|
|
@@ -689,8 +692,10 @@ do not leave zone border
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
- gen->foreach_neighbour (currentPos,processNeighbours2);
|
|
|
+ if (onlyStraight)
|
|
|
+ gen->foreachDirectNeighbour(currentPos, processNeighbours2);
|
|
|
+ else
|
|
|
+ gen->foreach_neighbour(currentPos, processNeighbours2);
|
|
|
|
|
|
|
|
|
if (anotherPos.valid())
|
|
|
@@ -800,6 +805,81 @@ bool CRmgTemplateZone::createRoad(CMapGenerator* gen, const int3& src, const int
|
|
|
|
|
|
}
|
|
|
|
|
|
+bool CRmgTemplateZone::connectPath(CMapGenerator* gen, const int3& src, bool onlyStraight)
|
|
|
+///connect current tile to any other free tile within zone
|
|
|
+{
|
|
|
+ //A* algorithm taken from Wiki http://en.wikipedia.org/wiki/A*_search_algorithm
|
|
|
+
|
|
|
+ std::set<int3> closed; // The set of nodes already evaluated.
|
|
|
+ std::set<int3> open{ src }; // The set of tentative nodes to be evaluated, initially containing the start node
|
|
|
+ std::map<int3, int3> cameFrom; // The map of navigated nodes.
|
|
|
+ std::map<int3, float> distances;
|
|
|
+
|
|
|
+ int3 currentNode = src;
|
|
|
+ gen->setRoad(src, ERoadType::NO_ROAD); //just in case zone guard already has road under it. Road under nodes will be added at very end
|
|
|
+
|
|
|
+ cameFrom[src] = int3(-1, -1, -1); //first node points to finish condition
|
|
|
+ distances[src] = 0;
|
|
|
+ // Cost from start along best known path.
|
|
|
+ // Estimated total cost from start to goal through y.
|
|
|
+
|
|
|
+ while (open.size())
|
|
|
+ {
|
|
|
+ int3 currentNode = *boost::min_element(open, [&distances](const int3 &pos1, const int3 &pos2) -> bool
|
|
|
+ {
|
|
|
+ return distances[pos1] < distances[pos2];
|
|
|
+ });
|
|
|
+
|
|
|
+ vstd::erase_if_present(open, currentNode);
|
|
|
+ closed.insert(currentNode);
|
|
|
+
|
|
|
+ if (gen->isFree(currentNode))
|
|
|
+ {
|
|
|
+ // Trace the path using the saved parent information and return path
|
|
|
+ int3 backTracking = currentNode;
|
|
|
+ while (cameFrom[backTracking].valid())
|
|
|
+ {
|
|
|
+ gen->setOccupied(backTracking, ETileType::FREE);
|
|
|
+ backTracking = cameFrom[backTracking];
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ auto foo = [gen, this, &open, &closed, &cameFrom, ¤tNode, &distances](int3& pos) -> void
|
|
|
+ {
|
|
|
+ int distance = distances[currentNode] + 1;
|
|
|
+ int bestDistanceSoFar = 1e6; //FIXME: boost::limits
|
|
|
+ auto it = distances.find(pos);
|
|
|
+ if (it != distances.end())
|
|
|
+ bestDistanceSoFar = it->second;
|
|
|
+
|
|
|
+ if (distance < bestDistanceSoFar || !vstd::contains(closed, pos))
|
|
|
+ {
|
|
|
+ auto obj = gen->map->getTile(pos).topVisitableObj();
|
|
|
+ if (gen->isFree(pos))
|
|
|
+ {
|
|
|
+ if (vstd::contains(this->tileinfo, pos))
|
|
|
+ {
|
|
|
+ cameFrom[pos] = currentNode;
|
|
|
+ open.insert(pos);
|
|
|
+ distances[pos] = distance;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ if (onlyStraight)
|
|
|
+ gen->foreachDirectNeighbour(currentNode, foo);
|
|
|
+ else
|
|
|
+ gen->foreach_neighbour(currentNode, foo);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ logGlobal->warnStream() << boost::format("Failed to connect tile %s with free paths") % src;
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
void CRmgTemplateZone::addRequiredObject(CGObjectInstance * obj, si32 strength)
|
|
|
{
|
|
|
@@ -1034,7 +1114,7 @@ bool CRmgTemplateZone::createTreasurePile(CMapGenerator* gen, int3 &pos, float m
|
|
|
gen->setOccupied(tile, ETileType::BLOCKED); //so that crunch path doesn't cut through objects
|
|
|
}
|
|
|
|
|
|
- if (!crunchPath (gen, closestTile, closestFreeTile))
|
|
|
+ if (!crunchPath (gen, closestTile, closestFreeTile, false))
|
|
|
{
|
|
|
//we can't connect this pile, just block it off and start over
|
|
|
for (auto treasure : treasures)
|
|
|
@@ -1914,7 +1994,7 @@ bool CRmgTemplateZone::guardObject(CMapGenerator* gen, CGObjectInstance* object,
|
|
|
{
|
|
|
//crunching path may fail if center of the zone is directly over wide object
|
|
|
//make sure object is accessible before surrounding it with blocked tiles
|
|
|
- if (crunchPath (gen, tile, findClosestTile(freePaths, tile), addToFreePaths ? &freePaths : nullptr))
|
|
|
+ if (crunchPath (gen, tile, findClosestTile(freePaths, tile), false, addToFreePaths ? &freePaths : nullptr))
|
|
|
{
|
|
|
guardTile = tile;
|
|
|
break;
|