1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

- Use straight paths for some connections

- More fixes for template schema
This commit is contained in:
DjWarmonger 2015-06-02 16:40:32 +02:00
parent d6225f50e1
commit a68b58d969
4 changed files with 96 additions and 15 deletions

View File

@ -43,11 +43,11 @@
"type": "number",
"minimum": 1,
"additionalProperties" : false,
}
},
"connection":
{
required: ["a", "b", "guard"]
properties:{
"required": ["a", "b", "guard"],
"properties":{
"a":{
"type" : "string"
},

View File

@ -483,7 +483,7 @@ void CMapGenerator::createConnections()
{
if (isBlocked(tile)) //tiles may be occupied by subterranean gates already placed
continue;
foreach_neighbour (tile, [&guardPos, tile, &otherZoneTiles, this](int3 &pos)
foreachDirectNeighbour (tile, [&guardPos, tile, &otherZoneTiles, this](int3 &pos) //must be direct since paths also also generated between direct neighbours
{
//if (vstd::contains(otherZoneTiles, pos) && !this->isBlocked(pos))
if (vstd::contains(otherZoneTiles, pos))
@ -494,8 +494,8 @@ void CMapGenerator::createConnections()
setOccupied (guardPos, ETileType::FREE); //just in case monster is too weak to spawn
zoneA->addMonster (this, guardPos, connection.getGuardStrength(), false, true);
//zones can make paths only in their own area
zoneA->crunchPath(this, guardPos, posA, zoneA->getFreePaths()); //make connection towards our zone center
zoneB->crunchPath(this, guardPos, posB, zoneB->getFreePaths()); //make connection towards other zone center
zoneA->crunchPath(this, guardPos, posA, true, zoneA->getFreePaths()); //make connection towards our zone center
zoneB->crunchPath(this, guardPos, posB, true, zoneB->getFreePaths()); //make connection towards other zone center
zoneA->addRoadNode(guardPos);
zoneB->addRoadNode(guardPos);

View File

@ -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, &currentNode, &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;

View File

@ -170,7 +170,8 @@ public:
void createTreasures(CMapGenerator* gen);
void createObstacles1(CMapGenerator* gen);
void createObstacles2(CMapGenerator* gen);
bool crunchPath(CMapGenerator* gen, const int3 &src, const int3 &dst, std::set<int3>* clearedTiles = nullptr);
bool crunchPath(CMapGenerator* gen, const int3 &src, const int3 &dst, bool onlyStraight, std::set<int3>* clearedTiles = nullptr);
bool connectPath(CMapGenerator* gen, const int3& src, bool onlyStraight);
std::vector<int3> getAccessibleOffsets (CMapGenerator* gen, CGObjectInstance* object);