mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-25 22:42:04 +02:00
Allow filtering obstacles by faction aligmnment
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include "ObstacleSetHandler.h"
|
||||
|
||||
#include "../modding/IdentifierStorage.h"
|
||||
#include "../constants/stringConstants.h"
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
@@ -32,21 +33,41 @@ 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):
|
||||
ObstacleSetFilter::ObstacleSetFilter(std::vector<ObstacleSet::EObstacleType> allowedTypes, TerrainId terrain = TerrainId::ANY_TERRAIN, EAlignment alignment = EAlignment::ANY):
|
||||
allowedTypes(allowedTypes),
|
||||
terrain(terrain)
|
||||
terrain(terrain),
|
||||
alignment(alignment)
|
||||
{
|
||||
}
|
||||
|
||||
ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain = TerrainId::ANY_TERRAIN):
|
||||
ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain = TerrainId::ANY_TERRAIN, EAlignment alignment = EAlignment::ANY):
|
||||
allowedTypes({allowedType}),
|
||||
terrain(terrain)
|
||||
terrain(terrain),
|
||||
alignment(alignment)
|
||||
{
|
||||
}
|
||||
|
||||
bool ObstacleSetFilter::filter(const ObstacleSet &set) const
|
||||
{
|
||||
return (vstd::contains(set.getTerrains(), terrain) || terrain == TerrainId::ANY_TERRAIN);
|
||||
if (terrain != TerrainId::ANY_TERRAIN && !vstd::contains(set.getTerrains(), terrain))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Also check specific factions
|
||||
auto alignments = set.getAlignments();
|
||||
|
||||
if (alignment != EAlignment::ANY && !alignments.empty() && !vstd::contains(alignments, alignment))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TerrainId ObstacleSetFilter::getTerrain() const
|
||||
{
|
||||
return terrain;
|
||||
}
|
||||
|
||||
std::set<TerrainId> ObstacleSet::getTerrains() const
|
||||
@@ -69,6 +90,16 @@ void ObstacleSet::addTerrain(TerrainId terrain)
|
||||
this->allowedTerrains.insert(terrain);
|
||||
}
|
||||
|
||||
void ObstacleSet::addAlignment(EAlignment alignment)
|
||||
{
|
||||
this->allowedAlignments.insert(alignment);
|
||||
}
|
||||
|
||||
std::set<EAlignment> ObstacleSet::getAlignments() const
|
||||
{
|
||||
return allowedAlignments;
|
||||
}
|
||||
|
||||
ObstacleSet::EObstacleType ObstacleSet::getType() const
|
||||
{
|
||||
return type;
|
||||
@@ -185,6 +216,16 @@ std::vector<ObstacleSet::EObstacleType> ObstacleSetFilter::getAllowedTypes() con
|
||||
return allowedTypes;
|
||||
}
|
||||
|
||||
void ObstacleSetFilter::setType(ObstacleSet::EObstacleType type)
|
||||
{
|
||||
allowedTypes = {type};
|
||||
}
|
||||
|
||||
void ObstacleSetFilter::setTypes(std::vector<ObstacleSet::EObstacleType> types)
|
||||
{
|
||||
this->allowedTypes = types;
|
||||
}
|
||||
|
||||
std::vector<JsonNode> ObstacleSetHandler::loadLegacyData()
|
||||
{
|
||||
return {};
|
||||
@@ -248,6 +289,29 @@ std::shared_ptr<ObstacleSet> ObstacleSetHandler::loadFromJson(const std::string
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Move this parser to some utils
|
||||
auto parseAlignment = [](const std::string & str) ->EAlignment
|
||||
{
|
||||
int alignment = vstd::find_pos(GameConstants::ALIGNMENT_NAMES, str);
|
||||
if (alignment == -1)
|
||||
logGlobal->error("Incorrect alignment: ", str);
|
||||
else
|
||||
return static_cast<EAlignment>(alignment);
|
||||
};
|
||||
|
||||
if (json["alignment"].isString())
|
||||
{
|
||||
os->addAlignment(parseAlignment(json["alignment"].String()));
|
||||
}
|
||||
else if (json["alignment"].isVector())
|
||||
{
|
||||
auto alignments = json["alignment"].Vector();
|
||||
for (const auto & node : alignments)
|
||||
{
|
||||
os->addAlignment(parseAlignment(node.String()));
|
||||
}
|
||||
}
|
||||
|
||||
auto templates = json["templates"].Vector();
|
||||
for (const auto & node : templates)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user