mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-17 01:32:21 +02:00
Refactor obstacles placer
# Conflicts: # lib/rmg/ObstaclePlacer.cpp # lib/rmg/ObstaclePlacer.h
This commit is contained in:
@ -22,22 +22,10 @@
|
|||||||
#include "CMapGenerator.h"
|
#include "CMapGenerator.h"
|
||||||
#include "../CRandomGenerator.h"
|
#include "../CRandomGenerator.h"
|
||||||
#include "Functions.h"
|
#include "Functions.h"
|
||||||
|
#include "../mapping/CMapEditManager.h"
|
||||||
|
|
||||||
void ObstaclePlacer::process()
|
void ObstacleProxy::collectPossibleObstacles(const Terrain & terrain)
|
||||||
{
|
{
|
||||||
auto * manager = zone.getModificator<ObjectManager>();
|
|
||||||
if(!manager)
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto * riverManager = zone.getModificator<RiverPlacer>();
|
|
||||||
|
|
||||||
typedef std::vector<std::shared_ptr<const ObjectTemplate>> ObstacleVector;
|
|
||||||
//obstacleVector possibleObstacles;
|
|
||||||
|
|
||||||
std::map<int, ObstacleVector> obstaclesBySize;
|
|
||||||
typedef std::pair<int, ObstacleVector> ObstaclePair;
|
|
||||||
std::vector<ObstaclePair> possibleObstacles;
|
|
||||||
|
|
||||||
//get all possible obstacles for this terrain
|
//get all possible obstacles for this terrain
|
||||||
for(auto primaryID : VLC->objtypeh->knownObjects())
|
for(auto primaryID : VLC->objtypeh->knownObjects())
|
||||||
{
|
{
|
||||||
@ -48,7 +36,7 @@ void ObstaclePlacer::process()
|
|||||||
{
|
{
|
||||||
for(auto temp : handler->getTemplates())
|
for(auto temp : handler->getTemplates())
|
||||||
{
|
{
|
||||||
if(temp->canBePlacedAt(zone.getTerrainType()) && temp->getBlockMapOffset().valid())
|
if(temp->canBePlacedAt(terrain) && temp->getBlockMapOffset().valid())
|
||||||
obstaclesBySize[temp->getBlockedOffsets().size()].push_back(temp);
|
obstaclesBySize[temp->getBlockedOffsets().size()].push_back(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,26 +50,10 @@ void ObstaclePlacer::process()
|
|||||||
{
|
{
|
||||||
return p1.first > p2.first; //bigger obstacles first
|
return p1.first > p2.first; //bigger obstacles first
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
auto blockedArea = zone.area().getSubarea([this](const int3 & t)
|
int ObstacleProxy::getWeightedObjects(const int3 & tile, const CMap * map, CRandomGenerator & rand, std::list<rmg::Object> & allObjects, std::vector<std::pair<rmg::Object*, int3>> & weightedObjects)
|
||||||
{
|
{
|
||||||
return map.shouldBeBlocked(t);
|
|
||||||
});
|
|
||||||
blockedArea.subtract(zone.areaUsed());
|
|
||||||
zone.areaPossible().subtract(blockedArea);
|
|
||||||
|
|
||||||
|
|
||||||
auto prohibitedArea = zone.freePaths() + zone.areaUsed() + manager->getVisitableArea();
|
|
||||||
|
|
||||||
//reverse order, since obstacles begin in bottom-right corner, while the map coordinates begin in top-left
|
|
||||||
auto blockedTiles = blockedArea.getTilesVector();
|
|
||||||
int tilePos = 0;
|
|
||||||
while(!blockedArea.empty() && tilePos < blockedArea.getTilesVector().size())
|
|
||||||
{
|
|
||||||
auto tile = blockedArea.getTilesVector()[tilePos];
|
|
||||||
|
|
||||||
std::list<rmg::Object> allObjects;
|
|
||||||
std::vector<std::pair<rmg::Object*, int3>> weightedObjects; //obj + position
|
|
||||||
int maxWeight = std::numeric_limits<int>::min();
|
int maxWeight = std::numeric_limits<int>::min();
|
||||||
for(int i = 0; i < possibleObstacles.size(); ++i)
|
for(int i = 0; i < possibleObstacles.size(); ++i)
|
||||||
{
|
{
|
||||||
@ -89,9 +61,9 @@ void ObstaclePlacer::process()
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto shuffledObstacles = possibleObstacles[i].second;
|
auto shuffledObstacles = possibleObstacles[i].second;
|
||||||
RandomGeneratorUtil::randomShuffle(shuffledObstacles, generator.rand);
|
RandomGeneratorUtil::randomShuffle(shuffledObstacles, rand);
|
||||||
|
|
||||||
for(auto & temp : shuffledObstacles)
|
for(auto temp : shuffledObstacles)
|
||||||
{
|
{
|
||||||
auto handler = VLC->objtypeh->getHandlerFor(temp->id, temp->subid);
|
auto handler = VLC->objtypeh->getHandlerFor(temp->id, temp->subid);
|
||||||
auto obj = handler->create(temp);
|
auto obj = handler->create(temp);
|
||||||
@ -100,19 +72,16 @@ void ObstaclePlacer::process()
|
|||||||
for(auto & offset : obj->getBlockedOffsets())
|
for(auto & offset : obj->getBlockedOffsets())
|
||||||
{
|
{
|
||||||
rmgObject->setPosition(tile - offset);
|
rmgObject->setPosition(tile - offset);
|
||||||
if(!map.isOnMap(rmgObject->getPosition()))
|
if(!map->isInTheMap(rmgObject->getPosition()))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if(!rmgObject->getArea().getSubarea([this](const int3 & t)
|
if(!rmgObject->getArea().getSubarea([map](const int3 & t)
|
||||||
{
|
{
|
||||||
return !map.isOnMap(t);
|
return !map->isInTheMap(t);
|
||||||
}).empty())
|
}).empty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if(prohibitedArea.overlap(rmgObject->getArea()))
|
if(isProhibited(rmgObject->getArea()))
|
||||||
continue;
|
|
||||||
|
|
||||||
if(!zone.area().contains(rmgObject->getArea()))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int coverageBlocked = 0;
|
int coverageBlocked = 0;
|
||||||
@ -120,9 +89,10 @@ void ObstaclePlacer::process()
|
|||||||
//do not use area intersection in optimization purposes
|
//do not use area intersection in optimization purposes
|
||||||
for(auto & t : rmgObject->getArea().getTilesVector())
|
for(auto & t : rmgObject->getArea().getTilesVector())
|
||||||
{
|
{
|
||||||
if(map.shouldBeBlocked(t))
|
auto coverage = verifyCoverage(t);
|
||||||
|
if(coverage.first)
|
||||||
++coverageBlocked;
|
++coverageBlocked;
|
||||||
if(zone.areaPossible().contains(t))
|
if(coverage.second)
|
||||||
++coveragePossible;
|
++coveragePossible;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,26 +118,38 @@ void ObstaclePlacer::process()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return maxWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstacleProxy::placeObstacles(CMap * map, CRandomGenerator & rand)
|
||||||
|
{
|
||||||
|
//reverse order, since obstacles begin in bottom-right corner, while the map coordinates begin in top-left
|
||||||
|
auto blockedTiles = blockedArea.getTilesVector();
|
||||||
|
int tilePos = 0;
|
||||||
|
std::set<CGObjectInstance*> objs;
|
||||||
|
|
||||||
|
while(!blockedArea.empty() && tilePos < blockedArea.getTilesVector().size())
|
||||||
|
{
|
||||||
|
auto tile = blockedArea.getTilesVector()[tilePos];
|
||||||
|
|
||||||
|
std::list<rmg::Object> allObjects;
|
||||||
|
std::vector<std::pair<rmg::Object*, int3>> weightedObjects;
|
||||||
|
int maxWeight = getWeightedObjects(tile, map, rand, allObjects, weightedObjects);
|
||||||
|
|
||||||
if(weightedObjects.empty())
|
if(weightedObjects.empty())
|
||||||
{
|
{
|
||||||
tilePos += 1;
|
tilePos += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, generator.rand);
|
auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, rand);
|
||||||
objIter->first->setPosition(objIter->second);
|
objIter->first->setPosition(objIter->second);
|
||||||
manager->placeObject(*objIter->first, false, false);
|
placeObject(*objIter->first, objs);
|
||||||
|
|
||||||
blockedArea.subtract(objIter->first->getArea());
|
blockedArea.subtract(objIter->first->getArea());
|
||||||
tilePos = 0;
|
tilePos = 0;
|
||||||
|
|
||||||
//river processing
|
postProcess(*objIter->first);
|
||||||
if(riverManager)
|
|
||||||
{
|
|
||||||
if(objIter->first->instances().front()->object().typeName == "mountain")
|
|
||||||
riverManager->riverSource().unite(objIter->first->getArea());
|
|
||||||
if(objIter->first->instances().front()->object().typeName == "lake")
|
|
||||||
riverManager->riverSink().unite(objIter->first->getArea());
|
|
||||||
}
|
|
||||||
|
|
||||||
if(maxWeight < 0)
|
if(maxWeight < 0)
|
||||||
logGlobal->warn("Placed obstacle with negative weight at %s", objIter->second.toString());
|
logGlobal->warn("Placed obstacle with negative weight at %s", objIter->second.toString());
|
||||||
@ -178,6 +160,62 @@ void ObstaclePlacer::process()
|
|||||||
o.clear();
|
o.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finalInsertion(map->getEditManager(), objs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstacleProxy::finalInsertion(CMapEditManager * manager, std::set<CGObjectInstance*> & instances)
|
||||||
|
{
|
||||||
|
manager->insertObjects(instances); //insert as one operation - for undo purposes
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<bool, bool> ObstacleProxy::verifyCoverage(const int3 & t) const
|
||||||
|
{
|
||||||
|
std::pair<bool, bool> result(false, false);
|
||||||
|
if(blockedArea.contains(t))
|
||||||
|
result.first = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstacleProxy::placeObject(rmg::Object & object, std::set<CGObjectInstance*> & instances)
|
||||||
|
{
|
||||||
|
for (auto * instance : object.instances())
|
||||||
|
{
|
||||||
|
instances.insert(&instance->object());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstacleProxy::postProcess(const rmg::Object & object)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ObstacleProxy::isProhibited(const rmg::Area & objArea) const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void ObstaclePlacer::process()
|
||||||
|
{
|
||||||
|
manager = zone.getModificator<ObjectManager>();
|
||||||
|
if(!manager)
|
||||||
|
return;
|
||||||
|
|
||||||
|
riverManager = zone.getModificator<RiverPlacer>();
|
||||||
|
|
||||||
|
collectPossibleObstacles(zone.getTerrainType());
|
||||||
|
|
||||||
|
blockedArea = zone.area().getSubarea([this](const int3 & t)
|
||||||
|
{
|
||||||
|
return map.shouldBeBlocked(t);
|
||||||
|
});
|
||||||
|
blockedArea.subtract(zone.areaUsed());
|
||||||
|
zone.areaPossible().subtract(blockedArea);
|
||||||
|
|
||||||
|
prohibitedArea = zone.freePaths() + zone.areaUsed() + manager->getVisitableArea();
|
||||||
|
|
||||||
|
placeObstacles(&map.map(), generator.rand);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObstaclePlacer::init()
|
void ObstaclePlacer::init()
|
||||||
@ -189,3 +227,45 @@ void ObstaclePlacer::init()
|
|||||||
DEPENDENCY(RoadPlacer);
|
DEPENDENCY(RoadPlacer);
|
||||||
DEPENDENCY_ALL(RockPlacer);
|
DEPENDENCY_ALL(RockPlacer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<bool, bool> ObstaclePlacer::verifyCoverage(const int3 & t) const
|
||||||
|
{
|
||||||
|
std::pair<bool, bool> result(false, false);
|
||||||
|
if(map.shouldBeBlocked(t))
|
||||||
|
result.first = true;
|
||||||
|
if(zone.areaPossible().contains(t))
|
||||||
|
result.second = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstaclePlacer::placeObject(rmg::Object & object, std::set<CGObjectInstance*> &)
|
||||||
|
{
|
||||||
|
manager->placeObject(object, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstaclePlacer::postProcess(const rmg::Object & object)
|
||||||
|
{
|
||||||
|
//river processing
|
||||||
|
if(riverManager)
|
||||||
|
{
|
||||||
|
if(object.instances().front()->object().typeName == "mountain")
|
||||||
|
riverManager->riverSource().unite(object.getArea());
|
||||||
|
if(object.instances().front()->object().typeName == "lake")
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObstaclePlacer::finalInsertion(CMapEditManager *, std::set<CGObjectInstance*> &)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
@ -11,11 +11,61 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Zone.h"
|
#include "Zone.h"
|
||||||
|
|
||||||
class ObstaclePlacer: public Modificator
|
class CMap;
|
||||||
|
class CMapEditManager;
|
||||||
|
class RiverPlacer;
|
||||||
|
class ObjectManager;
|
||||||
|
class DLL_LINKAGE ObstacleProxy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ObstacleProxy() = default;
|
||||||
|
virtual ~ObstacleProxy() = default;
|
||||||
|
|
||||||
|
rmg::Area blockedArea;
|
||||||
|
|
||||||
|
void collectPossibleObstacles(const Terrain & terrain);
|
||||||
|
|
||||||
|
void placeObstacles(CMap * map, CRandomGenerator & rand);
|
||||||
|
|
||||||
|
virtual std::pair<bool, bool> verifyCoverage(const int3 & t) const;
|
||||||
|
|
||||||
|
virtual void placeObject(rmg::Object & object, std::set<CGObjectInstance*> & instances);
|
||||||
|
|
||||||
|
virtual void postProcess(const rmg::Object & object);
|
||||||
|
|
||||||
|
virtual bool isProhibited(const rmg::Area & objArea) const;
|
||||||
|
|
||||||
|
virtual void finalInsertion(CMapEditManager * manager, std::set<CGObjectInstance*> & instances);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int getWeightedObjects(const int3 & tile, const CMap * map, CRandomGenerator & rand, std::list<rmg::Object> & allObjects, std::vector<std::pair<rmg::Object*, int3>> & weightedObjects);
|
||||||
|
|
||||||
|
typedef std::vector<std::shared_ptr<const ObjectTemplate>> ObstacleVector;
|
||||||
|
std::map<int, ObstacleVector> obstaclesBySize;
|
||||||
|
typedef std::pair<int, ObstacleVector> ObstaclePair;
|
||||||
|
std::vector<ObstaclePair> possibleObstacles;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ObstaclePlacer: public Modificator, public ObstacleProxy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MODIFICATOR(ObstaclePlacer);
|
MODIFICATOR(ObstaclePlacer);
|
||||||
|
|
||||||
void process() override;
|
void process() override;
|
||||||
void init() override;
|
void init() override;
|
||||||
|
|
||||||
|
std::pair<bool, bool> verifyCoverage(const int3 & t) const override;
|
||||||
|
|
||||||
|
void placeObject(rmg::Object & object, std::set<CGObjectInstance*> & instances) override;
|
||||||
|
|
||||||
|
void postProcess(const rmg::Object & object) override;
|
||||||
|
|
||||||
|
bool isProhibited(const rmg::Area & objArea) const override;
|
||||||
|
|
||||||
|
void finalInsertion(CMapEditManager * manager, std::set<CGObjectInstance*> & instances) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
rmg::Area prohibitedArea;
|
||||||
|
RiverPlacer * riverManager;
|
||||||
|
ObjectManager * manager;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user