1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Issue2888 (#421)

Fixed issue 2888
* Merged AFTER_ATTACK & BEFORE_ATTACK cast modes.
* Introduced new caster class for creature ability usage
* Added few tests
This commit is contained in:
Alexander Shishkin
2018-03-02 13:22:51 +03:00
committed by GitHub
parent 386805c4e0
commit 8b002ad774
26 changed files with 450 additions and 170 deletions

View File

@@ -18,6 +18,8 @@
#include "../lib/CArtHandler.h"
#include "../lib/CBuildingHandler.h"
#include "../lib/CHeroHandler.h"
#include "../lib/spells/AbilityCaster.h"
#include "../lib/spells/BonusCaster.h"
#include "../lib/spells/CSpellHandler.h"
#include "../lib/spells/ISpellMechanics.h"
#include "../lib/spells/Problem.h"
@@ -141,11 +143,6 @@ public:
logGlobal->error("Unexpected call to ObstacleCasterProxy::getCasterName");
}
void getCastDescription(const Spell * spell, MetaString & text) const override
{
logGlobal->error("Unexpected call to ObstacleCasterProxy::getCastDescription");
}
void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override
{
logGlobal->error("Unexpected call to ObstacleCasterProxy::getCastDescription");
@@ -163,95 +160,6 @@ private:
const SpellCreatedObstacle * obs;
};
class BonusCasterProxy : public Caster
{
public:
BonusCasterProxy(const CGHeroInstance * hero_, std::shared_ptr<const Bonus> bonus_)
: hero(hero_),
bonus(bonus_)
{
}
~BonusCasterProxy() = default;
ui8 getSpellSchoolLevel(const Spell * spell, int * outSelectedSchool = nullptr) const override
{
return hero->getSpellSchoolLevel(spell, outSelectedSchool);
}
int getEffectLevel(const Spell * spell) const override
{
return hero->getEffectLevel(spell);
}
int64_t getSpellBonus(const Spell * spell, int64_t base, const battle::Unit * affectedStack) const override
{
return hero->getSpellBonus(spell, base, affectedStack);
}
int64_t getSpecificSpellBonus(const Spell * spell, int64_t base) const override
{
return hero->getSpecificSpellBonus(spell, base);
}
int getEffectPower(const Spell * spell) const override
{
return hero->getEffectPower(spell);
}
int getEnchantPower(const Spell * spell) const override
{
return hero->getEnchantPower(spell);
}
int64_t getEffectValue(const Spell * spell) const override
{
return hero->getEffectValue(spell);
}
const PlayerColor getOwner() const override
{
return hero->getOwner();
}
void getCasterName(MetaString & text) const override
{
if(!bonus->description.empty())
text.addReplacement(bonus->description);
else
hero->getCasterName(text);
}
void getCastDescription(const Spell * spell, MetaString & text) const override
{
text.addTxt(MetaString::GENERAL_TXT, 196);
getCasterName(text);
text.addReplacement(MetaString::SPELL_NAME, spell->getIndex());
}
void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override
{
const bool singleTarget = attacked.size() == 1;
const int textIndex = singleTarget ? 195 : 196;
text.addTxt(MetaString::GENERAL_TXT, textIndex);
getCasterName(text);
text.addReplacement(MetaString::SPELL_NAME, spell->getIndex());
if(singleTarget)
attacked.at(0)->addNameReplacement(text, true);
}
void spendMana(const PacketSender * server, const int spellCost) const override
{
logGlobal->error("Unexpected call to BonusCasterProxy::spendMana");
}
private:
const CGHeroInstance * hero;
std::shared_ptr<const Bonus> bonus;
};
}//
CondSh<bool> battleMadeAction(false);
@@ -5530,10 +5438,8 @@ bool CGameHandler::dig(const CGHeroInstance *h)
return true;
}
void CGameHandler::attackCasting(bool ranged, Bonus::BonusType attackMode, const CStack * attacker, const CStack * defender)
void CGameHandler::attackCasting(bool ranged, Bonus::BonusType attackMode, const battle::Unit * attacker, const battle::Unit * defender)
{
spells::Mode mode = (attackMode == Bonus::SPELL_AFTER_ATTACK) ? spells::Mode::AFTER_ATTACK : spells::Mode::BEFORE_ATTACK;
if(attacker->hasBonusOfType(attackMode))
{
std::set<SpellID> spellsToCast;
@@ -5563,7 +5469,9 @@ void CGameHandler::attackCasting(bool ranged, Bonus::BonusType attackMode, const
vstd::amin(chance, 100);
const CSpell * spell = SpellID(spellID).toSpell();
if(!spell->canBeCastAt(gs->curB, mode, attacker, defender->getPosition()))
spells::AbilityCaster caster(attacker, spellLevel);
if(!spell->canBeCastAt(gs->curB, spells::Mode::PASSIVE, &caster, defender->getPosition()))
continue;
//check if spell should be cast (probability handling)
@@ -5573,8 +5481,7 @@ void CGameHandler::attackCasting(bool ranged, Bonus::BonusType attackMode, const
//casting
if(castMe)
{
spells::BattleCast parameters(gs->curB, attacker, mode, spell);
parameters.setSpellLevel(spellLevel);
spells::BattleCast parameters(gs->curB, &caster, spells::Mode::PASSIVE, spell);
parameters.aimToUnit(defender);
parameters.cast(spellEnv);
}
@@ -5623,8 +5530,9 @@ void CGameHandler::handleAfterAttackCasting(bool ranged, const CStack * attacker
//TODO: death stare was not originally available for multiple-hex attacks, but...
const CSpell * spell = SpellID(SpellID::DEATH_STARE).toSpell();
spells::BattleCast parameters(gs->curB, attacker, spells::Mode::AFTER_ATTACK, spell);
parameters.setSpellLevel(0);
spells::AbilityCaster caster(attacker, 0);
spells::BattleCast parameters(gs->curB, &caster, spells::Mode::PASSIVE, spell);
parameters.aimToUnit(defender);
parameters.setEffectValue(staredCreatures);
parameters.cast(spellEnv);
@@ -5646,8 +5554,9 @@ void CGameHandler::handleAfterAttackCasting(bool ranged, const CStack * attacker
{
const CSpell * spell = SpellID(SpellID::ACID_BREATH_DAMAGE).toSpell();
spells::BattleCast parameters(gs->curB, attacker, spells::Mode::AFTER_ATTACK, spell);
parameters.setSpellLevel(0);
spells::AbilityCaster caster(attacker, 0);
spells::BattleCast parameters(gs->curB, &caster, spells::Mode::PASSIVE, spell);
parameters.aimToUnit(defender);
parameters.setEffectValue(acidDamage * attacker->getCount());
parameters.cast(spellEnv);
@@ -6066,7 +5975,7 @@ void CGameHandler::runBattle()
for (auto b : *bl)
{
spells::BonusCasterProxy caster(h, b);
spells::BonusCaster caster(h, b);
const CSpell * spell = SpellID(b->subtype).toSpell();