Browse Source

Added option to allow self-casting

Ivan Savenko 1 year ago
parent
commit
1194419884

+ 4 - 0
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,
 

+ 1 - 0
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;
 

+ 12 - 9
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);
 }

+ 7 - 0
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<si32>(json["level"].Integer());
 	spell->power = static_cast<si32>(json["power"].Integer());
 

+ 2 - 0
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<spells::ISpellMechanicsFactory> mechanics;//(!) do not serialize

+ 1 - 0
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 &());