1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/lib/mapping/ObstacleProxy.cpp

393 lines
11 KiB
C++
Raw Normal View History

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"
#include "../mapObjectConstructors/AObjectTypeHandler.h"
#include "../mapObjectConstructors/CObjectClassesHandler.h"
#include "../mapObjects/CGObjectInstance.h"
#include "../mapObjects/ObjectTemplate.h"
2024-04-04 21:39:01 +02:00
#include "../mapObjects/ObstacleSetHandler.h"
#include "../VCMI_Lib.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
});
}
bool ObstacleProxy::prepareBiome(const ObstacleSetFilter & filter, CRandomGenerator & rand)
2024-04-04 21:39:01 +02:00
{
possibleObstacles.clear();
std::vector<std::shared_ptr<ObstacleSet>> obstacleSets;
2024-04-04 21:39:01 +02:00
size_t selectedSets = 0;
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;
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
{
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?
}
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
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
logGlobal->info("Added large set of type %s", obstacleSets.back()->getType());
2024-04-04 21:39:01 +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);
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
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);
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--;
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)
{
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;
};
int ObstacleProxy::getWeightedObjects(const int3 & tile, CRandomGenerator & 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())
{
auto newPos = tile - offset;
2023-05-20 12:02:30 +02:00
if(!isInTheMap(newPos))
2023-05-20 12:02:30 +02:00
continue;
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-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;
}
std::set<CGObjectInstance*> ObstacleProxy::createObstacles(CRandomGenerator & 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;
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);
}
2023-10-20 01:25:06 +02:00
std::set<CGObjectInstance*> EditorObstaclePlacer::placeObstacles(CRandomGenerator & rand)
2023-05-20 12:02:30 +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
}
VCMI_LIB_NAMESPACE_END