diff --git a/AI/VCAI/Goals/GatherArmy.cpp b/AI/VCAI/Goals/GatherArmy.cpp index e6e0557f0..490e137df 100644 --- a/AI/VCAI/Goals/GatherArmy.cpp +++ b/AI/VCAI/Goals/GatherArmy.cpp @@ -152,8 +152,7 @@ TGoalVec GatherArmy::getAllPossibleSubgoals() { for(auto & creatureID : creLevel.second) { - auto creature = VLC->creh->objects[creatureID]; - if(ai->ah->freeResources().canAfford(creature->getFullRecruitCost())) + if(ai->ah->freeResources().canAfford(creatureID.toCreature()->getFullRecruitCost())) objs.push_back(obj); //TODO: reserve resources? } } diff --git a/client/CGameInfo.cpp b/client/CGameInfo.cpp index 3d025118f..1e84a9496 100644 --- a/client/CGameInfo.cpp +++ b/client/CGameInfo.cpp @@ -97,11 +97,6 @@ const IGameSettings * CGameInfo::settings() const return globalServices->settings(); } -void CGameInfo::updateEntity(Metatype metatype, int32_t index, const JsonNode & data) -{ - logGlobal->error("CGameInfo::updateEntity call is not expected."); -} - spells::effects::Registry * CGameInfo::spellEffects() { return nullptr; diff --git a/client/CGameInfo.h b/client/CGameInfo.h index c07bb3c7d..edc8f46f3 100644 --- a/client/CGameInfo.h +++ b/client/CGameInfo.h @@ -73,8 +73,6 @@ public: const ObstacleService * obstacles() const override; const IGameSettings * settings() const override; - void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) override; - const spells::effects::Registry * spellEffects() const override; spells::effects::Registry * spellEffects() override; diff --git a/client/battle/BattleProjectileController.cpp b/client/battle/BattleProjectileController.cpp index 404540c82..ba6b9778a 100644 --- a/client/battle/BattleProjectileController.cpp +++ b/client/battle/BattleProjectileController.cpp @@ -164,7 +164,7 @@ const CCreature & BattleProjectileController::getShooter(const CStack * stack) c if(creature->animation.missleFrameAngles.empty()) { logAnim->error("Mod error: Creature '%s' on the Archer's tower is not a shooter. Mod should be fixed. Trying to use archer's data instead...", creature->getNameSingularTranslated()); - creature = CGI->creh->objects[CreatureID::ARCHER]; + creature = CreatureID(CreatureID::ARCHER).toCreature(); } return *creature; diff --git a/client/battle/BattleSiegeController.cpp b/client/battle/BattleSiegeController.cpp index 660440f1e..6d27a5945 100644 --- a/client/battle/BattleSiegeController.cpp +++ b/client/battle/BattleSiegeController.cpp @@ -188,7 +188,7 @@ BattleSiegeController::BattleSiegeController(BattleInterface & owner, const CGTo const CCreature *BattleSiegeController::getTurretCreature() const { - return CGI->creh->objects[town->town->clientInfo.siegeShooter]; + return town->town->clientInfo.siegeShooter.toCreature(); } Point BattleSiegeController::getTurretCreaturePosition( BattleHex position ) const diff --git a/client/lobby/OptionsTab.cpp b/client/lobby/OptionsTab.cpp index 0ee53b900..3f87eb5e1 100644 --- a/client/lobby/OptionsTab.cpp +++ b/client/lobby/OptionsTab.cpp @@ -446,7 +446,7 @@ int OptionsTab::SelectionWindow::calcLines(FactionID faction) int count = 0; for(auto & elemh : allowedHeroes) { - CHero * type = VLC->heroh->objects[elemh]; + const CHero * type = elemh.toHeroType(); if(type->heroClass->faction == faction) count++; } @@ -603,7 +603,7 @@ void OptionsTab::SelectionWindow::genContentHeroes() for(auto & elem : allowedHeroes) { - CHero * type = VLC->heroh->objects[elem]; + const CHero * type = elem.toHeroType(); if(type->heroClass->faction == selectedFaction) { diff --git a/client/lobby/RandomMapTab.cpp b/client/lobby/RandomMapTab.cpp index 310f9dae2..68c5ab846 100644 --- a/client/lobby/RandomMapTab.cpp +++ b/client/lobby/RandomMapTab.cpp @@ -114,12 +114,12 @@ RandomMapTab::RandomMapTab(): GH.windows().createAndPushWindow(*this); }); - for(auto road : VLC->roadTypeHandler->objects) + for(const auto & road : VLC->roadTypeHandler->objects) { std::string cbRoadType = "selectRoad_" + road->getJsonKey(); - addCallback(cbRoadType, [&, road](bool on) + addCallback(cbRoadType, [&, roadID = road->getId()](bool on) { - mapGenOptions->setRoadEnabled(road->getId(), on); + mapGenOptions->setRoadEnabled(roadID, on); updateMapInfoByHost(); }); } @@ -372,7 +372,7 @@ void RandomMapTab::setMapGenOptions(std::shared_ptr opts) else w->setTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE); } - for(auto r : VLC->roadTypeHandler->objects) + for(const auto & r : VLC->roadTypeHandler->objects) { // Workaround for vcmi-extras bug std::string jsonKey = r->getJsonKey(); @@ -605,4 +605,4 @@ void RandomMapTab::loadOptions() updateMapInfoByHost(); // TODO: Save & load difficulty? -} \ No newline at end of file +} diff --git a/client/widgets/CComponent.cpp b/client/widgets/CComponent.cpp index 043dde6fa..5d3497840 100644 --- a/client/widgets/CComponent.cpp +++ b/client/widgets/CComponent.cpp @@ -222,15 +222,15 @@ std::string CComponent::getDescription() const case ComponentType::CREATURE: return ""; case ComponentType::ARTIFACT: - return VLC->artifacts()->getById(data.subType.as())->getDescriptionTranslated(); + return CGI->artifacts()->getById(data.subType.as())->getDescriptionTranslated(); case ComponentType::SPELL_SCROLL: { - auto description = VLC->arth->objects[ArtifactID::SPELL_SCROLL]->getDescriptionTranslated(); + auto description = ArtifactID(ArtifactID::SPELL_SCROLL).toEntity(CGI)->getDescriptionTranslated(); ArtifactUtils::insertScrrollSpellName(description, data.subType.as()); return description; } case ComponentType::SPELL: - return VLC->spells()->getById(data.subType.as())->getDescriptionTranslated(data.value.value_or(0)); + return CGI->spells()->getById(data.subType.as())->getDescriptionTranslated(data.value.value_or(0)); case ComponentType::MORALE: return CGI->generaltexth->heroscrn[ 4 - (data.value.value_or(0)>0) + (data.value.value_or(0)<0)]; case ComponentType::LUCK: diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 2fbd723c2..e27f20223 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -272,7 +272,7 @@ CDwellingInfoBox::CDwellingInfoBox(int centerX, int centerY, const CGTownInstanc OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE); background->colorize(Town->tempOwner); - const CCreature * creature = CGI->creh->objects.at(Town->creatures.at(level).second.back()); + const CCreature * creature = Town->creatures.at(level).second.back().toCreature(); title = std::make_shared(80, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, creature->getNamePluralTranslated()); animation = std::make_shared(30, 44, creature, true, true); @@ -1049,10 +1049,9 @@ CCreaInfo::CCreaInfo(Point position, const CGTownInstance * Town, int Level, boo } addUsedEvents(LCLICK | SHOW_POPUP | HOVER); - ui32 creatureID = town->creatures[level].second.back(); - creature = CGI->creh->objects[creatureID]; + CreatureID creatureID = town->creatures[level].second.back(); - picture = std::make_shared(AnimationPath::builtin("CPRSMALL"), creature->getIconIndex(), 0, 8, 0); + picture = std::make_shared(AnimationPath::builtin("CPRSMALL"), creatureID.toEntity(VLC)->getIconIndex(), 0, 8, 0); std::string value; if(showAvailable) @@ -1802,9 +1801,9 @@ CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance * const CCreature * CFortScreen::RecruitArea::getMyCreature() { if(!town->creatures.at(level).second.empty()) // built - return VLC->creh->objects[town->creatures.at(level).second.back()]; + return town->creatures.at(level).second.back().toCreature(); if(!town->town->creatures.at(level).empty()) // there are creatures on this level - return VLC->creh->objects[town->town->creatures.at(level).front()]; + return town->town->creatures.at(level).front().toCreature(); return nullptr; } @@ -1888,7 +1887,7 @@ CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner, const ImagePath & i for(size_t j=0; jtown->mageGuildLevel() && owner->town->spells[i].size()>j) - spells.push_back(std::make_shared(positions[i][j], CGI->spellh->objects[owner->town->spells[i][j]])); + spells.push_back(std::make_shared(positions[i][j], owner->town->spells[i][j].toSpell())); else emptyScrolls.push_back(std::make_shared(AnimationPath::builtin("TPMAGES.DEF"), 1, 0, positions[i][j].x, positions[i][j].y)); } @@ -1938,7 +1937,7 @@ CBlacksmithDialog::CBlacksmithDialog(bool possible, CreatureID creMachineID, Art animBG = std::make_shared(ImagePath::builtin("TPSMITBK"), 64, 50); animBG->needRefresh = true; - const CCreature * creature = CGI->creh->objects[creMachineID]; + const CCreature * creature = creMachineID.toCreature(); anim = std::make_shared(64, 50, creature->animDefName); anim->clipRect(113,125,200,150); diff --git a/client/windows/CHeroOverview.cpp b/client/windows/CHeroOverview.cpp index 2a5412dc1..390d94674 100644 --- a/client/windows/CHeroOverview.cpp +++ b/client/windows/CHeroOverview.cpp @@ -124,7 +124,7 @@ void CHeroOverview::genControls() r = Rect(302, 3 * borderOffset + yOffset + 62, 292, 32); backgroundRectangles.push_back(std::make_shared(r.resize(1), rectangleColor, borderColor)); - auto stacksCountChances = VLC->settings()->getVector(EGameSettings::HEROES_STARTING_STACKS_CHANCES); + auto stacksCountChances = CGI->settings()->getVector(EGameSettings::HEROES_STARTING_STACKS_CHANCES); // army int space = (260 - 7 * 32) / 6; diff --git a/client/windows/CSpellWindow.cpp b/client/windows/CSpellWindow.cpp index d752c24af..8a0d800d4 100644 --- a/client/windows/CSpellWindow.cpp +++ b/client/windows/CSpellWindow.cpp @@ -290,11 +290,11 @@ void CSpellWindow::processSpells() //initializing castable spells mySpells.reserve(CGI->spellh->objects.size()); - for(const CSpell * spell : CGI->spellh->objects) + for(auto const & spell : CGI->spellh->objects) { bool searchTextFound = !searchBox || boost::algorithm::contains(boost::algorithm::to_lower_copy(spell->getNameTranslated()), boost::algorithm::to_lower_copy(searchBox->getText())); - if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell) && searchTextFound) - mySpells.push_back(spell); + if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell.get()) && searchTextFound) + mySpells.push_back(spell.get()); } SpellbookSpellSorter spellsorter; diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 1bd07ffb4..48ea11681 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -261,7 +261,7 @@ void CRecruitmentWindow::availableCreaturesChanged() //create new cards for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second)) - cards.push_back(std::make_shared(this, CGI->creh->objects[creature], amount)); + cards.push_back(std::make_shared(this, creature.toCreature(), amount)); } const int creatureWidth = 102; diff --git a/include/vcmi/Entity.h b/include/vcmi/Entity.h index eded843b7..937049f2e 100644 --- a/include/vcmi/Entity.h +++ b/include/vcmi/Entity.h @@ -30,7 +30,7 @@ public: virtual bool isNativeTerrain(TerrainId terrain) const; }; -class DLL_LINKAGE Entity +class DLL_LINKAGE Entity : boost::noncopyable { public: using IconRegistar = std::function; diff --git a/include/vcmi/Services.h b/include/vcmi/Services.h index ea7977331..55abbd41e 100644 --- a/include/vcmi/Services.h +++ b/include/vcmi/Services.h @@ -61,8 +61,6 @@ public: virtual const ObstacleService * obstacles() const = 0; virtual const IGameSettings * settings() const = 0; - virtual void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) = 0; - virtual const spells::effects::Registry * spellEffects() const = 0; virtual spells::effects::Registry * spellEffects() = 0; //TODO: put map object types registry access here diff --git a/lib/BattleFieldHandler.cpp b/lib/BattleFieldHandler.cpp index 4d1eb5591..7482eff5a 100644 --- a/lib/BattleFieldHandler.cpp +++ b/lib/BattleFieldHandler.cpp @@ -15,11 +15,11 @@ VCMI_LIB_NAMESPACE_BEGIN -BattleFieldInfo * BattleFieldHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) +std::shared_ptr BattleFieldHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); - auto * info = new BattleFieldInfo(BattleField(index), identifier); + auto info = std::make_shared(BattleField(index), identifier); info->modScope = scope; info->graphics = ImagePath::fromJson(json["graphics"]); diff --git a/lib/BattleFieldHandler.h b/lib/BattleFieldHandler.h index b338cd772..ca2ddc8ce 100644 --- a/lib/BattleFieldHandler.h +++ b/lib/BattleFieldHandler.h @@ -64,7 +64,7 @@ public: class BattleFieldHandler : public CHandlerBase { public: - virtual BattleFieldInfo * loadFromJson( + std::shared_ptr loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 89668a9cb..1c0467d4a 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -373,7 +373,7 @@ std::vector CArtHandler::loadLegacyData() void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data) { - auto * object = loadFromJson(scope, data, name, objects.size()); + auto object = loadFromJson(scope, data, name, objects.size()); object->iconIndex = object->getIndex() + 5; @@ -384,7 +384,7 @@ void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) { - auto * object = loadFromJson(scope, data, name, index); + auto object = loadFromJson(scope, data, name, index); object->iconIndex = object->getIndex(); @@ -400,12 +400,12 @@ const std::vector & CArtHandler::getTypeNames() const return typeNames; } -CArtifact * CArtHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) +std::shared_ptr CArtHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); assert(!scope.empty()); - CArtifact * art = new CArtifact(); + auto art = std::make_shared(); if(!node["growing"].isNull()) { for(auto bonus : node["growing"]["bonusesPerLevel"].Vector()) @@ -442,10 +442,10 @@ CArtifact * CArtHandler::loadFromJson(const std::string & scope, const JsonNode art->price = static_cast(node["value"].Float()); art->onlyOnWaterMap = node["onlyOnWaterMap"].Bool(); - loadSlots(art, node); - loadClass(art, node); - loadType(art, node); - loadComponents(art, node); + loadSlots(art.get(), node); + loadClass(art.get(), node); + loadType(art.get(), node); + loadComponents(art.get(), node); for(const auto & b : node["bonuses"].Vector()) { @@ -663,7 +663,7 @@ std::set CArtHandler::getDefaultAllowed() const { std::set allowedArtifacts; - for (auto artifact : objects) + for (const auto & artifact : objects) { if (!artifact->isCombined()) allowedArtifacts.insert(artifact->getId()); diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 4eca1aada..124713299 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -14,6 +14,7 @@ #include "bonuses/Bonus.h" #include "bonuses/CBonusSystemNode.h" +#include "ConstTransitivePtr.h" #include "GameConstants.h" #include "IHandlerBase.h" #include "serializer/Serializeable.h" @@ -163,7 +164,7 @@ public: protected: const std::vector & getTypeNames() const override; - CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; private: void addSlot(CArtifact * art, const std::string & slotID) const; diff --git a/lib/CBonusTypeHandler.cpp b/lib/CBonusTypeHandler.cpp index 58bbd0b9b..9fc92572a 100644 --- a/lib/CBonusTypeHandler.cpp +++ b/lib/CBonusTypeHandler.cpp @@ -20,6 +20,7 @@ #include "CGeneralTextHandler.h" #include "json/JsonUtils.h" #include "spells/CSpellHandler.h" +#include "VCMI_Lib.h" template class std::vector; diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index 0e6293449..bb8885e37 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -587,12 +587,12 @@ std::vector CCreatureHandler::loadLegacyData() return h3Data; } -CCreature * CCreatureHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) +std::shared_ptr CCreatureHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); assert(!scope.empty()); - auto * cre = new CCreature(); + auto cre = std::make_shared(); if(node["hasDoubleWeek"].Bool()) { @@ -635,9 +635,9 @@ CCreature * CCreatureHandler::loadFromJson(const std::string & scope, const Json if(!node["shots"].isNull()) cre->addBonus(node["shots"].Integer(), BonusType::SHOTS); - loadStackExperience(cre, node["stackExperience"]); - loadJsonAnimation(cre, node["graphics"]); - loadCreatureJson(cre, node); + loadStackExperience(cre.get(), node["stackExperience"]); + loadJsonAnimation(cre.get(), node["graphics"]); + loadCreatureJson(cre.get(), node); for(const auto & extraName : node["extraNames"].Vector()) { diff --git a/lib/CCreatureHandler.h b/lib/CCreatureHandler.h index 7dc897389..b96f5eb7c 100644 --- a/lib/CCreatureHandler.h +++ b/lib/CCreatureHandler.h @@ -209,7 +209,7 @@ private: protected: const std::vector & getTypeNames() const override; - CCreature * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; public: std::set doubledCreatures; //they get double week diff --git a/lib/CCreatureSet.h b/lib/CCreatureSet.h index 27a056fe3..51fbbe2ed 100644 --- a/lib/CCreatureSet.h +++ b/lib/CCreatureSet.h @@ -16,6 +16,7 @@ #include "CArtHandler.h" #include "CArtifactInstance.h" #include "CCreatureHandler.h" +#include "VCMI_Lib.h" #include diff --git a/lib/CGameInfoCallback.cpp b/lib/CGameInfoCallback.cpp index 090059a7d..f1d48b59f 100644 --- a/lib/CGameInfoCallback.cpp +++ b/lib/CGameInfoCallback.cpp @@ -370,7 +370,7 @@ bool CGameInfoCallback::getHeroInfo(const CGObjectInstance * hero, InfoAboutHero int maxAIValue = 0; const CCreature * mostStrong = nullptr; - for(auto creature : VLC->creh->objects) + for(const auto & creature : VLC->creh->objects) { if(creature->getFaction() == factionIndex && static_cast(creature->getAIValue()) > maxAIValue) { diff --git a/lib/CHeroHandler.cpp b/lib/CHeroHandler.cpp index e73f9bd0b..8485d4671 100644 --- a/lib/CHeroHandler.cpp +++ b/lib/CHeroHandler.cpp @@ -247,14 +247,14 @@ const std::vector & CHeroClassHandler::getTypeNames() const return typeNames; } -CHeroClass * CHeroClassHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) +std::shared_ptr CHeroClassHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); assert(!scope.empty()); std::string affinityStr[2] = { "might", "magic" }; - auto * heroClass = new CHeroClass(); + auto heroClass = std::make_shared(); heroClass->id = HeroClassID(index); heroClass->identifier = identifier; @@ -277,10 +277,10 @@ CHeroClass * CHeroClassHandler::loadFromJson(const std::string & scope, const Js heroClass->affinity = CHeroClass::MIGHT; } - fillPrimarySkillData(node, heroClass, PrimarySkill::ATTACK); - fillPrimarySkillData(node, heroClass, PrimarySkill::DEFENSE); - fillPrimarySkillData(node, heroClass, PrimarySkill::SPELL_POWER); - fillPrimarySkillData(node, heroClass, PrimarySkill::KNOWLEDGE); + fillPrimarySkillData(node, heroClass.get(), PrimarySkill::ATTACK); + fillPrimarySkillData(node, heroClass.get(), PrimarySkill::DEFENSE); + fillPrimarySkillData(node, heroClass.get(), PrimarySkill::SPELL_POWER); + fillPrimarySkillData(node, heroClass.get(), PrimarySkill::KNOWLEDGE); auto percentSumm = std::accumulate(heroClass->primarySkillLowLevel.begin(), heroClass->primarySkillLowLevel.end(), 0); if(percentSumm <= 0) @@ -432,12 +432,12 @@ const std::vector & CHeroHandler::getTypeNames() const return typeNames; } -CHero * CHeroHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) +std::shared_ptr CHeroHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); assert(!scope.empty()); - auto * hero = new CHero(); + auto hero = std::make_shared(); hero->ID = HeroTypeID(index); hero->identifier = identifier; hero->modScope = scope; @@ -459,9 +459,9 @@ CHero * CHeroHandler::loadFromJson(const std::string & scope, const JsonNode & n hero->portraitLarge = node["images"]["large"].String(); hero->battleImage = AnimationPath::fromJson(node["battleImage"]); - loadHeroArmy(hero, node); - loadHeroSkills(hero, node); - loadHeroSpecialty(hero, node); + loadHeroArmy(hero.get(), node); + loadHeroSkills(hero.get(), node); + loadHeroSpecialty(hero.get(), node); VLC->identifiers()->requestIdentifier("heroClass", node["class"], [=](si32 classID) @@ -753,7 +753,7 @@ void CHeroHandler::loadObject(std::string scope, std::string name, const JsonNod { size_t index = objects.size(); static const int specialFramesCount = 2; // reserved for 2 special frames - auto * object = loadFromJson(scope, data, name, index); + auto object = loadFromJson(scope, data, name, index); object->imageIndex = static_cast(index) + specialFramesCount; objects.emplace_back(object); @@ -766,7 +766,7 @@ void CHeroHandler::loadObject(std::string scope, std::string name, const JsonNod void CHeroHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) { - auto * object = loadFromJson(scope, data, name, index); + auto object = loadFromJson(scope, data, name, index); object->imageIndex = static_cast(index); assert(objects[index] == nullptr); // ensure that this id was not loaded before diff --git a/lib/CHeroHandler.h b/lib/CHeroHandler.h index 1b066d029..4ebe01981 100644 --- a/lib/CHeroHandler.h +++ b/lib/CHeroHandler.h @@ -171,7 +171,7 @@ public: protected: const std::vector & getTypeNames() const override; - CHeroClass * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; }; @@ -209,7 +209,7 @@ public: protected: const std::vector & getTypeNames() const override; - CHero * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; }; VCMI_LIB_NAMESPACE_END diff --git a/lib/CSkillHandler.cpp b/lib/CSkillHandler.cpp index 9db6f7362..2a58f22e7 100644 --- a/lib/CSkillHandler.cpp +++ b/lib/CSkillHandler.cpp @@ -22,6 +22,7 @@ #include "modding/ModUtility.h" #include "modding/ModScope.h" #include "constants/StringConstants.h" +#include "VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN @@ -190,7 +191,7 @@ const std::vector & CSkillHandler::getTypeNames() const return typeNames; } -CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) +std::shared_ptr CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); assert(!scope.empty()); @@ -199,7 +200,7 @@ CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & major = json["obligatoryMajor"].Bool(); minor = json["obligatoryMinor"].Bool(); - auto * skill = new CSkill(SecondarySkill((si32)index), identifier, major, minor); + auto skill = std::make_shared(SecondarySkill((si32)index), identifier, major, minor); skill->modScope = scope; skill->onlyOnWaterMap = json["onlyOnWaterMap"].Bool(); diff --git a/lib/CSkillHandler.h b/lib/CSkillHandler.h index fe699edcb..5edaf73e5 100644 --- a/lib/CSkillHandler.h +++ b/lib/CSkillHandler.h @@ -94,7 +94,7 @@ public: protected: const std::vector & getTypeNames() const override; - CSkill * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; }; VCMI_LIB_NAMESPACE_END diff --git a/lib/CTownHandler.cpp b/lib/CTownHandler.cpp index b65af0271..f9e0e1473 100644 --- a/lib/CTownHandler.cpp +++ b/lib/CTownHandler.cpp @@ -1036,11 +1036,11 @@ void CTownHandler::loadPuzzle(CFaction &faction, const JsonNode &source) const assert(faction.puzzleMap.size() == GameConstants::PUZZLE_MAP_PIECES); } -CFaction * CTownHandler::loadFromJson(const std::string & scope, const JsonNode & source, const std::string & identifier, size_t index) +std::shared_ptr CTownHandler::loadFromJson(const std::string & scope, const JsonNode & source, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); - auto * faction = new CFaction(); + auto faction = std::make_shared(); faction->index = static_cast(index); faction->modScope = scope; @@ -1091,7 +1091,7 @@ CFaction * CTownHandler::loadFromJson(const std::string & scope, const JsonNode if (!source["town"].isNull()) { faction->town = new CTown(); - faction->town->faction = faction; + faction->town->faction = faction.get(); loadTown(faction->town, source["town"]); } else @@ -1105,7 +1105,7 @@ CFaction * CTownHandler::loadFromJson(const std::string & scope, const JsonNode void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data) { - auto * object = loadFromJson(scope, data, name, objects.size()); + auto object = loadFromJson(scope, data, name, objects.size()); objects.emplace_back(object); @@ -1144,7 +1144,7 @@ void CTownHandler::loadObject(std::string scope, std::string name, const JsonNod void CTownHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) { - auto * object = loadFromJson(scope, data, name, index); + auto object = loadFromJson(scope, data, name, index); if (objects.size() > index) assert(objects[index] == nullptr); // ensure that this id was not loaded before @@ -1260,7 +1260,7 @@ std::set CTownHandler::getDefaultAllowed() const { std::set allowedFactions; - for(auto town : objects) + for(const auto & town : objects) if (town->town != nullptr && !town->special) allowedFactions.insert(town->getId()); @@ -1273,7 +1273,7 @@ std::set CTownHandler::getAllowedFactions(bool withTown) const return getDefaultAllowed(); std::set result; - for(auto town : objects) + for(const auto & town : objects) result.insert(town->getId()); return result; diff --git a/lib/CTownHandler.h b/lib/CTownHandler.h index f755d59e4..f3101c7c6 100644 --- a/lib/CTownHandler.h +++ b/lib/CTownHandler.h @@ -352,7 +352,7 @@ public: protected: const std::vector & getTypeNames() const override; - CFaction * loadFromJson(const std::string & scope, const JsonNode & data, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & data, const std::string & identifier, size_t index) override; }; VCMI_LIB_NAMESPACE_END diff --git a/lib/IHandlerBase.cpp b/lib/IHandlerBase.cpp index 1512c0680..a82c2b97e 100644 --- a/lib/IHandlerBase.cpp +++ b/lib/IHandlerBase.cpp @@ -13,6 +13,7 @@ #include "modding/IdentifierStorage.h" #include "modding/ModScope.h" #include "modding/CModHandler.h" +#include "VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/IHandlerBase.h b/lib/IHandlerBase.h index 1666acee1..022617de9 100644 --- a/lib/IHandlerBase.h +++ b/lib/IHandlerBase.h @@ -9,9 +9,6 @@ */ #pragma once -#include "../lib/ConstTransitivePtr.h" -#include "VCMI_Lib.h" - VCMI_LIB_NAMESPACE_BEGIN class JsonNode; @@ -60,13 +57,7 @@ template } public: - virtual ~CHandlerBase() - { - for(auto & o : objects) - { - o.dellNull(); - } - } + using ObjectPtr = std::shared_ptr<_Object>; const Entity * getBaseByIndex(const int32_t index) const override { @@ -95,23 +86,19 @@ public: void loadObject(std::string scope, std::string name, const JsonNode & data) override { - auto object = loadFromJson(scope, data, name, objects.size()); - - objects.push_back(object); + objects.push_back(loadFromJson(scope, data, name, objects.size())); for(const auto & type_name : getTypeNames()) - registerObject(scope, type_name, name, object->getIndex()); + registerObject(scope, type_name, name, objects.back()->getIndex()); } void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override { - auto object = loadFromJson(scope, data, name, index); - assert(objects[index] == nullptr); // ensure that this id was not loaded before - objects[index] = object; + objects[index] = loadFromJson(scope, data, name, index); for(const auto & type_name : getTypeNames()) - registerObject(scope, type_name, name, object->getIndex()); + registerObject(scope, type_name, name, objects[index]->getIndex()); } const _Object * operator[] (const _ObjectID id) const @@ -124,25 +111,13 @@ public: return getObjectImpl(index); } - void updateEntity(int32_t index, const JsonNode & data) - { - if(index < 0 || index >= objects.size()) - { - logMod->error("%s id %d is invalid", getTypeNames()[0], index); - } - else - { - objects.at(index)->updateFrom(data); - } - } - size_t size() const { return objects.size(); } protected: - virtual _Object * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0; + virtual ObjectPtr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0; virtual const std::vector & getTypeNames() const = 0; template @@ -159,7 +134,7 @@ protected: } public: //todo: make private - std::vector> objects; + std::vector objects; }; VCMI_LIB_NAMESPACE_END diff --git a/lib/ObstacleHandler.cpp b/lib/ObstacleHandler.cpp index d7b613959..cf66a7dda 100644 --- a/lib/ObstacleHandler.cpp +++ b/lib/ObstacleHandler.cpp @@ -12,6 +12,7 @@ #include "BattleFieldHandler.h" #include "json/JsonNode.h" #include "modding/IdentifierStorage.h" +#include "VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN @@ -84,11 +85,11 @@ bool ObstacleInfo::isAppropriate(const TerrainId terrainType, const BattleField return vstd::contains(allowedTerrains, terrainType); } -ObstacleInfo * ObstacleHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) +std::shared_ptr ObstacleHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); - auto * info = new ObstacleInfo(Obstacle(index), identifier); + auto info = std::make_shared(Obstacle(index), identifier); info->animation = AnimationPath::fromJson(json["animation"]); info->width = json["width"].Integer(); diff --git a/lib/ObstacleHandler.h b/lib/ObstacleHandler.h index 840016542..10e66d8be 100644 --- a/lib/ObstacleHandler.h +++ b/lib/ObstacleHandler.h @@ -64,8 +64,8 @@ public: class ObstacleHandler: public CHandlerBase { -public: - ObstacleInfo * loadFromJson(const std::string & scope, +public: + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; diff --git a/lib/RiverHandler.cpp b/lib/RiverHandler.cpp index 7ed62a7a5..b000e8427 100644 --- a/lib/RiverHandler.cpp +++ b/lib/RiverHandler.cpp @@ -13,17 +13,18 @@ #include "CGeneralTextHandler.h" #include "GameSettings.h" #include "json/JsonNode.h" +#include "VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN RiverTypeHandler::RiverTypeHandler() { - objects.push_back(new RiverType()); + objects.emplace_back(new RiverType()); VLC->generaltexth->registerString("core", objects[0]->getNameTextID(), ""); } -RiverType * RiverTypeHandler::loadFromJson( +std::shared_ptr RiverTypeHandler::loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, @@ -31,7 +32,7 @@ RiverType * RiverTypeHandler::loadFromJson( { assert(identifier.find(':') == std::string::npos); - auto * info = new RiverType; + auto info = std::make_shared(); info->id = RiverId(index); info->identifier = identifier; diff --git a/lib/RiverHandler.h b/lib/RiverHandler.h index c13736e76..fe165a13c 100644 --- a/lib/RiverHandler.h +++ b/lib/RiverHandler.h @@ -61,7 +61,7 @@ public: class DLL_LINKAGE RiverTypeHandler : public CHandlerBase { public: - virtual RiverType * loadFromJson( + std::shared_ptr loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, diff --git a/lib/RoadHandler.cpp b/lib/RoadHandler.cpp index 5ebbe54f1..fbcee0653 100644 --- a/lib/RoadHandler.cpp +++ b/lib/RoadHandler.cpp @@ -13,17 +13,18 @@ #include "CGeneralTextHandler.h" #include "GameSettings.h" #include "json/JsonNode.h" +#include "VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN RoadTypeHandler::RoadTypeHandler() { - objects.push_back(new RoadType()); + objects.emplace_back(new RoadType()); VLC->generaltexth->registerString("core", objects[0]->getNameTextID(), ""); } -RoadType * RoadTypeHandler::loadFromJson( +std::shared_ptr RoadTypeHandler::loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, @@ -31,7 +32,7 @@ RoadType * RoadTypeHandler::loadFromJson( { assert(identifier.find(':') == std::string::npos); - auto * info = new RoadType; + auto info = std::make_shared(); info->id = RoadId(index); info->identifier = identifier; diff --git a/lib/RoadHandler.h b/lib/RoadHandler.h index 5f31d1858..c9b3e857a 100644 --- a/lib/RoadHandler.h +++ b/lib/RoadHandler.h @@ -51,7 +51,7 @@ public: class DLL_LINKAGE RoadTypeHandler : public CHandlerBase { public: - virtual RoadType * loadFromJson( + std::shared_ptr loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, diff --git a/lib/TerrainHandler.cpp b/lib/TerrainHandler.cpp index 6980d98b8..d66bf521d 100644 --- a/lib/TerrainHandler.cpp +++ b/lib/TerrainHandler.cpp @@ -14,14 +14,15 @@ #include "GameSettings.h" #include "json/JsonNode.h" #include "modding/IdentifierStorage.h" +#include "VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN -TerrainType * TerrainTypeHandler::loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) +std::shared_ptr TerrainTypeHandler::loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); - auto * info = new TerrainType; + auto info = std::make_shared(); info->id = TerrainId(index); info->identifier = identifier; diff --git a/lib/TerrainHandler.h b/lib/TerrainHandler.h index 59c6ab62b..ae2bed46d 100644 --- a/lib/TerrainHandler.h +++ b/lib/TerrainHandler.h @@ -101,7 +101,7 @@ public: class DLL_LINKAGE TerrainTypeHandler : public CHandlerBase { public: - virtual TerrainType * loadFromJson( + std::shared_ptr loadFromJson( const std::string & scope, const JsonNode & json, const std::string & identifier, diff --git a/lib/VCMI_Lib.cpp b/lib/VCMI_Lib.cpp index 885f4c4c0..6a1a84fd6 100644 --- a/lib/VCMI_Lib.cpp +++ b/lib/VCMI_Lib.cpp @@ -141,37 +141,6 @@ const IGameSettings * LibClasses::settings() const return settingsHandler.get(); } -void LibClasses::updateEntity(Metatype metatype, int32_t index, const JsonNode & data) -{ - switch(metatype) - { - case Metatype::ARTIFACT: - arth->updateEntity(index, data); - break; - case Metatype::CREATURE: - creh->updateEntity(index, data); - break; - case Metatype::FACTION: - townh->updateEntity(index, data); - break; - case Metatype::HERO_CLASS: - heroclassesh->updateEntity(index, data); - break; - case Metatype::HERO_TYPE: - heroh->updateEntity(index, data); - break; - case Metatype::SKILL: - skillh->updateEntity(index, data); - break; - case Metatype::SPELL: - spellh->updateEntity(index, data); - break; - default: - logGlobal->error("Invalid Metatype id %d", static_cast(metatype)); - break; - } -} - void LibClasses::loadFilesystem(bool extractArchives) { CStopWatch loadTime; diff --git a/lib/VCMI_Lib.h b/lib/VCMI_Lib.h index fe5798150..927ff5e4e 100644 --- a/lib/VCMI_Lib.h +++ b/lib/VCMI_Lib.h @@ -72,8 +72,6 @@ public: const ObstacleService * obstacles() const override; const IGameSettings * settings() const override; - void updateEntity(Metatype metatype, int32_t index, const JsonNode & data) override; - const spells::effects::Registry * spellEffects() const override; spells::effects::Registry * spellEffects() override; diff --git a/lib/battle/CObstacleInstance.cpp b/lib/battle/CObstacleInstance.cpp index ccf01f07c..3c018c919 100644 --- a/lib/battle/CObstacleInstance.cpp +++ b/lib/battle/CObstacleInstance.cpp @@ -107,7 +107,6 @@ SpellID CObstacleInstance::getTrigger() const void CObstacleInstance::serializeJson(JsonSerializeFormat & handler) { - auto obstacleInfo = getInfo(); auto hidden = false; auto needAnimationOffsetFix = obstacleType == CObstacleInstance::USUAL; int animationYOffset = 0; @@ -117,11 +116,7 @@ void CObstacleInstance::serializeJson(JsonSerializeFormat & handler) //We need only a subset of obstacle info for correct render handler.serializeInt("position", pos); - handler.serializeStruct("appearSound", obstacleInfo.appearSound); - handler.serializeStruct("appearAnimation", obstacleInfo.appearAnimation); - handler.serializeStruct("animation", obstacleInfo.animation); handler.serializeInt("animationYOffset", animationYOffset); - handler.serializeBool("hidden", hidden); handler.serializeBool("needAnimationOffsetFix", needAnimationOffsetFix); } diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 054bde39f..756ff8943 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -282,7 +282,7 @@ void CGameState::updateEntity(Metatype metatype, int32_t index, const JsonNode & } break; default: - services->updateEntity(metatype, index, data); + logGlobal->error("This metatype update is not implemented"); break; } } diff --git a/lib/mapObjects/ObstacleSetHandler.cpp b/lib/mapObjects/ObstacleSetHandler.cpp index 76baa9918..aef9461a8 100644 --- a/lib/mapObjects/ObstacleSetHandler.cpp +++ b/lib/mapObjects/ObstacleSetHandler.cpp @@ -14,6 +14,7 @@ #include "../modding/IdentifierStorage.h" #include "../constants/StringConstants.h" #include "../TerrainHandler.h" +#include "../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/mapping/CDrawRoadsOperation.cpp b/lib/mapping/CDrawRoadsOperation.cpp index 9ea49db26..2cf1bbc94 100644 --- a/lib/mapping/CDrawRoadsOperation.cpp +++ b/lib/mapping/CDrawRoadsOperation.cpp @@ -15,6 +15,7 @@ #include "../CRandomGenerator.h" #include "../RoadHandler.h" #include "../RiverHandler.h" +#include "../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/mapping/MapEditUtils.cpp b/lib/mapping/MapEditUtils.cpp index d0691e082..cee52194f 100644 --- a/lib/mapping/MapEditUtils.cpp +++ b/lib/mapping/MapEditUtils.cpp @@ -13,6 +13,7 @@ #include "../filesystem/Filesystem.h" #include "../TerrainHandler.h" +#include "../VCMI_Lib.h" #include "CMap.h" #include "CMapOperation.h" diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index 26e7c5bb2..a6ffd69c4 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -296,9 +296,9 @@ void CMapFormatJson::serializeAllowedFactions(JsonSerializeFormat & handler, std if(handler.saving) { - for(auto faction : VLC->townh->objects) - if(faction->town && vstd::contains(value, faction->getId())) - temp.insert(faction->getId()); + for(auto const factionID : VLC->townh->getDefaultAllowed()) + if(vstd::contains(value, factionID)) + temp.insert(factionID); } handler.serializeLIC("allowedFactions", &FactionID::decode, &FactionID::encode, VLC->townh->getDefaultAllowed(), temp); diff --git a/lib/mapping/ObstacleProxy.cpp b/lib/mapping/ObstacleProxy.cpp index 88cd97f73..e1f9fe024 100644 --- a/lib/mapping/ObstacleProxy.cpp +++ b/lib/mapping/ObstacleProxy.cpp @@ -16,6 +16,7 @@ #include "../mapObjects/CGObjectInstance.h" #include "../mapObjects/ObjectTemplate.h" #include "../mapObjects/ObstacleSetHandler.h" +#include "../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/modding/CModHandler.cpp b/lib/modding/CModHandler.cpp index 6bbec82ca..04e066d4a 100644 --- a/lib/modding/CModHandler.cpp +++ b/lib/modding/CModHandler.cpp @@ -27,6 +27,7 @@ #include "../filesystem/Filesystem.h" #include "../json/JsonUtils.h" #include "../spells/CSpellHandler.h" +#include "../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/modding/ContentTypeHandler.cpp b/lib/modding/ContentTypeHandler.cpp index 102e037c7..a14b81d42 100644 --- a/lib/modding/ContentTypeHandler.cpp +++ b/lib/modding/ContentTypeHandler.cpp @@ -36,6 +36,7 @@ #include "../mapObjectConstructors/CObjectClassesHandler.h" #include "../rmg/CRmgTemplateStorage.h" #include "../spells/CSpellHandler.h" +#include "../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/rmg/CMapGenerator.cpp b/lib/rmg/CMapGenerator.cpp index 34ef5fe7d..8bf067f49 100644 --- a/lib/rmg/CMapGenerator.cpp +++ b/lib/rmg/CMapGenerator.cpp @@ -100,8 +100,9 @@ const CMapGenOptions& CMapGenerator::getMapGenOptions() const void CMapGenerator::initQuestArtsRemaining() { //TODO: Move to QuestArtifactPlacer? - for (auto art : VLC->arth->objects) + for (auto artID : VLC->arth->getDefaultAllowed()) { + auto art = artID.toArtifact(); //Don't use parts of combined artifacts if (art->aClass == CArtifact::ART_TREASURE && VLC->arth->legalArtifact(art->getId()) && art->getPartOf().empty()) questArtifacts.push_back(art->getId()); diff --git a/lib/rmg/CRmgTemplate.cpp b/lib/rmg/CRmgTemplate.cpp index ac7a491e3..7e79f9f4d 100644 --- a/lib/rmg/CRmgTemplate.cpp +++ b/lib/rmg/CRmgTemplate.cpp @@ -176,7 +176,7 @@ void ZoneOptions::setTerrainTypes(const std::set & value) std::set ZoneOptions::getDefaultTerrainTypes() const { std::set terrains; - for (auto terrain : VLC->terrainTypeHandler->objects) + for(const auto & terrain : VLC->terrainTypeHandler->objects) { if (terrain->isLand() && terrain->isPassable()) { diff --git a/lib/rmg/CZonePlacer.cpp b/lib/rmg/CZonePlacer.cpp index 304089090..6ee45c8f2 100644 --- a/lib/rmg/CZonePlacer.cpp +++ b/lib/rmg/CZonePlacer.cpp @@ -16,6 +16,7 @@ #include "../TerrainHandler.h" #include "../mapping/CMap.h" #include "../mapping/CMapEditManager.h" +#include "../VCMI_Lib.h" #include "CMapGenOptions.h" #include "RmgMap.h" #include "Zone.h" diff --git a/lib/rmg/RmgMap.cpp b/lib/rmg/RmgMap.cpp index 1eaffb1e3..78485e404 100644 --- a/lib/rmg/RmgMap.cpp +++ b/lib/rmg/RmgMap.cpp @@ -16,6 +16,7 @@ #include "../mapping/CMapEditManager.h" #include "../mapping/CMap.h" #include "../CTownHandler.h" +#include "../VCMI_Lib.h" #include "modificators/ObjectManager.h" #include "modificators/RoadPlacer.h" #include "modificators/TreasurePlacer.h" diff --git a/lib/rmg/modificators/ObjectManager.cpp b/lib/rmg/modificators/ObjectManager.cpp index 849a94998..97c1af859 100644 --- a/lib/rmg/modificators/ObjectManager.cpp +++ b/lib/rmg/modificators/ObjectManager.cpp @@ -725,7 +725,7 @@ CGCreature * ObjectManager::chooseGuard(si32 strength, bool zoneGuard) CreatureID creId = CreatureID::NONE; int amount = 0; std::vector possibleCreatures; - for(auto cre : VLC->creh->objects) + for(auto const & cre : VLC->creh->objects) { if(cre->special) continue; diff --git a/lib/rmg/modificators/RiverPlacer.cpp b/lib/rmg/modificators/RiverPlacer.cpp index 0201ac008..303613803 100644 --- a/lib/rmg/modificators/RiverPlacer.cpp +++ b/lib/rmg/modificators/RiverPlacer.cpp @@ -20,6 +20,7 @@ #include "../../mapObjects/ObjectTemplate.h" #include "../../mapping/CMap.h" #include "../../mapping/CMapEditManager.h" +#include "../../VCMI_Lib.h" #include "../RmgPath.h" #include "ObjectManager.h" #include "ObstaclePlacer.h" diff --git a/lib/rmg/modificators/RoadPlacer.cpp b/lib/rmg/modificators/RoadPlacer.cpp index 3f74188a5..5ff18f011 100644 --- a/lib/rmg/modificators/RoadPlacer.cpp +++ b/lib/rmg/modificators/RoadPlacer.cpp @@ -21,6 +21,7 @@ #include "../../modding/IdentifierStorage.h" #include "../../modding/ModScope.h" #include "../../TerrainHandler.h" +#include "../../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/rmg/modificators/RockPlacer.cpp b/lib/rmg/modificators/RockPlacer.cpp index e3f68cdce..6798156bf 100644 --- a/lib/rmg/modificators/RockPlacer.cpp +++ b/lib/rmg/modificators/RockPlacer.cpp @@ -20,6 +20,7 @@ #include "../../TerrainHandler.h" #include "../../CRandomGenerator.h" #include "../../mapping/CMapEditManager.h" +#include "../../VCMI_Lib.h" #include "../TileInfo.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/rmg/modificators/TerrainPainter.cpp b/lib/rmg/modificators/TerrainPainter.cpp index 5d88871ee..3648fb8a8 100644 --- a/lib/rmg/modificators/TerrainPainter.cpp +++ b/lib/rmg/modificators/TerrainPainter.cpp @@ -76,7 +76,7 @@ void TerrainPainter::initTerrainType() { //Fill with all terain types by default { - for (auto terrain : VLC->terrainTypeHandler->objects) + for (const auto & terrain : VLC->terrainTypeHandler->objects) { if (terrain->isLand() && terrain->isPassable()) { diff --git a/lib/rmg/modificators/TownPlacer.cpp b/lib/rmg/modificators/TownPlacer.cpp index 984a0722c..dbfd93d21 100644 --- a/lib/rmg/modificators/TownPlacer.cpp +++ b/lib/rmg/modificators/TownPlacer.cpp @@ -77,11 +77,9 @@ void TownPlacer::placeTowns(ObjectManager & manager) town->builtBuildings.insert(BuildingID::FORT); town->builtBuildings.insert(BuildingID::DEFAULT); - for(auto spell : VLC->spellh->objects) //add all regular spells to town - { - if(!spell->isSpecial() && !spell->isCreatureAbility()) - town->possibleSpells.push_back(spell->id); - } + + for(auto spellID : VLC->spellh->getDefaultAllowed()) //add all regular spells to town + town->possibleSpells.push_back(spellID); auto position = placeMainTown(manager, *town); @@ -202,11 +200,8 @@ void TownPlacer::addNewTowns(int count, bool hasFort, const PlayerColor & player town->builtBuildings.insert(BuildingID::FORT); town->builtBuildings.insert(BuildingID::DEFAULT); - for(auto spell : VLC->spellh->objects) //add all regular spells to town - { - if(!spell->isSpecial() && !spell->isCreatureAbility()) - town->possibleSpells.push_back(spell->id); - } + for(auto spellID : VLC->spellh->getDefaultAllowed()) //add all regular spells to town + town->possibleSpells.push_back(spellID); if(totalTowns <= 0) { diff --git a/lib/rmg/modificators/TreasurePlacer.cpp b/lib/rmg/modificators/TreasurePlacer.cpp index 127c4d99c..fd44c5761 100644 --- a/lib/rmg/modificators/TreasurePlacer.cpp +++ b/lib/rmg/modificators/TreasurePlacer.cpp @@ -163,7 +163,7 @@ void TreasurePlacer::addAllPossibleObjects() oi.maxPerZone = std::numeric_limits::max(); std::vector creatures; //native creatures for this zone - for(auto cre : VLC->creh->objects) + for(auto const & cre : VLC->creh->objects) { if(!cre->special && cre->getFaction() == zone.getTownType()) { @@ -221,12 +221,10 @@ void TreasurePlacer::addAllPossibleObjects() auto * obj = dynamic_cast(factory->create(map.mapInstance->cb, nullptr)); std::vector out; - for(auto spell : VLC->spellh->objects) //spellh size appears to be greater (?) + for(auto spellID : VLC->spellh->getDefaultAllowed()) { - if(map.isAllowedSpell(spell->id) && spell->getLevel() == i + 1) - { - out.push_back(spell->id); - } + if(map.isAllowedSpell(spellID) && spellID.toSpell()->getLevel() == i + 1) + out.push_back(spellID); } auto * a = ArtifactUtils::createScroll(*RandomGeneratorUtil::nextItem(out, zone.getRand())); obj->storedArtifact = a; @@ -354,10 +352,10 @@ void TreasurePlacer::addAllPossibleObjects() auto * obj = dynamic_cast(factory->create(map.mapInstance->cb, nullptr)); std::vector spells; - for(auto spell : VLC->spellh->objects) + for(auto spellID : VLC->spellh->getDefaultAllowed()) { - if(map.isAllowedSpell(spell->id) && spell->getLevel() == i) - spells.push_back(spell.get()); + if(map.isAllowedSpell(spellID) && spellID.toSpell()->getLevel() == i) + spells.push_back(spellID.toSpell()); } RandomGeneratorUtil::randomShuffle(spells, zone.getRand()); @@ -387,10 +385,10 @@ void TreasurePlacer::addAllPossibleObjects() auto * obj = dynamic_cast(factory->create(map.mapInstance->cb, nullptr)); std::vector spells; - for(auto spell : VLC->spellh->objects) + for(auto spellID : VLC->spellh->getDefaultAllowed()) { - if(map.isAllowedSpell(spell->id) && spell->hasSchool(SpellSchool(i))) - spells.push_back(spell.get()); + if(map.isAllowedSpell(spellID) && spellID.toSpell()->hasSchool(SpellSchool(i))) + spells.push_back(spellID.toSpell()); } RandomGeneratorUtil::randomShuffle(spells, zone.getRand()); @@ -419,10 +417,10 @@ void TreasurePlacer::addAllPossibleObjects() auto * obj = dynamic_cast(factory->create(map.mapInstance->cb, nullptr)); std::vector spells; - for(auto spell : VLC->spellh->objects) + for(auto spellID : VLC->spellh->getDefaultAllowed()) { - if(map.isAllowedSpell(spell->id)) - spells.push_back(spell.get()); + if(map.isAllowedSpell(spellID)) + spells.push_back(spellID.toSpell()); } RandomGeneratorUtil::randomShuffle(spells, zone.getRand()); diff --git a/lib/rmg/threadpool/MapProxy.cpp b/lib/rmg/threadpool/MapProxy.cpp index af872e2c5..87cb42455 100644 --- a/lib/rmg/threadpool/MapProxy.cpp +++ b/lib/rmg/threadpool/MapProxy.cpp @@ -10,6 +10,7 @@ #include "MapProxy.h" #include "../../TerrainHandler.h" +#include "../../VCMI_Lib.h" VCMI_LIB_NAMESPACE_BEGIN diff --git a/lib/spells/CSpellHandler.cpp b/lib/spells/CSpellHandler.cpp index 679f1ec62..db8244aeb 100644 --- a/lib/spells/CSpellHandler.cpp +++ b/lib/spells/CSpellHandler.cpp @@ -676,7 +676,7 @@ const std::vector & CSpellHandler::getTypeNames() const return typeNames; } -CSpell * CSpellHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) +std::shared_ptr CSpellHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) { assert(identifier.find(':') == std::string::npos); assert(!scope.empty()); @@ -685,7 +685,7 @@ CSpell * CSpellHandler::loadFromJson(const std::string & scope, const JsonNode & SpellID id(static_cast(index)); - auto * spell = new CSpell(); + auto spell = std::make_shared(); spell->id = id; spell->identifier = identifier; spell->modScope = scope; diff --git a/lib/spells/CSpellHandler.h b/lib/spells/CSpellHandler.h index f8826e23d..b552765eb 100644 --- a/lib/spells/CSpellHandler.h +++ b/lib/spells/CSpellHandler.h @@ -355,7 +355,7 @@ public: protected: const std::vector & getTypeNames() const override; - CSpell * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; + std::shared_ptr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override; }; VCMI_LIB_NAMESPACE_END diff --git a/mapeditor/inspector/armywidget.cpp b/mapeditor/inspector/armywidget.cpp index cadd580f3..efaf0adbc 100644 --- a/mapeditor/inspector/armywidget.cpp +++ b/mapeditor/inspector/armywidget.cpp @@ -36,7 +36,7 @@ ArmyWidget::ArmyWidget(CArmedInstance & a, QWidget *parent) : for(int c = 0; c < VLC->creh->objects.size(); ++c) { - auto creature = VLC->creh->objects[c]; + auto const & creature = VLC->creh->objects[c]; uiSlots[i]->insertItem(c + 1, creature->getNamePluralTranslated().c_str()); uiSlots[i]->setItemData(c + 1, creature->getIndex()); } diff --git a/mapeditor/inspector/heroskillswidget.cpp b/mapeditor/inspector/heroskillswidget.cpp index d1254e4b8..05ced71f0 100644 --- a/mapeditor/inspector/heroskillswidget.cpp +++ b/mapeditor/inspector/heroskillswidget.cpp @@ -34,7 +34,7 @@ HeroSkillsWidget::HeroSkillsWidget(CGHeroInstance & h, QWidget *parent) : ui->labelKnowledge->setText(QString::fromStdString(NPrimarySkill::names[3])); auto * delegate = new InspectorDelegate; - for(auto s : VLC->skillh->objects) + for(auto const & s : VLC->skillh->objects) delegate->options.push_back({QString::fromStdString(s->getNameTranslated()), QVariant::fromValue(s->getId().getNum())}); ui->skills->setItemDelegateForColumn(0, delegate); diff --git a/mapeditor/inspector/herospellwidget.cpp b/mapeditor/inspector/herospellwidget.cpp index 063bb90ee..6fe3a49ef 100644 --- a/mapeditor/inspector/herospellwidget.cpp +++ b/mapeditor/inspector/herospellwidget.cpp @@ -44,14 +44,16 @@ void HeroSpellWidget::obtainData() void HeroSpellWidget::initSpellLists() { QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 }; - auto spells = VLC->spellh->objects; + for (int i = 0; i < GameConstants::SPELL_LEVELS; i++) { - std::vector> spellsByLevel; - auto getSpellsByLevel = [i](auto spell) { - return spell->getLevel() == i + 1 && !spell->isSpecial() && !spell->isCreatureAbility(); - }; - vstd::copy_if(spells, std::back_inserter(spellsByLevel), getSpellsByLevel); + std::vector spellsByLevel; + for (auto const & spellID : VLC->spellh->getDefaultAllowed()) + { + if (spellID.toSpell()->getLevel() == i + 1) + spellsByLevel.push_back(spellID.toSpell()); + } + spellLists[i]->clear(); for (auto spell : spellsByLevel) { @@ -130,4 +132,4 @@ void HeroSpellDelegate::setModelData(QWidget * editor, QAbstractItemModel * mode { QStyledItemDelegate::setModelData(editor, model, index); } -} \ No newline at end of file +} diff --git a/mapeditor/inspector/inspector.cpp b/mapeditor/inspector/inspector.cpp index b5de5ccf4..57174b68c 100644 --- a/mapeditor/inspector/inspector.cpp +++ b/mapeditor/inspector/inspector.cpp @@ -139,7 +139,7 @@ void Initializer::initialize(CGHeroInstance * o) if(o->ID == Obj::HERO) { - for(auto t : VLC->heroh->objects) + for(auto const & t : VLC->heroh->objects) { if(t->heroClass->getId() == HeroClassID(o->subID)) { @@ -171,7 +171,7 @@ void Initializer::initialize(CGTownInstance * o) if(lvl > 2) o->builtBuildings.insert(BuildingID::CASTLE); if(lvl > 3) o->builtBuildings.insert(BuildingID::CAPITOL); - for(auto spell : VLC->spellh->objects) //add all regular spells to town + for(auto const & spell : VLC->spellh->objects) //add all regular spells to town { if(!spell->isSpecial() && !spell->isCreatureAbility()) o->possibleSpells.push_back(spell->id); @@ -185,7 +185,7 @@ void Initializer::initialize(CGArtifact * o) if(o->ID == Obj::SPELL_SCROLL) { std::vector out; - for(auto spell : VLC->spellh->objects) //spellh size appears to be greater (?) + for(auto const & spell : VLC->spellh->objects) //spellh size appears to be greater (?) { if(VLC->spellh->getDefaultAllowed().count(spell->id) != 0) { @@ -357,7 +357,7 @@ void Inspector::updateProperties(CGArtifact * o) if(spellId != SpellID::NONE) { auto * delegate = new InspectorDelegate; - for(auto spell : VLC->spellh->objects) + for(auto const & spell : VLC->spellh->objects) { if(controller.map()->allowedSpells.count(spell->id) != 0) delegate->options.push_back({QObject::tr(spell->getNameTranslated().c_str()), QVariant::fromValue(int(spell->getId()))}); @@ -699,7 +699,7 @@ void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVari if(key == "Hero type") { - for(auto t : VLC->heroh->objects) + for(auto const & t : VLC->heroh->objects) { if(t->getId() == value.toInt()) { diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index ada8f11e9..f0a699680 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -594,7 +594,7 @@ void MainWindow::loadObjectsTree() { auto *b = new QPushButton(QString::fromStdString(terrain->getNameTranslated())); ui->terrainLayout->addWidget(b); - connect(b, &QPushButton::clicked, this, [this, terrain]{ terrainButtonClicked(terrain->getId()); }); + connect(b, &QPushButton::clicked, this, [this, terrainID=terrain->getId()]{ terrainButtonClicked(terrainID); }); //filter QString displayName = QString::fromStdString(terrain->getNameTranslated()); @@ -609,7 +609,7 @@ void MainWindow::loadObjectsTree() { auto *b = new QPushButton(QString::fromStdString(road->getNameTranslated())); ui->roadLayout->addWidget(b); - connect(b, &QPushButton::clicked, this, [this, road]{ roadOrRiverButtonClicked(road->getIndex(), true); }); + connect(b, &QPushButton::clicked, this, [this, roadID=road->getIndex()]{ roadOrRiverButtonClicked(roadID, true); }); } //add spacer to keep terrain button on the top ui->roadLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding)); @@ -618,7 +618,7 @@ void MainWindow::loadObjectsTree() { auto *b = new QPushButton(QString::fromStdString(river->getNameTranslated())); ui->riverLayout->addWidget(b); - connect(b, &QPushButton::clicked, this, [this, river]{ roadOrRiverButtonClicked(river->getIndex(), false); }); + connect(b, &QPushButton::clicked, this, [this, riverID=river->getIndex()]{ roadOrRiverButtonClicked(riverID, false); }); } //add spacer to keep terrain button on the top ui->riverLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding)); diff --git a/mapeditor/mapcontroller.cpp b/mapeditor/mapcontroller.cpp index 1e6da2589..4fb1fc4c7 100644 --- a/mapeditor/mapcontroller.cpp +++ b/mapeditor/mapcontroller.cpp @@ -137,7 +137,7 @@ void MapController::repairMap(CMap * map) const map->allowedHeroes.insert(nih->getHeroType()); - auto type = VLC->heroh->objects[nih->subID]; + auto const & type = VLC->heroh->objects[nih->subID]; assert(type->heroClass); //TODO: find a way to get proper type name if(obj->ID == Obj::HERO) @@ -187,7 +187,7 @@ void MapController::repairMap(CMap * map) const if(art->ID == Obj::SPELL_SCROLL && !art->storedArtifact) { std::vector out; - for(auto spell : VLC->spellh->objects) //spellh size appears to be greater (?) + for(auto const & spell : VLC->spellh->objects) //spellh size appears to be greater (?) { //if(map->isAllowedSpell(spell->id)) { @@ -239,7 +239,7 @@ void MapController::setMap(std::unique_ptr cmap) void MapController::initObstaclePainters(CMap * map) { - for (auto terrain : VLC->terrainTypeHandler->objects) + for (auto const & terrain : VLC->terrainTypeHandler->objects) { auto terrainId = terrain->getId(); _obstaclePainters[terrainId] = std::make_unique(map); diff --git a/mapeditor/mapsettings/mapsettings.cpp b/mapeditor/mapsettings/mapsettings.cpp index 978916e77..7e579cb81 100644 --- a/mapeditor/mapsettings/mapsettings.cpp +++ b/mapeditor/mapsettings/mapsettings.cpp @@ -30,7 +30,7 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) : show(); - for(auto objectPtr : VLC->skillh->objects) + for(auto const & objectPtr : VLC->skillh->objects) { auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated())); item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex())); @@ -38,7 +38,7 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) : item->setCheckState(controller.map()->allowedAbilities.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked); ui->listAbilities->addItem(item); } - for(auto objectPtr : VLC->spellh->objects) + for(auto const & objectPtr : VLC->spellh->objects) { auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated())); item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex())); @@ -46,7 +46,7 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) : item->setCheckState(controller.map()->allowedSpells.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked); ui->listSpells->addItem(item); } - for(auto objectPtr : VLC->arth->objects) + for(auto const & objectPtr : VLC->arth->objects) { auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated())); item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex())); @@ -54,7 +54,7 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) : item->setCheckState(controller.map()->allowedArtifact.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked); ui->listArts->addItem(item); } - for(auto objectPtr : VLC->heroh->objects) + for(auto const & objectPtr : VLC->heroh->objects) { auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated())); item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex())); diff --git a/mapeditor/mapview.cpp b/mapeditor/mapview.cpp index 5e37b48ff..d8c055af4 100644 --- a/mapeditor/mapview.cpp +++ b/mapeditor/mapview.cpp @@ -16,6 +16,7 @@ #include "../lib/mapObjectConstructors/AObjectTypeHandler.h" #include "../lib/mapObjectConstructors/CObjectClassesHandler.h" #include "../lib/mapping/CMap.h" +#include "../lib/VCMI_Lib.h" MinimapView::MinimapView(QWidget * parent): diff --git a/mapeditor/playerparams.cpp b/mapeditor/playerparams.cpp index 226b3249a..780ca11da 100644 --- a/mapeditor/playerparams.cpp +++ b/mapeditor/playerparams.cpp @@ -48,7 +48,7 @@ PlayerParams::PlayerParams(MapController & ctrl, int playerId, QWidget *parent) //load factions for(auto idx : VLC->townh->getAllowedFactions()) { - const CFaction * faction = VLC->townh->objects.at(idx); + const auto & faction = VLC->townh->objects.at(idx); auto * item = new QListWidgetItem(QString::fromStdString(faction->getNameTranslated())); item->setData(Qt::UserRole, QVariant::fromValue(idx.getNum())); item->setFlags(item->flags() | Qt::ItemIsUserCheckable); diff --git a/scripting/lua/LuaScriptingContext.cpp b/scripting/lua/LuaScriptingContext.cpp index 47aacb458..5c351f623 100644 --- a/scripting/lua/LuaScriptingContext.cpp +++ b/scripting/lua/LuaScriptingContext.cpp @@ -14,6 +14,7 @@ #include #include #include +#include "vcmi/Services.h" #include "LuaStack.h" diff --git a/test/scripting/ScriptFixture.cpp b/test/scripting/ScriptFixture.cpp index 0ff6e3b2b..27fb3ae75 100644 --- a/test/scripting/ScriptFixture.cpp +++ b/test/scripting/ScriptFixture.cpp @@ -9,6 +9,7 @@ */ #include "StdInc.h" #include "lib/modding/ModScope.h" +#include "lib/VCMI_Lib.h" #include "ScriptFixture.h"