mirror of
https://github.com/vcmi/vcmi.git
synced 2025-04-21 12:06:49 +02:00
- Use straight paths for some connections
- More fixes for template schema
This commit is contained in:
parent
d6225f50e1
commit
a68b58d969
@ -43,11 +43,11 @@
|
|||||||
"type": "number",
|
"type": "number",
|
||||||
"minimum": 1,
|
"minimum": 1,
|
||||||
"additionalProperties" : false,
|
"additionalProperties" : false,
|
||||||
}
|
},
|
||||||
"connection":
|
"connection":
|
||||||
{
|
{
|
||||||
required: ["a", "b", "guard"]
|
"required": ["a", "b", "guard"],
|
||||||
properties:{
|
"properties":{
|
||||||
"a":{
|
"a":{
|
||||||
"type" : "string"
|
"type" : "string"
|
||||||
},
|
},
|
||||||
|
@ -483,7 +483,7 @@ void CMapGenerator::createConnections()
|
|||||||
{
|
{
|
||||||
if (isBlocked(tile)) //tiles may be occupied by subterranean gates already placed
|
if (isBlocked(tile)) //tiles may be occupied by subterranean gates already placed
|
||||||
continue;
|
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) && !this->isBlocked(pos))
|
||||||
if (vstd::contains(otherZoneTiles, 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
|
setOccupied (guardPos, ETileType::FREE); //just in case monster is too weak to spawn
|
||||||
zoneA->addMonster (this, guardPos, connection.getGuardStrength(), false, true);
|
zoneA->addMonster (this, guardPos, connection.getGuardStrength(), false, true);
|
||||||
//zones can make paths only in their own area
|
//zones can make paths only in their own area
|
||||||
zoneA->crunchPath(this, guardPos, posA, zoneA->getFreePaths()); //make connection towards our zone center
|
zoneA->crunchPath(this, guardPos, posA, true, zoneA->getFreePaths()); //make connection towards our zone center
|
||||||
zoneB->crunchPath(this, guardPos, posB, zoneB->getFreePaths()); //make connection towards other zone center
|
zoneB->crunchPath(this, guardPos, posB, true, zoneB->getFreePaths()); //make connection towards other zone center
|
||||||
|
|
||||||
zoneA->addRoadNode(guardPos);
|
zoneA->addRoadNode(guardPos);
|
||||||
zoneB->addRoadNode(guardPos);
|
zoneB->addRoadNode(guardPos);
|
||||||
|
@ -535,11 +535,11 @@ void CRmgTemplateZone::fractalize(CMapGenerator* gen)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//connect with all the paths
|
//connect with all the paths
|
||||||
crunchPath(gen, node, findClosestTile(freePaths, node), &freePaths);
|
crunchPath(gen, node, findClosestTile(freePaths, node), true, &freePaths);
|
||||||
//connect with nearby nodes
|
//connect with nearby nodes
|
||||||
for (auto nearbyNode : nearbyNodes)
|
for (auto nearbyNode : nearbyNodes)
|
||||||
{
|
{
|
||||||
crunchPath(gen, node, nearbyNode, &freePaths);
|
crunchPath(gen, node, nearbyNode, true, &freePaths);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto node : nodes)
|
for (auto node : nodes)
|
||||||
@ -608,7 +608,7 @@ void CRmgTemplateZone::fractalize(CMapGenerator* gen)
|
|||||||
//logGlobal->infoStream() << boost::format ("Zone %d subdivided fractally") %id;
|
//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.
|
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);
|
int3 anotherPos(-1, -1, -1);
|
||||||
|
|
||||||
@ -689,8 +692,10 @@ do not leave zone border
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
if (onlyStraight)
|
||||||
gen->foreach_neighbour (currentPos,processNeighbours2);
|
gen->foreachDirectNeighbour(currentPos, processNeighbours2);
|
||||||
|
else
|
||||||
|
gen->foreach_neighbour(currentPos, processNeighbours2);
|
||||||
|
|
||||||
|
|
||||||
if (anotherPos.valid())
|
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)
|
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
|
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
|
//we can't connect this pile, just block it off and start over
|
||||||
for (auto treasure : treasures)
|
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
|
//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
|
//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;
|
guardTile = tile;
|
||||||
break;
|
break;
|
||||||
|
@ -170,7 +170,8 @@ public:
|
|||||||
void createTreasures(CMapGenerator* gen);
|
void createTreasures(CMapGenerator* gen);
|
||||||
void createObstacles1(CMapGenerator* gen);
|
void createObstacles1(CMapGenerator* gen);
|
||||||
void createObstacles2(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);
|
std::vector<int3> getAccessibleOffsets (CMapGenerator* gen, CGObjectInstance* object);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user