1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-12 10:03:53 +02:00
vcmi/lib/TerrainHandler.cpp

205 lines
5.1 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 "TerrainHandler.h"
2023-03-15 21:34:29 +02:00
#include "GameSettings.h"
#include "json/JsonNode.h"
#include "modding/IdentifierStorage.h"
#include "texts/CGeneralTextHandler.h"
#include "texts/CLegacyConfigParser.h"
#include "VCMI_Lib.h"
VCMI_LIB_NAMESPACE_BEGIN
std::shared_ptr<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
{
assert(identifier.find(':') == std::string::npos);
auto info = std::make_shared<TerrainType>();
2022-09-19 16:13:58 +02:00
info->id = TerrainId(index);
2023-02-13 00:07:28 +02:00
info->identifier = identifier;
info->modScope = scope;
info->moveCost = static_cast<int>(json["moveCost"].Integer());
if (json["music"].isVector())
{
for (auto const & entry : json["music"].Vector())
info->musicFilename.push_back(AudioPath::fromJson(entry));
}
else
{
info->musicFilename.push_back(AudioPath::fromJson(json["music"]));
}
info->tilesFilename = AnimationPath::fromJson(json["tiles"]);
2023-09-04 12:03:15 +02:00
info->horseSound = AudioPath::fromJson(json["horseSound"]);
info->horseSoundPenalty = AudioPath::fromJson(json["horseSoundPenalty"]);
info->transitionRequired = json["transitionRequired"].Bool();
info->terrainViewPatterns = json["terrainViewPatterns"].String();
VLC->generaltexth->registerString(scope, info->getNameTextID(), json["text"].String());
2022-09-19 16:13:58 +02:00
const JsonVector & unblockedVec = json["minimapUnblocked"].Vector();
info->minimapUnblocked =
2022-09-19 16:13:58 +02:00
{
2023-03-13 23:26:44 +02:00
static_cast<ui8>(unblockedVec[0].Float()),
static_cast<ui8>(unblockedVec[1].Float()),
static_cast<ui8>(unblockedVec[2].Float())
};
2022-09-19 16:13:58 +02:00
const JsonVector &blockedVec = json["minimapBlocked"].Vector();
info->minimapBlocked =
{
2023-03-13 23:26:44 +02:00
static_cast<ui8>(blockedVec[0].Float()),
static_cast<ui8>(blockedVec[1].Float()),
static_cast<ui8>(blockedVec[2].Float())
};
2022-12-20 20:52:52 +02:00
info->passabilityType = 0;
for(const auto& node : json["type"].Vector())
2022-09-19 16:13:58 +02:00
{
//Set bits
2023-03-13 23:26:44 +02:00
const auto & s = node.String();
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-12-20 20:52:52 +02:00
info->river = River::NO_RIVER;
if(!json["river"].isNull())
{
VLC->identifiers()->requestIdentifier("river", json["river"], [info](int32_t identifier)
2022-12-20 20:52:52 +02:00
{
info->river = RiverId(identifier);
});
}
for(const auto & t : json["paletteAnimation"].Vector())
{
TerrainPaletteAnimation element{
static_cast<int>(t["start"].Integer()),
static_cast<int>(t["length"].Integer())
};
info->paletteAnimation.push_back(element);
}
info->shortIdentifier = json["shortIdentifier"].String();
assert(info->shortIdentifier.length() == 2);
2023-03-13 23:26:44 +02:00
for(const auto & t : json["battleFields"].Vector())
{
VLC->identifiers()->requestIdentifier("battlefield", t, [info](int32_t identifier)
{
info->battleFields.emplace_back(identifier);
});
}
2023-03-13 23:26:44 +02:00
for(const auto & t : json["prohibitTransitions"].Vector())
{
VLC->identifiers()->requestIdentifier("terrain", t, [info](int32_t identifier)
{
info->prohibitTransitions.emplace_back(identifier);
});
}
info->rockTerrain = ETerrainId::ROCK;
if(!json["rockTerrain"].isNull())
2022-09-21 11:34:23 +02:00
{
VLC->identifiers()->requestIdentifier("terrain", json["rockTerrain"], [info](int32_t identifier)
{
info->rockTerrain = TerrainId(identifier);
});
2022-09-21 11:34:23 +02:00
}
return info;
}
const std::vector<std::string> & TerrainTypeHandler::getTypeNames() const
{
static const std::vector<std::string> typeNames = { "terrain" };
return typeNames;
}
2023-03-15 21:34:29 +02:00
std::vector<JsonNode> TerrainTypeHandler::loadLegacyData()
{
2023-03-15 21:34:29 +02:00
size_t dataSize = VLC->settings()->getInteger(EGameSettings::TEXTS_TERRAIN);
2022-12-20 20:26:54 +02:00
objects.resize(dataSize);
CLegacyConfigParser terrainParser(TextPath::builtin("DATA/TERRNAME.TXT"));
2022-12-20 20:26:54 +02:00
std::vector<JsonNode> result;
do
{
JsonNode terrain;
terrain["text"].String() = terrainParser.readString();
result.push_back(terrain);
}
while (terrainParser.endLine());
return result;
}
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
{
return passabilityType & PassabilityType::WATER;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isRock() const
{
return passabilityType & PassabilityType::ROCK;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isPassable() const
{
return !isRock();
}
bool TerrainType::isSurface() const
{
return passabilityType & PassabilityType::SURFACE;
}
2022-09-19 16:13:58 +02:00
bool TerrainType::isUnderground() const
{
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
}
2023-02-13 00:07:28 +02:00
std::string TerrainType::getJsonKey() const
{
return modScope + ":" + identifier;
}
std::string TerrainType::getNameTextID() const
{
2023-02-13 00:07:28 +02:00
return TextIdentifier( "terrain", modScope, identifier, "name" ).get();
}
std::string TerrainType::getNameTranslated() const
{
return VLC->generaltexth->translate(getNameTextID());
}
VCMI_LIB_NAMESPACE_END