2023-03-27 09:09:58 +02:00
|
|
|
/*
|
|
|
|
* ObjectDistributor.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "ObjectDistributor.h"
|
|
|
|
|
2023-05-20 11:45:08 +02:00
|
|
|
#include "../../VCMI_Lib.h"
|
|
|
|
#include "../RmgMap.h"
|
|
|
|
#include "../CMapGenerator.h"
|
2023-03-27 09:09:58 +02:00
|
|
|
#include "TreasurePlacer.h"
|
2023-12-24 10:36:26 +02:00
|
|
|
#include "PrisonHeroPlacer.h"
|
2023-04-29 11:46:03 +02:00
|
|
|
#include "QuestArtifactPlacer.h"
|
2023-03-27 09:09:58 +02:00
|
|
|
#include "TownPlacer.h"
|
|
|
|
#include "TerrainPainter.h"
|
2023-06-02 20:47:37 +02:00
|
|
|
#include "../../mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
|
#include "../../mapObjectConstructors/CObjectClassesHandler.h"
|
2023-05-20 11:45:08 +02:00
|
|
|
#include "../../mapObjects/MapObjects.h"
|
|
|
|
#include "../Functions.h"
|
|
|
|
#include "../RmgObject.h"
|
2023-03-27 09:09:58 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void ObjectDistributor::process()
|
|
|
|
{
|
2023-05-20 13:15:46 +02:00
|
|
|
distributeLimitedObjects();
|
2023-06-07 15:08:50 +02:00
|
|
|
distributePrisons();
|
2023-05-20 13:15:46 +02:00
|
|
|
distributeSeerHuts();
|
2023-03-27 09:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectDistributor::init()
|
|
|
|
{
|
2023-05-19 20:30:15 +02:00
|
|
|
//All of the terrain types need to be determined
|
|
|
|
DEPENDENCY_ALL(TerrainPainter);
|
2023-03-27 09:09:58 +02:00
|
|
|
POSTFUNCTION(TreasurePlacer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectDistributor::distributeLimitedObjects()
|
|
|
|
{
|
|
|
|
ObjectInfo oi;
|
|
|
|
auto zones = map.getZones();
|
|
|
|
|
|
|
|
for (auto primaryID : VLC->objtypeh->knownObjects())
|
|
|
|
{
|
|
|
|
for (auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
|
|
|
|
{
|
|
|
|
auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
|
|
|
|
if (!handler->isStaticObject() && handler->getRMGInfo().value)
|
|
|
|
{
|
|
|
|
auto rmgInfo = handler->getRMGInfo();
|
|
|
|
|
|
|
|
//Skip objects which don't have global per-map limit here
|
|
|
|
if (rmgInfo.mapLimit)
|
|
|
|
{
|
|
|
|
//Count all zones where this object can be placed
|
|
|
|
std::vector<std::shared_ptr<Zone>> matchingZones;
|
|
|
|
|
|
|
|
for (const auto& it : zones)
|
|
|
|
{
|
2023-03-27 17:29:46 +02:00
|
|
|
if (!handler->getTemplates(it.second->getTerrainType()).empty() &&
|
|
|
|
rmgInfo.value <= it.second->getMaxTreasureValue())
|
2023-03-27 09:09:58 +02:00
|
|
|
{
|
|
|
|
matchingZones.push_back(it.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t numZones = matchingZones.size();
|
|
|
|
if (!numZones)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto rmgInfo = handler->getRMGInfo();
|
|
|
|
|
2023-12-07 18:41:41 +02:00
|
|
|
RandomGeneratorUtil::randomShuffle(matchingZones, zone.getRand());
|
2023-03-27 09:09:58 +02:00
|
|
|
for (auto& zone : matchingZones)
|
|
|
|
{
|
2024-01-09 19:08:22 +02:00
|
|
|
oi.generateObject = [cb=map.mapInstance->cb, primaryID, secondaryID]() -> CGObjectInstance *
|
2023-03-27 09:09:58 +02:00
|
|
|
{
|
2024-01-09 19:08:22 +02:00
|
|
|
return VLC->objtypeh->getHandlerFor(primaryID, secondaryID)->create(cb, nullptr);
|
2023-03-27 09:09:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
oi.value = rmgInfo.value;
|
|
|
|
oi.probability = rmgInfo.rarity;
|
2023-09-30 23:06:38 +02:00
|
|
|
oi.setTemplates(primaryID, secondaryID, zone->getTerrainType());
|
2023-03-27 09:09:58 +02:00
|
|
|
|
|
|
|
//Rounding up will make sure all possible objects are exhausted
|
2023-04-16 19:42:56 +02:00
|
|
|
uint32_t mapLimit = rmgInfo.mapLimit.value();
|
2023-03-27 09:09:58 +02:00
|
|
|
uint32_t maxPerZone = std::ceil(float(mapLimit) / numZones);
|
|
|
|
|
|
|
|
//But not more than zone limit
|
|
|
|
oi.maxPerZone = std::min(maxPerZone, rmgInfo.zoneLimit);
|
|
|
|
numZones--;
|
|
|
|
|
|
|
|
rmgInfo.setMapLimit(mapLimit - oi.maxPerZone);
|
|
|
|
//Don't add objects with 0 count remaining
|
2023-09-30 23:06:38 +02:00
|
|
|
if(oi.maxPerZone && !oi.templates.empty())
|
2023-03-27 09:09:58 +02:00
|
|
|
{
|
|
|
|
zone->getModificator<TreasurePlacer>()->addObjectToRandomPool(oi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 11:46:03 +02:00
|
|
|
void ObjectDistributor::distributeSeerHuts()
|
|
|
|
{
|
|
|
|
//TODO: Move typedef outside the class?
|
|
|
|
|
|
|
|
//Copy by value to random shuffle
|
|
|
|
const auto & zoneMap = map.getZones();
|
|
|
|
RmgMap::ZoneVector zones(zoneMap.begin(), zoneMap.end());
|
|
|
|
|
2023-05-20 11:46:32 +02:00
|
|
|
RandomGeneratorUtil::randomShuffle(zones, zone.getRand());
|
2023-04-29 11:46:03 +02:00
|
|
|
|
2023-05-05 10:30:36 +02:00
|
|
|
const auto & possibleQuestArts = generator.getAllPossibleQuestArtifacts();
|
2023-04-29 11:46:03 +02:00
|
|
|
size_t availableArts = possibleQuestArts.size();
|
|
|
|
auto artIt = possibleQuestArts.begin();
|
|
|
|
for (int i = zones.size() - 1; i >= 0 ; i--)
|
|
|
|
{
|
|
|
|
size_t localArts = std::ceil((float)availableArts / (i + 1));
|
|
|
|
availableArts -= localArts;
|
|
|
|
|
|
|
|
auto * qap = zones[i].second->getModificator<QuestArtifactPlacer>();
|
|
|
|
if (qap)
|
|
|
|
{
|
|
|
|
for (;localArts > 0 && artIt != possibleQuestArts.end(); artIt++, localArts--)
|
|
|
|
{
|
|
|
|
qap->addRandomArtifact(*artIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:30:36 +02:00
|
|
|
void ObjectDistributor::distributePrisons()
|
|
|
|
{
|
|
|
|
//Copy by value to random shuffle
|
|
|
|
const auto & zoneMap = map.getZones();
|
|
|
|
RmgMap::ZoneVector zones(zoneMap.begin(), zoneMap.end());
|
|
|
|
|
2023-05-20 11:46:32 +02:00
|
|
|
RandomGeneratorUtil::randomShuffle(zones, zone.getRand());
|
2023-05-05 10:30:36 +02:00
|
|
|
|
2023-12-24 10:36:26 +02:00
|
|
|
// TODO: Some shorthand for unique Modificator
|
|
|
|
PrisonHeroPlacer * prisonHeroPlacer = nullptr;
|
|
|
|
for(auto & z : map.getZones())
|
|
|
|
{
|
|
|
|
prisonHeroPlacer = z.second->getModificator<PrisonHeroPlacer>();
|
|
|
|
if (prisonHeroPlacer)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t allowedPrisons = prisonHeroPlacer->getPrisonsRemaning();
|
2023-05-05 10:30:36 +02:00
|
|
|
for (int i = zones.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto zone = zones[i].second;
|
|
|
|
auto * tp = zone->getModificator<TreasurePlacer>();
|
|
|
|
if (tp)
|
|
|
|
{
|
|
|
|
tp->setMaxPrisons(std::ceil(float(allowedPrisons) / (i + 1)));
|
|
|
|
allowedPrisons -= tp->getMaxPrisons();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 20:47:37 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|