2023-05-20 12:02:30 +02:00
|
|
|
/*
|
|
|
|
* ObstacleProxy.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-05-21 07:26:36 +02:00
|
|
|
#include "StdInc.h"
|
2023-05-20 12:02:30 +02:00
|
|
|
#include "ObstacleProxy.h"
|
|
|
|
#include "../mapping/CMap.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../mapObjectConstructors/CObjectClassesHandler.h"
|
2024-01-09 16:43:36 +02:00
|
|
|
#include "../mapObjects/CGObjectInstance.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../mapObjects/ObjectTemplate.h"
|
2024-04-04 21:39:01 +02:00
|
|
|
#include "../mapObjects/ObstacleSetHandler.h"
|
2024-05-17 00:05:51 +02:00
|
|
|
#include "../VCMI_Lib.h"
|
2023-05-20 12:02:30 +02:00
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
|
|
|
|
2023-05-20 12:02:30 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void ObstacleProxy::collectPossibleObstacles(TerrainId 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(const auto & temp : handler->getTemplates())
|
|
|
|
{
|
|
|
|
if(temp->canBePlacedAt(terrain) && temp->getBlockMapOffset().valid())
|
|
|
|
obstaclesBySize[temp->getBlockedOffsets().size()].push_back(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-06 15:06:08 +02:00
|
|
|
sortObstacles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObstacleProxy::sortObstacles()
|
|
|
|
{
|
2023-05-20 12:02:30 +02:00
|
|
|
for(const auto & o : obstaclesBySize)
|
|
|
|
{
|
|
|
|
possibleObstacles.emplace_back(o);
|
|
|
|
}
|
|
|
|
boost::sort(possibleObstacles, [](const ObstaclePair &p1, const ObstaclePair &p2) -> bool
|
|
|
|
{
|
|
|
|
return p1.first > p2.first; //bigger obstacles first
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
bool ObstacleProxy::prepareBiome(const ObstacleSetFilter & filter, vstd::RNG & rand)
|
2024-04-04 21:39:01 +02:00
|
|
|
{
|
|
|
|
possibleObstacles.clear();
|
|
|
|
|
2024-04-08 12:50:41 +02:00
|
|
|
std::vector<std::shared_ptr<ObstacleSet>> obstacleSets;
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
size_t selectedSets = 0;
|
2024-04-13 20:56:05 +02:00
|
|
|
const size_t MINIMUM_SETS = 3; // Original Lava has only 4 types of sets
|
2024-04-04 21:39:01 +02:00
|
|
|
const size_t MAXIMUM_SETS = 9;
|
|
|
|
const size_t MIN_SMALL_SETS = 3;
|
|
|
|
const size_t MAX_SMALL_SETS = 5;
|
|
|
|
|
2024-04-11 18:10:14 +02:00
|
|
|
auto terrain = filter.getTerrain();
|
|
|
|
auto localFilter = filter;
|
|
|
|
localFilter.setType(ObstacleSet::EObstacleType::MOUNTAINS);
|
|
|
|
|
|
|
|
TObstacleTypes mountainSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
if (!mountainSets.empty())
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(*RandomGeneratorUtil::nextItem(mountainSets, rand));
|
|
|
|
selectedSets++;
|
|
|
|
logGlobal->info("Mountain set added");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-11 18:10:14 +02:00
|
|
|
logGlobal->warn("No mountain sets found for terrain %s", TerrainId::encode(terrain.getNum()));
|
2024-04-04 21:39:01 +02:00
|
|
|
// FIXME: Do we ever want to generate obstacles without any mountains?
|
|
|
|
}
|
|
|
|
|
2024-04-11 18:10:14 +02:00
|
|
|
localFilter.setType(ObstacleSet::EObstacleType::TREES);
|
|
|
|
TObstacleTypes treeSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
// 1 or 2 tree sets
|
|
|
|
size_t treeSetsCount = std::min<size_t>(treeSets.size(), rand.nextInt(1, 2));
|
|
|
|
for (size_t i = 0; i < treeSetsCount; i++)
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(*RandomGeneratorUtil::nextItem(treeSets, rand));
|
|
|
|
selectedSets++;
|
|
|
|
}
|
|
|
|
logGlobal->info("Added %d tree sets", treeSetsCount);
|
|
|
|
|
|
|
|
// Some obstacle types may be completely missing from water, but it's not a problem
|
2024-04-11 18:10:14 +02:00
|
|
|
localFilter.setTypes({ObstacleSet::EObstacleType::LAKES, ObstacleSet::EObstacleType::CRATERS});
|
|
|
|
TObstacleTypes largeSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
// We probably don't want to have lakes and craters at the same time, choose one of them
|
|
|
|
|
|
|
|
if (!largeSets.empty())
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(*RandomGeneratorUtil::nextItem(largeSets, rand));
|
|
|
|
selectedSets++;
|
|
|
|
|
|
|
|
// TODO: Convert to string
|
2024-04-08 12:50:41 +02:00
|
|
|
logGlobal->info("Added large set of type %s", obstacleSets.back()->getType());
|
2024-04-04 21:39:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-11 18:10:14 +02:00
|
|
|
localFilter.setType(ObstacleSet::EObstacleType::ROCKS);
|
|
|
|
TObstacleTypes rockSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
size_t rockSetsCount = std::min<size_t>(rockSets.size(), rand.nextInt(1, 2));
|
|
|
|
for (size_t i = 0; i < rockSetsCount; i++)
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(*RandomGeneratorUtil::nextItem(rockSets, rand));
|
|
|
|
selectedSets++;
|
|
|
|
}
|
|
|
|
logGlobal->info("Added %d rock sets", rockSetsCount);
|
|
|
|
|
2024-04-11 18:10:14 +02:00
|
|
|
localFilter.setType(ObstacleSet::EObstacleType::PLANTS);
|
|
|
|
TObstacleTypes plantSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
// 1 or 2 sets (3 - rock sets)
|
|
|
|
size_t plantSetsCount = std::min<size_t>(plantSets.size(), rand.nextInt(1, std::max<size_t>(3 - rockSetsCount, 2)));
|
|
|
|
for (size_t i = 0; i < plantSetsCount; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(*RandomGeneratorUtil::nextItem(plantSets, rand));
|
|
|
|
selectedSets++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logGlobal->info("Added %d plant sets", plantSetsCount);
|
|
|
|
|
|
|
|
//3 to 5 of total small sets (rocks, plants, structures, animals and others)
|
|
|
|
//This gives total of 6 to 9 different sets
|
|
|
|
|
|
|
|
size_t maxSmallSets = std::min<size_t>(MAX_SMALL_SETS, std::max(MIN_SMALL_SETS, MAXIMUM_SETS - selectedSets));
|
|
|
|
|
2024-04-06 18:12:44 +02:00
|
|
|
size_t smallSets = rand.nextInt(MIN_SMALL_SETS, maxSmallSets);
|
2024-04-04 21:39:01 +02:00
|
|
|
|
2024-04-11 18:10:14 +02:00
|
|
|
localFilter.setTypes({ObstacleSet::EObstacleType::STRUCTURES, ObstacleSet::EObstacleType::ANIMALS});
|
|
|
|
TObstacleTypes smallObstacleSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
RandomGeneratorUtil::randomShuffle(smallObstacleSets, rand);
|
|
|
|
|
2024-04-11 18:10:14 +02:00
|
|
|
localFilter.setType(ObstacleSet::EObstacleType::OTHER);
|
|
|
|
TObstacleTypes otherSets = VLC->biomeHandler->getObstacles(localFilter);
|
2024-04-04 21:39:01 +02:00
|
|
|
RandomGeneratorUtil::randomShuffle(otherSets, rand);
|
|
|
|
|
2024-04-06 18:12:44 +02:00
|
|
|
while (smallSets > 0)
|
2024-04-04 21:39:01 +02:00
|
|
|
{
|
|
|
|
if (!smallObstacleSets.empty())
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(smallObstacleSets.back());
|
|
|
|
smallObstacleSets.pop_back();
|
|
|
|
selectedSets++;
|
2024-04-06 18:12:44 +02:00
|
|
|
smallSets--;
|
2024-04-08 12:50:41 +02:00
|
|
|
logGlobal->info("Added small set of type %s", obstacleSets.back()->getType());
|
2024-04-04 21:39:01 +02:00
|
|
|
}
|
|
|
|
else if(otherSets.empty())
|
|
|
|
{
|
|
|
|
logGlobal->warn("No other sets found for terrain %s", terrain.encode(terrain.getNum()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-04-06 18:12:44 +02:00
|
|
|
if (smallSets > 0)
|
2024-04-04 21:39:01 +02:00
|
|
|
{
|
|
|
|
// Fill with whatever's left
|
|
|
|
if (!otherSets.empty())
|
|
|
|
{
|
|
|
|
obstacleSets.push_back(otherSets.back());
|
|
|
|
otherSets.pop_back();
|
|
|
|
selectedSets++;
|
2024-04-06 18:12:44 +02:00
|
|
|
smallSets--;
|
2024-04-04 21:39:01 +02:00
|
|
|
|
|
|
|
logGlobal->info("Added set of other obstacles");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy this set to our possible obstacles
|
2024-04-05 08:31:05 +02:00
|
|
|
|
2024-04-13 19:33:47 +02:00
|
|
|
if (selectedSets >= MINIMUM_SETS ||
|
2024-04-05 08:31:05 +02:00
|
|
|
(terrain == TerrainId::WATER && selectedSets > 0))
|
2024-04-04 21:39:01 +02:00
|
|
|
{
|
|
|
|
obstaclesBySize.clear();
|
|
|
|
for (const auto & os : obstacleSets)
|
|
|
|
{
|
2024-04-08 12:50:41 +02:00
|
|
|
for (const auto & temp : os->getObstacles())
|
2024-04-04 21:39:01 +02:00
|
|
|
{
|
|
|
|
if(temp->getBlockMapOffset().valid())
|
|
|
|
{
|
|
|
|
obstaclesBySize[temp->getBlockedOffsets().size()].push_back(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 15:06:08 +02:00
|
|
|
sortObstacles();
|
2024-04-05 08:31:05 +02:00
|
|
|
|
2024-04-04 21:39:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-06 15:06:08 +02:00
|
|
|
return false; // Proceed with old method
|
2024-04-04 21:39:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 12:02:30 +02:00
|
|
|
void ObstacleProxy::addBlockedTile(const int3& tile)
|
|
|
|
{
|
|
|
|
blockedArea.add(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObstacleProxy::setBlockedArea(const rmg::Area& area)
|
|
|
|
{
|
|
|
|
blockedArea = area;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObstacleProxy::clearBlockedArea()
|
|
|
|
{
|
|
|
|
blockedArea.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ObstacleProxy::isProhibited(const rmg::Area& objArea) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
int ObstacleProxy::getWeightedObjects(const int3 & tile, vstd::RNG & rand, IGameCallback * cb, std::list<rmg::Object> & allObjects, std::vector<std::pair<rmg::Object*, int3>> & weightedObjects)
|
2023-05-20 12:02:30 +02:00
|
|
|
{
|
|
|
|
int maxWeight = std::numeric_limits<int>::min();
|
|
|
|
for(auto & possibleObstacle : possibleObstacles)
|
|
|
|
{
|
|
|
|
if(!possibleObstacle.first)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto shuffledObstacles = possibleObstacle.second;
|
|
|
|
RandomGeneratorUtil::randomShuffle(shuffledObstacles, rand);
|
|
|
|
|
|
|
|
for(const auto & temp : shuffledObstacles)
|
|
|
|
{
|
|
|
|
auto handler = VLC->objtypeh->getHandlerFor(temp->id, temp->subid);
|
2024-01-01 16:37:48 +02:00
|
|
|
auto * obj = handler->create(nullptr, temp);
|
2023-05-20 12:02:30 +02:00
|
|
|
allObjects.emplace_back(*obj);
|
|
|
|
rmg::Object * rmgObject = &allObjects.back();
|
|
|
|
for(const auto & offset : obj->getBlockedOffsets())
|
|
|
|
{
|
2023-12-13 23:06:47 +02:00
|
|
|
auto newPos = tile - offset;
|
2023-05-20 12:02:30 +02:00
|
|
|
|
2023-12-13 23:06:47 +02:00
|
|
|
if(!isInTheMap(newPos))
|
2023-05-20 12:02:30 +02:00
|
|
|
continue;
|
|
|
|
|
2023-12-13 23:06:47 +02:00
|
|
|
rmgObject->setPosition(newPos);
|
|
|
|
|
|
|
|
bool isInTheMapEntirely = true;
|
|
|
|
for (const auto & t : rmgObject->getArea().getTiles())
|
|
|
|
{
|
|
|
|
if (!isInTheMap(t))
|
|
|
|
{
|
|
|
|
isInTheMapEntirely = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (!isInTheMapEntirely)
|
2023-05-20 12:02:30 +02:00
|
|
|
{
|
|
|
|
continue;
|
2023-12-13 23:06:47 +02:00
|
|
|
}
|
2023-05-20 12:02:30 +02:00
|
|
|
|
|
|
|
if(isProhibited(rmgObject->getArea()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int coverageBlocked = 0;
|
|
|
|
int coveragePossible = 0;
|
|
|
|
//do not use area intersection in optimization purposes
|
|
|
|
for(const auto & t : rmgObject->getArea().getTilesVector())
|
|
|
|
{
|
|
|
|
auto coverage = verifyCoverage(t);
|
|
|
|
if(coverage.first)
|
|
|
|
++coverageBlocked;
|
|
|
|
if(coverage.second)
|
|
|
|
++coveragePossible;
|
|
|
|
}
|
|
|
|
|
|
|
|
int coverageOverlap = possibleObstacle.first - coverageBlocked - coveragePossible;
|
|
|
|
int weight = possibleObstacle.first + coverageBlocked - coverageOverlap * possibleObstacle.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;
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
std::set<CGObjectInstance*> ObstacleProxy::createObstacles(vstd::RNG & rand, IGameCallback * cb)
|
2023-05-20 12:02:30 +02:00
|
|
|
{
|
|
|
|
//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;
|
2024-01-09 19:08:22 +02:00
|
|
|
int maxWeight = getWeightedObjects(tile, rand, cb, allObjects, weightedObjects);
|
2023-05-20 12:02:30 +02:00
|
|
|
|
|
|
|
if(weightedObjects.empty())
|
|
|
|
{
|
|
|
|
tilePos += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto objIter = RandomGeneratorUtil::nextItem(weightedObjects, rand);
|
|
|
|
objIter->first->setPosition(objIter->second);
|
|
|
|
placeObject(*objIter->first, objs);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return objs;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: Only editor placer obstacles directly
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return {blockedArea.contains(t), false};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObstacleProxy::placeObject(rmg::Object & object, std::set<CGObjectInstance*> & instances)
|
|
|
|
{
|
|
|
|
for (auto * instance : object.instances())
|
|
|
|
{
|
|
|
|
instances.insert(&instance->object());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorObstaclePlacer::EditorObstaclePlacer(CMap* map):
|
|
|
|
map(map)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorObstaclePlacer::isInTheMap(const int3& tile)
|
|
|
|
{
|
|
|
|
return map->isInTheMap(tile);
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
std::set<CGObjectInstance*> EditorObstaclePlacer::placeObstacles(vstd::RNG & rand)
|
2023-05-20 12:02:30 +02:00
|
|
|
{
|
2024-01-09 19:08:22 +02:00
|
|
|
auto obstacles = createObstacles(rand, map->cb);
|
2023-05-20 13:47:36 +02:00
|
|
|
finalInsertion(map->getEditManager(), obstacles);
|
2023-10-20 01:25:06 +02:00
|
|
|
return obstacles;
|
2023-05-20 12:02:30 +02:00
|
|
|
}
|
|
|
|
|
2023-06-02 20:47:37 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|