1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-23 21:29:13 +02:00

Use the most suitable template for object

This commit is contained in:
Tomasz Zieliński 2023-03-28 17:53:08 +02:00
parent 49c029ea6c
commit b184e80b72
3 changed files with 33 additions and 18 deletions

@ -105,7 +105,12 @@ public:
inline bool canBePlacedAtAnyTerrain() const
{
return anyTerrain;
};
};
const std::set<TerrainId>& getAllowedTerrains() const
{
return allowedTerrains;
}
// Checks if object can be placed on specific terrain
bool canBePlacedAt(TerrainId terrain) const;

@ -79,7 +79,15 @@ void ObjectDistributor::distributeLimitedObjects()
for (auto& zone : matchingZones)
{
auto temp = handler->getTemplates(zone->getTerrainType()).front();
//We already know there are some templates
auto templates = handler->getTemplates(zone->getTerrainType());
//Assume the template with fewest terrains is the most suitable
auto temp = *boost::min_element(templates, [](std::shared_ptr<const ObjectTemplate> lhs, std::shared_ptr<const ObjectTemplate> rhs) -> bool
{
return lhs->getAllowedTerrains().size() < rhs->getAllowedTerrains().size();
});
oi.generateObject = [temp]() -> CGObjectInstance *
{
return VLC->objtypeh->getHandlerFor(temp->id, temp->subid)->create(temp);

@ -71,23 +71,25 @@ void TreasurePlacer::addAllPossibleObjects()
continue;
}
for(const auto & temp : handler->getTemplates())
{
if(temp->canBePlacedAt(zone.getTerrainType()))
{
oi.generateObject = [temp]() -> CGObjectInstance *
{
return VLC->objtypeh->getHandlerFor(temp->id, temp->subid)->create(temp);
};
oi.value = rmgInfo.value;
oi.probability = rmgInfo.rarity;
oi.templ = temp;
oi.maxPerZone = rmgInfo.zoneLimit;
addObjectToRandomPool(oi);
auto templates = handler->getTemplates(zone.getTerrainType());
if (templates.empty())
continue;
break;
}
}
//Assume the template with fewest terrains is the most suitable
auto temp = *boost::min_element(templates, [](std::shared_ptr<const ObjectTemplate> lhs, std::shared_ptr<const ObjectTemplate> rhs) -> bool
{
return lhs->getAllowedTerrains().size() < rhs->getAllowedTerrains().size();
});
oi.generateObject = [temp]() -> CGObjectInstance *
{
return VLC->objtypeh->getHandlerFor(temp->id, temp->subid)->create(temp);
};
oi.value = rmgInfo.value;
oi.probability = rmgInfo.rarity;
oi.templ = temp;
oi.maxPerZone = rmgInfo.zoneLimit;
addObjectToRandomPool(oi);
}
}
}