mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-22 22:13:35 +02:00
Implemented automatic obstacle placement
This commit is contained in:
parent
2a3c603822
commit
ff6cd104cf
@ -22,6 +22,183 @@
|
||||
#include "CMapGenerator.h"
|
||||
#include "../CRandomGenerator.h"
|
||||
#include "Functions.h"
|
||||
#include "../mapping/CMapEditManager.h"
|
||||
|
||||
void ObstacleProxy::collectPossibleObstacles(const Terrain & terrain)
|
||||
{
|
||||
//get all possible obstacles for this terrain
|
||||
for(auto primaryID : VLC->objtypeh->knownObjects())
|
||||
{
|
||||
for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
|
||||
{
|
||||
auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
|
||||
if(handler->isStaticObject())
|
||||
{
|
||||
for(auto temp : handler->getTemplates())
|
||||
{
|
||||
if(temp.canBePlacedAt(terrain) && temp.getBlockMapOffset().valid())
|
||||
obstaclesBySize[temp.getBlockedOffsets().size()].push_back(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(auto o : obstaclesBySize)
|
||||
{
|
||||
possibleObstacles.push_back(o);
|
||||
}
|
||||
boost::sort(possibleObstacles, [](const ObstaclePair &p1, const ObstaclePair &p2) -> bool
|
||||
{
|
||||
return p1.first > p2.first; //bigger obstacles first
|
||||
});
|
||||
}
|
||||
|
||||
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(CMapEditManager * manager, rmg::Object & object)
|
||||
{
|
||||
for(auto * instance : object.instances())
|
||||
{
|
||||
manager->insertObject(&instance->object());
|
||||
}
|
||||
//manager->placeObject(*objIter->first, false, false);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
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())
|
||||
{
|
||||
tilePos += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, rand);
|
||||
objIter->first->setPosition(objIter->second);
|
||||
placeObject(map->getEditManager(), *objIter->first);
|
||||
blockedArea.subtract(objIter->first->getArea());
|
||||
tilePos = 0;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ObstacleProxy::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(oobject.getArea());
|
||||
}*/
|
||||
}
|
||||
|
||||
bool ObstacleProxy::isProhibited(const rmg::Area & objArea) const
|
||||
{
|
||||
/*if(prohibitedArea.overlap(rmgObject->getArea()))
|
||||
continue;
|
||||
|
||||
if(!totalArea.contains(rmgObject->getArea()))
|
||||
continue;*/
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ObstaclePlacer::process()
|
||||
{
|
||||
|
@ -11,6 +11,37 @@
|
||||
#pragma once
|
||||
#include "Zone.h"
|
||||
|
||||
class CMap;
|
||||
class CMapEditManager;
|
||||
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(CMapEditManager * manager, rmg::Object & object);
|
||||
|
||||
virtual void postProcess(const rmg::Object & object);
|
||||
|
||||
virtual bool isProhibited(const rmg::Area & objArea) const;
|
||||
|
||||
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<ObjectTemplate> ObstacleVector;
|
||||
std::map<int, ObstacleVector> obstaclesBySize;
|
||||
typedef std::pair<int, ObstacleVector> ObstaclePair;
|
||||
std::vector<ObstaclePair> possibleObstacles;
|
||||
};
|
||||
|
||||
class ObstaclePlacer: public Modificator
|
||||
{
|
||||
public:
|
||||
|
@ -23,7 +23,7 @@ namespace rmg
|
||||
void toAbsolute(Tileset & tiles, const int3 & position);
|
||||
void toRelative(Tileset & tiles, const int3 & position);
|
||||
|
||||
class Area
|
||||
class DLL_LINKAGE Area
|
||||
{
|
||||
public:
|
||||
Area() = default;
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "../lib/mapping/CMapEditManager.h"
|
||||
#include "../lib/Terrain.h"
|
||||
#include "../lib/mapObjects/CObjectClassesHandler.h"
|
||||
#include "../lib/rmg/ObstaclePlacer.h"
|
||||
|
||||
|
||||
#include "CGameInfo.h"
|
||||
@ -723,3 +724,35 @@ void MainWindow::on_filter_textChanged(const QString &arg1)
|
||||
objectBrowser->sort(0);
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_actionFill_triggered()
|
||||
{
|
||||
if(!map || !scenes[mapLevel])
|
||||
return;
|
||||
|
||||
auto selection = scenes[mapLevel]->selectionTerrainView.selection();
|
||||
if(selection.empty())
|
||||
return;
|
||||
|
||||
//split by zones
|
||||
std::map<Terrain, ObstacleProxy> terrainSelected;
|
||||
for(auto & t : selection)
|
||||
{
|
||||
auto tl = map->getTile(t);
|
||||
if(tl.blocked || tl.visitable)
|
||||
continue;
|
||||
|
||||
terrainSelected[tl.terType].blockedArea.add(t);
|
||||
}
|
||||
|
||||
for(auto & sel : terrainSelected)
|
||||
{
|
||||
sel.second.collectPossibleObstacles(sel.first);
|
||||
sel.second.placeObstacles(map.get(), CRandomGenerator::getDefault());
|
||||
}
|
||||
|
||||
scenes[mapLevel]->selectionObjectsView.deleteSelection();
|
||||
resetMapHandler();
|
||||
scenes[mapLevel]->updateViews();
|
||||
}
|
||||
|
||||
|
@ -70,6 +70,8 @@ private slots:
|
||||
|
||||
void on_filter_textChanged(const QString &arg1);
|
||||
|
||||
void on_actionFill_triggered();
|
||||
|
||||
public slots:
|
||||
|
||||
void treeViewSelected(const QModelIndex &selected, const QModelIndex &deselected);
|
||||
|
@ -724,9 +724,6 @@
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionFill">
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Fill</string>
|
||||
</property>
|
||||
|
Loading…
Reference in New Issue
Block a user