1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Also allow filtering biomes by faction(s)

This commit is contained in:
Tomasz Zieliński
2024-04-12 10:25:13 +02:00
parent 6899acc1d7
commit 4fa7f0e93d
7 changed files with 115 additions and 23 deletions

View File

@@ -33,16 +33,18 @@ void ObstacleSet::addObstacle(std::shared_ptr<const ObjectTemplate> obstacle)
obstacles.push_back(obstacle);
}
ObstacleSetFilter::ObstacleSetFilter(std::vector<ObstacleSet::EObstacleType> allowedTypes, TerrainId terrain = TerrainId::ANY_TERRAIN, EAlignment alignment = EAlignment::ANY):
ObstacleSetFilter::ObstacleSetFilter(std::vector<ObstacleSet::EObstacleType> allowedTypes, TerrainId terrain = TerrainId::ANY_TERRAIN, FactionID faction = FactionID::ANY, EAlignment alignment = EAlignment::ANY):
allowedTypes(allowedTypes),
terrain(terrain),
faction(faction),
alignment(alignment)
{
}
ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain = TerrainId::ANY_TERRAIN, EAlignment alignment = EAlignment::ANY):
ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain = TerrainId::ANY_TERRAIN, FactionID faction = FactionID::ANY, EAlignment alignment = EAlignment::ANY):
allowedTypes({allowedType}),
terrain(terrain),
faction(faction),
alignment(alignment)
{
}
@@ -54,6 +56,15 @@ bool ObstacleSetFilter::filter(const ObstacleSet &set) const
return false;
}
if (faction != FactionID::ANY)
{
auto factions = set.getFactions();
if (!factions.empty() && !vstd::contains(factions, faction))
{
return false;
}
}
// TODO: Also check specific factions
if (alignment != EAlignment::ANY)
{
@@ -92,6 +103,16 @@ void ObstacleSet::addTerrain(TerrainId terrain)
this->allowedTerrains.insert(terrain);
}
std::set<FactionID> ObstacleSet::getFactions() const
{
return allowedFactions;
}
void ObstacleSet::addFaction(FactionID faction)
{
this->allowedFactions.insert(faction);
}
void ObstacleSet::addAlignment(EAlignment alignment)
{
this->allowedAlignments.insert(alignment);
@@ -295,6 +316,28 @@ std::shared_ptr<ObstacleSet> ObstacleSetHandler::loadFromJson(const std::string
}
}
auto parseFaction = [os, scope](const std::string & str) -> FactionID
{
VLC->identifiers()->requestIdentifier(scope, "faction", str, [os](si32 id)
{
os->addFaction(FactionID(id));
});
};
if (biome["faction"].isString())
{
auto factionName = biome["faction"].String();
parseFaction(factionName);
}
else if (biome["faction"].isVector())
{
auto factions = biome["faction"].Vector();
for (const auto & node : factions)
{
parseFaction(node.String());
}
}
// TODO: Move this parser to some utils
auto parseAlignment = [](const std::string & str) ->EAlignment
{