diff --git a/docs/modders/Entities_Format/Spell_Format.md b/docs/modders/Entities_Format/Spell_Format.md index 350851acf..0fa81c50e 100644 --- a/docs/modders/Entities_Format/Spell_Format.md +++ b/docs/modders/Entities_Format/Spell_Format.md @@ -61,6 +61,10 @@ "positive": true, }, + // If true, then creature capable of casting this spell can cast this spell on itself + // If false, then creature can only cast this spell on other units + "canCastOnSelf" : false, + // If true, spell won't be available on a map without water "onlyOnWaterMap" : true, diff --git a/include/vcmi/spells/Spell.h b/include/vcmi/spells/Spell.h index 60e7b8023..1822639e1 100644 --- a/include/vcmi/spells/Spell.h +++ b/include/vcmi/spells/Spell.h @@ -44,6 +44,7 @@ public: virtual bool isMagical() const = 0; //Should this spell considered as magical effect or as ability (like dendroid's bind) virtual bool hasSchool(SpellSchool school) const = 0; + virtual bool canCastOnSelf() const = 0; virtual void forEachSchool(const SchoolCallback & cb) const = 0; virtual int32_t getCost(const int32_t skillLevel) const = 0; diff --git a/lib/spells/BattleSpellMechanics.cpp b/lib/spells/BattleSpellMechanics.cpp index d4a80902d..42ba4458c 100644 --- a/lib/spells/BattleSpellMechanics.cpp +++ b/lib/spells/BattleSpellMechanics.cpp @@ -215,17 +215,20 @@ bool BattleSpellMechanics::canBeCastAt(const Target & target, Problem & problem) const battle::Unit * mainTarget = nullptr; - if(spellTarget.front().unitValue) + if (!getSpell()->canCastOnSelf()) { - mainTarget = target.front().unitValue; - } - else if(spellTarget.front().hexValue.isValid()) - { - mainTarget = battle()->battleGetUnitByPos(target.front().hexValue, true); - } + if(spellTarget.front().unitValue) + { + mainTarget = target.front().unitValue; + } + else if(spellTarget.front().hexValue.isValid()) + { + mainTarget = battle()->battleGetUnitByPos(target.front().hexValue, true); + } - if (mainTarget && mainTarget == caster) - return false; // can't cast on self + if (mainTarget && mainTarget == caster) + return false; // can't cast on self + } return effects->applicable(problem, this, target, spellTarget); } diff --git a/lib/spells/CSpellHandler.cpp b/lib/spells/CSpellHandler.cpp index aeceb6f7f..9d1e4d7fc 100644 --- a/lib/spells/CSpellHandler.cpp +++ b/lib/spells/CSpellHandler.cpp @@ -76,6 +76,7 @@ CSpell::CSpell(): power(0), combat(false), creatureAbility(false), + castOnSelf(false), positiveness(ESpellPositiveness::NEUTRAL), defaultProbability(0), rising(false), @@ -285,6 +286,11 @@ bool CSpell::hasBattleEffects() const return levels[0].battleEffects.getType() == JsonNode::JsonType::DATA_STRUCT && !levels[0].battleEffects.Struct().empty(); } +bool CSpell::canCastOnSelf() const +{ + return castOnSelf; +} + const std::string & CSpell::getIconImmune() const { return iconImmune; @@ -702,6 +708,7 @@ CSpell * CSpellHandler::loadFromJson(const std::string & scope, const JsonNode & spell->school[info.id] = schoolNames[info.jsonName].Bool(); } + spell->castOnSelf = json["canCastOnSelf"].Bool(); spell->level = static_cast(json["level"].Integer()); spell->power = static_cast(json["power"].Integer()); diff --git a/lib/spells/CSpellHandler.h b/lib/spells/CSpellHandler.h index efe807831..666e58b51 100644 --- a/lib/spells/CSpellHandler.h +++ b/lib/spells/CSpellHandler.h @@ -203,6 +203,7 @@ public: int64_t calculateDamage(const spells::Caster * caster) const override; bool hasSchool(SpellSchool school) const override; + bool canCastOnSelf() const override; /** * Calls cb for each school this spell belongs to @@ -329,6 +330,7 @@ private: si32 power; //spell's power bool combat; //is this spell combat (true) or adventure (false) bool creatureAbility; //if true, only creatures can use this spell + bool castOnSelf; // if set, creature caster can cast this spell on itself si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative std::unique_ptr mechanics;//(!) do not serialize diff --git a/test/mock/mock_spells_Spell.h b/test/mock/mock_spells_Spell.h index 3e8aa442e..ccd33d129 100644 --- a/test/mock/mock_spells_Spell.h +++ b/test/mock/mock_spells_Spell.h @@ -45,6 +45,7 @@ public: MOCK_CONST_METHOD0(isOffensive, bool()); MOCK_CONST_METHOD0(isSpecial, bool()); MOCK_CONST_METHOD0(isMagical, bool()); + MOCK_CONST_METHOD0(canCastOnSelf, bool()); MOCK_CONST_METHOD1(hasSchool, bool(SpellSchool)); MOCK_CONST_METHOD1(forEachSchool, void(const SchoolCallback &)); MOCK_CONST_METHOD0(getCastSound, const std::string &());