1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/Terrain.cpp

277 lines
6.4 KiB
C++
Raw Normal View History

/*
* Terrain.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "Terrain.h"
#include "VCMI_Lib.h"
#include "CModHandler.h"
//regular expression to change id for string at config
//("allowedTerrain"\s*:\s*\[.*)9(.*\],\n)
//\1"rock"\2
2022-09-19 16:13:58 +02:00
TerrainTypeHandler::TerrainTypeHandler()
{
auto allConfigs = VLC->modh->getActiveMods();
allConfigs.insert(allConfigs.begin(), "core");
2022-09-19 16:13:58 +02:00
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")))
continue;
JsonNode terrs(mod, ResourceID("config/terrains.json"));
for(auto & terr : terrs.Struct())
{
2022-09-21 11:34:23 +02:00
auto * info = new TerrainType(terr.first); //set name
2022-09-19 16:13:58 +02:00
info->moveCost = terr.second["moveCost"].Integer();
const JsonVector &unblockedVec = terr.second["minimapUnblocked"].Vector();
2022-09-19 16:13:58 +02:00
info->minimapUnblocked =
{
ui8(unblockedVec[0].Float()),
ui8(unblockedVec[1].Float()),
ui8(unblockedVec[2].Float())
};
const JsonVector &blockedVec = terr.second["minimapBlocked"].Vector();
2022-09-19 16:13:58 +02:00
info->minimapBlocked =
{
ui8(blockedVec[0].Float()),
ui8(blockedVec[1].Float()),
ui8(blockedVec[2].Float())
};
2022-09-19 16:13:58 +02:00
info->musicFilename = terr.second["music"].String();
info->tilesFilename = terr.second["tiles"].String();
if(terr.second["type"].isNull())
{
2022-09-19 16:13:58 +02:00
info->passabilityType = TerrainType::PassabilityType::LAND;
}
else
{
auto s = terr.second["type"].String();
2022-09-19 16:13:58 +02:00
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())
{
2022-09-21 11:34:23 +02:00
info->rockTerrain = Terrain::ROCK;
}
else
{
auto rockTerrainName = terr.second["rockTerrain"].String();
resolveLater.push_back([this, rockTerrainName, info]()
2022-09-21 11:34:23 +02:00
{
info->rockTerrain = getInfoByName(rockTerrainName)->id;
2022-09-21 11:34:23 +02:00
});
}
if(terr.second["river"].isNull())
{
2022-09-19 16:13:58 +02:00
info->river = RIVER_NAMES[0];
}
else
{
2022-09-19 16:13:58 +02:00
info->river = terr.second["river"].String();
}
if(terr.second["horseSoundId"].isNull())
{
2022-09-19 16:13:58 +02:00
info->horseSoundId = 9; //rock sound as default
}
else
{
2022-09-21 13:43:00 +02:00
info->horseSoundId = terr.second["horseSoundId"].Float();
}
if(!terr.second["text"].isNull())
{
2022-09-19 16:13:58 +02:00
info->terrainText = terr.second["text"].String();
}
if(terr.second["code"].isNull())
{
2022-09-19 16:13:58 +02:00
info->typeCode = terr.first.substr(0, 2);
}
else
{
2022-09-19 16:13:58 +02:00
info->typeCode = terr.second["code"].String();
assert(info->typeCode.length() == 2);
}
2022-06-22 10:41:02 +02:00
if(!terr.second["battleFields"].isNull())
{
for(auto & t : terr.second["battleFields"].Vector())
{
2022-09-19 16:13:58 +02:00
info->battleFields.emplace_back(t.String());
2022-06-22 10:41:02 +02:00
}
}
if(!terr.second["prohibitTransitions"].isNull())
{
for(auto & t : terr.second["prohibitTransitions"].Vector())
{
2022-09-21 11:34:23 +02:00
std::string prohibitedTerrainName = t.String();
resolveLater.push_back([this, prohibitedTerrainName, info]()
{
info->prohibitTransitions.emplace_back(getInfoByName(prohibitedTerrainName)->id);
});
}
}
2022-09-19 16:13:58 +02:00
info->transitionRequired = false;
2022-06-22 10:41:02 +02:00
if(!terr.second["transitionRequired"].isNull())
{
2022-09-19 16:13:58 +02:00
info->transitionRequired = terr.second["transitionRequired"].Bool();
2022-06-22 10:41:02 +02:00
}
2022-09-19 16:13:58 +02:00
info->terrainViewPatterns = "normal";
2022-06-22 10:41:02 +02:00
if(!terr.second["terrainViewPatterns"].isNull())
{
2022-09-19 16:13:58 +02:00
info->terrainViewPatterns = terr.second["terrainViewPatterns"].String();
2022-06-22 10:41:02 +02:00
}
2022-09-19 16:13:58 +02:00
2022-09-21 11:34:23 +02:00
TTerrain id = Terrain::WRONG;
2022-09-19 16:13:58 +02:00
if(!terr.second["originalTerrainId"].isNull())
2022-09-07 02:20:02 +02:00
{
2022-09-19 16:13:58 +02:00
//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);
2022-09-07 02:20:02 +02:00
}
info->id = id;
}
}
2022-09-19 16:13:58 +02:00
for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
{
//Make sure that original terrains are loaded
assert(objects(i));
}
recreateTerrainMaps();
2022-09-19 16:13:58 +02:00
for (auto& functor : resolveLater)
{
functor();
}
}
2022-09-21 11:34:23 +02:00
void TerrainTypeHandler::recreateTerrainMaps()
{
for (const TerrainType * terrainInfo : objects)
{
terrainInfoByName[terrainInfo->name] = terrainInfo;
terrainInfoByCode[terrainInfo->typeCode] = terrainInfo;
terrainInfoById[terrainInfo->id] = terrainInfo;
}
}
const std::vector<TerrainType *> & TerrainTypeHandler::terrains() const
{
2022-09-19 16:13:58 +02:00
return objects;
}
2022-09-19 16:13:58 +02:00
const TerrainType* TerrainTypeHandler::getInfoByName(const std::string& terrainName) const
{
2022-09-21 11:34:23 +02:00
return terrainInfoByName.at(terrainName);
2022-09-07 02:20:02 +02:00
}
2022-09-21 11:34:23 +02:00
const TerrainType* TerrainTypeHandler::getInfoByCode(const std::string& terrainCode) const
2022-09-07 02:20:02 +02:00
{
2022-09-21 11:34:23 +02:00
return terrainInfoByCode.at(terrainCode);
}
2022-09-19 16:13:58 +02:00
const TerrainType* TerrainTypeHandler::getInfoById(TTerrain id) const
{
2022-09-21 11:34:23 +02:00
return terrainInfoById.at(id);
}
2022-09-19 16:13:58 +02:00
std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType)
{
return os << static_cast<const std::string &>(terrainType);
}
2022-09-19 16:13:58 +02:00
TerrainType::operator std::string() const
{
return name;
}
2022-09-21 11:34:23 +02:00
TerrainType::TerrainType(const std::string& _name):
name(_name),
2022-09-21 11:34:23 +02:00
id(Terrain::WRONG),
rockTerrain(Terrain::ROCK),
moveCost(100),
horseSoundId(0),
passabilityType(PassabilityType::LAND),
transitionRequired(false)
{
}
2022-09-19 16:13:58 +02:00
TerrainType& TerrainType::operator=(const TerrainType & other)
{
2022-09-19 16:13:58 +02:00
//TODO
name = other.name;
return *this;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::operator==(const TerrainType& other)
{
2022-09-19 16:13:58 +02:00
return id == other.id;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::operator!=(const TerrainType& other)
{
2022-09-19 16:13:58 +02:00
return id != other.id;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::operator<(const TerrainType& other)
{
2022-09-19 16:13:58 +02:00
return id < other.id;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isLand() const
{
return !isWater();
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isWater() const
{
2022-09-19 16:13:58 +02:00
return passabilityType == PassabilityType::WATER;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isPassable() const
{
2022-09-19 16:13:58 +02:00
return passabilityType != PassabilityType::ROCK;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isUnderground() const
{
2022-09-19 16:13:58 +02:00
return passabilityType != PassabilityType::SUBTERRANEAN;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isTransitionRequired() const
2022-06-22 10:41:02 +02:00
{
2022-09-19 16:13:58 +02:00
return transitionRequired;
2022-06-22 10:41:02 +02:00
}