1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00
This commit is contained in:
Tomasz Zieliński 2022-09-19 16:13:58 +02:00
parent 7ba271edf1
commit b20f649521
12 changed files with 221 additions and 184 deletions

View File

@ -113,7 +113,7 @@ CSoundHandler::CSoundHandler():
{
horseSounds[Terrain::createTerrainTypeH3M(h3mTerrId++)] = snd;
}
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
//since all sounds are hardcoded, let's keep it
if(vstd::contains(horseSounds, terrain))
@ -364,7 +364,7 @@ CMusicHandler::CMusicHandler():
addEntryToSet("enemy-turn", file.getName(), file.getName());
}
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
auto & entry = Terrain::Manager::getInfo(terrain);
addEntryToSet("terrain", terrain, "Music/" + entry.musicFilename);

View File

@ -189,7 +189,7 @@ void CMapHandler::initTerrainGraphics()
};
std::map<std::string, std::string> terrainFiles;
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
terrainFiles[terrain] = Terrain::Manager::getInfo(terrain).tilesFilename;
}

View File

@ -499,7 +499,7 @@ std::map<Terrain, std::pair<SDL_Color, SDL_Color> > CMinimap::loadColors()
{
std::map<Terrain, std::pair<SDL_Color, SDL_Color> > ret;
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
auto & m = Terrain::Manager::getInfo(terrain);
SDL_Color normal =

View File

@ -338,7 +338,7 @@ CGeneralTextHandler::CGeneralTextHandler()
{
terrainNames[Terrain::createTerrainTypeH3M(i)] = h3mTerrainNames[i];
}
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
if(!Terrain::Manager::getInfo(terrain).terrainText.empty())
terrainNames[terrain] = Terrain::Manager::getInfo(terrain).terrainText;

View File

@ -344,9 +344,9 @@ CHeroHandler::~CHeroHandler() = default;
CHeroHandler::CHeroHandler()
{
loadTerrains();
for(int i = 0; i < Terrain::Manager::terrains().size(); ++i)
for(int i = 0; i < VLC->terrainTypeHandler::terrains().size(); ++i)
{
VLC->modh->identifiers.registerObject("core", "terrain", Terrain::Manager::terrains()[i], i);
VLC->modh->identifiers.registerObject("core", "terrain", VLC->terrainTypeHandler::terrains()[i], i);
}
loadBallistics();
loadExperience();
@ -972,7 +972,7 @@ ui64 CHeroHandler::reqExp (ui32 level) const
void CHeroHandler::loadTerrains()
{
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
terrCosts[terrain] = Terrain::Manager::getInfo(terrain).moveCost;
}

View File

@ -1008,7 +1008,7 @@ bool CPathfinderHelper::passOneTurnLimitCheck(const PathNodeInfo & source) const
TurnInfo::BonusCache::BonusCache(TConstBonusListPtr bl)
{
for(int i = 0; i < Terrain::Manager::terrains().size(); ++i)
for(int i = 0; i < VLC->terrainTypeHandler::terrains().size(); ++i)
{
noTerrainPenalty.push_back(static_cast<bool>(
bl->getFirst(Selector::type()(Bonus::NO_TERRAIN_PENALTY).And(Selector::subtype()(i)))));

View File

@ -832,6 +832,28 @@ public:
ID_LIKE_OPERATORS(Obj, Obj::EObj)
namespace Terrain
{
enum ETerrain : TTerrain
{
ANY_TERRAIN = -3,
WRONG = -2,
BORDER = -1,
FIRST_REGULAR_TERRAIN = 0,
DIRT = 0,
SAND,
GRASS,
SNOW,
SWAMP,
ROUGH,
SUBTERRANEAN,
LAVA,
WATER,
ROCK,
ORIGINAL_TERRAIN_COUNT
};
}
namespace SecSkillLevel
{
enum SecSkillLevel
@ -1183,6 +1205,7 @@ typedef si64 TExpType;
typedef std::pair<si64, si64> TDmgRange;
typedef si32 TBonusSubtype;
typedef si32 TQuantity;
typedef ui8 TTerrain;
typedef int TRmgTemplateZoneId;

View File

@ -17,31 +17,15 @@
//("allowedTerrain"\s*:\s*\[.*)9(.*\],\n)
//\1"rock"\2
const Terrain Terrain::ANY("ANY");
Terrain Terrain::createTerrainTypeH3M(int tId)
{
static std::array<std::string, 10> terrainsH3M
{
"dirt", "sand", "grass", "snow", "swamp", "rough", "subterra", "lava", "water", "rock"
};
return Terrain(terrainsH3M.at(tId));
}
Terrain Terrain::createTerrainByCode(const std::string & typeCode)
{
for(const auto & terrain : Manager::terrains())
{
if(Manager::getInfo(terrain).typeCode == typeCode)
return terrain;
}
return Terrain::ANY;
}
Terrain::Manager::Manager()
TerrainTypeHandler::TerrainTypeHandler()
{
auto allConfigs = VLC->modh->getActiveMods();
allConfigs.insert(allConfigs.begin(), "core");
std::vector<std::function<void()>> resolveLater;
objects.resize(Terrain::ORIGINAL_TERRAIN_COUNT, nullptr); //make space for original terrains
for(auto & mod : allConfigs)
{
if(!CResourceHandler::get(mod)->existsResource(ResourceID("config/terrains.json")))
@ -50,10 +34,11 @@ Terrain::Manager::Manager()
JsonNode terrs(mod, ResourceID("config/terrains.json"));
for(auto & terr : terrs.Struct())
{
Terrain::Info info;
info.moveCost = terr.second["moveCost"].Integer();
auto * info = new TerrainType(terr.first);
info->moveCost = terr.second["moveCost"].Integer();
const JsonVector &unblockedVec = terr.second["minimapUnblocked"].Vector();
info.minimapUnblocked =
info->minimapUnblocked =
{
ui8(unblockedVec[0].Float()),
ui8(unblockedVec[1].Float()),
@ -61,75 +46,75 @@ Terrain::Manager::Manager()
};
const JsonVector &blockedVec = terr.second["minimapBlocked"].Vector();
info.minimapBlocked =
info->minimapBlocked =
{
ui8(blockedVec[0].Float()),
ui8(blockedVec[1].Float()),
ui8(blockedVec[2].Float())
};
info.musicFilename = terr.second["music"].String();
info.tilesFilename = terr.second["tiles"].String();
info->musicFilename = terr.second["music"].String();
info->tilesFilename = terr.second["tiles"].String();
if(terr.second["type"].isNull())
{
info.type = Terrain::Info::Type::Land;
info->passabilityType = TerrainType::PassabilityType::LAND;
}
else
{
auto s = terr.second["type"].String();
if(s == "LAND") info.type = Terrain::Info::Type::Land;
if(s == "WATER") info.type = Terrain::Info::Type::Water;
if(s == "ROCK") info.type = Terrain::Info::Type::Rock;
if(s == "SUB") info.type = Terrain::Info::Type::Subterranean;
if(s == "LAND") info->passabilityType = TerrainType::PassabilityType::LAND;
if(s == "WATER") info->passabilityType = TerrainType::PassabilityType::WATER;
if(s == "ROCK") info->passabilityType = TerrainType::PassabilityType::ROCK;
if(s == "SUB") info->passabilityType = TerrainType::PassabilityType::SUBTERRANEAN;
}
if(terr.second["rockTerrain"].isNull())
{
info.rockTerrain = "rock";
info->rockTerrain = "rock";
}
else
{
info.rockTerrain = terr.second["rockTerrain"].String();
info->rockTerrain = terr.second["rockTerrain"].String();
}
if(terr.second["river"].isNull())
{
info.river = RIVER_NAMES[0];
info->river = RIVER_NAMES[0];
}
else
{
info.river = terr.second["river"].String();
info->river = terr.second["river"].String();
}
if(terr.second["horseSoundId"].isNull())
{
info.horseSoundId = 9; //rock sound as default
info->horseSoundId = 9; //rock sound as default
}
else
{
info.horseSoundId = terr.second["horseSoundId"].Integer();
info->horseSoundId = terr.second["horseSoundId"].Integer();
}
if(!terr.second["text"].isNull())
{
info.terrainText = terr.second["text"].String();
info->terrainText = terr.second["text"].String();
}
if(terr.second["code"].isNull())
{
info.typeCode = terr.first.substr(0, 2);
info->typeCode = terr.first.substr(0, 2);
}
else
{
info.typeCode = terr.second["code"].String();
assert(info.typeCode.length() == 2);
info->typeCode = terr.second["code"].String();
assert(info->typeCode.length() == 2);
}
if(!terr.second["battleFields"].isNull())
{
for(auto & t : terr.second["battleFields"].Vector())
{
info.battleFields.emplace_back(t.String());
info->battleFields.emplace_back(t.String());
}
}
@ -137,123 +122,133 @@ Terrain::Manager::Manager()
{
for(auto & t : terr.second["prohibitTransitions"].Vector())
{
info.prohibitTransitions.emplace_back(t.String());
info->prohibitTransitions.emplace_back(t.String());
}
}
info.transitionRequired = false;
info->transitionRequired = false;
if(!terr.second["transitionRequired"].isNull())
{
info.transitionRequired = terr.second["transitionRequired"].Bool();
info->transitionRequired = terr.second["transitionRequired"].Bool();
}
info.terrainViewPatterns = "normal";
info->terrainViewPatterns = "normal";
if(!terr.second["terrainViewPatterns"].isNull())
{
info.terrainViewPatterns = terr.second["terrainViewPatterns"].String();
info->terrainViewPatterns = terr.second["terrainViewPatterns"].String();
}
//TODO: handle 10 origina terrains
terrainInfo[terr.first] = info;
if(!terrainId.count(terr.first))
terrainInfoByName[terr.first] = info;
terrainInfoByCode[info->typeCode] = info;
terrainInfoById[info->id] = info;
TTerrain id;
if(!terr.second["originalTerrainId"].isNull())
{
terrainId[terr.first] = terrainVault.size();
terrainVault.push_back(terr.first);
//place in reserved slot
id = (TTerrain)(terr.second["originalTerrainId"].Float());
objects[id] = info;
}
else
{
//append at the end
id = objects.size();
objects.push_back(info);
}
}
}
for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
{
//Make sure that original terrains are loaded
assert(objects(i));
}
//TODO: add ids to resolve
for (auto& functor : resolveLater)
{
functor();
}
}
Terrain::Manager & Terrain::Manager::get()
const std::vector<TerrainType *> & TerrainTypeHandler::terrains()
{
static Terrain::Manager manager;
return manager;
return objects;
}
const std::vector<Terrain> & Terrain::Manager::terrains()
const TerrainType* TerrainTypeHandler::getInfoByName(const std::string& terrainName) const
{
return Terrain::Manager::get().terrainVault;
}
int Terrain::Manager::id(const Terrain & terrain)
const TerrainType* TerrainTypeHandler::getInfoByCode(const std::string& terrainName) const
{
if(terrain.name == "ANY") return -3;
if(terrain.name == "WRONG") return -2;
if(terrain.name == "BORDER") return -1;
return Terrain::Manager::get().terrainId.at(terrain);
}
const Terrain::Info & Terrain::Manager::getInfo(const Terrain & terrain)
const TerrainType* TerrainTypeHandler::getInfoById(TTerrain id) const
{
return Terrain::Manager::get().terrainInfo.at(static_cast<std::string>(terrain));
}
std::ostream & operator<<(std::ostream & os, const Terrain terrainType)
std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType)
{
return os << static_cast<const std::string &>(terrainType);
}
Terrain::operator std::string() const
TerrainType::operator std::string() const
{
return name;
}
Terrain::Terrain(const std::string & _name) : name(_name)
TerrainType::TerrainType(const std::string & _name) : name(_name)
{}
Terrain& Terrain::operator=(const Terrain & _name)
TerrainType& TerrainType::operator=(const TerrainType & other)
{
name = _name.name;
//TODO
name = other.name;
return *this;
}
Terrain& Terrain::operator=(const std::string & _name)
bool TerrainType::operator==(const TerrainType& other)
{
name = _name;
return *this;
return id == other.id;
}
bool operator==(const Terrain & l, const Terrain & r)
bool TerrainType::operator!=(const TerrainType& other)
{
return l.name == r.name;
return id != other.id;
}
bool operator!=(const Terrain & l, const Terrain & r)
bool TerrainType::operator<(const TerrainType& other)
{
return l.name != r.name;
return id < other.id;
}
bool operator<(const Terrain & l, const Terrain & r)
{
return l.name < r.name;
}
int Terrain::id() const
{
return Terrain::Manager::id(*this);
}
bool Terrain::isLand() const
bool TerrainType::isLand() const
{
return !isWater();
}
bool Terrain::isWater() const
bool TerrainType::isWater() const
{
return Terrain::Manager::getInfo(*this).type == Terrain::Info::Type::Water;
return passabilityType == PassabilityType::WATER;
}
bool Terrain::isPassable() const
bool TerrainType::isPassable() const
{
return Terrain::Manager::getInfo(*this).type != Terrain::Info::Type::Rock;
return passabilityType != PassabilityType::ROCK;
}
bool Terrain::isUnderground() const
bool TerrainType::isUnderground() const
{
return Terrain::Manager::getInfo(*this).type == Terrain::Info::Type::Subterranean;
return passabilityType != PassabilityType::SUBTERRANEAN;
}
bool Terrain::isNative() const
bool TerrainType::isTransitionRequired() const
{
return name.empty();
}
bool Terrain::isTransitionRequired() const
{
return Terrain::Manager::getInfo(*this).transitionRequired;
return transitionRequired;
}

View File

@ -15,92 +15,111 @@
#include "JsonNode.h"
class DLL_LINKAGE Terrain
class DLL_LINKAGE TerrainType
{
public:
friend class Manager;
struct Info
enum class PassabilityType
{
enum class Type
{
Land, Water, Subterranean, Rock
};
int moveCost;
bool transitionRequired;
std::array<int, 3> minimapBlocked;
std::array<int, 3> minimapUnblocked;
std::string musicFilename;
std::string tilesFilename;
std::string terrainText;
std::string typeCode;
std::string terrainViewPatterns;
std::string rockTerrain;
std::string river;
int horseSoundId;
Type type;
std::vector<std::string> battleFields;
std::vector<Terrain> prohibitTransitions;
LAND,
WATER,
SUBTERRANEAN,
ROCK
};
class DLL_LINKAGE Manager
{
public:
static const std::vector<Terrain> & terrains();
static const Info & getInfo(const Terrain &);
static int id(const Terrain &);
private:
static Manager & get();
Manager();
std::unordered_map<std::string, Info> terrainInfo;
std::vector<Terrain> terrainVault;
std::map<Terrain, int> terrainId;
};
std::vector<std::string> battleFields;
std::vector<TTerrain> prohibitTransitions;
std::array<int, 3> minimapBlocked;
std::array<int, 3> minimapUnblocked;
std::string name;
std::string musicFilename;
std::string tilesFilename;
std::string terrainText;
std::string typeCode;
std::string terrainViewPatterns;
std::string rockTerrain;
std::string river;
TTerrain id;
int moveCost;
int horseSoundId;
PassabilityType passabilityType;
bool transitionRequired;
/*enum EETerrainType
{
ANY_TERRAIN = -3,
WRONG = -2, BORDER = -1, DIRT, SAND, GRASS, SNOW, SWAMP,
ROUGH, SUBTERRANEAN, LAVA, WATER, ROCK // ROCK is also intended to be max value.
};*/
TerrainType(const std::string & _type = "");
Terrain(const std::string & _type = "");
static Terrain createTerrainTypeH3M(int tId);
static Terrain createTerrainByCode(const std::string & typeCode);
TerrainType& operator=(const TerrainType & _type);
int id() const; //TODO: has to be completely removed
Terrain& operator=(const Terrain & _type);
Terrain& operator=(const std::string & _type);
DLL_LINKAGE friend bool operator==(const Terrain & l, const Terrain & r);
DLL_LINKAGE friend bool operator!=(const Terrain & l, const Terrain & r);
DLL_LINKAGE friend bool operator<(const Terrain & l, const Terrain & r);
static const Terrain ANY;
bool operator==(const TerrainType & other);
bool operator!=(const TerrainType & other);
bool operator<(const TerrainType & other);
bool isLand() const;
bool isWater() const;
bool isPassable() const; //ROCK
bool isUnderground() const;
bool isNative() const;
bool isTransitionRequired() const;
operator std::string() const;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & battleFields;
h & prohibitTransitions;
h & minimapBlocked;
h & minimapUnblocked;
h & name;
h & musicFilename;
h & tilesFilename;
h & terrainText;
h & typeCode;
h & terrainViewPatterns;
h & rockTerrain;
h & river;
h & id;
h & moveCost;
h & horseSoundId;
h & passabilityType;
h & transitionRequired;
}
protected:
std::string name;
};
DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const Terrain terrainType);
DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType);
class DLL_LINKAGE TerrainTypeHandler //TODO: handlerBase
{
public:
TerrainTypeHandler();
const std::vector<TerrainType *> & terrains();
const TerrainType * getInfoByName(const std::string & terrainName) const;
const TerrainType * getInfoByCode(const std::string & terrainName) const;
const TerrainType * getInfoById(TTerrain id) const;
//TODO: road, river types?
private:
std::vector<TerrainType *> objects;
std::unordered_map<std::string, const TerrainType*> terrainInfoByName;
std::unordered_map<std::string, const TerrainType*> terrainInfoByCode;
std::unordered_map<TTerrain, const TerrainType*> terrainInfoById;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & name;
if (!saving)
{
//TODO: recreate at load
}
/*
h & terrainInfoByName;
h & terrainInfoByCode;
h & terrainInfoById;
*/
}
};

View File

@ -165,7 +165,7 @@ void ObjectTemplate::readTxt(CLegacyConfigParser & parser)
//assuming that object can be placed on other land terrains
if(allowedTerrains.size() >= 8 && !allowedTerrains.count(Terrain("water")))
{
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
if(terrain.isLand() && terrain.isPassable())
allowedTerrains.insert(terrain);
@ -238,7 +238,7 @@ void ObjectTemplate::readMap(CBinaryReader & reader)
//assuming that object can be placed on other land terrains
if(allowedTerrains.size() >= 8 && !allowedTerrains.count(Terrain("water")))
{
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
{
if(terrain.isLand() && terrain.isPassable())
allowedTerrains.insert(terrain);
@ -289,7 +289,7 @@ void ObjectTemplate::readJson(const JsonNode &node, const bool withTerrain)
}
else
{
for(auto & i : Terrain::Manager::terrains())
for(auto & i : VLC->terrainTypeHandler::terrains())
{
if(!i.isPassable() || i.isWater())
continue;
@ -369,7 +369,7 @@ void ObjectTemplate::writeJson(JsonNode & node, const bool withTerrain) const
if(withTerrain)
{
//assumed that ROCK and WATER terrains are not included
if(allowedTerrains.size() < (Terrain::Manager::terrains().size() - 2))
if(allowedTerrains.size() < (VLC->terrainTypeHandler::terrains().size() - 2))
{
JsonVector & data = node["allowedTerrains"].Vector();

View File

@ -67,12 +67,12 @@ class TerrainEncoder
public:
static si32 decode(const std::string & identifier)
{
return vstd::find_pos(Terrain::Manager::terrains(), identifier);
return vstd::find_pos(VLC->terrainTypeHandler::terrains(), identifier);
}
static std::string encode(const si32 index)
{
return (index >=0 && index < Terrain::Manager::terrains().size()) ? static_cast<std::string>(Terrain::Manager::terrains()[index]) : "<INVALID TERRAIN>";
return (index >=0 && index < VLC->terrainTypeHandler::terrains().size()) ? static_cast<std::string>(VLC->terrainTypeHandler::terrains()[index]) : "<INVALID TERRAIN>";
}
};
@ -149,7 +149,7 @@ ZoneOptions::ZoneOptions()
terrainTypeLikeZone(NO_ZONE),
treasureLikeZone(NO_ZONE)
{
for(auto & terr : Terrain::Manager::terrains())
for(auto & terr : VLC->terrainTypeHandler::terrains())
if(terr.isLand() && terr.isPassable())
terrainTypes.insert(terr);
}

View File

@ -117,7 +117,7 @@ void initTerrainType(Zone & zone, CMapGenerator & gen)
{
//collect all water terrain types
std::vector<Terrain> waterTerrains;
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
if(terrain.isWater())
waterTerrains.push_back(terrain);
@ -142,7 +142,7 @@ void initTerrainType(Zone & zone, CMapGenerator & gen)
{
//collect all underground terrain types
std::vector<Terrain> undegroundTerrains;
for(auto & terrain : Terrain::Manager::terrains())
for(auto & terrain : VLC->terrainTypeHandler::terrains())
if(terrain.isUnderground())
undegroundTerrains.push_back(terrain);