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

280 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"
2022-12-20 20:26:54 +02:00
#include "CGeneralTextHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
//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(), 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-09-23 19:26:38 +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
{
TerrainType * info = new TerrainType;
2022-09-19 16:13:58 +02:00
info->id = TerrainId(index);
info->identifier = identifier;
2022-09-19 16:13:58 +02:00
info->moveCost = static_cast<int>(json["moveCost"].Integer());
info->musicFilename = json["music"].String();
info->tilesFilename = json["tiles"].String();
info->horseSound = json["horseSound"].String();
info->transitionRequired = json["transitionRequired"].Bool();
info->terrainViewPatterns = json["terrainViewPatterns"].String();
info->nameTranslated = json["nameTranslated"].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
{
ui8(unblockedVec[0].Float()),
ui8(unblockedVec[1].Float()),
ui8(unblockedVec[2].Float())
};
2022-09-19 16:13:58 +02:00
const JsonVector &blockedVec = json["minimapBlocked"].Vector();
info->minimapBlocked =
{
ui8(blockedVec[0].Float()),
ui8(blockedVec[1].Float()),
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
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-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);
});
}
info->shortIdentifier = json["shortIdentifier"].String();
assert(info->shortIdentifier.length() == 2);
for(auto & t : json["battleFields"].Vector())
{
VLC->modh->identifiers.requestIdentifier("battlefield", t, [info](int32_t identifier)
{
info->battleFields.emplace_back(identifier);
});
}
for(auto & t : json["prohibitTransitions"].Vector())
{
VLC->modh->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
{
2022-12-20 20:26:54 +02:00
VLC->modh->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;
}
std::vector<JsonNode> TerrainTypeHandler::loadLegacyData(size_t dataSize)
{
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;
}
std::vector<bool> TerrainTypeHandler::getDefaultAllowed() const
{
return {};
}
2022-12-21 00:04:39 +02:00
RiverTypeHandler::RiverTypeHandler()
{
objects.push_back(new RiverType);
}
RiverType * RiverTypeHandler::loadFromJson(
const std::string & scope,
const JsonNode & json,
const std::string & identifier,
size_t index)
{
RiverType * info = new RiverType;
info->id = RiverId(index);
info->identifier = identifier;
info->tilesFilename = json["tilesFilename"].String();
info->shortIdentifier = json["shortIdentifier"].String();
info->deltaName = json["delta"].String();
2022-09-07 02:20:02 +02:00
return info;
}
const std::vector<std::string> & RiverTypeHandler::getTypeNames() const
{
static const std::vector<std::string> typeNames = { "river" };
return typeNames;
}
std::vector<JsonNode> RiverTypeHandler::loadLegacyData(size_t dataSize)
{
2022-12-20 20:52:52 +02:00
objects.resize(dataSize);
return {};
}
std::vector<bool> RiverTypeHandler::getDefaultAllowed() const
{
return {};
}
2022-12-21 00:04:39 +02:00
RoadTypeHandler::RoadTypeHandler()
{
objects.push_back(new RoadType);
}
RoadType * RoadTypeHandler::loadFromJson(
const std::string & scope,
const JsonNode & json,
const std::string & identifier,
size_t index)
{
RoadType * info = new RoadType;
info->id = RoadId(index);
info->identifier = identifier;
info->tilesFilename = json["animation"].String();
info->shortIdentifier = json["code"].String();
info->movementCost = json["moveCost"].Integer();
return info;
}
const std::vector<std::string> & RoadTypeHandler::getTypeNames() const
{
static const std::vector<std::string> typeNames = { "river" };
return typeNames;
}
std::vector<JsonNode> RoadTypeHandler::loadLegacyData(size_t dataSize)
{
2022-12-20 20:52:52 +02:00
objects.resize(dataSize);
return {};
}
std::vector<bool> RoadTypeHandler::getDefaultAllowed() const
{
return {};
}
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::isPassable() const
{
return !(passabilityType & PassabilityType::ROCK);
}
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::isSurfaceCartographerCompatible() const
{
return isSurface();
}
bool TerrainType::isUndergroundCartographerCompatible() const
{
return isLand() && isPassable() && !isSurface();
}
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
}
TerrainType::TerrainType()
{}
2022-12-21 00:04:39 +02:00
RiverType::RiverType():
id(River::NO_RIVER)
{}
2022-12-21 00:04:39 +02:00
RoadType::RoadType():
id(Road::NO_ROAD),
movementCost(GameConstants::BASE_MOVEMENT_COST)
{}
VCMI_LIB_NAMESPACE_END