2022-06-20 16:39:50 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
#include "StdInc.h"
|
2022-06-20 16:39:50 +02:00
|
|
|
#include "Terrain.h"
|
|
|
|
#include "VCMI_Lib.h"
|
|
|
|
#include "CModHandler.h"
|
2022-12-20 20:26:54 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2022-06-20 16:39:50 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2022-06-20 16:39:50 +02:00
|
|
|
//regular expression to change id for string at config
|
|
|
|
//("allowedTerrain"\s*:\s*\[.*)9(.*\],\n)
|
|
|
|
//\1"rock"\2
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
/*
|
2022-09-19 16:13:58 +02:00
|
|
|
TerrainTypeHandler::TerrainTypeHandler()
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
|
|
|
auto allConfigs = VLC->modh->getActiveMods();
|
2022-12-07 15:18:19 +02:00
|
|
|
allConfigs.insert(allConfigs.begin(), CModHandler::scopeBuiltin());
|
2022-09-19 16:13:58 +02:00
|
|
|
|
2022-09-23 19:26:38 +02:00
|
|
|
initRivers(allConfigs);
|
|
|
|
recreateRiverMaps();
|
|
|
|
initRoads(allConfigs);
|
|
|
|
recreateRoadMaps();
|
|
|
|
initTerrains(allConfigs); //maps will be populated inside
|
2022-12-20 16:14:06 +02:00
|
|
|
}*/
|
2022-09-23 19:26:38 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
TerrainType * TerrainTypeHandler::loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
|
2022-09-23 19:26:38 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
TerrainType * info = new TerrainType;
|
2022-09-19 16:13:58 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
info->id = TerrainId(index);
|
2022-09-19 16:13:58 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
info->moveCost = static_cast<int>(json["moveCost"].Integer());
|
|
|
|
info->musicFilename = json["music"].String();
|
|
|
|
info->tilesFilename = json["tiles"].String();
|
|
|
|
info->horseSoundId = static_cast<int>(json["horseSoundId"].Float());
|
|
|
|
info->transitionRequired = json["transitionRequired"].Bool();
|
|
|
|
info->terrainViewPatterns = json["terrainViewPatterns"].String();
|
|
|
|
info->terrainText = json["text"].String();
|
2022-09-19 16:13:58 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
const JsonVector & unblockedVec = json["minimapUnblocked"].Vector();
|
|
|
|
info->minimapUnblocked =
|
2022-09-19 16:13:58 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
ui8(unblockedVec[0].Float()),
|
|
|
|
ui8(unblockedVec[1].Float()),
|
|
|
|
ui8(unblockedVec[2].Float())
|
|
|
|
};
|
2022-09-19 16:13:58 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
const JsonVector &blockedVec = json["minimapBlocked"].Vector();
|
|
|
|
info->minimapBlocked =
|
|
|
|
{
|
|
|
|
ui8(blockedVec[0].Float()),
|
|
|
|
ui8(blockedVec[1].Float()),
|
|
|
|
ui8(blockedVec[2].Float())
|
|
|
|
};
|
2022-09-21 13:43:57 +02:00
|
|
|
|
2022-12-20 20:52:52 +02:00
|
|
|
info->passabilityType = 0;
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
for(const auto& node : json["type"].Vector())
|
2022-09-19 16:13:58 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
//Set bits
|
|
|
|
auto s = node.String();
|
|
|
|
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 == "SURFACE") info->passabilityType |= TerrainType::PassabilityType::SURFACE;
|
|
|
|
if (s == "SUB") info->passabilityType |= TerrainType::PassabilityType::SUBTERRANEAN;
|
2022-09-19 16:13:58 +02:00
|
|
|
}
|
2022-06-20 16:39:50 +02:00
|
|
|
|
2022-12-20 20:52:52 +02:00
|
|
|
info->river = River::NO_RIVER;
|
|
|
|
if(!json["river"].isNull())
|
|
|
|
{
|
|
|
|
VLC->modh->identifiers.requestIdentifier("river", json["river"], [info](int32_t identifier)
|
|
|
|
{
|
|
|
|
info->river = RiverId(identifier);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2022-09-23 16:24:01 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
info->typeCode = json["code"].String();
|
|
|
|
assert(info->typeCode.length() == 2);
|
2022-09-23 16:24:01 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
for(auto & t : json["battleFields"].Vector())
|
|
|
|
info->battleFields.emplace_back(t.String());
|
2022-09-23 16:24:01 +02:00
|
|
|
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
//Update terrain with this id in the future, after all terrain types are populated
|
2022-09-23 16:24:01 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
for(auto & t : json["prohibitTransitions"].Vector())
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
VLC->modh->identifiers.requestIdentifier("terrain", t, [info](int32_t identifier)
|
2022-09-23 18:57:16 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
info->prohibitTransitions.push_back(TerrainId(identifier));
|
|
|
|
});
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 18:35:40 +02:00
|
|
|
info->rockTerrain = ETerrainId::ROCK;
|
2022-09-26 09:17:55 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
if(!json["rockTerrain"].isNull())
|
2022-09-21 11:34:23 +02:00
|
|
|
{
|
2022-12-20 20:26:54 +02:00
|
|
|
VLC->modh->identifiers.requestIdentifier("terrain", json["rockTerrain"], [info](int32_t identifier)
|
2022-12-20 16:14:06 +02:00
|
|
|
{
|
|
|
|
info->rockTerrain = TerrainId(identifier);
|
|
|
|
});
|
2022-09-21 11:34:23 +02:00
|
|
|
}
|
2022-09-23 18:57:16 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
return info;
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
const std::vector<std::string> & TerrainTypeHandler::getTypeNames() const
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
static const std::vector<std::string> typeNames = { "terrain" };
|
|
|
|
return typeNames;
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
std::vector<JsonNode> TerrainTypeHandler::loadLegacyData(size_t dataSize)
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-12-20 20:26:54 +02:00
|
|
|
objects.resize(dataSize);
|
|
|
|
|
|
|
|
CLegacyConfigParser terrainParser("DATA/TERRNAME.TXT");
|
|
|
|
|
|
|
|
std::vector<JsonNode> result;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
JsonNode terrain;
|
|
|
|
terrain["text"].String() = terrainParser.readString();
|
|
|
|
result.push_back(terrain);
|
|
|
|
}
|
|
|
|
while (terrainParser.endLine());
|
|
|
|
|
|
|
|
return result;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
std::vector<bool> TerrainTypeHandler::getDefaultAllowed() const
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
return {};
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
RiverType * RiverTypeHandler::loadFromJson(
|
|
|
|
const std::string & scope,
|
|
|
|
const JsonNode & json,
|
|
|
|
const std::string & identifier,
|
|
|
|
size_t index)
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
RiverType * info = new RiverType;
|
2022-09-23 16:24:01 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
info->fileName = json["animation"].String();
|
|
|
|
info->code = json["code"].String();
|
|
|
|
info->deltaName = json["delta"].String();
|
2022-09-07 02:20:02 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
return info;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
const std::vector<std::string> & RiverTypeHandler::getTypeNames() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
static const std::vector<std::string> typeNames = { "river" };
|
|
|
|
return typeNames;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
std::vector<JsonNode> RiverTypeHandler::loadLegacyData(size_t dataSize)
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 20:52:52 +02:00
|
|
|
objects.resize(dataSize);
|
2022-12-20 16:14:06 +02:00
|
|
|
return {};
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
std::vector<bool> RiverTypeHandler::getDefaultAllowed() const
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
return {};
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
RoadType * RoadTypeHandler::loadFromJson(
|
|
|
|
const std::string & scope,
|
|
|
|
const JsonNode & json,
|
|
|
|
const std::string & identifier,
|
|
|
|
size_t index)
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
RoadType * info = new RoadType;
|
2022-09-23 16:24:01 +02:00
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
info->fileName = json["animation"].String();
|
|
|
|
info->code = json["code"].String();
|
|
|
|
info->movementCost = json["moveCost"].Integer();
|
|
|
|
|
|
|
|
return info;
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
const std::vector<std::string> & RoadTypeHandler::getTypeNames() const
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
static const std::vector<std::string> typeNames = { "river" };
|
|
|
|
return typeNames;
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
std::vector<JsonNode> RoadTypeHandler::loadLegacyData(size_t dataSize)
|
2022-09-23 16:24:01 +02:00
|
|
|
{
|
2022-12-20 20:52:52 +02:00
|
|
|
objects.resize(dataSize);
|
2022-12-20 16:14:06 +02:00
|
|
|
return {};
|
2022-09-23 16:24:01 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 16:14:06 +02:00
|
|
|
std::vector<bool> RoadTypeHandler::getDefaultAllowed() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-12-20 16:14:06 +02:00
|
|
|
return {};
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
2022-12-20 16:14:06 +02:00
|
|
|
|
2022-09-19 16:13:58 +02:00
|
|
|
TerrainType::operator std::string() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
2022-12-20 16:14:06 +02:00
|
|
|
|
2022-09-19 16:13:58 +02:00
|
|
|
bool TerrainType::operator==(const TerrainType& other)
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-09-19 16:13:58 +02:00
|
|
|
return id == other.id;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 16:13:58 +02:00
|
|
|
bool TerrainType::operator!=(const TerrainType& other)
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-09-19 16:13:58 +02:00
|
|
|
return id != other.id;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 16:13:58 +02:00
|
|
|
bool TerrainType::operator<(const TerrainType& other)
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-09-19 16:13:58 +02:00
|
|
|
return id < other.id;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 16:13:58 +02:00
|
|
|
bool TerrainType::isLand() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
|
|
|
return !isWater();
|
|
|
|
}
|
2022-09-19 16:13:58 +02:00
|
|
|
|
|
|
|
bool TerrainType::isWater() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-09-22 18:23:31 +02:00
|
|
|
return passabilityType & PassabilityType::WATER;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
2022-09-19 16:13:58 +02:00
|
|
|
|
|
|
|
bool TerrainType::isPassable() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-09-22 18:23:31 +02:00
|
|
|
return !(passabilityType & PassabilityType::ROCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainType::isSurface() const
|
|
|
|
{
|
|
|
|
return passabilityType & PassabilityType::SURFACE;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
2022-09-19 16:13:58 +02:00
|
|
|
|
|
|
|
bool TerrainType::isUnderground() const
|
2022-06-20 16:39:50 +02:00
|
|
|
{
|
2022-09-22 18:23:31 +02:00
|
|
|
return passabilityType & PassabilityType::SUBTERRANEAN;
|
2022-06-20 16:39:50 +02:00
|
|
|
}
|
2022-09-19 16:13:58 +02:00
|
|
|
|
2022-12-30 16:09:09 +02:00
|
|
|
bool TerrainType::isSurfaceCartographerCompatible() const
|
|
|
|
{
|
2022-12-30 17:04:19 +02:00
|
|
|
return isSurface();
|
2022-12-30 16:09:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TerrainType::isUndergroundCartographerCompatible() const
|
|
|
|
{
|
2022-12-30 17:04:19 +02:00
|
|
|
return isLand() && isPassable() && !isSurface();
|
2022-12-30 16:09:09 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-09-23 16:24:01 +02:00
|
|
|
|
2022-12-20 18:35:40 +02:00
|
|
|
TerrainType::TerrainType()
|
|
|
|
{}
|
|
|
|
|
|
|
|
RiverType::RiverType()
|
|
|
|
{}
|
|
|
|
|
|
|
|
RoadType::RoadType()
|
|
|
|
{}
|
2022-09-30 10:26:36 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|