mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
Handle RoadType by value / reference
This commit is contained in:
@@ -172,7 +172,7 @@ void CMapHandler::initTerrainGraphics()
|
||||
}
|
||||
};
|
||||
|
||||
//TODO: use if as a key
|
||||
//TODO: use id as a key
|
||||
std::map<std::string, std::string> terrainFiles;
|
||||
std::map<std::string, std::string> riverFiles;
|
||||
std::map<std::string, std::string> roadFiles;
|
||||
@@ -184,9 +184,9 @@ void CMapHandler::initTerrainGraphics()
|
||||
{
|
||||
riverFiles[river.fileName] = river.fileName;
|
||||
}
|
||||
for(const auto * road : VLC->terrainTypeHandler->roads())
|
||||
for(const auto & road : VLC->terrainTypeHandler->roads())
|
||||
{
|
||||
roadFiles[road->fileName] = road->fileName;
|
||||
roadFiles[road.fileName] = road.fileName;
|
||||
}
|
||||
|
||||
loadFlipped(terrainAnimations, terrainImages, terrainFiles);
|
||||
|
@@ -859,6 +859,7 @@ namespace Road
|
||||
enum ERoad : ui8
|
||||
{
|
||||
NO_ROAD = 0,
|
||||
FIRST_REGULAR_ROAD = 1,
|
||||
DIRT_ROAD = 1,
|
||||
GRAVEL_ROAD = 2,
|
||||
COBBLESTONE_ROAD = 3,
|
||||
|
@@ -29,14 +29,6 @@ TerrainTypeHandler::TerrainTypeHandler()
|
||||
initTerrains(allConfigs); //maps will be populated inside
|
||||
}
|
||||
|
||||
TerrainTypeHandler::~TerrainTypeHandler()
|
||||
{
|
||||
for (const auto * road : roadTypes)
|
||||
{
|
||||
delete road;
|
||||
}
|
||||
}
|
||||
|
||||
void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfigs)
|
||||
{
|
||||
std::vector<std::function<void()>> resolveLater;
|
||||
@@ -78,7 +70,7 @@ void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfig
|
||||
}
|
||||
else if (terr.second["type"].getType() == JsonNode::JsonType::DATA_VECTOR)
|
||||
{
|
||||
for (const auto& node : terr.second["type"].Vector())
|
||||
for(const auto& node : terr.second["type"].Vector())
|
||||
{
|
||||
//Set bits
|
||||
auto s = node.String();
|
||||
@@ -197,7 +189,7 @@ void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfig
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
|
||||
for(size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
|
||||
{
|
||||
//Make sure that original terrains are loaded
|
||||
assert(objects(i).id != Terrain::WRONG);
|
||||
@@ -205,7 +197,7 @@ void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfig
|
||||
|
||||
recreateTerrainMaps();
|
||||
|
||||
for (auto& functor : resolveLater)
|
||||
for(auto& functor : resolveLater)
|
||||
{
|
||||
functor();
|
||||
}
|
||||
@@ -214,22 +206,21 @@ void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfig
|
||||
void TerrainTypeHandler::initRivers(const std::vector<std::string> & allConfigs)
|
||||
{
|
||||
riverTypes.resize(River::ORIGINAL_RIVER_COUNT); //make space for original rivers
|
||||
//First object will be default
|
||||
//First object will be default NO_RIVER
|
||||
|
||||
for (auto & mod : allConfigs)
|
||||
for(auto & mod : allConfigs)
|
||||
{
|
||||
if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/rivers.json")))
|
||||
continue;
|
||||
|
||||
JsonNode rivs(mod, ResourceID("config/rivers.json"));
|
||||
for (auto & river : rivs.Struct())
|
||||
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();
|
||||
//info->movementCost = river.second["moveCost"].Integer();
|
||||
|
||||
if (!river.second["originalRiverId"].isNull())
|
||||
{
|
||||
@@ -249,31 +240,31 @@ void TerrainTypeHandler::initRivers(const std::vector<std::string> & allConfigs)
|
||||
|
||||
void TerrainTypeHandler::initRoads(const std::vector<std::string> & allConfigs)
|
||||
{
|
||||
roadTypes.resize(Road::ORIGINAL_ROAD_COUNT, nullptr); //make space for original rivers
|
||||
roadTypes[Road::NO_ROAD] = new RoadType(); //default
|
||||
roadTypes.resize(Road::ORIGINAL_ROAD_COUNT); //make space for original rivers
|
||||
//first object will be default NO_ROAD
|
||||
|
||||
for (auto & mod : allConfigs)
|
||||
for(auto & mod : allConfigs)
|
||||
{
|
||||
if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/roads.json")))
|
||||
continue;
|
||||
|
||||
JsonNode rds(mod, ResourceID("config/roads.json"));
|
||||
for (auto & road : rds.Struct())
|
||||
for(auto & road : rds.Struct())
|
||||
{
|
||||
auto * info = new RoadType();
|
||||
RoadType info;
|
||||
|
||||
info->fileName = road.second["animation"].String();
|
||||
info->code = road.second["code"].String();
|
||||
info->movementCost = static_cast<ui8>(road.second["moveCost"].Float());
|
||||
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())
|
||||
{
|
||||
info->id = static_cast<TRoadId>(road.second["originalRoadId"].Float());
|
||||
roadTypes[info->id] = info;
|
||||
info.id = static_cast<TRoadId>(road.second["originalRoadId"].Float());
|
||||
roadTypes[info.id] = info;
|
||||
}
|
||||
else
|
||||
{
|
||||
info->id = static_cast<TRoadId>(roadTypes.size());
|
||||
info.id = static_cast<TRoadId>(roadTypes.size());
|
||||
roadTypes.push_back(info);
|
||||
}
|
||||
}
|
||||
@@ -286,7 +277,7 @@ void TerrainTypeHandler::recreateTerrainMaps()
|
||||
{
|
||||
//This assumes the vector will never be updated or reallocated in the future
|
||||
|
||||
for (size_t i = 0; i < objects.size(); i++)
|
||||
for(size_t i = 0; i < objects.size(); i++)
|
||||
{
|
||||
const auto * terrainInfo = &objects[i];
|
||||
|
||||
@@ -298,9 +289,9 @@ void TerrainTypeHandler::recreateTerrainMaps()
|
||||
|
||||
void TerrainTypeHandler::recreateRiverMaps()
|
||||
{
|
||||
for (size_t i = River::FIRST_REGULAR_RIVER ; i < riverTypes.size(); i++)
|
||||
for(size_t i = River::FIRST_REGULAR_RIVER ; i < riverTypes.size(); i++)
|
||||
{
|
||||
const auto* riverInfo = &riverTypes[i];
|
||||
const auto * riverInfo = &riverTypes[i];
|
||||
|
||||
riverInfoByName[riverInfo->fileName] = riverInfo;
|
||||
riverInfoByCode[riverInfo->code] = riverInfo;
|
||||
@@ -310,10 +301,9 @@ void TerrainTypeHandler::recreateRiverMaps()
|
||||
|
||||
void TerrainTypeHandler::recreateRoadMaps()
|
||||
{
|
||||
for (const RoadType * roadInfo : roadTypes)
|
||||
for(size_t i = Road::FIRST_REGULAR_ROAD ; i < roadTypes.size(); i++)
|
||||
{
|
||||
if (roadInfo->id == Road::NO_ROAD)
|
||||
continue;
|
||||
const auto * roadInfo = &roadTypes[i];
|
||||
|
||||
roadInfoByName[roadInfo->fileName] = roadInfo;
|
||||
roadInfoByCode[roadInfo->code] = roadInfo;
|
||||
@@ -332,7 +322,7 @@ const std::vector<RiverType>& TerrainTypeHandler::rivers() const
|
||||
return riverTypes;
|
||||
}
|
||||
|
||||
const std::vector<RoadType*>& TerrainTypeHandler::roads() const
|
||||
const std::vector<RoadType>& TerrainTypeHandler::roads() const
|
||||
{
|
||||
return roadTypes;
|
||||
}
|
||||
@@ -498,4 +488,14 @@ RoadType::RoadType(const std::string& fileName, const std::string& code, TRoadId
|
||||
id(id),
|
||||
movementCost(GameConstants::BASE_MOVEMENT_COST)
|
||||
{
|
||||
}
|
||||
|
||||
RoadType& RoadType::operator=(const RoadType& other)
|
||||
{
|
||||
fileName = other.fileName;
|
||||
code = other.code;
|
||||
id = other.id;
|
||||
movementCost = other.movementCost;
|
||||
|
||||
return *this;
|
||||
}
|
@@ -119,6 +119,8 @@ public:
|
||||
|
||||
RoadType(const std::string & fileName = "", const std::string& code = "", TRoadId id = Road::NO_ROAD);
|
||||
|
||||
RoadType& operator=(const RoadType & other);
|
||||
|
||||
template <typename Handler> void serialize(Handler& h, const int version)
|
||||
{
|
||||
h & fileName;
|
||||
@@ -135,7 +137,7 @@ class DLL_LINKAGE TerrainTypeHandler //TODO: public IHandlerBase ?
|
||||
public:
|
||||
|
||||
TerrainTypeHandler();
|
||||
~TerrainTypeHandler();
|
||||
~TerrainTypeHandler() {};
|
||||
|
||||
const std::vector<TerrainType> & terrains() const;
|
||||
const TerrainType * getInfoByName(const std::string & terrainName) const;
|
||||
@@ -147,7 +149,7 @@ public:
|
||||
const RiverType * getRiverByCode(const std::string & riverCode) const;
|
||||
const RiverType * getRiverById(TRiverId id) const;
|
||||
|
||||
const std::vector<RoadType *> & roads() const;
|
||||
const std::vector<RoadType> & roads() const;
|
||||
const RoadType * getRoadByName(const std::string & roadName) const;
|
||||
const RoadType * getRoadByCode(const std::string & roadCode) const;
|
||||
const RoadType * getRoadById(TRoadId id) const;
|
||||
@@ -170,7 +172,7 @@ private:
|
||||
|
||||
std::vector<TerrainType> objects;
|
||||
std::vector<RiverType> riverTypes;
|
||||
std::vector<RoadType *> roadTypes;
|
||||
std::vector<RoadType> roadTypes;
|
||||
|
||||
std::unordered_map<std::string, const TerrainType*> terrainInfoByName;
|
||||
std::unordered_map<std::string, const TerrainType*> terrainInfoByCode;
|
||||
|
@@ -336,7 +336,7 @@ std::string CDrawRiversOperation::getLabel() const
|
||||
|
||||
void CDrawRoadsOperation::executeTile(TerrainTile & tile)
|
||||
{
|
||||
tile.roadType = VLC->terrainTypeHandler->roads()[roadType];
|
||||
tile.roadType = const_cast<RoadType*>(&VLC->terrainTypeHandler->roads()[roadType]);
|
||||
}
|
||||
|
||||
void CDrawRiversOperation::executeTile(TerrainTile & tile)
|
||||
|
@@ -128,7 +128,7 @@ TerrainTile::TerrainTile():
|
||||
terView(0),
|
||||
riverType(const_cast<RiverType*>(&VLC->terrainTypeHandler->rivers()[0])),
|
||||
riverDir(0),
|
||||
roadType(VLC->terrainTypeHandler->roads()[0]),
|
||||
roadType(const_cast<RoadType*>(&VLC->terrainTypeHandler->roads()[0])),
|
||||
roadDir(0),
|
||||
extTileFlags(0),
|
||||
visitable(false),
|
||||
|
@@ -939,7 +939,7 @@ void CMapLoaderH3M::readTerrain()
|
||||
tile.terView = reader.readUInt8();
|
||||
tile.riverType = const_cast<RiverType*>(&rivers[reader.readUInt8()]);
|
||||
tile.riverDir = reader.readUInt8();
|
||||
tile.roadType = roads[reader.readUInt8()];
|
||||
tile.roadType = const_cast<RoadType*>(&roads[reader.readUInt8()]);
|
||||
tile.roadDir = reader.readUInt8();
|
||||
tile.extTileFlags = reader.readUInt8();
|
||||
tile.blocked = ((!tile.terType->isPassable() || tile.terType->id == Terrain::BORDER ) ? true : false); //underground tiles are always blocked
|
||||
|
Reference in New Issue
Block a user