1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

CArtifactInstance moved to own file

This commit is contained in:
SoundSSGood 2023-06-22 17:08:16 +03:00
parent 58fc2efd41
commit d44821e733
7 changed files with 262 additions and 198 deletions

View File

@ -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)

View File

@ -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

View File

@ -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<ArtifactInstanceID>(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<CArtifactInstance*>(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<const CArtifactInstance*>(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;

View File

@ -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<CArtifactInstance> art;
ArtifactPosition slot;
template <typename Handler> 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<PartInfo> 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<CArtifact> 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 <typename Handler> void serialize(Handler &h, const int version)
{
h & static_cast<CBonusSystemNode&>(*this);
h & artType;
h & id;
h & partsInfo;
BONUS_TREE_DESERIALIZATION_FIX
}
};
class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
{
public:

166
lib/CArtifactInstance.cpp Normal file
View File

@ -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<CArtifactInstance*>(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<const CArtifactInstance*>(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<CArtifactInstance*>(this);
if(auto growingArtType = dynamic_cast<const CGrowingArtifact*>(static_cast<const CArtifact*>(artInst->artType)))
{
auto bonus = std::make_shared<Bonus>();
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>(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>(bonus.second));
}
}
}
}
void CArtifactInstance::init()
{
// Artifact to be randomized
id = static_cast<ArtifactInstanceID>(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);
}

91
lib/CArtifactInstance.h Normal file
View File

@ -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<CArtifactInstance> art;
ArtifactPosition slot;
template <typename Handler> 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<PartInfo> 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<CArtifact> 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 <typename Handler> void serialize(Handler & h, const int version)
{
h & static_cast<CBonusSystemNode&>(*this);
h & artType;
h & id;
h & partsInfo;
BONUS_TREE_DESERIALIZATION_FIX
}
};
VCMI_LIB_NAMESPACE_END

View File

@ -13,6 +13,7 @@
#include "bonuses/CBonusSystemNode.h"
#include "GameConstants.h"
#include "CArtHandler.h"
#include "CArtifactInstance.h"
#include "CCreatureHandler.h"
#include <vcmi/Entity.h>