2017-07-20 06:08:49 +02:00
|
|
|
/*
|
|
|
|
* TargetCondition.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 "TargetCondition.h"
|
|
|
|
|
|
|
|
#include "../GameConstants.h"
|
|
|
|
#include "../CBonusTypeHandler.h"
|
|
|
|
#include "../battle/CBattleInfoCallback.h"
|
|
|
|
#include "../battle/Unit.h"
|
2023-04-30 16:35:15 +02:00
|
|
|
#include "../bonuses/BonusParams.h"
|
2023-04-30 19:34:25 +02:00
|
|
|
#include "../bonuses/BonusList.h"
|
2023-10-05 20:18:11 +02:00
|
|
|
#include "../bonuses/BonusSubtypes.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../modding/IdentifierStorage.h"
|
|
|
|
#include "../modding/ModUtility.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../serializer/JsonSerializeFormat.h"
|
|
|
|
#include "../VCMI_Lib.h"
|
|
|
|
|
2023-08-19 19:19:59 +02:00
|
|
|
#include <vcmi/spells/Spell.h>
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
|
|
|
|
class TargetConditionItemBase : public TargetConditionItem
|
|
|
|
{
|
|
|
|
public:
|
2023-02-11 17:18:05 +02:00
|
|
|
bool inverted = false;
|
|
|
|
bool exclusive = false;
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
void setInverted(bool value) override
|
|
|
|
{
|
|
|
|
inverted = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setExclusive(bool value) override
|
|
|
|
{
|
|
|
|
exclusive = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExclusive() const override
|
|
|
|
{
|
|
|
|
return exclusive;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReceptive(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
bool result = check(m, target);
|
2019-03-20 21:58:15 +02:00
|
|
|
return inverted != result;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool check(const Mechanics * m, const battle::Unit * target) const = 0;
|
|
|
|
};
|
|
|
|
|
2023-03-09 01:20:21 +02:00
|
|
|
class SelectorCondition : public TargetConditionItemBase
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
public:
|
2023-03-09 01:20:21 +02:00
|
|
|
SelectorCondition(const CSelector & csel):
|
|
|
|
sel(csel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
SelectorCondition(const CSelector & csel, si32 minVal, si32 maxVal):
|
|
|
|
sel(csel),
|
|
|
|
minVal(minVal),
|
|
|
|
maxVal(maxVal)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
2023-03-09 01:20:21 +02:00
|
|
|
if(target->hasBonus(sel)) {
|
|
|
|
auto b = target->valOfBonuses(sel,"");
|
|
|
|
return b >= minVal && b <= maxVal;
|
|
|
|
}
|
|
|
|
return false;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-03-09 01:20:21 +02:00
|
|
|
CSelector sel;
|
|
|
|
si32 minVal = std::numeric_limits<si32>::min();
|
|
|
|
si32 maxVal = std::numeric_limits<si32>::max();
|
2017-07-20 06:08:49 +02:00
|
|
|
};
|
|
|
|
|
2023-05-03 22:45:55 +02:00
|
|
|
class ResistanceCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
if(m->isPositiveSpell()) //Always pass on positive
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return target->magicResistance() < 100;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
class CreatureCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
2023-02-11 17:18:05 +02:00
|
|
|
CreatureCondition(const CreatureID & type_): type(type_) {}
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
return target->creatureId() == type;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CreatureID type;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AbsoluteLevelCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AbsoluteLevelCondition()
|
|
|
|
{
|
|
|
|
inverted = false;
|
|
|
|
exclusive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
2023-03-21 11:18:31 +02:00
|
|
|
|
|
|
|
if(!m->isMagicalEffect()) //Always pass on non-magical
|
|
|
|
return true;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
std::stringstream cachingStr;
|
2023-05-01 00:20:01 +02:00
|
|
|
cachingStr << "type_" << vstd::to_underlying(BonusType::LEVEL_SPELL_IMMUNITY) << "addInfo_1";
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
TConstBonusListPtr levelImmunities = target->getBonuses(Selector::type()(BonusType::LEVEL_SPELL_IMMUNITY).And(Selector::info()(1)), cachingStr.str());
|
2023-03-21 16:56:15 +02:00
|
|
|
return (levelImmunities->size() == 0 || levelImmunities->totalValue() < m->getSpellLevel() || m->getSpellLevel() <= 0);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AbsoluteSpellCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AbsoluteSpellCondition()
|
|
|
|
{
|
|
|
|
inverted = false;
|
|
|
|
exclusive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
std::stringstream cachingStr;
|
2023-05-01 00:20:01 +02:00
|
|
|
cachingStr << "type_" << vstd::to_underlying(BonusType::SPELL_IMMUNITY) << "subtype_" << m->getSpellIndex() << "addInfo_1";
|
2023-10-05 15:13:52 +02:00
|
|
|
return !target->hasBonus(Selector::typeSubtypeInfo(BonusType::SPELL_IMMUNITY, TBonusSubtype(m->getSpellId()), 1), cachingStr.str());
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ElementalCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ElementalCondition()
|
|
|
|
{
|
|
|
|
inverted = true;
|
|
|
|
exclusive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
bool elementalImmune = false;
|
2023-08-19 19:19:59 +02:00
|
|
|
auto bearer = target->getBonusBearer();
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-08-21 21:10:32 +02:00
|
|
|
m->getSpell()->forEachSchool([&](const SpellSchool & cnf, bool & stop)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2023-10-05 15:13:52 +02:00
|
|
|
if (bearer->hasBonusOfType(BonusType::SPELL_SCHOOL_IMMUNITY, TBonusSubtype(cnf)))
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
elementalImmune = true;
|
2023-08-19 19:19:59 +02:00
|
|
|
stop = true; //only bonus from one school is used
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
else if(!m->isPositiveSpell()) //negative or indifferent
|
|
|
|
{
|
2023-10-05 15:13:52 +02:00
|
|
|
if (bearer->hasBonusOfType(BonusType::NEGATIVE_EFFECTS_IMMUNITY, TBonusSubtype(cnf)))
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
elementalImmune = true;
|
2023-08-19 19:19:59 +02:00
|
|
|
stop = true; //only bonus from one school is used
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-19 19:19:59 +02:00
|
|
|
});
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
return elementalImmune;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NormalLevelCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NormalLevelCondition()
|
|
|
|
{
|
|
|
|
inverted = false;
|
|
|
|
exclusive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
2023-03-21 11:18:31 +02:00
|
|
|
if(!m->isMagicalEffect()) //Always pass on non-magical
|
|
|
|
return true;
|
2023-05-01 00:20:01 +02:00
|
|
|
TConstBonusListPtr levelImmunities = target->getBonuses(Selector::type()(BonusType::LEVEL_SPELL_IMMUNITY));
|
2019-03-29 13:10:26 +02:00
|
|
|
return levelImmunities->size() == 0 ||
|
|
|
|
levelImmunities->totalValue() < m->getSpellLevel() ||
|
|
|
|
m->getSpellLevel() <= 0;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NormalSpellCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NormalSpellCondition()
|
|
|
|
{
|
|
|
|
inverted = false;
|
|
|
|
exclusive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
2023-10-05 15:13:52 +02:00
|
|
|
return !target->hasBonusOfType(BonusType::SPELL_IMMUNITY, TBonusSubtype(m->getSpellId()));
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//for Hypnotize
|
|
|
|
class HealthValueCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
//todo: maybe do not resist on passive cast
|
|
|
|
//TODO: what with other creatures casting hypnotize, Faerie Dragons style?
|
|
|
|
int64_t subjectHealth = target->getAvailableHealth();
|
|
|
|
//apply 'damage' bonus for hypnotize, including hero specialty
|
|
|
|
auto maxHealth = m->applySpellBonus(m->getEffectValue(), target);
|
2019-03-20 21:58:15 +02:00
|
|
|
return subjectHealth <= maxHealth;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SpellEffectCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
public:
|
2023-02-11 17:18:05 +02:00
|
|
|
SpellEffectCondition(const SpellID & spellID_): spellID(spellID_)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
std::stringstream builder;
|
2023-05-01 00:20:01 +02:00
|
|
|
builder << "source_" << vstd::to_underlying(BonusSource::SPELL_EFFECT) << "id_" << spellID.num;
|
2017-07-20 06:08:49 +02:00
|
|
|
cachingString = builder.str();
|
|
|
|
|
2023-10-10 17:05:18 +02:00
|
|
|
selector = Selector::source(BonusSource::SPELL_EFFECT, TBonusSourceID(spellID));
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
return target->hasBonus(selector, cachingString);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CSelector selector;
|
|
|
|
std::string cachingString;
|
|
|
|
SpellID spellID;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReceptiveFeatureCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
|
|
|
return m->isPositiveSpell() && target->hasBonus(selector, cachingString);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-05-01 00:20:01 +02:00
|
|
|
CSelector selector = Selector::type()(BonusType::RECEPTIVE);
|
2023-02-11 17:18:05 +02:00
|
|
|
std::string cachingString = "type_RECEPTIVE";
|
2017-07-20 06:08:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ImmunityNegationCondition : public TargetConditionItemBase
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool check(const Mechanics * m, const battle::Unit * target) const override
|
|
|
|
{
|
2023-10-05 15:13:52 +02:00
|
|
|
const bool battleWideNegation = target->hasBonusOfType(BonusType::NEGATE_ALL_NATURAL_IMMUNITIES, BonusSubtypes::immunityBattleWide);
|
|
|
|
const bool heroNegation = target->hasBonusOfType(BonusType::NEGATE_ALL_NATURAL_IMMUNITIES, BonusSubtypes::immunityEnemyHero);
|
2023-04-10 17:41:25 +02:00
|
|
|
//Non-magical effects is not affected by orb of vulnerability
|
|
|
|
if(!m->isMagicalEffect())
|
|
|
|
return false;
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
//anyone can cast on artifact holder`s stacks
|
|
|
|
if(heroNegation)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//this stack is from other player
|
|
|
|
else if(battleWideNegation)
|
|
|
|
{
|
2022-08-22 03:30:38 +02:00
|
|
|
if(m->ownerMatches(target, false))
|
2017-07-20 06:08:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DefaultTargetConditionItemFactory : public TargetConditionItemFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Object createAbsoluteLevel() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> antimagicCondition = std::make_shared<AbsoluteLevelCondition>();
|
|
|
|
return antimagicCondition;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object createAbsoluteSpell() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> alCondition = std::make_shared<AbsoluteSpellCondition>();
|
|
|
|
return alCondition;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object createElemental() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> elementalCondition = std::make_shared<ElementalCondition>();
|
|
|
|
return elementalCondition;
|
|
|
|
}
|
|
|
|
|
2023-05-03 22:45:55 +02:00
|
|
|
Object createResistance() const override
|
|
|
|
{
|
|
|
|
static auto elementalCondition = std::make_shared<ResistanceCondition>();
|
|
|
|
return elementalCondition;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
Object createNormalLevel() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> nlCondition = std::make_shared<NormalLevelCondition>();
|
|
|
|
return nlCondition;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object createNormalSpell() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> nsCondition = std::make_shared<NormalSpellCondition>();
|
|
|
|
return nsCondition;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object createConfigurable(std::string scope, std::string type, std::string identifier) const override
|
|
|
|
{
|
|
|
|
if(type == "bonus")
|
|
|
|
{
|
|
|
|
//TODO: support custom bonus types
|
|
|
|
|
|
|
|
auto it = bonusNameMap.find(identifier);
|
|
|
|
if(it != bonusNameMap.end())
|
2023-03-09 01:20:21 +02:00
|
|
|
return std::make_shared<SelectorCondition>(Selector::type()(it->second));
|
2023-03-21 16:56:15 +02:00
|
|
|
|
2023-03-09 01:20:21 +02:00
|
|
|
auto params = BonusParams(identifier, "", -1);
|
|
|
|
if(params.isConverted)
|
|
|
|
{
|
2023-05-05 18:08:36 +02:00
|
|
|
if(params.val)
|
|
|
|
return std::make_shared<SelectorCondition>(params.toSelector(), *params.val, *params.val);
|
2023-03-09 01:20:21 +02:00
|
|
|
return std::make_shared<SelectorCondition>(params.toSelector());
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
logMod->error("Invalid bonus type %s in spell target condition.", identifier);
|
|
|
|
}
|
|
|
|
else if(type == "creature")
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
auto rawId = VLC->identifiers()->getIdentifier(scope, type, identifier, true);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
if(rawId)
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::make_shared<CreatureCondition>(CreatureID(rawId.value()));
|
2017-07-20 06:08:49 +02:00
|
|
|
else
|
|
|
|
logMod->error("Invalid creature %s type in spell target condition.", identifier);
|
|
|
|
}
|
|
|
|
else if(type == "spell")
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
auto rawId = VLC->identifiers()->getIdentifier(scope, type, identifier, true);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
if(rawId)
|
2023-04-16 19:42:56 +02:00
|
|
|
return std::make_shared<SpellEffectCondition>(SpellID(rawId.value()));
|
2017-07-20 06:08:49 +02:00
|
|
|
else
|
|
|
|
logMod->error("Invalid spell %s in spell target condition.", identifier);
|
|
|
|
}
|
|
|
|
else if(type == "healthValueSpecial")
|
|
|
|
{
|
|
|
|
return std::make_shared<HealthValueCondition>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logMod->error("Invalid type %s in spell target condition.", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object();
|
|
|
|
}
|
|
|
|
|
2023-03-09 01:20:21 +02:00
|
|
|
Object createFromJsonStruct(const JsonNode & jsonStruct) const override
|
2023-03-21 16:56:15 +02:00
|
|
|
{
|
2023-03-09 01:20:21 +02:00
|
|
|
auto type = jsonStruct["type"].String();
|
|
|
|
auto parameters = jsonStruct["parameters"];
|
|
|
|
if(type == "selector")
|
|
|
|
{
|
|
|
|
auto minVal = std::numeric_limits<si32>::min();
|
|
|
|
auto maxVal = std::numeric_limits<si32>::max();
|
|
|
|
if(parameters["minVal"].isNumber())
|
|
|
|
minVal = parameters["minVal"].Integer();
|
|
|
|
if(parameters["maxVal"].isNumber())
|
|
|
|
maxVal = parameters["maxVal"].Integer();
|
|
|
|
auto sel = JsonUtils::parseSelector(parameters);
|
|
|
|
return std::make_shared<SelectorCondition>(sel, minVal, maxVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
logMod->error("Invalid type %s in spell target condition.", type);
|
|
|
|
return Object();
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
Object createReceptiveFeature() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> condition = std::make_shared<ReceptiveFeatureCondition>();
|
|
|
|
return condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object createImmunityNegation() const override
|
|
|
|
{
|
|
|
|
static std::shared_ptr<TargetConditionItem> condition = std::make_shared<ImmunityNegationCondition>();
|
|
|
|
return condition;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const TargetConditionItemFactory * TargetConditionItemFactory::getDefault()
|
|
|
|
{
|
|
|
|
static std::unique_ptr<TargetConditionItemFactory> singleton;
|
|
|
|
|
|
|
|
if(!singleton)
|
2022-12-07 23:36:20 +02:00
|
|
|
singleton = std::make_unique<DefaultTargetConditionItemFactory>();
|
2017-07-20 06:08:49 +02:00
|
|
|
return singleton.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TargetCondition::isReceptive(const Mechanics * m, const battle::Unit * target) const
|
|
|
|
{
|
|
|
|
if(!check(absolute, m, target))
|
|
|
|
return false;
|
|
|
|
|
2023-02-11 17:18:05 +02:00
|
|
|
for(const auto & item : negation)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(item->isReceptive(m, target))
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-20 21:58:15 +02:00
|
|
|
return check(normal, m, target);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TargetCondition::serializeJson(JsonSerializeFormat & handler, const ItemFactory * itemFactory)
|
|
|
|
{
|
|
|
|
if(handler.saving)
|
|
|
|
{
|
|
|
|
logGlobal->error("Spell target condition saving is not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
absolute.clear();
|
|
|
|
normal.clear();
|
|
|
|
negation.clear();
|
|
|
|
|
|
|
|
absolute.push_back(itemFactory->createAbsoluteSpell());
|
2023-03-21 11:18:31 +02:00
|
|
|
absolute.push_back(itemFactory->createAbsoluteLevel());
|
|
|
|
normal.push_back(itemFactory->createElemental());
|
2023-05-03 22:45:55 +02:00
|
|
|
normal.push_back(itemFactory->createResistance());
|
2023-03-21 11:18:31 +02:00
|
|
|
normal.push_back(itemFactory->createNormalLevel());
|
|
|
|
normal.push_back(itemFactory->createNormalSpell());
|
|
|
|
negation.push_back(itemFactory->createReceptiveFeature());
|
|
|
|
negation.push_back(itemFactory->createImmunityNegation());
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
auto anyOf = handler.enterStruct("anyOf");
|
|
|
|
loadConditions(anyOf->getCurrent(), false, false, itemFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto allOf = handler.enterStruct("allOf");
|
|
|
|
loadConditions(allOf->getCurrent(), true, false, itemFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto noneOf = handler.enterStruct("noneOf");
|
|
|
|
loadConditions(noneOf->getCurrent(), true, true, itemFactory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TargetCondition::check(const ItemVector & condition, const Mechanics * m, const battle::Unit * target) const
|
|
|
|
{
|
|
|
|
bool nonExclusiveCheck = false;
|
|
|
|
bool nonExclusiveExits = false;
|
|
|
|
|
2023-02-11 17:18:05 +02:00
|
|
|
for(const auto & item : condition)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(item->isExclusive())
|
|
|
|
{
|
|
|
|
if(!item->isReceptive(m, target))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(item->isReceptive(m, target))
|
|
|
|
nonExclusiveCheck = true;
|
|
|
|
nonExclusiveExits = true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 21:58:15 +02:00
|
|
|
return !nonExclusiveExits || nonExclusiveCheck;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TargetCondition::loadConditions(const JsonNode & source, bool exclusive, bool inverted, const ItemFactory * itemFactory)
|
|
|
|
{
|
2023-02-11 17:18:05 +02:00
|
|
|
for(const auto & keyValue : source.Struct())
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
bool isAbsolute;
|
|
|
|
|
|
|
|
const JsonNode & value = keyValue.second;
|
|
|
|
|
|
|
|
if(value.String() == "absolute")
|
|
|
|
isAbsolute = true;
|
|
|
|
else if(value.String() == "normal")
|
|
|
|
isAbsolute = false;
|
2023-03-09 01:20:21 +02:00
|
|
|
else if(value.isStruct()) //assume conditions have a new struct format
|
|
|
|
isAbsolute = value["absolute"].Bool();
|
2017-07-20 06:08:49 +02:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2023-03-09 01:20:21 +02:00
|
|
|
std::shared_ptr<TargetConditionItem> item;
|
|
|
|
if(value.isStruct())
|
|
|
|
item = itemFactory->createFromJsonStruct(value);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string scope;
|
|
|
|
std::string type;
|
|
|
|
std::string identifier;
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-07-30 19:12:25 +02:00
|
|
|
ModUtility::parseIdentifier(keyValue.first, scope, type, identifier);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-07-15 12:05:27 +02:00
|
|
|
item = itemFactory->createConfigurable(keyValue.second.meta, type, identifier);
|
2023-03-09 01:20:21 +02:00
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
if(item)
|
|
|
|
{
|
|
|
|
item->setExclusive(exclusive);
|
|
|
|
item->setInverted(inverted);
|
|
|
|
|
|
|
|
if(isAbsolute)
|
|
|
|
absolute.push_back(item);
|
|
|
|
else
|
|
|
|
normal.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|