2022-08-09 07:54:32 +02:00
|
|
|
/*
|
|
|
|
* RoadPlacer.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "RoadPlacer.h"
|
2022-09-04 08:54:06 +02:00
|
|
|
#include "ObjectManager.h"
|
2023-05-19 20:30:15 +02:00
|
|
|
#include "ObstaclePlacer.h"
|
2023-06-10 14:58:12 +02:00
|
|
|
#include "RockFiller.h"
|
2023-05-20 10:17:37 +02:00
|
|
|
#include "../Functions.h"
|
|
|
|
#include "../CMapGenerator.h"
|
|
|
|
#include "../threadpool/MapProxy.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../../mapping/CMapEditManager.h"
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "../../mapObjects/CGObjectInstance.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../../modding/IdentifierStorage.h"
|
|
|
|
#include "../../modding/ModScope.h"
|
2023-08-06 21:14:16 +02:00
|
|
|
#include "../../TerrainHandler.h"
|
2022-08-09 07:54:32 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-08-06 21:14:16 +02:00
|
|
|
class TerrainType;
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
void RoadPlacer::process()
|
|
|
|
{
|
2022-12-17 01:52:40 +02:00
|
|
|
if(generator.getConfig().defaultRoadType.empty() && generator.getConfig().secondaryRoadType.empty())
|
|
|
|
return; //do not generate roads at all
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
connectRoads();
|
|
|
|
}
|
|
|
|
|
2024-05-01 09:16:10 +02:00
|
|
|
void RoadPlacer::postProcess()
|
|
|
|
{
|
|
|
|
//Draw dirt roads if there are only mines
|
|
|
|
drawRoads(noRoadNodes);
|
|
|
|
}
|
|
|
|
|
2023-06-10 14:58:12 +02:00
|
|
|
void RoadPlacer::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
rmg::Area & RoadPlacer::areaForRoads()
|
|
|
|
{
|
|
|
|
return areaRoads;
|
|
|
|
}
|
|
|
|
|
|
|
|
rmg::Area & RoadPlacer::areaIsolated()
|
|
|
|
{
|
|
|
|
return isolated;
|
|
|
|
}
|
|
|
|
|
2024-03-01 18:48:07 +02:00
|
|
|
rmg::Area & RoadPlacer::areaVisitable()
|
|
|
|
{
|
|
|
|
return visitableTiles;
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
const rmg::Area & RoadPlacer::getRoads() const
|
|
|
|
{
|
|
|
|
return roads;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RoadPlacer::createRoad(const int3 & dst)
|
|
|
|
{
|
2023-07-21 14:55:34 +02:00
|
|
|
auto searchArea = zone.areaPossible() + zone.freePaths() + areaRoads + roads;
|
|
|
|
|
2024-05-10 15:35:33 +02:00
|
|
|
rmg::Area border(zone.area()->getBorder());
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
rmg::Path path(searchArea);
|
|
|
|
path.connect(roads);
|
2023-07-21 14:55:34 +02:00
|
|
|
|
2024-03-01 18:48:07 +02:00
|
|
|
const float VISITABLE_PENALTY = 1.33f;
|
|
|
|
|
2024-05-10 15:35:33 +02:00
|
|
|
auto simpleRoutig = [this, &border, &VISITABLE_PENALTY](const int3& src, const int3& dst)
|
2023-07-21 14:55:34 +02:00
|
|
|
{
|
|
|
|
if(areaIsolated().contains(dst))
|
|
|
|
{
|
2024-05-01 12:15:07 +02:00
|
|
|
return 1000.0f; //Do not route road behind objects that are not visitable from top, such as Monoliths
|
2023-07-21 14:55:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-10 15:35:33 +02:00
|
|
|
float ret = dst.dist2d(src);
|
|
|
|
|
|
|
|
// TODO: Prefer zig-zag connections
|
2024-05-01 12:15:07 +02:00
|
|
|
|
2024-03-01 18:48:07 +02:00
|
|
|
if (visitableTiles.contains(src) || visitableTiles.contains(dst))
|
|
|
|
{
|
|
|
|
ret *= VISITABLE_PENALTY;
|
|
|
|
}
|
2024-05-10 15:35:33 +02:00
|
|
|
float dist = border.distance(dst);
|
|
|
|
if(dist > 1)
|
|
|
|
{
|
|
|
|
ret /= dist;
|
|
|
|
}
|
2024-03-01 18:48:07 +02:00
|
|
|
return ret;
|
2023-07-21 14:55:34 +02:00
|
|
|
}
|
|
|
|
};
|
2022-08-09 07:54:32 +02:00
|
|
|
|
2023-07-21 14:55:34 +02:00
|
|
|
auto res = path.search(dst, true, simpleRoutig);
|
2022-08-09 07:54:32 +02:00
|
|
|
if(!res.valid())
|
|
|
|
{
|
2024-05-10 15:35:33 +02:00
|
|
|
auto desperateRoutig = [this, &border, &VISITABLE_PENALTY](const int3& src, const int3& dst) -> float
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-07-21 14:55:34 +02:00
|
|
|
//Do not allow connections straight up through object not visitable from top
|
|
|
|
if(std::abs((src - dst).y) == 1)
|
|
|
|
{
|
|
|
|
if(areaIsolated().contains(dst) || areaIsolated().contains(src))
|
|
|
|
{
|
2023-12-07 13:57:39 +02:00
|
|
|
return 1e12;
|
2023-07-21 14:55:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(areaIsolated().contains(dst))
|
|
|
|
{
|
|
|
|
return 1e6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 15:35:33 +02:00
|
|
|
auto ret = dst.dist2d(src);
|
2024-03-01 18:48:07 +02:00
|
|
|
|
|
|
|
if (visitableTiles.contains(src) || visitableTiles.contains(dst))
|
|
|
|
{
|
|
|
|
ret *= VISITABLE_PENALTY;
|
|
|
|
}
|
2024-05-10 15:35:33 +02:00
|
|
|
float dist = border.distance(dst);
|
|
|
|
if(dist > 1)
|
|
|
|
{
|
|
|
|
ret /= dist;
|
|
|
|
}
|
2024-03-01 18:48:07 +02:00
|
|
|
return ret;
|
2023-07-21 14:55:34 +02:00
|
|
|
};
|
|
|
|
res = path.search(dst, false, desperateRoutig);
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
if(!res.valid())
|
|
|
|
{
|
2023-07-21 14:55:34 +02:00
|
|
|
logGlobal->warn("Failed to create road to node %s", dst.toString());
|
2022-08-09 07:54:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roads.unite(res.getPathArea());
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-04 08:54:06 +02:00
|
|
|
void RoadPlacer::drawRoads(bool secondary)
|
2023-07-08 08:44:10 +02:00
|
|
|
{
|
2024-05-01 13:58:24 +02:00
|
|
|
//Do not draw roads on underground rock or water
|
|
|
|
roads.erase_if([this](const int3& pos) -> bool
|
2023-05-19 20:30:15 +02:00
|
|
|
{
|
2024-05-01 13:58:24 +02:00
|
|
|
const auto* terrain = map.getTile(pos).terType;
|
|
|
|
return !terrain->isPassable() || !terrain->isLand();
|
|
|
|
});
|
2023-05-19 20:30:15 +02:00
|
|
|
|
2023-07-21 14:55:49 +02:00
|
|
|
if(!generator.getMapGenOptions().isRoadEnabled())
|
2023-07-08 08:44:10 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-07 20:17:20 +02:00
|
|
|
if((secondary && generator.getConfig().secondaryRoadType.empty())
|
|
|
|
|| (!secondary && generator.getConfig().defaultRoadType.empty()))
|
|
|
|
return;
|
|
|
|
|
2023-07-08 08:44:10 +02:00
|
|
|
//TODO: Allow custom road type for object
|
|
|
|
//TODO: Remove these default types
|
|
|
|
|
2023-05-19 20:30:15 +02:00
|
|
|
auto tiles = roads.getTilesVector();
|
2022-12-20 16:14:06 +02:00
|
|
|
|
2022-12-17 02:54:01 +02:00
|
|
|
std::string roadName = (secondary ? generator.getConfig().secondaryRoadType : generator.getConfig().defaultRoadType);
|
2023-07-30 19:12:25 +02:00
|
|
|
RoadId roadType(*VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "road", roadName));
|
2023-07-08 08:44:10 +02:00
|
|
|
|
|
|
|
//If our road type is not enabled, choose highest below it
|
|
|
|
for (int8_t bestRoad = roadType.getNum(); bestRoad > RoadId(Road::NO_ROAD).getNum(); bestRoad--)
|
|
|
|
{
|
2023-07-21 14:55:49 +02:00
|
|
|
if(generator.getMapGenOptions().isRoadEnabled(RoadId(bestRoad)))
|
2023-07-08 08:44:10 +02:00
|
|
|
{
|
|
|
|
mapProxy->drawRoads(zone.getRand(), tiles, RoadId(bestRoad));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoadPlacer::addRoadNode(const int3& node)
|
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
RecursiveLock lock(externalAccessMutex);
|
2024-05-01 15:27:19 +02:00
|
|
|
roadNodes.insert(node);
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoadPlacer::connectRoads()
|
|
|
|
{
|
2022-09-04 08:54:06 +02:00
|
|
|
//Assumes objects are already placed
|
2023-07-21 14:55:49 +02:00
|
|
|
if(roadNodes.size() < 2)
|
2022-09-04 08:54:06 +02:00
|
|
|
{
|
|
|
|
//If there are no nodes, draw roads to mines
|
|
|
|
noRoadNodes = true;
|
2023-07-21 14:55:49 +02:00
|
|
|
if(auto* m = zone.getModificator<ObjectManager>())
|
2022-09-04 08:54:06 +02:00
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
for(auto * object : m->getMines())
|
2022-09-04 08:54:06 +02:00
|
|
|
{
|
|
|
|
addRoadNode(object->visitablePos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(roadNodes.size() < 2)
|
2022-08-09 07:54:32 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
//take any tile from road nodes as destination zone for all other road nodes
|
2023-05-19 20:30:15 +02:00
|
|
|
RecursiveLock lock(externalAccessMutex);
|
2022-08-09 07:54:32 +02:00
|
|
|
if(roads.empty())
|
|
|
|
roads.add(*roadNodes.begin());
|
2023-02-11 18:05:02 +02:00
|
|
|
|
|
|
|
for(const auto & node : roadNodes)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-07-21 14:55:34 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
createRoad(node);
|
|
|
|
}
|
|
|
|
catch (const rmgException& e)
|
|
|
|
{
|
|
|
|
logGlobal->warn("Handled exception while drawing road to node %s: %s", node.toString(), e.what());
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
logGlobal->error("Unhandled exception while drawing road to node %s: %s", node.toString(), e.what());
|
2023-10-27 16:35:03 +02:00
|
|
|
throw;
|
2023-07-21 14:55:34 +02:00
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
2024-05-01 09:16:10 +02:00
|
|
|
if (!zone.isUnderground())
|
|
|
|
{
|
|
|
|
// Otherwise roads will be drawn only after rock is placed
|
|
|
|
postProcess();
|
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char RoadPlacer::dump(const int3 & t)
|
|
|
|
{
|
2024-05-01 09:16:10 +02:00
|
|
|
if(vstd::contains(roadNodes, t))
|
2022-08-09 07:54:32 +02:00
|
|
|
return '@';
|
|
|
|
if(roads.contains(t))
|
|
|
|
return '+';
|
|
|
|
if(isolated.contains(t))
|
|
|
|
return 'i';
|
|
|
|
return Modificator::dump(t);
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|