From 2bd30556a365925d6c86b2db45d9670f1399b87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zieli=C5=84ski?= Date: Mon, 26 Sep 2022 09:17:55 +0200 Subject: [PATCH] Store TerrainType by havlue and handle by reference --- client/CMusicHandler.cpp | 10 +- client/mapHandler.cpp | 4 +- client/widgets/AdventureMapClasses.cpp | 16 +-- lib/CGeneralTextHandler.cpp | 6 +- lib/CHeroHandler.cpp | 6 +- lib/CPathfinder.cpp | 4 +- lib/HeroBonus.cpp | 4 +- lib/Terrain.cpp | 175 ++++++++++++++----------- lib/Terrain.h | 4 +- lib/mapObjects/ObjectTemplate.cpp | 18 +-- lib/mapping/CMapOperation.cpp | 2 +- lib/mapping/MapEditUtils.cpp | 2 +- lib/mapping/MapFormatH3M.cpp | 8 +- lib/rmg/CRmgTemplate.cpp | 8 +- lib/rmg/CZonePlacer.cpp | 4 +- lib/rmg/ConnectionsPlacer.cpp | 4 +- lib/rmg/Functions.cpp | 12 +- lib/rmg/RiverPlacer.cpp | 6 +- lib/rmg/RmgObject.cpp | 2 +- lib/rmg/RockPlacer.cpp | 4 +- 20 files changed, 162 insertions(+), 137 deletions(-) diff --git a/client/CMusicHandler.cpp b/client/CMusicHandler.cpp index 473b2cc1a..eead0ee36 100644 --- a/client/CMusicHandler.cpp +++ b/client/CMusicHandler.cpp @@ -120,14 +120,14 @@ CSoundHandler::CSoundHandler(): void CSoundHandler::loadHorseSounds() { const auto & terrains = CGI->terrainTypeHandler->terrains(); - for(const auto * terrain : terrains) + for(const auto & terrain : terrains) { //since all sounds are hardcoded, let's keep it - if(vstd::contains(horseSounds, terrain->id)) + if(vstd::contains(horseSounds, terrain.id)) continue; //Use already existing horse sound - horseSounds[terrain->id] = horseSounds.at(terrains[terrain->id]->horseSoundId); + horseSounds[terrain.id] = horseSounds.at(terrains[terrain.id].horseSoundId); } } @@ -376,9 +376,9 @@ CMusicHandler::CMusicHandler(): void CMusicHandler::loadTerrainSounds() { - for (const auto * terrain : CGI->terrainTypeHandler->terrains()) + for (const auto & terrain : CGI->terrainTypeHandler->terrains()) { - addEntryToSet("terrain", terrain->name, "Music/" + terrain->musicFilename); + addEntryToSet("terrain", terrain.name, "Music/" + terrain.musicFilename); } } diff --git a/client/mapHandler.cpp b/client/mapHandler.cpp index a631a8b29..e6177153b 100644 --- a/client/mapHandler.cpp +++ b/client/mapHandler.cpp @@ -176,9 +176,9 @@ void CMapHandler::initTerrainGraphics() std::map terrainFiles; std::map riverFiles; std::map roadFiles; - for(const auto * terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { - terrainFiles[terrain->name] = terrain->tilesFilename; + terrainFiles[terrain.name] = terrain.tilesFilename; } for(const auto * river : VLC->terrainTypeHandler->rivers()) { diff --git a/client/widgets/AdventureMapClasses.cpp b/client/widgets/AdventureMapClasses.cpp index 5aa52c64f..4f9697cb3 100644 --- a/client/widgets/AdventureMapClasses.cpp +++ b/client/widgets/AdventureMapClasses.cpp @@ -499,25 +499,25 @@ std::map > CMinimap::loadColors() { std::map > ret; - for(const auto * terrain : CGI->terrainTypeHandler->terrains()) + for(const auto & terrain : CGI->terrainTypeHandler->terrains()) { SDL_Color normal = { - ui8(terrain->minimapUnblocked[0]), - ui8(terrain->minimapUnblocked[1]), - ui8(terrain->minimapUnblocked[2]), + ui8(terrain.minimapUnblocked[0]), + ui8(terrain.minimapUnblocked[1]), + ui8(terrain.minimapUnblocked[2]), ui8(255) }; SDL_Color blocked = { - ui8(terrain->minimapBlocked[0]), - ui8(terrain->minimapBlocked[1]), - ui8(terrain->minimapBlocked[2]), + ui8(terrain.minimapBlocked[0]), + ui8(terrain.minimapBlocked[1]), + ui8(terrain.minimapBlocked[2]), ui8(255) }; - ret[terrain->id] = std::make_pair(normal, blocked); + ret[terrain.id] = std::make_pair(normal, blocked); } return ret; } diff --git a/lib/CGeneralTextHandler.cpp b/lib/CGeneralTextHandler.cpp index f4ad41280..985b02a81 100644 --- a/lib/CGeneralTextHandler.cpp +++ b/lib/CGeneralTextHandler.cpp @@ -338,10 +338,10 @@ CGeneralTextHandler::CGeneralTextHandler() { terrainNames[i] = h3mTerrainNames[i]; } - for(const auto * terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { - if(!terrain->terrainText.empty()) - terrainNames[terrain->id] = terrain->terrainText; + if(!terrain.terrainText.empty()) + terrainNames[terrain.id] = terrain.terrainText; } diff --git a/lib/CHeroHandler.cpp b/lib/CHeroHandler.cpp index a215f27f4..213ef23b9 100644 --- a/lib/CHeroHandler.cpp +++ b/lib/CHeroHandler.cpp @@ -346,7 +346,7 @@ CHeroHandler::CHeroHandler() loadTerrains(); for(int i = 0; i < VLC->terrainTypeHandler->terrains().size(); ++i) { - VLC->modh->identifiers.registerObject("core", "terrain", VLC->terrainTypeHandler->terrains()[i]->name, i); + VLC->modh->identifiers.registerObject("core", "terrain", VLC->terrainTypeHandler->terrains()[i].name, i); } loadBallistics(); loadExperience(); @@ -972,9 +972,9 @@ ui64 CHeroHandler::reqExp (ui32 level) const void CHeroHandler::loadTerrains() { - for(const auto * terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { - terrCosts[terrain->id] = terrain->moveCost; + terrCosts[terrain.id] = terrain.moveCost; } } diff --git a/lib/CPathfinder.cpp b/lib/CPathfinder.cpp index e99cc13d4..973f1d4fa 100644 --- a/lib/CPathfinder.cpp +++ b/lib/CPathfinder.cpp @@ -1008,10 +1008,10 @@ bool CPathfinderHelper::passOneTurnLimitCheck(const PathNodeInfo & source) const TurnInfo::BonusCache::BonusCache(TConstBonusListPtr bl) { - for(const auto * terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { noTerrainPenalty.push_back(static_cast( - bl->getFirst(Selector::type()(Bonus::NO_TERRAIN_PENALTY).And(Selector::subtype()(terrain->id))))); + bl->getFirst(Selector::type()(Bonus::NO_TERRAIN_PENALTY).And(Selector::subtype()(terrain.id))))); } freeShipBoarding = static_cast(bl->getFirst(Selector::type()(Bonus::FREE_SHIP_BOARDING))); diff --git a/lib/HeroBonus.cpp b/lib/HeroBonus.cpp index 6eb9e9813..4890e7f17 100644 --- a/lib/HeroBonus.cpp +++ b/lib/HeroBonus.cpp @@ -2137,7 +2137,7 @@ int CreatureTerrainLimiter::limit(const BonusLimitationContext &context) const std::string CreatureTerrainLimiter::toString() const { boost::format fmt("CreatureTerrainLimiter(terrainType=%s)"); - auto terrainName = VLC->terrainTypeHandler->terrains()[terrainType]->name; + auto terrainName = VLC->terrainTypeHandler->terrains()[terrainType].name; fmt % (terrainType == Terrain::NATIVE_TERRAIN ? "native" : terrainName); return fmt.str(); } @@ -2147,7 +2147,7 @@ JsonNode CreatureTerrainLimiter::toJsonNode() const JsonNode root(JsonNode::JsonType::DATA_STRUCT); root["type"].String() = "CREATURE_TERRAIN_LIMITER"; - auto terrainName = VLC->terrainTypeHandler->terrains()[terrainType]->name; + auto terrainName = VLC->terrainTypeHandler->terrains()[terrainType].name; root["parameters"].Vector().push_back(JsonUtils::stringNode(terrainName)); return root; diff --git a/lib/Terrain.cpp b/lib/Terrain.cpp index 78612ba5f..a95e322a2 100644 --- a/lib/Terrain.cpp +++ b/lib/Terrain.cpp @@ -31,10 +31,6 @@ TerrainTypeHandler::TerrainTypeHandler() TerrainTypeHandler::~TerrainTypeHandler() { - for (const auto * terrain : objects) - { - delete terrain; - } for (const auto * river : riverTypes) { delete river; @@ -49,7 +45,7 @@ void TerrainTypeHandler::initTerrains(const std::vector & allConfig { std::vector> resolveLater; - objects.resize(Terrain::ORIGINAL_TERRAIN_COUNT, nullptr); //make space for original terrains + objects.resize(Terrain::ORIGINAL_TERRAIN_COUNT); //make space for original terrains for(auto & mod : allConfigs) { @@ -59,11 +55,11 @@ void TerrainTypeHandler::initTerrains(const std::vector & allConfig JsonNode terrs(mod, ResourceID("config/terrains.json")); for(auto & terr : terrs.Struct()) { - auto * info = new TerrainType(terr.first); //set name + TerrainType info(terr.first); //set name - info->moveCost = terr.second["moveCost"].Integer(); + info.moveCost = static_cast(terr.second["moveCost"].Integer()); const JsonVector &unblockedVec = terr.second["minimapUnblocked"].Vector(); - info->minimapUnblocked = + info.minimapUnblocked = { ui8(unblockedVec[0].Float()), ui8(unblockedVec[1].Float()), @@ -71,18 +67,18 @@ void TerrainTypeHandler::initTerrains(const std::vector & allConfig }; const JsonVector &blockedVec = terr.second["minimapBlocked"].Vector(); - info->minimapBlocked = + 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(); + 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; + info.passabilityType = TerrainType::PassabilityType::LAND | TerrainType::PassabilityType::SURFACE; } else if (terr.second["type"].getType() == JsonNode::JsonType::DATA_VECTOR) { @@ -90,122 +86,125 @@ void TerrainTypeHandler::initTerrains(const std::vector & allConfig { //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; + 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["rockTerrain"].isNull()) - { - info->rockTerrain = Terrain::ROCK; - } - else - { - auto rockTerrainName = terr.second["rockTerrain"].String(); - resolveLater.push_back([this, rockTerrainName, info]() - { - info->rockTerrain = getInfoByName(rockTerrainName)->id; - }); + 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; + info.river = River::NO_RIVER; } else { - info->river = getRiverByCode(terr.second["river"].String())->id; + info.river = getRiverByCode(terr.second["river"].String())->id; } if(terr.second["horseSoundId"].isNull()) { - info->horseSoundId = Terrain::ROCK; //rock sound as default + info.horseSoundId = Terrain::ROCK; //rock sound as default } else { - info->horseSoundId = terr.second["horseSoundId"].Float(); + info.horseSoundId = static_cast(terr.second["horseSoundId"].Float()); } if(!terr.second["text"].isNull()) { - info->terrainText = terr.second["text"].String(); + info.terrainText = terr.second["text"].String(); } if(terr.second["code"].isNull()) { - info->typeCode = terr.first.substr(0, 2); + info.typeCode = terr.first.substr(0, 2); } else { - info->typeCode = terr.second["code"].String(); - assert(info->typeCode.length() == 2); + info.typeCode = terr.second["code"].String(); + assert(info.typeCode.length() == 2); } if(!terr.second["battleFields"].isNull()) { for(auto & t : terr.second["battleFields"].Vector()) { - info->battleFields.emplace_back(t.String()); + info.battleFields.emplace_back(t.String()); } } + info.transitionRequired = false; + if(!terr.second["transitionRequired"].isNull()) + { + info.transitionRequired = terr.second["transitionRequired"].Bool(); + } + + info.terrainViewPatterns = "normal"; + if(!terr.second["terrainViewPatterns"].isNull()) + { + info.terrainViewPatterns = terr.second["terrainViewPatterns"].String(); + } + + if(!terr.second["originalTerrainId"].isNull()) + { + //place in reserved slot + info.id = (TTerrainId)(terr.second["originalTerrainId"].Float()); + objects[info.id] = info; + } + else + { + //append at the end + info.id = static_cast(objects.size()); + objects.push_back(info); + } + 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, info]() + resolveLater.push_back([this, prohibitedTerrainName, id]() { - info->prohibitTransitions.emplace_back(getInfoByName(prohibitedTerrainName)->id); + //FIXME: is that reference to the element in vector? + objects[id].prohibitTransitions.emplace_back(getInfoByName(prohibitedTerrainName)->id); }); } } - - info->transitionRequired = false; - if(!terr.second["transitionRequired"].isNull()) - { - info->transitionRequired = terr.second["transitionRequired"].Bool(); - } - - info->terrainViewPatterns = "normal"; - if(!terr.second["terrainViewPatterns"].isNull()) - { - info->terrainViewPatterns = terr.second["terrainViewPatterns"].String(); - } - TTerrainId id = Terrain::WRONG; - if(!terr.second["originalTerrainId"].isNull()) + if(terr.second["rockTerrain"].isNull()) { - //place in reserved slot - id = (TTerrainId)(terr.second["originalTerrainId"].Float()); - objects[id] = info; + objects[id].rockTerrain = Terrain::ROCK; } else { - //append at the end - id = objects.size(); - objects.push_back(info); + 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; + }); } - info->id = id; } } for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++) { //Make sure that original terrains are loaded - assert(objects(i)); + assert(objects(i).id != Terrain::WRONG); } recreateTerrainMaps(); @@ -243,7 +242,7 @@ void TerrainTypeHandler::initRivers(const std::vector & allConfigs) } else { - info->id = riverTypes.size(); + info->id = static_cast(riverTypes.size()); riverTypes.push_back(info); } } @@ -278,7 +277,7 @@ void TerrainTypeHandler::initRoads(const std::vector & allConfigs) } else { - info->id = roadTypes.size(); + info->id = static_cast(roadTypes.size()); roadTypes.push_back(info); } } @@ -289,8 +288,12 @@ void TerrainTypeHandler::initRoads(const std::vector & allConfigs) void TerrainTypeHandler::recreateTerrainMaps() { - for (const TerrainType * terrainInfo : objects) + //This assumes the vector will never be updated or reallocated in the future + + for (size_t i = 0; i < objects.size(); i++) { + const auto * terrainInfo = &objects[i]; + terrainInfoByName[terrainInfo->name] = terrainInfo; terrainInfoByCode[terrainInfo->typeCode] = terrainInfo; terrainInfoById[terrainInfo->id] = terrainInfo; @@ -323,8 +326,9 @@ void TerrainTypeHandler::recreateRoadMaps() } } -const std::vector & TerrainTypeHandler::terrains() const +const std::vector & TerrainTypeHandler::terrains() const { + //FIXME: somehow make it non-copyable? Pointers must point t original data and not its copy return objects; } @@ -394,10 +398,13 @@ TerrainType::operator std::string() const } TerrainType::TerrainType(const std::string& _name): + minimapBlocked({0,0,0}), //black + minimapUnblocked({ 128,128,128 }), //grey name(_name), + river(River::NO_RIVER), id(Terrain::WRONG), rockTerrain(Terrain::ROCK), - moveCost(100), + moveCost(GameConstants::BASE_MOVEMENT_COST), horseSoundId(0), passabilityType(0), transitionRequired(false) @@ -406,8 +413,25 @@ TerrainType::TerrainType(const std::string& _name): TerrainType& TerrainType::operator=(const TerrainType & other) { - //TODO + battleFields = other.battleFields; + prohibitTransitions = other.prohibitTransitions; + minimapBlocked = other.minimapBlocked; + minimapUnblocked = other.minimapUnblocked; 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; } @@ -466,6 +490,7 @@ RiverType::RiverType(const std::string & fileName, const std::string & code, TRi RoadType::RoadType(const std::string& fileName, const std::string& code, TRoadId id): fileName(fileName), code(code), - id(id) + id(id), + movementCost(GameConstants::BASE_MOVEMENT_COST) { } \ No newline at end of file diff --git a/lib/Terrain.h b/lib/Terrain.h index cf4b06f1c..a4da258c7 100644 --- a/lib/Terrain.h +++ b/lib/Terrain.h @@ -135,7 +135,7 @@ public: TerrainTypeHandler(); ~TerrainTypeHandler(); - const std::vector & terrains() const; + const std::vector & terrains() const; const TerrainType * getInfoByName(const std::string & terrainName) const; const TerrainType * getInfoByCode(const std::string & terrainCode) const; const TerrainType * getInfoById(TTerrainId id) const; @@ -166,7 +166,7 @@ public: private: - std::vector objects; + std::vector objects; std::vector riverTypes; std::vector roadTypes; diff --git a/lib/mapObjects/ObjectTemplate.cpp b/lib/mapObjects/ObjectTemplate.cpp index 26f1ccd41..dd444a53f 100644 --- a/lib/mapObjects/ObjectTemplate.cpp +++ b/lib/mapObjects/ObjectTemplate.cpp @@ -165,10 +165,10 @@ void ObjectTemplate::readTxt(CLegacyConfigParser & parser) //assuming that object can be placed on other land terrains if(allowedTerrains.size() >= 8 && !allowedTerrains.count(Terrain::WATER)) { - for(const auto * terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { - if(terrain->isLand() && terrain->isPassable()) - allowedTerrains.insert(terrain->id); + if(terrain.isLand() && terrain.isPassable()) + allowedTerrains.insert(terrain.id); } } @@ -238,10 +238,10 @@ void ObjectTemplate::readMap(CBinaryReader & reader) //assuming that object can be placed on other land terrains if(allowedTerrains.size() >= 8 && !allowedTerrains.count(Terrain::WATER)) { - for(const auto * terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { - if(terrain->isLand() && terrain->isPassable()) - allowedTerrains.insert(terrain->id); + if(terrain.isLand() && terrain.isPassable()) + allowedTerrains.insert(terrain.id); } } @@ -289,11 +289,11 @@ void ObjectTemplate::readJson(const JsonNode &node, const bool withTerrain) } else { - for(const auto* terrain : VLC->terrainTypeHandler->terrains()) + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) { - if(!terrain->isPassable() || terrain->isWater()) + if(!terrain.isPassable() || terrain.isWater()) continue; - allowedTerrains.insert(terrain->id); + allowedTerrains.insert(terrain.id); } } diff --git a/lib/mapping/CMapOperation.cpp b/lib/mapping/CMapOperation.cpp index 4392beb24..94178d21a 100644 --- a/lib/mapping/CMapOperation.cpp +++ b/lib/mapping/CMapOperation.cpp @@ -95,7 +95,7 @@ void CDrawTerrainOperation::execute() for(const auto & pos : terrainSel.getSelectedItems()) { auto & tile = map->getTile(pos); - tile.terType = VLC->terrainTypeHandler->terrains()[terType]; + tile.terType = const_cast(&VLC->terrainTypeHandler->terrains()[terType]); invalidateTerrainViews(pos); } diff --git a/lib/mapping/MapEditUtils.cpp b/lib/mapping/MapEditUtils.cpp index 603d009d7..7a5be3e33 100644 --- a/lib/mapping/MapEditUtils.cpp +++ b/lib/mapping/MapEditUtils.cpp @@ -270,7 +270,7 @@ CTerrainViewPatternConfig::~CTerrainViewPatternConfig() const std::vector & CTerrainViewPatternConfig::getTerrainViewPatterns(TTerrainId terrain) const { - auto iter = terrainViewPatterns.find(VLC->terrainTypeHandler->terrains()[terrain]->terrainViewPatterns); + auto iter = terrainViewPatterns.find(VLC->terrainTypeHandler->terrains()[terrain].terrainViewPatterns); if (iter == terrainViewPatterns.end()) return terrainViewPatterns.at("normal"); return iter->second; diff --git a/lib/mapping/MapFormatH3M.cpp b/lib/mapping/MapFormatH3M.cpp index a2705dc20..481ba7833 100644 --- a/lib/mapping/MapFormatH3M.cpp +++ b/lib/mapping/MapFormatH3M.cpp @@ -921,9 +921,9 @@ bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot) void CMapLoaderH3M::readTerrain() { map->initTerrain(); - const auto terrains = VLC->terrainTypeHandler->terrains(); - const auto rivers = VLC->terrainTypeHandler->rivers(); - const auto roads = VLC->terrainTypeHandler->roads(); + const auto & terrains = VLC->terrainTypeHandler->terrains(); + const auto & rivers = VLC->terrainTypeHandler->rivers(); + const auto & roads = VLC->terrainTypeHandler->roads(); // Read terrain int3 pos; @@ -935,7 +935,7 @@ void CMapLoaderH3M::readTerrain() for(pos.x = 0; pos.x < map->width; pos.x++) { auto & tile = map->getTile(pos); - tile.terType = terrains[reader.readUInt8()]; + tile.terType = const_cast(&terrains[reader.readUInt8()]); tile.terView = reader.readUInt8(); tile.riverType = rivers[reader.readUInt8()]; tile.riverDir = reader.readUInt8(); diff --git a/lib/rmg/CRmgTemplate.cpp b/lib/rmg/CRmgTemplate.cpp index 7b4439227..423742a70 100644 --- a/lib/rmg/CRmgTemplate.cpp +++ b/lib/rmg/CRmgTemplate.cpp @@ -73,7 +73,7 @@ public: static std::string encode(const si32 index) { const auto& terrains = VLC->terrainTypeHandler->terrains(); - return (index >=0 && index < terrains.size()) ? terrains[index]->name : ""; + return (index >=0 && index < terrains.size()) ? terrains[index].name : ""; } }; @@ -150,9 +150,9 @@ ZoneOptions::ZoneOptions() terrainTypeLikeZone(NO_ZONE), treasureLikeZone(NO_ZONE) { - for(const auto * terr : VLC->terrainTypeHandler->terrains()) - if(terr->isLand() && terr->isPassable()) - terrainTypes.insert(terr->id); + for(const auto & terr : VLC->terrainTypeHandler->terrains()) + if(terr.isLand() && terr.isPassable()) + terrainTypes.insert(terr.id); } ZoneOptions & ZoneOptions::operator=(const ZoneOptions & other) diff --git a/lib/rmg/CZonePlacer.cpp b/lib/rmg/CZonePlacer.cpp index e21d0e136..fa4b6daeb 100644 --- a/lib/rmg/CZonePlacer.cpp +++ b/lib/rmg/CZonePlacer.cpp @@ -198,8 +198,8 @@ void CZonePlacer::prepareZones(TZoneMap &zones, TZoneVector &zonesVector, const } else { - const auto * terrainType = VLC->terrainTypeHandler->terrains()[tt]; - if(terrainType->isUnderground() && !terrainType->isSurface()) + const auto & terrainType = VLC->terrainTypeHandler->terrains()[tt]; + if(terrainType.isUnderground() && !terrainType.isSurface()) { //underground only zonesOnLevel[1]++; diff --git a/lib/rmg/ConnectionsPlacer.cpp b/lib/rmg/ConnectionsPlacer.cpp index 9886bf4c3..89af592e9 100644 --- a/lib/rmg/ConnectionsPlacer.cpp +++ b/lib/rmg/ConnectionsPlacer.cpp @@ -83,8 +83,8 @@ void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & con //1. Try to make direct connection //Do if it's not prohibited by terrain settings const auto& terrains = VLC->terrainTypeHandler->terrains(); - bool directProhibited = vstd::contains(terrains[zone.getTerrainType()]->prohibitTransitions, otherZone->getTerrainType()) - || vstd::contains(terrains[otherZone->getTerrainType()]->prohibitTransitions, zone.getTerrainType()); + bool directProhibited = vstd::contains(terrains[zone.getTerrainType()].prohibitTransitions, otherZone->getTerrainType()) + || vstd::contains(terrains[otherZone->getTerrainType()].prohibitTransitions, zone.getTerrainType()); auto directConnectionIterator = dNeighbourZones.find(otherZoneId); if(!directProhibited && directConnectionIterator != dNeighbourZones.end()) { diff --git a/lib/rmg/Functions.cpp b/lib/rmg/Functions.cpp index 7195cde00..d409de87d 100644 --- a/lib/rmg/Functions.cpp +++ b/lib/rmg/Functions.cpp @@ -117,9 +117,9 @@ void initTerrainType(Zone & zone, CMapGenerator & gen) { //collect all water terrain types std::vector waterTerrains; - for(auto & terrain : VLC->terrainTypeHandler->terrains()) - if(terrain->isWater()) - waterTerrains.push_back(terrain->id); + for(const auto & terrain : VLC->terrainTypeHandler->terrains()) + if(terrain.isWater()) + waterTerrains.push_back(terrain.id); zone.setTerrainType(*RandomGeneratorUtil::nextItem(waterTerrains, gen.rand)); } @@ -135,18 +135,18 @@ void initTerrainType(Zone & zone, CMapGenerator & gen) } //Now, replace disallowed terrains on surface and in the underground - const auto* terrainType = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()]; + const auto & terrainType = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()]; if(zone.isUnderground()) { - if(!terrainType->isUnderground()) + if(!terrainType.isUnderground()) { zone.setTerrainType(Terrain::SUBTERRANEAN); } } else { - if (!terrainType->isSurface()) + if (!terrainType.isSurface()) { zone.setTerrainType(Terrain::DIRT); } diff --git a/lib/rmg/RiverPlacer.cpp b/lib/rmg/RiverPlacer.cpp index 0ce29b0e4..5e731dbf8 100644 --- a/lib/rmg/RiverPlacer.cpp +++ b/lib/rmg/RiverPlacer.cpp @@ -85,7 +85,7 @@ void RiverPlacer::init() void RiverPlacer::drawRivers() { map.getEditManager()->getTerrainSelection().setSelection(rivers.getTilesVector()); - map.getEditManager()->drawRiver(VLC->terrainTypeHandler->terrains()[zone.getTerrainType()]->river, &generator.rand); + map.getEditManager()->drawRiver(VLC->terrainTypeHandler->terrains()[zone.getTerrainType()].river, &generator.rand); } char RiverPlacer::dump(const int3 & t) @@ -194,7 +194,7 @@ void RiverPlacer::preprocess() //calculate delta positions if(connectedToWaterZoneId > -1) { - auto river = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()]->river; + auto river = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()].river; auto & a = neighbourZonesTiles[connectedToWaterZoneId]; auto availableArea = zone.areaPossible() + zone.freePaths(); for(auto & tileToProcess : availableArea.getTilesVector()) @@ -320,7 +320,7 @@ void RiverPlacer::preprocess() void RiverPlacer::connectRiver(const int3 & tile) { - auto riverType = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()]->river; + auto riverType = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()].river; auto river = VLC->terrainTypeHandler->rivers()[riverType]; if(river->id == River::NO_RIVER) return; diff --git a/lib/rmg/RmgObject.cpp b/lib/rmg/RmgObject.cpp index 68d5a7754..2e670778a 100644 --- a/lib/rmg/RmgObject.cpp +++ b/lib/rmg/RmgObject.cpp @@ -108,7 +108,7 @@ void Object::Instance::setTemplate(const TTerrainId & terrain) if(dObject.appearance->id == Obj::NO_OBJ) { auto templates = VLC->objtypeh->getHandlerFor(dObject.ID, dObject.subID)->getTemplates(terrain); - auto terrainName = VLC->terrainTypeHandler->terrains()[terrain]->name; + auto terrainName = VLC->terrainTypeHandler->terrains()[terrain].name; if (templates.empty()) { throw rmgException(boost::to_string(boost::format("Did not find graphics for object (%d,%d) at %s") % diff --git a/lib/rmg/RockPlacer.cpp b/lib/rmg/RockPlacer.cpp index 358a4269d..48c419fa0 100644 --- a/lib/rmg/RockPlacer.cpp +++ b/lib/rmg/RockPlacer.cpp @@ -22,8 +22,8 @@ void RockPlacer::process() { - rockTerrain = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()]->rockTerrain; - assert(!VLC->terrainTypeHandler->terrains()[rockTerrain]->isPassable()); + rockTerrain = VLC->terrainTypeHandler->terrains()[zone.getTerrainType()].rockTerrain; + assert(!VLC->terrainTypeHandler->terrains()[rockTerrain].isPassable()); accessibleArea = zone.freePaths() + zone.areaUsed(); if(auto * m = zone.getModificator())