From abad4b01ce9be0ed59fb046af1f15ca186b34033 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sun, 5 Nov 2023 18:58:07 +0200 Subject: [PATCH] Remove explicit convesion to int in operators --- include/vcmi/spells/Caster.h | 3 +- lib/ArtifactUtils.cpp | 2 +- lib/CArtHandler.cpp | 15 ++++----- lib/CArtHandler.h | 2 +- lib/CCreatureHandler.cpp | 8 ++--- lib/CCreatureSet.cpp | 14 ++++---- lib/CHeroHandler.cpp | 4 +-- lib/JsonRandom.cpp | 8 ++--- lib/battle/CUnitState.cpp | 2 +- lib/battle/CUnitState.h | 2 +- lib/constants/EntityIdentifiers.cpp | 6 ++-- lib/constants/EntityIdentifiers.h | 7 ++++ lib/gameState/CGameState.cpp | 3 +- .../CObjectClassesHandler.cpp | 32 +++++++++---------- lib/mapObjects/CArmedInstance.cpp | 2 +- lib/mapObjects/CArmedInstance.h | 2 +- lib/mapObjects/CGHeroInstance.cpp | 26 +++++++-------- lib/mapObjects/CGHeroInstance.h | 2 +- lib/mapObjects/CGTownInstance.cpp | 10 +++--- lib/mapObjects/MiscObjects.cpp | 4 +-- lib/mapObjects/MiscObjects.h | 2 +- lib/mapping/MapFormatH3M.cpp | 2 +- lib/mapping/MapFormatJson.cpp | 9 ++---- lib/rewardable/Interface.cpp | 2 +- lib/rmg/CMapGenerator.cpp | 7 +--- lib/rmg/modificators/ObjectManager.cpp | 6 ++-- lib/spells/AbilityCaster.cpp | 2 +- lib/spells/AbilityCaster.h | 2 +- lib/spells/CSpellHandler.cpp | 2 +- lib/spells/ExternalCaster.cpp | 2 +- lib/spells/ExternalCaster.h | 4 ++- lib/spells/ObstacleCasterProxy.cpp | 2 +- lib/spells/ObstacleCasterProxy.h | 2 +- lib/spells/ProxyCaster.cpp | 2 +- lib/spells/ProxyCaster.h | 2 +- 35 files changed, 97 insertions(+), 105 deletions(-) diff --git a/include/vcmi/spells/Caster.h b/include/vcmi/spells/Caster.h index 0d779a2a4..4158c0c53 100644 --- a/include/vcmi/spells/Caster.h +++ b/include/vcmi/spells/Caster.h @@ -17,6 +17,7 @@ class MetaString; class ServerCallback; class CGHeroInstance; class Spell; +class SpellSchool; namespace battle { @@ -38,7 +39,7 @@ public: /// returns level on which given spell would be cast by this(0 - none, 1 - basic etc); /// caster may not know this spell at all /// optionally returns number of selected school by arg - 0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic - virtual int32_t getSpellSchoolLevel(const Spell * spell, int32_t * outSelectedSchool = nullptr) const = 0; + virtual int32_t getSpellSchoolLevel(const Spell * spell, SpellSchool * outSelectedSchool = nullptr) const = 0; ///default spell school level for effect calculation virtual int32_t getEffectLevel(const Spell * spell) const = 0; diff --git a/lib/ArtifactUtils.cpp b/lib/ArtifactUtils.cpp index d5b9154ed..bfe46894c 100644 --- a/lib/ArtifactUtils.cpp +++ b/lib/ArtifactUtils.cpp @@ -216,7 +216,7 @@ DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(CArtifa DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(const ArtifactID & aid) { - return ArtifactUtils::createNewArtifactInstance(VLC->arth->objects[aid]); + return ArtifactUtils::createNewArtifactInstance((*VLC->arth)[aid]); } DLL_LINKAGE CArtifactInstance * ArtifactUtils::createArtifact(CMap * map, const ArtifactID & aid, SpellID spellID) diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 97351d3b6..0854429b8 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -371,7 +371,7 @@ void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode objects.emplace_back(object); - registerObject(scope, "artifact", name, object->id); + registerObject(scope, "artifact", name, object->id.getNum()); } void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) @@ -383,7 +383,7 @@ void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode assert(objects[index] == nullptr); // ensure that this id was not loaded before objects[index] = object; - registerObject(scope, "artifact", name, object->id); + registerObject(scope, "artifact", name, object->id.getNum()); } const std::vector & CArtHandler::getTypeNames() const @@ -630,7 +630,7 @@ void CArtHandler::makeItCommanderArt(CArtifact * a, bool onlyCommander) bool CArtHandler::legalArtifact(const ArtifactID & id) { - auto art = objects[id]; + auto art = id.toArtifact(); //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components if(art->isCombined()) @@ -639,13 +639,13 @@ bool CArtHandler::legalArtifact(const ArtifactID & id) if(art->aClass < CArtifact::ART_TREASURE || art->aClass > CArtifact::ART_RELIC) return false; // invalid class - if(!art->possibleSlots[ArtBearer::HERO].empty()) + if(art->possibleSlots.count(ArtBearer::HERO) && !art->possibleSlots.at(ArtBearer::HERO).empty()) return true; - if(!art->possibleSlots[ArtBearer::CREATURE].empty() && VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_ARTIFACT)) + if(art->possibleSlots.count(ArtBearer::CREATURE) && !art->possibleSlots.at(ArtBearer::CREATURE).empty() && VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_ARTIFACT)) return true; - if(!art->possibleSlots[ArtBearer::COMMANDER].empty() && VLC->settings()->getBoolean(EGameSettings::MODULE_COMMANDERS)) + if(art->possibleSlots.count(ArtBearer::COMMANDER) && !art->possibleSlots.at(ArtBearer::COMMANDER).empty() && VLC->settings()->getBoolean(EGameSettings::MODULE_COMMANDERS)) return true; return false; @@ -658,7 +658,7 @@ void CArtHandler::initAllowedArtifactsList(const std::set & allowed) for (ArtifactID i : allowed) { if (legalArtifact(ArtifactID(i))) - allowedArtifacts.push_back(objects[i]); + allowedArtifacts.push_back(i.toArtifact()); //keep im mind that artifact can be worn by more than one type of bearer } } @@ -682,7 +682,6 @@ void CArtHandler::afterLoadFinalization() { for(auto &bonus : art->getExportedBonusList()) { - assert(art == objects[art->id]); assert(bonus->source == BonusSource::ARTIFACT); bonus->sid = BonusSourceID(art->id); } diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index f9673a450..957d2b1c5 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -142,7 +142,7 @@ class DLL_LINKAGE CArtHandler : public CHandlerBase allowedArtifacts; + std::vector allowedArtifacts; void addBonuses(CArtifact *art, const JsonNode &bonusList); diff --git a/lib/CCreatureHandler.cpp b/lib/CCreatureHandler.cpp index 630992b51..3b72d3719 100644 --- a/lib/CCreatureHandler.cpp +++ b/lib/CCreatureHandler.cpp @@ -324,7 +324,7 @@ bool CCreature::isMyUpgrade(const CCreature *anotherCre) const bool CCreature::valid() const { - return this == VLC->creh->objects[idNumber]; + return this == (*VLC->creh)[idNumber]; } std::string CCreature::nodeName() const @@ -778,15 +778,13 @@ void CCreatureHandler::loadCrExpBon(CBonusSystemNode & globalEffects) } do //parse everything that's left { - CreatureID sid = static_cast(parser.readNumber()); //id = this particular creature ID + CreatureID sid = parser.readNumber(); //id = this particular creature ID b.sid = BonusSourceID(sid); bl.clear(); loadStackExp(b, bl, parser); for(const auto & b : bl) - { - objects[sid]->addNewBonus(b); //add directly to CCreature Node - } + (*this)[sid]->addNewBonus(b); //add directly to CCreature Node } while (parser.endLine()); diff --git a/lib/CCreatureSet.cpp b/lib/CCreatureSet.cpp index 8d79606ba..913ec72f6 100644 --- a/lib/CCreatureSet.cpp +++ b/lib/CCreatureSet.cpp @@ -79,7 +79,7 @@ bool CCreatureSet::setCreature(SlotID slot, CreatureID type, TQuantity quantity) SlotID CCreatureSet::getSlotFor(const CreatureID & creature, ui32 slotsAmount) const /*returns -1 if no slot available */ { - return getSlotFor(VLC->creh->objects[creature], slotsAmount); + return getSlotFor(creature.toCreature(), slotsAmount); } SlotID CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount) const @@ -304,7 +304,7 @@ void CCreatureSet::sweep() void CCreatureSet::addToSlot(const SlotID & slot, const CreatureID & cre, TQuantity count, bool allowMerging) { - const CCreature *c = VLC->creh->objects[cre]; + const CCreature *c = cre.toCreature(); if(!hasStackAtSlot(slot)) { @@ -749,10 +749,10 @@ void CStackInstance::giveStackExp(TExpType exp) void CStackInstance::setType(const CreatureID & creID) { - if(creID.getNum() >= 0 && creID.getNum() < VLC->creh->objects.size()) - setType(VLC->creh->objects[creID]); + if (creID == CreatureID::NONE) + setType(nullptr);//FIXME: unused branch? else - setType((const CCreature*)nullptr); + setType(creID.toCreature()); } void CStackInstance::setType(const CCreature *c) @@ -809,7 +809,7 @@ bool CStackInstance::valid(bool allowUnrandomized) const { if(!randomStack) { - return (type && type == VLC->creh->objects[type->getId()]); + return (type && type == type->getId().toEntity(VLC)); } else return allowUnrandomized; @@ -999,7 +999,7 @@ bool CCommanderInstance::gainsLevel() const CStackBasicDescriptor::CStackBasicDescriptor() = default; CStackBasicDescriptor::CStackBasicDescriptor(const CreatureID & id, TQuantity Count): - type(VLC->creh->objects[id]), + type(id.toCreature()), count(Count) { } diff --git a/lib/CHeroHandler.cpp b/lib/CHeroHandler.cpp index f71549e62..5de91ebe8 100644 --- a/lib/CHeroHandler.cpp +++ b/lib/CHeroHandler.cpp @@ -518,7 +518,7 @@ static std::vector> createCreatureSpecialty(CreatureID ba for (auto const & upgradeSourceID : oldTargets) { - const CCreature * upgradeSource = VLC->creh->objects[upgradeSourceID]; + const CCreature * upgradeSource = upgradeSourceID.toCreature(); targets.insert(upgradeSource->upgrades.begin(), upgradeSource->upgrades.end()); } @@ -528,7 +528,7 @@ static std::vector> createCreatureSpecialty(CreatureID ba for(CreatureID cid : targets) { - const CCreature &specCreature = *VLC->creh->objects[cid]; + auto const & specCreature = *cid.toCreature(); int stepSize = specCreature.getLevel() ? specCreature.getLevel() : 5; { diff --git a/lib/JsonRandom.cpp b/lib/JsonRandom.cpp index e87f9d6d2..69d0636f4 100644 --- a/lib/JsonRandom.cpp +++ b/lib/JsonRandom.cpp @@ -156,7 +156,7 @@ namespace JsonRandom for (auto const & artID : valuesSet) { - CArtifact * art = VLC->arth->objects[artID]; + const CArtifact * art = artID.toArtifact(); if(!vstd::iswithin(art->getPrice(), minValue, maxValue)) continue; @@ -482,13 +482,13 @@ namespace JsonRandom else logMod->warn("Failed to select suitable random creature!"); - stack.type = VLC->creh->objects[pickedCreature]; + stack.type = pickedCreature.toCreature(); stack.count = loadValue(value, rng, variables); if (!value["upgradeChance"].isNull() && !stack.type->upgrades.empty()) { if (int(value["upgradeChance"].Float()) > rng.nextInt(99)) // select random upgrade { - stack.type = VLC->creh->objects[*RandomGeneratorUtil::nextItem(stack.type->upgrades, rng)]; + stack.type = RandomGeneratorUtil::nextItem(stack.type->upgrades, rng)->toCreature(); } } return stack; @@ -523,7 +523,7 @@ namespace JsonRandom if (node["upgradeChance"].Float() > 0) { for(const auto & creaID : crea->upgrades) - info.allowedCreatures.push_back(VLC->creh->objects[creaID]); + info.allowedCreatures.push_back(creaID.toCreature()); } ret.push_back(info); } diff --git a/lib/battle/CUnitState.cpp b/lib/battle/CUnitState.cpp index 72b8bf3c8..9763dfa97 100644 --- a/lib/battle/CUnitState.cpp +++ b/lib/battle/CUnitState.cpp @@ -428,7 +428,7 @@ const CGHeroInstance * CUnitState::getHeroCaster() const return nullptr; } -int32_t CUnitState::getSpellSchoolLevel(const spells::Spell * spell, int32_t * outSelectedSchool) const +int32_t CUnitState::getSpellSchoolLevel(const spells::Spell * spell, SpellSchool * outSelectedSchool) const { int32_t skill = valOfBonuses(Selector::typeSubtype(BonusType::SPELLCASTER, BonusSubtypeID(spell->getId()))); vstd::abetween(skill, 0, 3); diff --git a/lib/battle/CUnitState.h b/lib/battle/CUnitState.h index e4597b4ad..9e0fb41c9 100644 --- a/lib/battle/CUnitState.h +++ b/lib/battle/CUnitState.h @@ -182,7 +182,7 @@ public: int32_t getCasterUnitId() const override; - int32_t getSpellSchoolLevel(const spells::Spell * spell, int32_t * outSelectedSchool = nullptr) const override; + int32_t getSpellSchoolLevel(const spells::Spell * spell, SpellSchool * outSelectedSchool = nullptr) const override; int32_t getEffectLevel(const spells::Spell * spell) const override; int64_t getSpellBonus(const spells::Spell * spell, int64_t base, const Unit * affectedStack) const override; diff --git a/lib/constants/EntityIdentifiers.cpp b/lib/constants/EntityIdentifiers.cpp index 73efb5b9c..43694c809 100644 --- a/lib/constants/EntityIdentifiers.cpp +++ b/lib/constants/EntityIdentifiers.cpp @@ -160,12 +160,12 @@ std::string CampaignScenarioID::encode(const si32 index) return std::to_string(index); } -std::string Obj::encode(int32_t index) +std::string MapObjectID::encode(int32_t index) { - return VLC->objtypeh->getObjectHandlerName(index); + return VLC->objtypeh->getObjectHandlerName(MapObjectID(index)); } -si32 Obj::decode(const std::string & identifier) +si32 MapObjectID::decode(const std::string & identifier) { auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "objects", identifier); if(rawId) diff --git a/lib/constants/EntityIdentifiers.h b/lib/constants/EntityIdentifiers.h index a789b2e85..a21c3dbeb 100644 --- a/lib/constants/EntityIdentifiers.h +++ b/lib/constants/EntityIdentifiers.h @@ -672,6 +672,12 @@ class ArtifactPosition : public IdentifierWithEnum::IdentifierWithEnum; + + // TODO: Remove + constexpr operator int32_t () const + { + return num; + } }; class ArtifactIDBase : public IdentifierBase @@ -728,6 +734,7 @@ public: FIRE_ELEMENTAL = 114, // for tests PSYCHIC_ELEMENTAL = 120, // for hardcoded ability MAGIC_ELEMENTAL = 121, // for hardcoded ability + AZURE_DRAGON = 132, CATAPULT = 145, BALLISTA = 146, FIRST_AID_TENT = 147, diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index d40f541b4..3a1e040f1 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -1860,8 +1860,7 @@ void CGameState::attachArmedObjects() bool CGameState::giveHeroArtifact(CGHeroInstance * h, const ArtifactID & aid) { - CArtifact * const artifact = VLC->arth->objects[aid]; //pointer to constant object - CArtifactInstance * ai = ArtifactUtils::createNewArtifactInstance(artifact); + CArtifactInstance * ai = ArtifactUtils::createNewArtifactInstance(aid); map->addNewArtifactInstance(ai); auto slot = ArtifactUtils::getArtAnyPosition(h, aid); if(ArtifactUtils::isSlotEquipment(slot) || ArtifactUtils::isSlotBackpack(slot)) diff --git a/lib/mapObjectConstructors/CObjectClassesHandler.cpp b/lib/mapObjectConstructors/CObjectClassesHandler.cpp index 712b8c910..89230a45b 100644 --- a/lib/mapObjectConstructors/CObjectClassesHandler.cpp +++ b/lib/mapObjectConstructors/CObjectClassesHandler.cpp @@ -288,27 +288,26 @@ void CObjectClassesHandler::loadObject(std::string scope, std::string name, cons void CObjectClassesHandler::loadSubObject(const std::string & identifier, JsonNode config, MapObjectID ID, MapObjectSubID subID) { config.setType(JsonNode::JsonType::DATA_STRUCT); // ensure that input is not NULL - assert(objects[ID]); + assert(objects[ID.getNum()]); - if ( subID >= objects[ID]->objects.size()) - objects[ID]->objects.resize(subID+1); + if ( subID.getNum() >= objects[ID.getNum()]->objects.size()) + objects[ID.getNum()]->objects.resize(subID.getNum()+1); - JsonUtils::inherit(config, objects.at(ID)->base); - loadSubObject(config.meta, identifier, config, objects[ID], subID); + JsonUtils::inherit(config, objects.at(ID.getNum())->base); + loadSubObject(config.meta, identifier, config, objects[ID.getNum()], subID.getNum()); } void CObjectClassesHandler::removeSubObject(MapObjectID ID, MapObjectSubID subID) { - assert(objects[ID]); - assert(subID < objects[ID]->objects.size()); - objects[ID]->objects[subID] = nullptr; + assert(objects[ID.getNum()]); + objects[ID.getNum()]->objects[subID.getNum()] = nullptr; } TObjectTypeHandler CObjectClassesHandler::getHandlerFor(MapObjectID type, MapObjectSubID subtype) const { try { - auto result = objects.at(type)->objects.at(subtype); + auto result = objects.at(type.getNum())->objects.at(subtype.getNum()); if (result != nullptr) return result; @@ -318,7 +317,7 @@ TObjectTypeHandler CObjectClassesHandler::getHandlerFor(MapObjectID type, MapObj // Leave catch block silently } - std::string errorString = "Failed to find object of type " + std::to_string(type) + "::" + std::to_string(subtype); + std::string errorString = "Failed to find object of type " + std::to_string(type.getNum()) + "::" + std::to_string(subtype.getNum()); logGlobal->error(errorString); throw std::runtime_error(errorString); } @@ -360,13 +359,13 @@ std::set CObjectClassesHandler::knownSubObjects(MapObjectID prim { std::set ret; - if (!objects.at(primaryID)) + if (!objects.at(primaryID.getNum())) { logGlobal->error("Failed to find object %d", primaryID); return ret; } - for(const auto & entry : objects.at(primaryID)->objects) + for(const auto & entry : objects.at(primaryID.getNum())->objects) if (entry) ret.insert(entry->subtype); @@ -460,7 +459,7 @@ std::string CObjectClassesHandler::getObjectName(MapObjectID type, MapObjectSubI if (handler && handler->hasNameTextID()) return handler->getNameTranslated(); else - return objects[type]->getNameTranslated(); + return objects[type.getNum()]->getNameTranslated(); } SObjectSounds CObjectClassesHandler::getObjectSounds(MapObjectID type, MapObjectSubID subtype) const @@ -472,20 +471,19 @@ SObjectSounds CObjectClassesHandler::getObjectSounds(MapObjectID type, MapObject if(type == Obj::PRISON || type == Obj::HERO || type == Obj::SPELL_SCROLL) subtype = 0; - assert(objects[type]); - assert(subtype < objects[type]->objects.size()); + assert(objects[type.getNum()]); return getHandlerFor(type, subtype)->getSounds(); } std::string CObjectClassesHandler::getObjectHandlerName(MapObjectID type) const { - return objects.at(type)->handlerName; + return objects.at(type.getNum())->handlerName; } std::string CObjectClassesHandler::getJsonKey(MapObjectID type) const { - return objects.at(type)->getJsonKey(); + return objects.at(type.getNum())->getJsonKey(); } VCMI_LIB_NAMESPACE_END diff --git a/lib/mapObjects/CArmedInstance.cpp b/lib/mapObjects/CArmedInstance.cpp index e6e74d09d..6c588df7b 100644 --- a/lib/mapObjects/CArmedInstance.cpp +++ b/lib/mapObjects/CArmedInstance.cpp @@ -19,7 +19,7 @@ VCMI_LIB_NAMESPACE_BEGIN -void CArmedInstance::randomizeArmy(int type) +void CArmedInstance::randomizeArmy(FactionID type) { for (auto & elem : stacks) { diff --git a/lib/mapObjects/CArmedInstance.h b/lib/mapObjects/CArmedInstance.h index 378dcf96c..95cb59d43 100644 --- a/lib/mapObjects/CArmedInstance.h +++ b/lib/mapObjects/CArmedInstance.h @@ -29,7 +29,7 @@ private: public: BattleInfo *battle; //set to the current battle, if engaged - void randomizeArmy(int type); + void randomizeArmy(FactionID type); virtual void updateMoraleBonusFromArmy(); void armyChanged() override; diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index 00b4e5dbc..80ae5b9f5 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -578,11 +578,11 @@ void CGHeroInstance::pickRandomObject(CRandomGenerator & rand) { ID = Obj::HERO; subID = cb->gameState()->pickNextHeroType(getOwner()); - type = VLC->heroh->objects[subID]; + type = VLC->heroh->objects[getHeroType().getNum()]; randomizeArmy(type->heroClass->faction); } else - type = VLC->heroh->objects[subID]; + type = VLC->heroh->objects[getHeroType().getNum()]; auto oldSubID = subID; @@ -657,7 +657,7 @@ int32_t CGHeroInstance::getCasterUnitId() const return id.getNum(); } -int32_t CGHeroInstance::getSpellSchoolLevel(const spells::Spell * spell, int32_t * outSelectedSchool) const +int32_t CGHeroInstance::getSpellSchoolLevel(const spells::Spell * spell, SpellSchool * outSelectedSchool) const { int32_t skill = -1; //skill level @@ -909,7 +909,7 @@ CStackBasicDescriptor CGHeroInstance::calculateNecromancy (const BattleResult &b // raise upgraded creature (at 2/3 rate) if no space available otherwise if(getSlotFor(creatureTypeRaised) == SlotID()) { - for(const CreatureID & upgraded : VLC->creh->objects[creatureTypeRaised]->upgrades) + for(const CreatureID & upgraded : creatureTypeRaised.toCreature()->upgrades) { if(getSlotFor(upgraded) != SlotID()) { @@ -920,7 +920,7 @@ CStackBasicDescriptor CGHeroInstance::calculateNecromancy (const BattleResult &b } } // calculate number of creatures raised - low level units contribute at 50% rate - const double raisedUnitHealth = VLC->creh->objects[creatureTypeRaised]->getMaxHealth(); + const double raisedUnitHealth = creatureTypeRaised.toCreature()->getMaxHealth(); double raisedUnits = 0; for(const auto & casualty : casualties) { @@ -1523,7 +1523,7 @@ std::string CGHeroInstance::getHeroTypeName() const } else { - return VLC->heroh->objects[getHeroType()]->getJsonKey(); + return getHeroType().toEntity(VLC)->getJsonKey(); } } return ""; @@ -1657,15 +1657,11 @@ void CGHeroInstance::serializeCommonOptions(JsonSerializeFormat & handler) for(size_t skillIndex = 0; skillIndex < secondarySkills.size(); ++skillIndex) { JsonArraySerializer inner = secondarySkills.enterArray(skillIndex); - const si32 rawId = secSkills.at(skillIndex).first; + SecondarySkill skillId = secSkills.at(skillIndex).first; - if(rawId < 0 || rawId >= VLC->skillh->size()) - logGlobal->error("Invalid secondary skill %d", rawId); - - auto value = (*VLC->skillh)[SecondarySkill(rawId)]->getJsonKey(); - handler.serializeString("skill", value); - value = NSecondarySkill::levels.at(secSkills.at(skillIndex).second); - handler.serializeString("level", value); + handler.serializeId("skill", skillId); + std::string skillLevel = NSecondarySkill::levels.at(secSkills.at(skillIndex).second); + handler.serializeString("level", skillLevel); } } } @@ -1757,7 +1753,7 @@ void CGHeroInstance::serializeJsonOptions(JsonSerializeFormat & handler) if(!appearance) { // crossoverDeserialize - type = VLC->heroh->objects[getHeroType()]; + type = VLC->heroh->objects[getHeroType().getNum()]; appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, type->heroClass->getIndex())->getTemplates().front(); } diff --git a/lib/mapObjects/CGHeroInstance.h b/lib/mapObjects/CGHeroInstance.h index 2ddc83e2c..1349b4687 100644 --- a/lib/mapObjects/CGHeroInstance.h +++ b/lib/mapObjects/CGHeroInstance.h @@ -274,7 +274,7 @@ public: ///spells::Caster int32_t getCasterUnitId() const override; - int32_t getSpellSchoolLevel(const spells::Spell * spell, int32_t * outSelectedSchool = nullptr) const override; + int32_t getSpellSchoolLevel(const spells::Spell * spell, SpellSchool * outSelectedSchool = nullptr) const override; int64_t getSpellBonus(const spells::Spell * spell, int64_t base, const battle::Unit * affectedStack) const override; int64_t getSpecificSpellBonus(const spells::Spell * spell, int64_t base) const override; diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 6d3cacbd6..84bda3112 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -136,7 +136,7 @@ GrowthInfo CGTownInstance::getGrowthInfo(int level) const if (creatures[level].second.empty()) return ret; //no dwelling - const CCreature *creature = VLC->creh->objects[creatures[level].second.back()]; + const Creature *creature = creatures[level].second.back().toEntity(VLC); const int base = creature->getGrowth(); int castleBonus = 0; @@ -489,8 +489,8 @@ void CGTownInstance::pickRandomObject(CRandomGenerator & rand) assert(ID == Obj::TOWN); // just in case setType(ID, subID); - town = (*VLC->townh)[subID]->town; - randomizeArmy(subID); + town = (*VLC->townh)[getFaction()]->town; + randomizeArmy(getFaction()); updateAppearance(); } @@ -571,7 +571,7 @@ void CGTownInstance::newTurn(CRandomGenerator & rand) const } else //upgrade { - cb->changeStackType(sl, VLC->creh->objects[*c->upgrades.begin()]); + cb->changeStackType(sl, c->upgrades.begin()->toCreature()); } } if ((stacksCount() < GameConstants::ARMY_SIZE && rand.nextInt(99) < 25) || Slots().empty()) //add new stack @@ -592,7 +592,7 @@ void CGTownInstance::newTurn(CRandomGenerator & rand) const { StackLocation sl(this, n); if (slotEmpty(n)) - cb->insertNewStack(sl, VLC->creh->objects[c], count); + cb->insertNewStack(sl, c.toCreature(), count); else //add to existing cb->changeStackCount(sl, count); } diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index 82c5cfe72..00968d887 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -33,7 +33,7 @@ VCMI_LIB_NAMESPACE_BEGIN -std::map > CGMagi::eyelist; +std::map > CGMagi::eyelist; ui8 CGObelisk::obeliskCount = 0; //how many obelisks are on map std::map CGObelisk::visited; //map: team_id => how many obelisks has been visited @@ -219,7 +219,7 @@ void CGMine::serializeJsonOptions(JsonSerializeFormat & handler) for(const auto & resID : abandonedMineResources) { JsonNode one(JsonNode::JsonType::DATA_STRING); - one.String() = GameConstants::RESOURCE_NAMES[resID]; + one.String() = GameConstants::RESOURCE_NAMES[resID.getNum()]; node.Vector().push_back(one); } handler.serializeRaw("possibleResources", node, std::nullopt); diff --git a/lib/mapObjects/MiscObjects.h b/lib/mapObjects/MiscObjects.h index de8ae3862..d761b2a89 100644 --- a/lib/mapObjects/MiscObjects.h +++ b/lib/mapObjects/MiscObjects.h @@ -338,7 +338,7 @@ protected: class DLL_LINKAGE CGMagi : public CGObjectInstance { public: - static std::map > eyelist; //[subID][id], supports multiple sets as in H5 + static std::map > eyelist; //[subID][id], supports multiple sets as in H5 static void reset(); diff --git a/lib/mapping/MapFormatH3M.cpp b/lib/mapping/MapFormatH3M.cpp index 02624ccdf..8003f257e 100644 --- a/lib/mapping/MapFormatH3M.cpp +++ b/lib/mapping/MapFormatH3M.cpp @@ -2095,7 +2095,7 @@ int CMapLoaderH3M::readQuest(IQuestObject * guard, const int3 & position) guard->quest->mission.creatures.resize(typeNumber); for(int hh = 0; hh < typeNumber; ++hh) { - guard->quest->mission.creatures[hh].type = VLC->creh->objects[reader->readCreature()]; + guard->quest->mission.creatures[hh].type = reader->readCreature().toCreature(); guard->quest->mission.creatures[hh].count = reader->readUInt16(); } break; diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index fda1e4329..90b747419 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -528,13 +528,10 @@ void CMapFormatJson::serializePlayerInfo(JsonSerializeFormat & handler) { std::string temp; if(hero->type) - { temp = hero->type->getJsonKey(); - } else - { - temp = VLC->heroh->objects[hero->subID]->getJsonKey(); - } + temp = hero->getHeroType().toEntity(VLC)->getJsonKey(); + handler.serializeString("type", temp); } } @@ -753,7 +750,7 @@ void CMapFormatJson::writeDisposedHeroes(JsonSerializeFormat & handler) for(DisposedHero & hero : map->disposedHeroes) { - std::string type = HeroTypeID::encode(hero.heroId); + std::string type = HeroTypeID::encode(hero.heroId.getNum()); auto definition = definitions->enterStruct(type); diff --git a/lib/rewardable/Interface.cpp b/lib/rewardable/Interface.cpp index b7cc60c25..534641f37 100644 --- a/lib/rewardable/Interface.cpp +++ b/lib/rewardable/Interface.cpp @@ -157,7 +157,7 @@ void Rewardable::Interface::grantRewardAfterLevelup(IGameCallback * cb, const Re } for(const ArtifactID & art : info.reward.artifacts) - cb->giveHeroNewArtifact(hero, VLC->arth->objects[art],ArtifactPosition::FIRST_AVAILABLE); + cb->giveHeroNewArtifact(hero, art.toArtifact(), ArtifactPosition::FIRST_AVAILABLE); if(!info.reward.spells.empty()) { diff --git a/lib/rmg/CMapGenerator.cpp b/lib/rmg/CMapGenerator.cpp index 3c66b3c39..78c03890e 100644 --- a/lib/rmg/CMapGenerator.cpp +++ b/lib/rmg/CMapGenerator.cpp @@ -98,12 +98,7 @@ const CMapGenOptions& CMapGenerator::getMapGenOptions() const void CMapGenerator::initPrisonsRemaining() { - allowedPrisons = 0; - for (auto isAllowed : map->getMap(this).allowedHeroes) - { - if (isAllowed) - allowedPrisons++; - } + allowedPrisons = map->getMap(this).allowedHeroes.size(); allowedPrisons = std::max (0, allowedPrisons - 16 * mapGenOptions.getHumanOrCpuPlayerCount()); //so at least 16 heroes will be available for every player } diff --git a/lib/rmg/modificators/ObjectManager.cpp b/lib/rmg/modificators/ObjectManager.cpp index 970a6f4bf..683f9f6e8 100644 --- a/lib/rmg/modificators/ObjectManager.cpp +++ b/lib/rmg/modificators/ObjectManager.cpp @@ -638,14 +638,14 @@ CGCreature * ObjectManager::chooseGuard(si32 strength, bool zoneGuard) if(!possibleCreatures.empty()) { creId = *RandomGeneratorUtil::nextItem(possibleCreatures, zone.getRand()); - amount = strength / VLC->creh->objects[creId]->getAIValue(); + amount = strength / creId.toEntity(VLC)->getAIValue(); if (amount >= 4) amount = static_cast(amount * zone.getRand().nextDouble(0.75, 1.25)); } else //just pick any available creature { - creId = CreatureID(132); //Azure Dragon - amount = strength / VLC->creh->objects[creId]->getAIValue(); + creId = CreatureID::AZURE_DRAGON; //Azure Dragon + amount = strength / creId.toEntity(VLC)->getAIValue(); } auto guardFactory = VLC->objtypeh->getHandlerFor(Obj::MONSTER, creId); diff --git a/lib/spells/AbilityCaster.cpp b/lib/spells/AbilityCaster.cpp index 01b1c105a..f6365684d 100644 --- a/lib/spells/AbilityCaster.cpp +++ b/lib/spells/AbilityCaster.cpp @@ -28,7 +28,7 @@ AbilityCaster::AbilityCaster(const battle::Unit * actualCaster_, int32_t baseSpe AbilityCaster::~AbilityCaster() = default; -int32_t AbilityCaster::getSpellSchoolLevel(const Spell * spell, int32_t * outSelectedSchool) const +int32_t AbilityCaster::getSpellSchoolLevel(const Spell * spell, SpellSchool * outSelectedSchool) const { auto skill = baseSpellLevel; const auto * unit = dynamic_cast(actualCaster); diff --git a/lib/spells/AbilityCaster.h b/lib/spells/AbilityCaster.h index bbfd723d7..35685c409 100644 --- a/lib/spells/AbilityCaster.h +++ b/lib/spells/AbilityCaster.h @@ -23,7 +23,7 @@ public: AbilityCaster(const battle::Unit * actualCaster_, int32_t baseSpellLevel_); virtual ~AbilityCaster(); - int32_t getSpellSchoolLevel(const Spell * spell, int32_t * outSelectedSchool = nullptr) const override; + int32_t getSpellSchoolLevel(const Spell * spell, SpellSchool * outSelectedSchool = nullptr) const override; int32_t getEffectLevel(const Spell * spell) const override; void getCastDescription(const Spell * spell, const std::vector & attacked, MetaString & text) const override; void spendMana(ServerCallback * server, const int32_t spellCost) const override; diff --git a/lib/spells/CSpellHandler.cpp b/lib/spells/CSpellHandler.cpp index 3af7be51e..1acb58c6b 100644 --- a/lib/spells/CSpellHandler.cpp +++ b/lib/spells/CSpellHandler.cpp @@ -154,7 +154,7 @@ void CSpell::forEachSchool(const std::functiongetSpellSchoolLevel(spell, outSelectedSchool); diff --git a/lib/spells/ProxyCaster.h b/lib/spells/ProxyCaster.h index 85fcf240c..67557dc02 100644 --- a/lib/spells/ProxyCaster.h +++ b/lib/spells/ProxyCaster.h @@ -24,7 +24,7 @@ public: virtual ~ProxyCaster(); int32_t getCasterUnitId() const override; - int32_t getSpellSchoolLevel(const Spell * spell, int32_t * outSelectedSchool = nullptr) const override; + int32_t getSpellSchoolLevel(const Spell * spell, SpellSchool * outSelectedSchool = nullptr) const override; int32_t getEffectLevel(const Spell * spell) const override; int64_t getSpellBonus(const Spell * spell, int64_t base, const battle::Unit * affectedStack) const override; int64_t getSpecificSpellBonus(const Spell * spell, int64_t base) const override;