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

505 lines
13 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"
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(), "core");
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
}
void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfigs)
{
2022-09-19 16:13:58 +02:00
std::vector<std::function<void()>> resolveLater;
objects.resize(Terrain::ORIGINAL_TERRAIN_COUNT); //make space for original terrains
2022-09-19 16:13:58 +02:00
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())
{
TerrainType info(terr.first); //set name
2022-09-19 16:13:58 +02:00
info.moveCost = static_cast<int>(terr.second["moveCost"].Integer());
const JsonVector &unblockedVec = terr.second["minimapUnblocked"].Vector();
info.minimapUnblocked =
{
ui8(unblockedVec[0].Float()),
ui8(unblockedVec[1].Float()),
ui8(unblockedVec[2].Float())
};
const JsonVector &blockedVec = terr.second["minimapBlocked"].Vector();
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();
if(terr.second["type"].isNull())
{
info.passabilityType = TerrainType::PassabilityType::LAND | TerrainType::PassabilityType::SURFACE;
}
else if (terr.second["type"].getType() == JsonNode::JsonType::DATA_VECTOR)
{
2022-09-26 10:20:39 +02:00
for(const auto& node : terr.second["type"].Vector())
{
//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;
}
}
else //should be string - one option only
{
auto s = terr.second["type"].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;
}
if(terr.second["river"].isNull())
{
info.river = River::NO_RIVER;
}
else
{
info.river = getRiverByCode(terr.second["river"].String())->id;
}
if(terr.second["horseSoundId"].isNull())
{
info.horseSoundId = Terrain::ROCK; //rock sound as default
}
else
{
info.horseSoundId = static_cast<int>(terr.second["horseSoundId"].Float());
}
if(!terr.second["text"].isNull())
{
info.terrainText = terr.second["text"].String();
}
if(terr.second["code"].isNull())
{
info.typeCode = terr.first.substr(0, 2);
}
else
{
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())
{
info.battleFields.emplace_back(t.String());
2022-06-22 10:41:02 +02:00
}
}
info.transitionRequired = false;
2022-06-22 10:41:02 +02:00
if(!terr.second["transitionRequired"].isNull())
{
info.transitionRequired = terr.second["transitionRequired"].Bool();
2022-06-22 10:41:02 +02:00
}
info.terrainViewPatterns = "normal";
2022-06-22 10:41:02 +02:00
if(!terr.second["terrainViewPatterns"].isNull())
{
info.terrainViewPatterns = terr.second["terrainViewPatterns"].String();
2022-06-22 10:41:02 +02:00
}
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
info.id = (TTerrainId)(terr.second["originalTerrainId"].Float());
objects[info.id] = info;
2022-09-19 16:13:58 +02:00
}
else
{
//append at the end
info.id = static_cast<TTerrainId>(objects.size());
2022-09-19 16:13:58 +02:00
objects.push_back(info);
2022-09-07 02:20:02 +02:00
}
TTerrainId id = info.id;
//Update terrain with this id in the future, after all terrain types are populated
if(!terr.second["prohibitTransitions"].isNull())
{
for(auto & t : terr.second["prohibitTransitions"].Vector())
{
std::string prohibitedTerrainName = t.String();
resolveLater.push_back([this, prohibitedTerrainName, id]()
{
//FIXME: is that reference to the element in vector?
objects[id].prohibitTransitions.emplace_back(getInfoByName(prohibitedTerrainName)->id);
});
}
}
if(terr.second["rockTerrain"].isNull())
{
objects[id].rockTerrain = Terrain::ROCK;
}
else
{
auto rockTerrainName = terr.second["rockTerrain"].String();
resolveLater.push_back([this, rockTerrainName, id]()
{
//FIXME: is that reference to the element in vector?
objects[id].rockTerrain = getInfoByName(rockTerrainName)->id;
});
}
}
}
2022-09-19 16:13:58 +02:00
2022-09-26 10:20:39 +02:00
for(size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
2022-09-19 16:13:58 +02:00
{
//Make sure that original terrains are loaded
assert(objects(i).id != Terrain::WRONG);
2022-09-19 16:13:58 +02:00
}
recreateTerrainMaps();
2022-09-26 10:20:39 +02:00
for(auto& functor : resolveLater)
2022-09-19 16:13:58 +02:00
{
functor();
}
}
2022-09-23 19:26:38 +02:00
void TerrainTypeHandler::initRivers(const std::vector<std::string> & allConfigs)
{
riverTypes.resize(River::ORIGINAL_RIVER_COUNT); //make space for original rivers
2022-09-26 10:20:39 +02:00
//First object will be default NO_RIVER
2022-09-26 10:20:39 +02:00
for(auto & mod : allConfigs)
{
if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/rivers.json")))
continue;
JsonNode rivs(mod, ResourceID("config/rivers.json"));
2022-09-26 10:20:39 +02:00
for(auto & river : rivs.Struct())
{
RiverType info;
info.fileName = river.second["animation"].String();
info.code = river.second["code"].String();
info.deltaName = river.second["delta"].String();
if (!river.second["originalRiverId"].isNull())
{
info.id = static_cast<TRiverId>(river.second["originalRiverId"].Float());
riverTypes[info.id] = info;
}
else
{
info.id = static_cast<TRiverId>(riverTypes.size());
riverTypes.push_back(info);
}
}
}
recreateRiverMaps();
}
2022-09-23 19:26:38 +02:00
void TerrainTypeHandler::initRoads(const std::vector<std::string> & allConfigs)
{
2022-09-26 10:20:39 +02:00
roadTypes.resize(Road::ORIGINAL_ROAD_COUNT); //make space for original rivers
//first object will be default NO_ROAD
2022-09-26 10:20:39 +02:00
for(auto & mod : allConfigs)
{
if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/roads.json")))
continue;
JsonNode rds(mod, ResourceID("config/roads.json"));
2022-09-26 10:20:39 +02:00
for(auto & road : rds.Struct())
{
2022-09-26 10:20:39 +02:00
RoadType info;
2022-09-26 10:20:39 +02:00
info.fileName = road.second["animation"].String();
info.code = road.second["code"].String();
info.movementCost = static_cast<ui8>(road.second["moveCost"].Float());
if (!road.second["originalRoadId"].isNull())
{
2022-09-26 10:20:39 +02:00
info.id = static_cast<TRoadId>(road.second["originalRoadId"].Float());
roadTypes[info.id] = info;
}
else
{
2022-09-26 10:20:39 +02:00
info.id = static_cast<TRoadId>(roadTypes.size());
roadTypes.push_back(info);
}
}
}
recreateRoadMaps();
}
2022-09-21 11:34:23 +02:00
void TerrainTypeHandler::recreateTerrainMaps()
{
//This assumes the vector will never be updated or reallocated in the future
2022-09-26 10:20:39 +02:00
for(size_t i = 0; i < objects.size(); i++)
2022-09-21 11:34:23 +02:00
{
const auto * terrainInfo = &objects[i];
2022-09-21 11:34:23 +02:00
terrainInfoByName[terrainInfo->name] = terrainInfo;
terrainInfoByCode[terrainInfo->typeCode] = terrainInfo;
terrainInfoById[terrainInfo->id] = terrainInfo;
}
}
void TerrainTypeHandler::recreateRiverMaps()
{
2022-09-26 10:20:39 +02:00
for(size_t i = River::FIRST_REGULAR_RIVER ; i < riverTypes.size(); i++)
{
2022-09-26 10:20:39 +02:00
const auto * riverInfo = &riverTypes[i];
riverInfoByName[riverInfo->fileName] = riverInfo;
riverInfoByCode[riverInfo->code] = riverInfo;
riverInfoById[riverInfo->id] = riverInfo;
}
}
void TerrainTypeHandler::recreateRoadMaps()
{
2022-09-26 10:20:39 +02:00
for(size_t i = Road::FIRST_REGULAR_ROAD ; i < roadTypes.size(); i++)
{
2022-09-26 10:20:39 +02:00
const auto * roadInfo = &roadTypes[i];
roadInfoByName[roadInfo->fileName] = roadInfo;
roadInfoByCode[roadInfo->code] = roadInfo;
roadInfoById[roadInfo->id] = roadInfo;
}
}
const std::vector<TerrainType> & TerrainTypeHandler::terrains() const
{
//FIXME: somehow make it non-copyable? Pointers must point to original data and not its copy
2022-09-19 16:13:58 +02:00
return objects;
}
const std::vector<RiverType>& TerrainTypeHandler::rivers() const
{
return riverTypes;
}
2022-09-26 10:20:39 +02:00
const std::vector<RoadType>& TerrainTypeHandler::roads() const
{
return roadTypes;
}
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-25 09:33:56 +02:00
const TerrainType* TerrainTypeHandler::getInfoById(TTerrainId id) const
{
2022-09-21 11:34:23 +02:00
return terrainInfoById.at(id);
}
const RiverType* TerrainTypeHandler::getRiverByName(const std::string& riverName) const
{
return riverInfoByName.at(riverName);
}
const RiverType* TerrainTypeHandler::getRiverByCode(const std::string& riverCode) const
{
return riverInfoByCode.at(riverCode);
}
2022-09-25 09:33:56 +02:00
const RiverType* TerrainTypeHandler::getRiverById(TRiverId id) const
{
return riverInfoById.at(id);
}
const RoadType* TerrainTypeHandler::getRoadByName(const std::string& roadName) const
{
return roadInfoByName.at(roadName);
}
const RoadType* TerrainTypeHandler::getRoadByCode(const std::string& roadCode) const
{
return roadInfoByCode.at(roadCode);
}
2022-09-25 09:33:56 +02:00
const RoadType* TerrainTypeHandler::getRoadById(TRoadId id) const
{
return roadInfoById.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):
minimapBlocked({0,0,0}), //black
minimapUnblocked({ 128,128,128 }), //grey
name(_name),
river(River::NO_RIVER),
2022-09-21 11:34:23 +02:00
id(Terrain::WRONG),
rockTerrain(Terrain::ROCK),
moveCost(GameConstants::BASE_MOVEMENT_COST),
2022-09-21 11:34:23 +02:00
horseSoundId(0),
passabilityType(0),
2022-09-21 11:34:23 +02:00
transitionRequired(false)
{
}
2022-09-19 16:13:58 +02:00
TerrainType& TerrainType::operator=(const TerrainType & other)
{
battleFields = other.battleFields;
prohibitTransitions = other.prohibitTransitions;
minimapBlocked = other.minimapBlocked;
minimapUnblocked = other.minimapUnblocked;
2022-09-19 16:13:58 +02:00
name = other.name;
musicFilename = other.musicFilename;
tilesFilename = other.tilesFilename;
terrainText = other.terrainText;
typeCode = other.typeCode;
terrainViewPatterns = other.terrainViewPatterns;
rockTerrain = other.rockTerrain;
river = other.river;
id = other.id;
moveCost = other.moveCost;
horseSoundId = other.horseSoundId;
passabilityType = other.passabilityType;
transitionRequired = other.transitionRequired;
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
{
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::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-25 09:33:56 +02:00
RiverType::RiverType(const std::string & fileName, const std::string & code, TRiverId id):
fileName(fileName),
code(code),
id(id)
{
}
RiverType& RiverType::operator=(const RiverType& other)
{
fileName = other.fileName;
code = other.code;
deltaName = other.deltaName;
id = other.id;
return *this;
}
2022-09-25 09:33:56 +02:00
RoadType::RoadType(const std::string& fileName, const std::string& code, TRoadId id):
fileName(fileName),
code(code),
id(id),
movementCost(GameConstants::BASE_MOVEMENT_COST)
{
2022-09-26 10:20:39 +02:00
}
RoadType& RoadType::operator=(const RoadType& other)
{
fileName = other.fileName;
code = other.code;
id = other.id;
movementCost = other.movementCost;
return *this;
2022-09-27 07:53:21 +02:00
}
VCMI_LIB_NAMESPACE_END