2022-08-09 07:54:32 +02:00
|
|
|
/*
|
|
|
|
* ObstaclePlacer.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 "ObstaclePlacer.h"
|
|
|
|
#include "ObjectManager.h"
|
|
|
|
#include "TreasurePlacer.h"
|
2023-06-10 14:58:12 +02:00
|
|
|
#include "RockFiller.h"
|
2022-08-09 07:54:32 +02:00
|
|
|
#include "WaterRoutes.h"
|
|
|
|
#include "WaterProxy.h"
|
|
|
|
#include "RoadPlacer.h"
|
|
|
|
#include "RiverPlacer.h"
|
2023-05-20 10:17:37 +02:00
|
|
|
#include "../RmgMap.h"
|
|
|
|
#include "../CMapGenerator.h"
|
|
|
|
#include "../../CRandomGenerator.h"
|
|
|
|
#include "../Functions.h"
|
|
|
|
#include "../../mapping/CMapEditManager.h"
|
|
|
|
#include "../../mapping/CMap.h"
|
|
|
|
#include "../../mapping/ObstacleProxy.h"
|
2022-08-09 07:54:32 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2022-09-17 15:43:59 +02:00
|
|
|
void ObstaclePlacer::process()
|
|
|
|
{
|
|
|
|
manager = zone.getModificator<ObjectManager>();
|
|
|
|
if(!manager)
|
|
|
|
return;
|
2023-05-19 20:30:15 +02:00
|
|
|
|
2022-09-17 15:43:59 +02:00
|
|
|
collectPossibleObstacles(zone.getTerrainType());
|
|
|
|
|
|
|
|
{
|
2023-06-10 14:56:03 +02:00
|
|
|
Zone::Lock lock(zone.areaMutex);
|
|
|
|
blockedArea = zone.area().getSubarea([this](const int3& t)
|
|
|
|
{
|
|
|
|
return map.shouldBeBlocked(t);
|
|
|
|
});
|
|
|
|
blockedArea.subtract(zone.areaUsed());
|
|
|
|
zone.areaPossible().subtract(blockedArea);
|
2023-05-19 20:30:15 +02:00
|
|
|
|
2023-06-10 14:56:03 +02:00
|
|
|
prohibitedArea = zone.freePaths() + zone.areaUsed() + manager->getVisitableArea();
|
|
|
|
|
|
|
|
//Progressively block tiles, but make sure they don't seal any gap between blocks
|
|
|
|
rmg::Area toBlock;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
toBlock.clear();
|
2023-12-18 14:52:03 +02:00
|
|
|
for (const auto& tile : zone.areaPossible().getTilesVector())
|
2023-06-10 14:56:03 +02:00
|
|
|
{
|
|
|
|
rmg::Area neighbors;
|
|
|
|
rmg::Area t;
|
|
|
|
t.add(tile);
|
|
|
|
|
|
|
|
for (const auto& n : t.getBorderOutside())
|
|
|
|
{
|
|
|
|
//Area outside the map is also impassable
|
|
|
|
if (!map.isOnMap(n) || map.shouldBeBlocked(n))
|
|
|
|
{
|
|
|
|
neighbors.add(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (neighbors.empty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//Will only be added if it doesn't connect two disjointed blocks
|
|
|
|
if (neighbors.connected(true)) //Do not block diagonal pass
|
|
|
|
{
|
|
|
|
toBlock.add(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zone.areaPossible().subtract(toBlock);
|
2023-12-18 14:52:03 +02:00
|
|
|
for (const auto& tile : toBlock.getTilesVector())
|
2023-06-10 14:56:03 +02:00
|
|
|
{
|
|
|
|
map.setOccupied(tile, ETileType::BLOCKED);
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (!toBlock.empty());
|
|
|
|
|
|
|
|
prohibitedArea.unite(zone.areaPossible());
|
|
|
|
}
|
2023-05-19 20:30:15 +02:00
|
|
|
|
2023-05-20 11:46:32 +02:00
|
|
|
auto objs = createObstacles(zone.getRand());
|
2023-05-19 20:30:15 +02:00
|
|
|
mapProxy->insertObjects(objs);
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObstaclePlacer::init()
|
|
|
|
{
|
|
|
|
DEPENDENCY(ObjectManager);
|
|
|
|
DEPENDENCY(TreasurePlacer);
|
|
|
|
DEPENDENCY(RoadPlacer);
|
2023-06-10 14:58:12 +02:00
|
|
|
if (zone.isUnderground())
|
|
|
|
{
|
|
|
|
DEPENDENCY(RockFiller);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEPENDENCY(WaterRoutes);
|
|
|
|
DEPENDENCY(WaterProxy);
|
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
2022-09-17 15:43:59 +02:00
|
|
|
|
2023-05-19 20:30:15 +02:00
|
|
|
bool ObstaclePlacer::isInTheMap(const int3& tile)
|
2022-09-17 15:43:59 +02:00
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
return map.isOnMap(tile);
|
2022-09-17 15:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObstaclePlacer::placeObject(rmg::Object & object, std::set<CGObjectInstance*> &)
|
|
|
|
{
|
|
|
|
manager->placeObject(object, false, false);
|
|
|
|
}
|
|
|
|
|
2023-05-19 20:30:15 +02:00
|
|
|
std::pair<bool, bool> ObstaclePlacer::verifyCoverage(const int3 & t) const
|
|
|
|
{
|
|
|
|
return {map.shouldBeBlocked(t), zone.areaPossible().contains(t)};
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:43:59 +02:00
|
|
|
void ObstaclePlacer::postProcess(const rmg::Object & object)
|
|
|
|
{
|
|
|
|
//river processing
|
2023-05-19 20:30:15 +02:00
|
|
|
riverManager = zone.getModificator<RiverPlacer>();
|
2022-09-17 15:43:59 +02:00
|
|
|
if(riverManager)
|
|
|
|
{
|
2022-09-18 23:18:17 +02:00
|
|
|
const auto objTypeName = object.instances().front()->object().typeName;
|
|
|
|
if(objTypeName == "mountain")
|
2022-09-17 15:43:59 +02:00
|
|
|
riverManager->riverSource().unite(object.getArea());
|
2022-09-18 23:18:17 +02:00
|
|
|
else if(objTypeName == "lake")
|
2022-09-17 15:43:59 +02:00
|
|
|
riverManager->riverSink().unite(object.getArea());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObstaclePlacer::isProhibited(const rmg::Area & objArea) const
|
|
|
|
{
|
|
|
|
if(prohibitedArea.overlap(objArea))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!zone.area().contains(objArea))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|