mirror of
https://github.com/vcmi/vcmi.git
synced 2025-01-02 00:10:22 +02:00
Refactor obstacles placer
# Conflicts: # lib/rmg/ObstaclePlacer.cpp # lib/rmg/ObstaclePlacer.h
This commit is contained in:
parent
2d4239d039
commit
1705d7e43f
@ -22,22 +22,10 @@
|
||||
#include "CMapGenerator.h"
|
||||
#include "../CRandomGenerator.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
|
||||
for(auto primaryID : VLC->objtypeh->knownObjects())
|
||||
{
|
||||
@ -48,7 +36,7 @@ void ObstaclePlacer::process()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -62,122 +50,172 @@ void ObstaclePlacer::process()
|
||||
{
|
||||
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)
|
||||
{
|
||||
int maxWeight = std::numeric_limits<int>::min();
|
||||
for(int i = 0; i < possibleObstacles.size(); ++i)
|
||||
{
|
||||
return map.shouldBeBlocked(t);
|
||||
});
|
||||
blockedArea.subtract(zone.areaUsed());
|
||||
zone.areaPossible().subtract(blockedArea);
|
||||
|
||||
|
||||
auto prohibitedArea = zone.freePaths() + zone.areaUsed() + manager->getVisitableArea();
|
||||
|
||||
if(!possibleObstacles[i].first)
|
||||
continue;
|
||||
|
||||
auto shuffledObstacles = possibleObstacles[i].second;
|
||||
RandomGeneratorUtil::randomShuffle(shuffledObstacles, rand);
|
||||
|
||||
for(auto temp : shuffledObstacles)
|
||||
{
|
||||
auto handler = VLC->objtypeh->getHandlerFor(temp->id, temp->subid);
|
||||
auto obj = handler->create(temp);
|
||||
allObjects.emplace_back(*obj);
|
||||
rmg::Object * rmgObject = &allObjects.back();
|
||||
for(auto & offset : obj->getBlockedOffsets())
|
||||
{
|
||||
rmgObject->setPosition(tile - offset);
|
||||
if(!map->isInTheMap(rmgObject->getPosition()))
|
||||
continue;
|
||||
|
||||
if(!rmgObject->getArea().getSubarea([map](const int3 & t)
|
||||
{
|
||||
return !map->isInTheMap(t);
|
||||
}).empty())
|
||||
continue;
|
||||
|
||||
if(isProhibited(rmgObject->getArea()))
|
||||
continue;
|
||||
|
||||
int coverageBlocked = 0;
|
||||
int coveragePossible = 0;
|
||||
//do not use area intersection in optimization purposes
|
||||
for(auto & t : rmgObject->getArea().getTilesVector())
|
||||
{
|
||||
auto coverage = verifyCoverage(t);
|
||||
if(coverage.first)
|
||||
++coverageBlocked;
|
||||
if(coverage.second)
|
||||
++coveragePossible;
|
||||
}
|
||||
|
||||
int coverageOverlap = possibleObstacles[i].first - coverageBlocked - coveragePossible;
|
||||
int weight = possibleObstacles[i].first + coverageBlocked - coverageOverlap * possibleObstacles[i].first;
|
||||
assert(coverageOverlap >= 0);
|
||||
|
||||
if(weight > maxWeight)
|
||||
{
|
||||
weightedObjects.clear();
|
||||
maxWeight = weight;
|
||||
weightedObjects.emplace_back(rmgObject, rmgObject->getPosition());
|
||||
if(weight > 0)
|
||||
break;
|
||||
}
|
||||
else if(weight == maxWeight)
|
||||
weightedObjects.emplace_back(rmgObject, rmgObject->getPosition());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(maxWeight > 0)
|
||||
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; //obj + position
|
||||
int maxWeight = std::numeric_limits<int>::min();
|
||||
for(int i = 0; i < possibleObstacles.size(); ++i)
|
||||
{
|
||||
if(!possibleObstacles[i].first)
|
||||
continue;
|
||||
|
||||
auto shuffledObstacles = possibleObstacles[i].second;
|
||||
RandomGeneratorUtil::randomShuffle(shuffledObstacles, generator.rand);
|
||||
|
||||
for(auto & temp : shuffledObstacles)
|
||||
{
|
||||
auto handler = VLC->objtypeh->getHandlerFor(temp->id, temp->subid);
|
||||
auto obj = handler->create(temp);
|
||||
allObjects.emplace_back(*obj);
|
||||
rmg::Object * rmgObject = &allObjects.back();
|
||||
for(auto & offset : obj->getBlockedOffsets())
|
||||
{
|
||||
rmgObject->setPosition(tile - offset);
|
||||
if(!map.isOnMap(rmgObject->getPosition()))
|
||||
continue;
|
||||
|
||||
if(!rmgObject->getArea().getSubarea([this](const int3 & t)
|
||||
{
|
||||
return !map.isOnMap(t);
|
||||
}).empty())
|
||||
continue;
|
||||
|
||||
if(prohibitedArea.overlap(rmgObject->getArea()))
|
||||
continue;
|
||||
|
||||
if(!zone.area().contains(rmgObject->getArea()))
|
||||
continue;
|
||||
|
||||
int coverageBlocked = 0;
|
||||
int coveragePossible = 0;
|
||||
//do not use area intersection in optimization purposes
|
||||
for(auto & t : rmgObject->getArea().getTilesVector())
|
||||
{
|
||||
if(map.shouldBeBlocked(t))
|
||||
++coverageBlocked;
|
||||
if(zone.areaPossible().contains(t))
|
||||
++coveragePossible;
|
||||
}
|
||||
|
||||
int coverageOverlap = possibleObstacles[i].first - coverageBlocked - coveragePossible;
|
||||
int weight = possibleObstacles[i].first + coverageBlocked - coverageOverlap * possibleObstacles[i].first;
|
||||
assert(coverageOverlap >= 0);
|
||||
|
||||
if(weight > maxWeight)
|
||||
{
|
||||
weightedObjects.clear();
|
||||
maxWeight = weight;
|
||||
weightedObjects.emplace_back(rmgObject, rmgObject->getPosition());
|
||||
if(weight > 0)
|
||||
break;
|
||||
}
|
||||
else if(weight == maxWeight)
|
||||
weightedObjects.emplace_back(rmgObject, rmgObject->getPosition());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(maxWeight > 0)
|
||||
break;
|
||||
}
|
||||
|
||||
std::vector<std::pair<rmg::Object*, int3>> weightedObjects;
|
||||
int maxWeight = getWeightedObjects(tile, map, rand, allObjects, weightedObjects);
|
||||
|
||||
if(weightedObjects.empty())
|
||||
{
|
||||
tilePos += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, generator.rand);
|
||||
|
||||
auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, rand);
|
||||
objIter->first->setPosition(objIter->second);
|
||||
manager->placeObject(*objIter->first, false, false);
|
||||
placeObject(*objIter->first, objs);
|
||||
|
||||
blockedArea.subtract(objIter->first->getArea());
|
||||
tilePos = 0;
|
||||
|
||||
//river processing
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
postProcess(*objIter->first);
|
||||
|
||||
if(maxWeight < 0)
|
||||
logGlobal->warn("Placed obstacle with negative weight at %s", objIter->second.toString());
|
||||
|
||||
|
||||
for(auto & o : allObjects)
|
||||
{
|
||||
if(&o != objIter->first)
|
||||
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()
|
||||
@ -189,3 +227,45 @@ void ObstaclePlacer::init()
|
||||
DEPENDENCY(RoadPlacer);
|
||||
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
|
||||
#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:
|
||||
MODIFICATOR(ObstaclePlacer);
|
||||
|
||||
void process() 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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user