From d44821e733f38507f5c2d2b4915852cdd40a003e Mon Sep 17 00:00:00 2001 From: SoundSSGood <87084363+SoundSSGood@users.noreply.github.com> Date: Thu, 22 Jun 2023 17:08:16 +0300 Subject: [PATCH] CArtifactInstance moved to own file --- client/widgets/CComponent.cpp | 1 + cmake_modules/VCMI_lib.cmake | 2 + lib/CArtHandler.cpp | 122 +------------------------ lib/CArtHandler.h | 77 ---------------- lib/CArtifactInstance.cpp | 166 ++++++++++++++++++++++++++++++++++ lib/CArtifactInstance.h | 91 +++++++++++++++++++ lib/CCreatureSet.h | 1 + 7 files changed, 262 insertions(+), 198 deletions(-) create mode 100644 lib/CArtifactInstance.cpp create mode 100644 lib/CArtifactInstance.h diff --git a/client/widgets/CComponent.cpp b/client/widgets/CComponent.cpp index 14b1d1bde..0db738cd0 100644 --- a/client/widgets/CComponent.cpp +++ b/client/widgets/CComponent.cpp @@ -34,6 +34,7 @@ #include "../../lib/CGeneralTextHandler.h" #include "../../lib/NetPacksBase.h" #include "../../lib/CArtHandler.h" +#include "../../lib/CArtifactInstance.h" CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize, EFonts font): perDay(false) diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index 4cd741a47..f325bfa4f 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -220,6 +220,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/BattleFieldHandler.cpp ${MAIN_LIB_DIR}/CAndroidVMHelper.cpp ${MAIN_LIB_DIR}/CArtHandler.cpp + ${MAIN_LIB_DIR}/CArtifactInstance.cpp ${MAIN_LIB_DIR}/CBonusTypeHandler.cpp ${MAIN_LIB_DIR}/CBuildingHandler.cpp ${MAIN_LIB_DIR}/CConfigHandler.cpp @@ -550,6 +551,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/BattleFieldHandler.h ${MAIN_LIB_DIR}/CAndroidVMHelper.h ${MAIN_LIB_DIR}/CArtHandler.h + ${MAIN_LIB_DIR}/CArtifactInstance.h ${MAIN_LIB_DIR}/CBonusTypeHandler.h ${MAIN_LIB_DIR}/CBuildingHandler.h ${MAIN_LIB_DIR}/CConfigHandler.h diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 92b9e7a20..e76b7c0d4 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -15,9 +15,7 @@ #include "CGeneralTextHandler.h" #include "CModHandler.h" #include "GameSettings.h" -#include "spells/CSpellHandler.h" #include "mapObjects/MapObjects.h" -#include "NetPacksBase.h" #include "StringConstants.h" #include "mapObjectConstructors/AObjectTypeHandler.h" @@ -763,129 +761,11 @@ void CArtHandler::afterLoadFinalization() CBonusSystemNode::treeHasChanged(); } -CArtifactInstance::CArtifactInstance() -{ - init(); -} - -CArtifactInstance::CArtifactInstance( CArtifact *Art) -{ - init(); - setType(Art); -} - -void CArtifactInstance::setType( CArtifact *Art ) -{ - artType = Art; - attachTo(*Art); -} - -std::string CArtifactInstance::nodeName() const -{ - return "Artifact instance of " + (artType ? artType->getJsonKey() : std::string("uninitialized")) + " type"; -} - -void CArtifactInstance::init() -{ - id = ArtifactInstanceID(); - id = static_cast(ArtifactID::NONE); //to be randomized - setNodeType(ARTIFACT_INSTANCE); -} - -std::string CArtifactInstance::getDescription() const -{ - std::string text = artType->getDescriptionTranslated(); - if(artType->getId() == ArtifactID::SPELL_SCROLL) - ArtifactUtils::insertScrrollSpellName(text, getScrollSpellID()); - return text; -} - -ArtifactID CArtifactInstance::getTypeId() const -{ - return artType->getId(); -} - -bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved) const -{ - return artType->canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved); -} - -void CArtifactInstance::putAt(const ArtifactLocation & al) -{ - al.getHolderArtSet()->putArtifact(al.slot, this); -} - -void CArtifactInstance::removeFrom(const ArtifactLocation & al) -{ - al.getHolderArtSet()->removeArtifact(al.slot); - for(auto & part : partsInfo) - { - if(part.slot != ArtifactPosition::PRE_FIRST) - part.slot = ArtifactPosition::PRE_FIRST; - } -} - -bool CArtifactInstance::canBeDisassembled() const -{ - return artType->canBeDisassembled(); -} - -void CArtifactInstance::move(const ArtifactLocation & src, const ArtifactLocation & dst) -{ - removeFrom(src); - putAt(dst); -} - -void CArtifactInstance::deserializationFix() -{ - setType(artType); - for(PartInfo & part : partsInfo) - attachTo(*part.art); -} - -bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const -{ - if(supposedPart == this) - return true; - - //check for constituents - for(const PartInfo & constituent : partsInfo) - if(constituent.art == supposedPart) - return true; - - return false; -} - -void CCombinedArtifactInstance::addArtInstAsPart(CArtifactInstance * art, const ArtifactPosition & slot) -{ - auto artInst = static_cast(this); - assert(vstd::contains_if(*artInst->artType->constituents, - [=](const CArtifact * partType) - { - return partType->getId() == art->getTypeId(); - })); - assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType); - partsInfo.emplace_back(art, slot); - artInst->attachTo(*art); -} - -SpellID CScrollArtifactInstance::getScrollSpellID() const -{ - auto artInst = static_cast(this); - const auto bonus = artInst->getBonusLocalFirst(Selector::type()(BonusType::SPELL)); - if (!bonus) - { - logMod->warn("Warning: %s doesn't bear any spell!", artInst->nodeName()); - return SpellID::NONE; - } - return SpellID(bonus->subtype); -} - CArtifactSet::~CArtifactSet() = default; const CArtifactInstance * CArtifactSet::getArt(const ArtifactPosition & pos, bool excludeLocked) const { - if(const ArtSlotInfo *si = getSlot(pos)) + if(const ArtSlotInfo * si = getSlot(pos)) { if(si->artifact && (!excludeLocked || !si->locked)) return si->artifact; diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index 8fa7dc0cb..bb2e02516 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -20,9 +20,7 @@ VCMI_LIB_NAMESPACE_BEGIN class CArtHandler; -class CArtifact; class CGHeroInstance; -struct ArtifactLocation; class CArtifactSet; class CArtifactInstance; class CRandomGenerator; @@ -133,81 +131,6 @@ public: } }; -class DLL_LINKAGE CCombinedArtifactInstance -{ -protected: - CCombinedArtifactInstance() = default; -public: - struct PartInfo - { - ConstTransitivePtr art; - ArtifactPosition slot; - template void serialize(Handler & h, const int version) - { - h & art; - h & slot; - } - PartInfo(CArtifactInstance * art = nullptr, const ArtifactPosition & slot = ArtifactPosition::PRE_FIRST) - : art(art), slot(slot) {}; - }; - std::vector partsInfo; - void addArtInstAsPart(CArtifactInstance * art, const ArtifactPosition & slot); -}; - -class DLL_LINKAGE CScrollArtifactInstance -{ -protected: - CScrollArtifactInstance() = default; -public: - SpellID getScrollSpellID() const; -}; - -class DLL_LINKAGE CGrowingArtifactInstance -{ -protected: - CGrowingArtifactInstance() = default; -public: - void growingUp(); -}; - -class DLL_LINKAGE CArtifactInstance - : public CBonusSystemNode, public CCombinedArtifactInstance, public CScrollArtifactInstance, public CGrowingArtifactInstance -{ -protected: - void init(); -public: - CArtifactInstance(CArtifact * art); - CArtifactInstance(); - - ConstTransitivePtr artType; - ArtifactInstanceID id; - - std::string nodeName() const override; - void deserializationFix(); - void setType(CArtifact * art); - - std::string getDescription() const; - - ArtifactID getTypeId() const; - bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; - bool canBeDisassembled() const; - /// Checks if this a part of this artifact: artifact instance is a part - /// of itself, additionally truth is returned for constituents of combined arts - bool isPart(const CArtifactInstance * supposedPart) const; - void putAt(const ArtifactLocation & al); - void removeFrom(const ArtifactLocation & al); - void move(const ArtifactLocation & src, const ArtifactLocation & dst); - - template void serialize(Handler &h, const int version) - { - h & static_cast(*this); - h & artType; - h & id; - h & partsInfo; - BONUS_TREE_DESERIALIZATION_FIX - } -}; - class DLL_LINKAGE CArtHandler : public CHandlerBase { public: diff --git a/lib/CArtifactInstance.cpp b/lib/CArtifactInstance.cpp new file mode 100644 index 000000000..c8dcede34 --- /dev/null +++ b/lib/CArtifactInstance.cpp @@ -0,0 +1,166 @@ +/* + * CArtifactInstance.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include "StdInc.h" +#include "CArtifactInstance.h" + +#include "ArtifactUtils.h" +#include "CArtHandler.h" +#include "NetPacksBase.h" + +void CCombinedArtifactInstance::addArtInstAsPart(CArtifactInstance * art, const ArtifactPosition & slot) +{ + auto artInst = static_cast(this); + assert(vstd::contains_if(*artInst->artType->constituents, + [=](const CArtifact * partType) + { + return partType->getId() == art->getTypeId(); + })); + assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType); + partsInfo.emplace_back(art, slot); + artInst->attachTo(*art); +} + +bool CCombinedArtifactInstance::isPart(const CArtifactInstance * supposedPart) const +{ + if(supposedPart == this) + return true; + + for(const PartInfo & constituent : partsInfo) + { + if(constituent.art == supposedPart) + return true; + } + + return false; +} + +SpellID CScrollArtifactInstance::getScrollSpellID() const +{ + auto artInst = static_cast(this); + const auto bonus = artInst->getBonusLocalFirst(Selector::type()(BonusType::SPELL)); + if(!bonus) + { + logMod->warn("Warning: %s doesn't bear any spell!", artInst->nodeName()); + return SpellID::NONE; + } + return SpellID(bonus->subtype); +} + +void CGrowingArtifactInstance::growingUp() +{ + auto artInst = static_cast(this); + + if(auto growingArtType = dynamic_cast(static_cast(artInst->artType))) + { + auto bonus = std::make_shared(); + bonus->type = BonusType::LEVEL_COUNTER; + bonus->val = 1; + bonus->duration = BonusDuration::COMMANDER_KILLED; + artInst->accumulateBonus(bonus); + + for(const auto & bonus : growingArtType->bonusesPerLevel) + { + // Every n levels + if(artInst->valOfBonuses(BonusType::LEVEL_COUNTER) % bonus.first == 0) + { + artInst->accumulateBonus(std::make_shared(bonus.second)); + } + } + for(const auto & bonus : growingArtType->thresholdBonuses) + { + // At n level + if(artInst->valOfBonuses(BonusType::LEVEL_COUNTER) == bonus.first) + { + artInst->addNewBonus(std::make_shared(bonus.second)); + } + } + } +} + +void CArtifactInstance::init() +{ + // Artifact to be randomized + id = static_cast(ArtifactID::NONE); + setNodeType(ARTIFACT_INSTANCE); +} + +CArtifactInstance::CArtifactInstance(CArtifact * art) +{ + init(); + setType(art); +} + +CArtifactInstance::CArtifactInstance() +{ + init(); +} + +void CArtifactInstance::setType(CArtifact * art) +{ + artType = art; + attachTo(*art); +} + +std::string CArtifactInstance::nodeName() const +{ + return "Artifact instance of " + (artType ? artType->getJsonKey() : std::string("uninitialized")) + " type"; +} + +std::string CArtifactInstance::getDescription() const +{ + std::string text = artType->getDescriptionTranslated(); + if(artType->getId() == ArtifactID::SPELL_SCROLL) + ArtifactUtils::insertScrrollSpellName(text, getScrollSpellID()); + return text; +} + +ArtifactID CArtifactInstance::getTypeId() const +{ + return artType->getId(); +} + +bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved) const +{ + return artType->canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved); +} + +bool CArtifactInstance::canBeDisassembled() const +{ + return artType->canBeDisassembled(); +} + +void CArtifactInstance::putAt(const ArtifactLocation & al) +{ + al.getHolderArtSet()->putArtifact(al.slot, this); +} + +void CArtifactInstance::removeFrom(const ArtifactLocation & al) +{ + al.getHolderArtSet()->removeArtifact(al.slot); + for(auto & part : partsInfo) + { + if(part.slot != ArtifactPosition::PRE_FIRST) + part.slot = ArtifactPosition::PRE_FIRST; + } +} + +void CArtifactInstance::move(const ArtifactLocation & src, const ArtifactLocation & dst) +{ + removeFrom(src); + putAt(dst); +} + +void CArtifactInstance::deserializationFix() +{ + setType(artType); + for(PartInfo & part : partsInfo) + attachTo(*part.art); +} diff --git a/lib/CArtifactInstance.h b/lib/CArtifactInstance.h new file mode 100644 index 000000000..aad5bf1d5 --- /dev/null +++ b/lib/CArtifactInstance.h @@ -0,0 +1,91 @@ +/* + * CArtifactInstance.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ +#pragma once + +#include "bonuses/CBonusSystemNode.h" +#include "GameConstants.h" + +VCMI_LIB_NAMESPACE_BEGIN + +struct ArtifactLocation; + +class DLL_LINKAGE CCombinedArtifactInstance +{ +protected: + CCombinedArtifactInstance() = default; +public: + struct PartInfo + { + ConstTransitivePtr art; + ArtifactPosition slot; + template void serialize(Handler & h, const int version) + { + h & art; + h & slot; + } + PartInfo(CArtifactInstance * art = nullptr, const ArtifactPosition & slot = ArtifactPosition::PRE_FIRST) + : art(art), slot(slot) {}; + }; + std::vector partsInfo; + void addArtInstAsPart(CArtifactInstance * art, const ArtifactPosition & slot); + // Checks if supposed part inst is part of this combined art inst + bool isPart(const CArtifactInstance * supposedPart) const; +}; + +class DLL_LINKAGE CScrollArtifactInstance +{ +protected: + CScrollArtifactInstance() = default; +public: + SpellID getScrollSpellID() const; +}; + +class DLL_LINKAGE CGrowingArtifactInstance +{ +protected: + CGrowingArtifactInstance() = default; +public: + void growingUp(); +}; + +class DLL_LINKAGE CArtifactInstance + : public CBonusSystemNode, public CCombinedArtifactInstance, public CScrollArtifactInstance, public CGrowingArtifactInstance +{ +protected: + void init(); +public: + ConstTransitivePtr artType; + ArtifactInstanceID id; + + CArtifactInstance(CArtifact * art); + CArtifactInstance(); + void setType(CArtifact * art); + std::string nodeName() const override; + std::string getDescription() const; + ArtifactID getTypeId() const; + + bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; + bool canBeDisassembled() const; + void putAt(const ArtifactLocation & al); + void removeFrom(const ArtifactLocation & al); + void move(const ArtifactLocation & src, const ArtifactLocation & dst); + + void deserializationFix(); + template void serialize(Handler & h, const int version) + { + h & static_cast(*this); + h & artType; + h & id; + h & partsInfo; + BONUS_TREE_DESERIALIZATION_FIX + } +}; + +VCMI_LIB_NAMESPACE_END diff --git a/lib/CCreatureSet.h b/lib/CCreatureSet.h index 662fcc9b7..09794f3c8 100644 --- a/lib/CCreatureSet.h +++ b/lib/CCreatureSet.h @@ -13,6 +13,7 @@ #include "bonuses/CBonusSystemNode.h" #include "GameConstants.h" #include "CArtHandler.h" +#include "CArtifactInstance.h" #include "CCreatureHandler.h" #include