diff --git a/AI/Nullkiller/AIGateway.cpp b/AI/Nullkiller/AIGateway.cpp index 5566d3140..f18c77e5c 100644 --- a/AI/Nullkiller/AIGateway.cpp +++ b/AI/Nullkiller/AIGateway.cpp @@ -1000,7 +1000,7 @@ void AIGateway::pickBestArtifacts(const CGHeroInstance * h, const CGHeroInstance //FIXME: why are the above possible to be null? bool emptySlotFound = false; - for(auto slot : artifact->artType->possibleSlots.at(target->bearerType())) + for(auto slot : artifact->artType->getPossibleSlots().at(target->bearerType())) { ArtifactLocation destLocation(target, slot); if(target->isPositionFree(slot) && artifact->canBePutAt(destLocation, true)) //combined artifacts are not always allowed to move @@ -1013,7 +1013,7 @@ void AIGateway::pickBestArtifacts(const CGHeroInstance * h, const CGHeroInstance } if(!emptySlotFound) //try to put that atifact in already occupied slot { - for(auto slot : artifact->artType->possibleSlots.at(target->bearerType())) + for(auto slot : artifact->artType->getPossibleSlots().at(target->bearerType())) { auto otherSlot = target->getSlot(slot); if(otherSlot && otherSlot->artifact) //we need to exchange artifact for better one diff --git a/AI/Nullkiller/AIUtility.cpp b/AI/Nullkiller/AIUtility.cpp index cdb260110..296c47780 100644 --- a/AI/Nullkiller/AIUtility.cpp +++ b/AI/Nullkiller/AIUtility.cpp @@ -306,10 +306,10 @@ bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2 auto art1 = a1->artType; auto art2 = a2->artType; - if(art1->price == art2->price) + if(art1->getPrice() == art2->getPrice()) return art1->valOfBonuses(BonusType::PRIMARY_SKILL) > art2->valOfBonuses(BonusType::PRIMARY_SKILL); else - return art1->price > art2->price; + return art1->getPrice() > art2->getPrice(); } bool isWeeklyRevisitable(const CGObjectInstance * obj) diff --git a/AI/VCAI/AIUtility.cpp b/AI/VCAI/AIUtility.cpp index a653dfcff..e73ddf191 100644 --- a/AI/VCAI/AIUtility.cpp +++ b/AI/VCAI/AIUtility.cpp @@ -256,8 +256,8 @@ bool compareArtifacts(const CArtifactInstance * a1, const CArtifactInstance * a2 auto art1 = a1->artType; auto art2 = a2->artType; - if(art1->price == art2->price) + if(art1->getPrice() == art2->getPrice()) return art1->valOfBonuses(BonusType::PRIMARY_SKILL) > art2->valOfBonuses(BonusType::PRIMARY_SKILL); else - return art1->price > art2->price; + return art1->getPrice() > art2->getPrice(); } diff --git a/AI/VCAI/VCAI.cpp b/AI/VCAI/VCAI.cpp index 0ec77b099..d027548c5 100644 --- a/AI/VCAI/VCAI.cpp +++ b/AI/VCAI/VCAI.cpp @@ -1187,7 +1187,7 @@ void VCAI::pickBestArtifacts(const CGHeroInstance * h, const CGHeroInstance * ot //FIXME: why are the above possible to be null? bool emptySlotFound = false; - for(auto slot : artifact->artType->possibleSlots.at(target->bearerType())) + for(auto slot : artifact->artType->getPossibleSlots().at(target->bearerType())) { ArtifactLocation destLocation(target, slot); if(target->isPositionFree(slot) && artifact->canBePutAt(destLocation, true)) //combined artifacts are not always allowed to move @@ -1200,7 +1200,7 @@ void VCAI::pickBestArtifacts(const CGHeroInstance * h, const CGHeroInstance * ot } if(!emptySlotFound) //try to put that atifact in already occupied slot { - for(auto slot : artifact->artType->possibleSlots.at(target->bearerType())) + for(auto slot : artifact->artType->getPossibleSlots().at(target->bearerType())) { auto otherSlot = target->getSlot(slot); if(otherSlot && otherSlot->artifact) //we need to exchange artifact for better one diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 91c6333d7..bcdc63153 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -800,7 +800,7 @@ void CCastleBuildings::enterBlacksmith(ArtifactID artifactID) bool possible = LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) >= price; if(possible) { - for(auto slot : art->possibleSlots.at(ArtBearer::HERO)) + for(auto slot : art->getPossibleSlots().at(ArtBearer::HERO)) { if(hero->getArt(slot) == nullptr) { diff --git a/client/windows/CCreatureWindow.cpp b/client/windows/CCreatureWindow.cpp index 214e7ec21..21d958c08 100644 --- a/client/windows/CCreatureWindow.cpp +++ b/client/windows/CCreatureWindow.cpp @@ -588,7 +588,7 @@ CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool s auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT); if(art) { - parent->stackArtifactIcon = std::make_shared("ARTIFACT", art->artType->iconIndex, 0, pos.x, pos.y); + parent->stackArtifactIcon = std::make_shared("ARTIFACT", art->artType->getIconIndex(), 0, pos.x, pos.y); parent->stackArtifactHelp = std::make_shared(Rect(pos, Point(44, 44)), CComponent::artifact); parent->stackArtifactHelp->type = art->artType->getId(); diff --git a/client/windows/CTradeWindow.cpp b/client/windows/CTradeWindow.cpp index c7e3f4ced..bc96ce464 100644 --- a/client/windows/CTradeWindow.cpp +++ b/client/windows/CTradeWindow.cpp @@ -803,7 +803,7 @@ void CMarketplaceWindow::makeDeal() leftIdToSend = hLeft->serial; break; case EMarketMode::ARTIFACT_RESOURCE: - leftIdToSend = hLeft->getArtInstance()->id.getNum(); + leftIdToSend = hLeft->getArtInstance()->getId().getNum(); break; case EMarketMode::RESOURCE_ARTIFACT: if(!ArtifactID(hRight->id).toArtifact()->canBePutAt(hero)) diff --git a/lib/ArtifactUtils.cpp b/lib/ArtifactUtils.cpp index f1ef234d1..c5ecf63f5 100644 --- a/lib/ArtifactUtils.cpp +++ b/lib/ArtifactUtils.cpp @@ -22,7 +22,7 @@ VCMI_LIB_NAMESPACE_BEGIN DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtAnyPosition(const CArtifactSet * target, const ArtifactID & aid) { const auto * art = aid.toArtifact(); - for(const auto & slot : art->possibleSlots.at(target->bearerType())) + for(const auto & slot : art->getPossibleSlots().at(target->bearerType())) { if(art->canBePutAt(target, slot)) return slot; diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 5a2c769b9..cf1c15aa5 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -51,21 +51,11 @@ bool CCombinedArtifact::isCombined() const return !(constituents.empty()); } -std::vector & CCombinedArtifact::getConstituents() -{ - return constituents; -} - const std::vector & CCombinedArtifact::getConstituents() const { return constituents; } -std::vector & CCombinedArtifact::getPartOf() -{ - return partOf; -} - const std::vector & CCombinedArtifact::getPartOf() const { return partOf; @@ -263,6 +253,8 @@ bool CArtifact::canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot, b } CArtifact::CArtifact() + : iconIndex(ArtifactID::NONE), + price(0) { setNodeType(ARTIFACT); possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty @@ -307,14 +299,21 @@ void CArtifact::addNewBonus(const std::shared_ptr& b) CBonusSystemNode::addNewBonus(b); } +const std::map> & CArtifact::getPossibleSlots() const +{ + return possibleSlots; +} + void CArtifact::updateFrom(const JsonNode& data) { //TODO:CArtifact::updateFrom } -void CArtifact::serializeJson(JsonSerializeFormat & handler) +void CArtifact::setImage(int32_t iconIndex, std::string image, std::string large) { - + this->iconIndex = iconIndex; + this->image = image; + this->large = large; } CArtHandler::~CArtHandler() = default; @@ -876,11 +875,11 @@ ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const const CArtifactInstance * CArtifactSet::getArtByInstanceId(const ArtifactInstanceID & artInstId) const { for(auto i : artifactsWorn) - if(i.second.artifact->id == artInstId) + if(i.second.artifact->getId() == artInstId) return i.second.artifact; for(auto i : artifactsInBackpack) - if(i.artifact->id == artInstId) + if(i.artifact->getId() == artInstId) return i.artifact; return nullptr; @@ -890,7 +889,7 @@ const ArtifactPosition CArtifactSet::getSlotByInstance(const CArtifactInstance * { if(artInst) { - for(auto & slot : artInst->artType->possibleSlots.at(bearerType())) + for(const auto & slot : artInst->artType->getPossibleSlots().at(bearerType())) if(getArt(slot) == artInst) return slot; @@ -934,7 +933,7 @@ void CArtifactSet::putArtifact(ArtifactPosition slot, CArtifactInstance * art) { const CArtifactInstance * mainPart = nullptr; for(const auto & part : art->getPartsInfo()) - if(vstd::contains(part.art->artType->possibleSlots.at(bearerType()), slot) + if(vstd::contains(part.art->artType->getPossibleSlots().at(bearerType()), slot) && (part.slot == ArtifactPosition::PRE_FIRST)) { mainPart = part.art; diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index dafb8c5c6..640de2c6c 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -51,9 +51,7 @@ protected: std::vector partOf; // Reverse map of constituents - combined arts that include this art public: bool isCombined() const; - std::vector & getConstituents(); const std::vector & getConstituents() const; - std::vector & getPartOf(); const std::vector & getPartOf() const; template void serialize(Handler & h, const int version) @@ -98,21 +96,21 @@ class DLL_LINKAGE CArtifact : public Artifact, public CBonusSystemNode, public CCombinedArtifact, public CScrollArtifact, public CGrowingArtifact { ArtifactID id; - + std::string image; + std::string large; // big image for custom artifacts, used in drag & drop + std::string advMapDef; // used for adventure map object std::string modScope; std::string identifier; + int32_t iconIndex; + uint32_t price; + CreatureID warMachine; + // Bearer Type => ids of slots where artifact can be placed + std::map> possibleSlots; public: enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes - std::string image; - std::string large; // big image for custom artifacts, used in drag & drop - std::string advMapDef; //used for adventure map object - si32 iconIndex = ArtifactID::NONE; - ui32 price = 0; - std::map > possibleSlots; //Bearer Type => ids of slots where artifact can be placed EartClass aClass = ART_SPECIAL; - CreatureID warMachine; int32_t getIndex() const override; int32_t getIconIndex() const override; @@ -137,11 +135,13 @@ public: int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other std::string nodeName() const override; void addNewBonus(const std::shared_ptr& b) override; + const std::map> & getPossibleSlots() const; virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE, bool assumeDestRemoved = false) const; void updateFrom(const JsonNode & data); - void serializeJson(JsonSerializeFormat & handler); + // Is used for testing purposes only + void setImage(int32_t iconIndex, std::string image, std::string large); template void serialize(Handler & h, const int version) { diff --git a/lib/CArtifactInstance.cpp b/lib/CArtifactInstance.cpp index 39b135754..679fa387b 100644 --- a/lib/CArtifactInstance.cpp +++ b/lib/CArtifactInstance.cpp @@ -46,6 +46,7 @@ bool CCombinedArtifactInstance::isPart(const CArtifactInstance * supposedPart) c std::vector & CCombinedArtifactInstance::getPartsInfo() { + // TODO romove this func. encapsulation violation return partsInfo; } @@ -140,6 +141,16 @@ ArtifactID CArtifactInstance::getTypeId() const return artType->getId(); } +ArtifactInstanceID CArtifactInstance::getId() const +{ + return id; +} + +void CArtifactInstance::setId(ArtifactInstanceID id) +{ + this->id = id; +} + bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved) const { return artType->canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved); diff --git a/lib/CArtifactInstance.h b/lib/CArtifactInstance.h index 7ddf8194e..079db214f 100644 --- a/lib/CArtifactInstance.h +++ b/lib/CArtifactInstance.h @@ -68,9 +68,10 @@ class DLL_LINKAGE CArtifactInstance { protected: void init(); + + ArtifactInstanceID id; public: ConstTransitivePtr artType; - ArtifactInstanceID id; CArtifactInstance(CArtifact * art); CArtifactInstance(); @@ -78,6 +79,8 @@ public: std::string nodeName() const override; std::string getDescription() const; ArtifactID getTypeId() const; + ArtifactInstanceID getId() const; + void setId(ArtifactInstanceID id); bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; bool isCombined() const; diff --git a/lib/JsonRandom.cpp b/lib/JsonRandom.cpp index 3be169f88..222d8aacf 100644 --- a/lib/JsonRandom.cpp +++ b/lib/JsonRandom.cpp @@ -206,7 +206,7 @@ namespace JsonRandom { CArtifact * art = VLC->arth->objects[artID]; - if(!vstd::iswithin(art->price, minValue, maxValue)) + if(!vstd::iswithin(art->getPrice(), minValue, maxValue)) return false; if(!allowedClasses.empty() && !allowedClasses.count(art->aClass)) @@ -217,7 +217,7 @@ namespace JsonRandom if(!allowedPositions.empty()) { - for(const auto & pos : art->possibleSlots[ArtBearer::HERO]) + for(const auto & pos : art->getPossibleSlots().at(ArtBearer::HERO)) { if(allowedPositions.count(pos)) return true; diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 6e03c38af..95da6a4e6 100644 --- a/lib/NetPacksLib.cpp +++ b/lib/NetPacksLib.cpp @@ -1949,8 +1949,8 @@ void AssembledArtifact::applyGs(CGameState *gs) constituentInstance->removeFrom(ArtifactLocation(al.artHolder, pos)); if(combineEquipped) { - if(!vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], al.slot) - && vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], pos)) + if(!vstd::contains(combinedArt->artType->getPossibleSlots().at(artSet->bearerType()), al.slot) + && vstd::contains(combinedArt->artType->getPossibleSlots().at(artSet->bearerType()), pos)) al.slot = pos; if(al.slot == pos) pos = ArtifactPosition::PRE_FIRST; diff --git a/lib/battle/BattleInfo.cpp b/lib/battle/BattleInfo.cpp index dd80dc0e8..861e45c52 100644 --- a/lib/battle/BattleInfo.cpp +++ b/lib/battle/BattleInfo.cpp @@ -378,7 +378,7 @@ BattleInfo * BattleInfo::setupBattle(const int3 & tile, TerrainId terrain, const if(nullptr != warMachineArt) { - CreatureID cre = warMachineArt->artType->warMachine; + CreatureID cre = warMachineArt->artType->getWarMachine(); if(cre != CreatureID::NONE) curB->generateNewStack(curB->nextUnitId(), CStackBasicDescriptor(cre, 1), side, SlotID::WAR_MACHINES_SLOT, hex); diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index dfbe598b3..0e4a384a9 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -406,10 +406,10 @@ void CGHeroInstance::initArmy(CRandomGenerator & rand, IArmyDescriptor * dst) ArtifactID aid = creature->warMachine; const CArtifact * art = aid.toArtifact(); - if(art != nullptr && !art->possibleSlots.at(ArtBearer::HERO).empty()) + if(art != nullptr && !art->getPossibleSlots().at(ArtBearer::HERO).empty()) { //TODO: should we try another possible slots? - ArtifactPosition slot = art->possibleSlots.at(ArtBearer::HERO).front(); + ArtifactPosition slot = art->getPossibleSlots().at(ArtBearer::HERO).front(); if(!getArt(slot)) putArtifact(slot, ArtifactUtils::createNewArtifactInstance(aid)); diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index 41639007c..c5e4fdbda 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -820,7 +820,7 @@ void CGArtifact::afterAddToMap(CMap * map) //Artifacts from map objects are never removed //FIXME: This should be revertible in map editor - if(ID == Obj::SPELL_SCROLL && storedArtifact && storedArtifact->id.getNum() < 0) + if(ID == Obj::SPELL_SCROLL && storedArtifact && storedArtifact->getId().getNum() < 0) map->addNewArtifactInstance(storedArtifact); } diff --git a/lib/mapping/CMap.cpp b/lib/mapping/CMap.cpp index 1533bdfbf..9f11fe561 100644 --- a/lib/mapping/CMap.cpp +++ b/lib/mapping/CMap.cpp @@ -466,7 +466,7 @@ void CMap::checkForObjectives() void CMap::addNewArtifactInstance(CArtifactInstance * art) { - art->id = ArtifactInstanceID(static_cast(artInstances.size())); + art->setId(static_cast(artInstances.size())); artInstances.emplace_back(art); } @@ -474,7 +474,7 @@ void CMap::eraseArtifactInstance(CArtifactInstance * art) { //TODO: handle for artifacts removed in map editor assert(artInstances[art->id.getNum()] == art); - artInstances[art->id.getNum()].dellNull(); + artInstances[art->getId().getNum()].dellNull(); } void CMap::addNewQuestInstance(CQuest* quest) diff --git a/lib/serializer/CSerializer.cpp b/lib/serializer/CSerializer.cpp index b8fc03770..1ce13bfe3 100644 --- a/lib/serializer/CSerializer.cpp +++ b/lib/serializer/CSerializer.cpp @@ -33,7 +33,7 @@ void CSerializer::addStdVecItems(CGameState *gs, LibClasses *lib) registerVectoredType(&lib->arth->objects, [](const CArtifact &art){ return art.getId(); }); registerVectoredType(&gs->map->artInstances, - [](const CArtifactInstance &artInst){ return artInst.id; }); + [](const CArtifactInstance &artInst){ return artInst.getId(); }); registerVectoredType(&gs->map->quests, [](const CQuest &q){ return q.qid; }); diff --git a/mapeditor/validator.cpp b/mapeditor/validator.cpp index 92950f0d4..a564c8204 100644 --- a/mapeditor/validator.cpp +++ b/mapeditor/validator.cpp @@ -140,7 +140,7 @@ std::list Validator::validate(const CMap * map) { if(ins->storedArtifact) { - if(!map->allowedSpell[ins->storedArtifact->id.getNum()]) + if(!map->allowedSpell[ins->storedArtifact->getId().getNum()]) issues.emplace_back(QString("Spell scroll %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false); } else diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 364f488fd..ea42aea0a 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -4160,9 +4160,9 @@ bool CGameHandler::buyArtifact(ObjectInstanceID hid, ArtifactID aid) { const CArtifact * art = aid.toArtifact(); COMPLAIN_RET_FALSE_IF(nullptr == art, "Invalid artifact index to buy"); - COMPLAIN_RET_FALSE_IF(art->warMachine == CreatureID::NONE, "War machine artifact required"); + COMPLAIN_RET_FALSE_IF(art->getWarMachine() == CreatureID::NONE, "War machine artifact required"); COMPLAIN_RET_FALSE_IF(hero->hasArt(aid),"Hero already has this machine!"); - const int price = art->price; + const int price = art->getPrice(); COMPLAIN_RET_FALSE_IF(getPlayerState(hero->getOwner())->resources[EGameResID::GOLD] < price, "Not enough gold!"); if ((town->hasBuilt(BuildingID::BLACKSMITH) && town->town->warMachine == aid) diff --git a/test/entity/CArtifactTest.cpp b/test/entity/CArtifactTest.cpp index b4a6b8e23..c3517d89b 100644 --- a/test/entity/CArtifactTest.cpp +++ b/test/entity/CArtifactTest.cpp @@ -32,9 +32,7 @@ protected: TEST_F(CArtifactTest, RegistersIcons) { - subject->iconIndex = 4242; - subject->image = "Test1"; - subject->large = "Test2"; + subject-> setImage(4242, "Test1", "Test2"); auto cb = [this](auto && PH1, auto && PH2, auto && PH3, auto && PH4) {