2023-06-22 16:08:16 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2023-10-23 15:38:05 +02:00
|
|
|
#include "networkPacks/ArtifactLocation.h"
|
2023-06-22 16:08:16 +02:00
|
|
|
|
2023-06-30 20:02:57 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2023-07-03 18:15:40 +02:00
|
|
|
void CCombinedArtifactInstance::addPart(CArtifactInstance * art, const ArtifactPosition & slot)
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
|
|
|
auto artInst = static_cast<CArtifactInstance*>(this);
|
2023-07-11 14:20:32 +02:00
|
|
|
assert(vstd::contains_if(artInst->artType->getConstituents(),
|
2023-06-22 16:08:16 +02:00
|
|
|
[=](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;
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:15:40 +02:00
|
|
|
const std::vector<CCombinedArtifactInstance::PartInfo> & CCombinedArtifactInstance::getPartsInfo() const
|
|
|
|
{
|
|
|
|
return partsInfo;
|
|
|
|
}
|
|
|
|
|
2024-09-06 16:59:40 +02:00
|
|
|
void CCombinedArtifactInstance::addPlacementMap(const CArtifactSet::ArtPlacementMap & placementMap)
|
2023-07-24 18:09:17 +02:00
|
|
|
{
|
|
|
|
if(!placementMap.empty())
|
2023-09-19 13:10:25 +02:00
|
|
|
for(auto & part : partsInfo)
|
2023-07-24 18:09:17 +02:00
|
|
|
{
|
2024-09-06 16:59:40 +02:00
|
|
|
if(placementMap.find(part.art) != placementMap.end())
|
|
|
|
part.slot = placementMap.at(part.art);
|
2023-07-24 18:09:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:08:16 +02:00
|
|
|
SpellID CScrollArtifactInstance::getScrollSpellID() const
|
|
|
|
{
|
|
|
|
auto artInst = static_cast<const CArtifactInstance*>(this);
|
2024-01-16 18:14:40 +02:00
|
|
|
const auto bonus = artInst->getFirstBonus(Selector::type()(BonusType::SPELL));
|
2023-06-22 16:08:16 +02:00
|
|
|
if(!bonus)
|
|
|
|
return SpellID::NONE;
|
2023-10-05 15:13:52 +02:00
|
|
|
return bonus->subtype.as<SpellID>();
|
2023-06-22 16:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGrowingArtifactInstance::growingUp()
|
|
|
|
{
|
|
|
|
auto artInst = static_cast<CArtifactInstance*>(this);
|
|
|
|
|
2023-06-29 17:34:07 +02:00
|
|
|
if(artInst->artType->isGrowing())
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
2023-06-29 17:34:07 +02:00
|
|
|
|
2023-06-22 16:08:16 +02:00
|
|
|
auto bonus = std::make_shared<Bonus>();
|
|
|
|
bonus->type = BonusType::LEVEL_COUNTER;
|
|
|
|
bonus->val = 1;
|
|
|
|
bonus->duration = BonusDuration::COMMANDER_KILLED;
|
|
|
|
artInst->accumulateBonus(bonus);
|
|
|
|
|
2023-07-03 18:15:40 +02:00
|
|
|
for(const auto & bonus : artInst->artType->getBonusesPerLevel())
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
|
|
|
// Every n levels
|
|
|
|
if(artInst->valOfBonuses(BonusType::LEVEL_COUNTER) % bonus.first == 0)
|
|
|
|
{
|
|
|
|
artInst->accumulateBonus(std::make_shared<Bonus>(bonus.second));
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 18:15:40 +02:00
|
|
|
for(const auto & bonus : artInst->artType->getThresholdBonuses())
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2023-12-31 23:43:35 +02:00
|
|
|
CArtifactInstance::CArtifactInstance(const CArtifact * art)
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
setType(art);
|
|
|
|
}
|
|
|
|
|
|
|
|
CArtifactInstance::CArtifactInstance()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2023-12-31 23:43:35 +02:00
|
|
|
void CArtifactInstance::setType(const CArtifact * art)
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
|
|
|
artType = art;
|
2024-01-01 12:49:17 +02:00
|
|
|
attachToSource(*art);
|
2023-06-22 16:08:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2023-06-29 17:34:07 +02:00
|
|
|
if(artType->isScroll())
|
2023-06-22 16:08:16 +02:00
|
|
|
ArtifactUtils::insertScrrollSpellName(text, getScrollSpellID());
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArtifactID CArtifactInstance::getTypeId() const
|
|
|
|
{
|
|
|
|
return artType->getId();
|
|
|
|
}
|
|
|
|
|
2023-07-03 22:11:56 +02:00
|
|
|
ArtifactInstanceID CArtifactInstance::getId() const
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CArtifactInstance::setId(ArtifactInstanceID id)
|
|
|
|
{
|
|
|
|
this->id = id;
|
|
|
|
}
|
|
|
|
|
2023-10-14 21:00:39 +02:00
|
|
|
bool CArtifactInstance::canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) const
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
2023-10-14 21:00:39 +02:00
|
|
|
return artType->canBePutAt(artSet, slot, assumeDestRemoved);
|
2023-06-22 16:08:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:34:07 +02:00
|
|
|
bool CArtifactInstance::isCombined() const
|
2023-06-22 16:08:16 +02:00
|
|
|
{
|
2023-06-29 17:34:07 +02:00
|
|
|
return artType->isCombined();
|
2023-06-22 16:08:16 +02:00
|
|
|
}
|
|
|
|
|
2023-11-13 23:16:02 +02:00
|
|
|
bool CArtifactInstance::isScroll() const
|
|
|
|
{
|
|
|
|
return artType->isScroll();
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:08:16 +02:00
|
|
|
void CArtifactInstance::deserializationFix()
|
|
|
|
{
|
|
|
|
setType(artType);
|
|
|
|
for(PartInfo & part : partsInfo)
|
|
|
|
attachTo(*part.art);
|
|
|
|
}
|
2023-06-30 20:02:57 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|