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

Allow multiple terrains per biome

This commit is contained in:
Tomasz Zieliński
2024-04-10 09:40:12 +02:00
parent 5c4d1703ca
commit 6c9d18a85c
3 changed files with 52 additions and 21 deletions

View File

@@ -15,9 +15,18 @@
"description" : "Type of the obstacle set" "description" : "Type of the obstacle set"
}, },
"terrain" : { "terrain" : {
// TODO: Allow multiple terrains "anyOf": [
"type" : "string", {
"description" : "Terrain of the obstacle set" "type" : "string",
"description" : "Terrain of the obstacle set"
},
{
"type" : "array",
"items" : { "type" : "string" },
"description" : "Terrains of the obstacle set"
}
]
} }
} }
}, },

View File

@@ -17,13 +17,13 @@ VCMI_LIB_NAMESPACE_BEGIN
ObstacleSet::ObstacleSet(): ObstacleSet::ObstacleSet():
type(INVALID), type(INVALID),
terrain(TerrainId::NONE) allowedTerrains({TerrainId::NONE})
{ {
} }
ObstacleSet::ObstacleSet(EObstacleType type, TerrainId terrain): ObstacleSet::ObstacleSet(EObstacleType type, TerrainId terrain):
type(type), type(type),
terrain(terrain) allowedTerrains({terrain})
{ {
} }
@@ -46,22 +46,27 @@ ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, Ter
bool ObstacleSetFilter::filter(const ObstacleSet &set) const bool ObstacleSetFilter::filter(const ObstacleSet &set) const
{ {
return (set.getTerrain() == terrain) || (terrain == TerrainId::ANY_TERRAIN); return (vstd::contains(set.getTerrains(), terrain) || terrain == TerrainId::ANY_TERRAIN);
} }
TerrainId ObstacleSetFilter::getTerrain() const std::set<TerrainId> ObstacleSet::getTerrains() const
{ {
return terrain; return allowedTerrains;
}
TerrainId ObstacleSet::getTerrain() const
{
return terrain;
} }
void ObstacleSet::setTerrain(TerrainId terrain) void ObstacleSet::setTerrain(TerrainId terrain)
{ {
this->terrain = terrain; this->allowedTerrains = {terrain};
}
void ObstacleSet::setTerrains(const std::set<TerrainId> & terrains)
{
this->allowedTerrains = terrains;
}
void ObstacleSet::addTerrain(TerrainId terrain)
{
this->allowedTerrains.insert(terrain);
} }
ObstacleSet::EObstacleType ObstacleSet::getType() const ObstacleSet::EObstacleType ObstacleSet::getType() const
@@ -221,12 +226,27 @@ std::shared_ptr<ObstacleSet> ObstacleSetHandler::loadFromJson(const std::string
auto biome = json["biome"].Struct(); auto biome = json["biome"].Struct();
os->setType(ObstacleSet::typeFromString(biome["objectType"].String())); os->setType(ObstacleSet::typeFromString(biome["objectType"].String()));
auto terrainName = biome["terrain"].String(); if (biome["terrain"].isString())
VLC->identifiers()->requestIdentifier(scope, "terrain", terrainName, [os](si32 id)
{ {
os->setTerrain(TerrainId(id)); auto terrainName = biome["terrain"].String();
});
VLC->identifiers()->requestIdentifier(scope, "terrain", terrainName, [os](si32 id)
{
os->setTerrain(TerrainId(id));
});
}
else // Other cases won't pass validation
{
auto terrains = biome["terrain"].Vector();
for (const auto & terrain : terrains)
{
VLC->identifiers()->requestIdentifier(scope, "terrain", terrain.String(), [os](si32 id)
{
os->addTerrain(TerrainId(id));
});
}
}
auto templates = json["templates"].Vector(); auto templates = json["templates"].Vector();
for (const auto & node : templates) for (const auto & node : templates)

View File

@@ -46,8 +46,10 @@ public:
EObstacleType getType() const; EObstacleType getType() const;
void setType(EObstacleType type); void setType(EObstacleType type);
TerrainId getTerrain() const; std::set<TerrainId> getTerrains() const;
void setTerrain(TerrainId terrain); void setTerrain(TerrainId terrain);
void setTerrains(const std::set<TerrainId> & terrains);
void addTerrain(TerrainId terrain);
static EObstacleType typeFromString(const std::string &str); static EObstacleType typeFromString(const std::string &str);
std::string toString() const; std::string toString() const;
@@ -56,7 +58,7 @@ public:
private: private:
EObstacleType type; EObstacleType type;
TerrainId terrain; std::set<TerrainId> allowedTerrains;
std::vector<std::shared_ptr<const ObjectTemplate>> obstacles; std::vector<std::shared_ptr<const ObjectTemplate>> obstacles;
}; };