CreatureSpellMechanics.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * CreatureSpellMechanics.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CreatureSpellMechanics.h"
  12. #include "../NetPacks.h"
  13. #include "../CStack.h"
  14. #include "../battle/BattleInfo.h"
  15. ///AcidBreathDamageMechanics
  16. AcidBreathDamageMechanics::AcidBreathDamageMechanics(const CSpell * s):
  17. DefaultSpellMechanics(s)
  18. {
  19. }
  20. void AcidBreathDamageMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
  21. {
  22. //todo: this should be effectValue
  23. //calculating dmg to display
  24. ctx.setDamageToDisplay(parameters.effectPower);
  25. for(auto & attackedCre : ctx.attackedCres)
  26. {
  27. BattleStackAttacked bsa;
  28. bsa.flags |= BattleStackAttacked::SPELL_EFFECT;
  29. bsa.spellID = owner->id;
  30. bsa.damageAmount = parameters.effectPower; //damage times the number of attackers
  31. bsa.stackAttacked = (attackedCre)->ID;
  32. bsa.attackerID = -1;
  33. (attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
  34. ctx.si.stacks.push_back(bsa);
  35. }
  36. }
  37. ESpellCastProblem::ESpellCastProblem AcidBreathDamageMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
  38. {
  39. //just in case
  40. if(!obj->alive())
  41. return ESpellCastProblem::WRONG_SPELL_TARGET;
  42. //there should be no immunities by design
  43. //but make it a bit configurable
  44. //ignore all immunities, except specific absolute immunity
  45. {
  46. //SPELL_IMMUNITY absolute case
  47. std::stringstream cachingStr;
  48. cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << owner->id.toEnum() << "addInfo_1";
  49. if(obj->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, owner->id.toEnum(), 1), cachingStr.str()))
  50. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  51. }
  52. return ESpellCastProblem::OK;
  53. }
  54. ///DeathStareMechanics
  55. DeathStareMechanics::DeathStareMechanics(const CSpell * s):
  56. DefaultSpellMechanics(s)
  57. {
  58. }
  59. void DeathStareMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
  60. {
  61. //calculating dmg to display
  62. si32 damageToDisplay = parameters.effectPower;
  63. if(!ctx.attackedCres.empty())
  64. vstd::amin(damageToDisplay, (*ctx.attackedCres.begin())->getCount()); //stack is already reduced after attack
  65. ctx.setDamageToDisplay(damageToDisplay);
  66. for(auto & attackedCre : ctx.attackedCres)
  67. {
  68. BattleStackAttacked bsa;
  69. bsa.flags |= BattleStackAttacked::SPELL_EFFECT;
  70. bsa.spellID = owner->id;
  71. bsa.damageAmount = parameters.effectPower * (attackedCre)->MaxHealth();//todo: move here all DeathStare calculation
  72. bsa.stackAttacked = (attackedCre)->ID;
  73. bsa.attackerID = -1;
  74. (attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
  75. ctx.si.stacks.push_back(bsa);
  76. }
  77. }
  78. ///DispellHelpfulMechanics
  79. DispellHelpfulMechanics::DispellHelpfulMechanics(const CSpell * s):
  80. DefaultSpellMechanics(s)
  81. {
  82. }
  83. void DispellHelpfulMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
  84. {
  85. DefaultSpellMechanics::applyBattle(battle, packet);
  86. doDispell(battle, packet, positiveSpellEffects);
  87. }
  88. ESpellCastProblem::ESpellCastProblem DispellHelpfulMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
  89. {
  90. if(!canDispell(obj, positiveSpellEffects, "DispellHelpfulMechanics::positiveSpellEffects"))
  91. return ESpellCastProblem::NO_SPELLS_TO_DISPEL;
  92. //use default algorithm only if there is no mechanics-related problem
  93. return DefaultSpellMechanics::isImmuneByStack(caster,obj);
  94. }
  95. bool DispellHelpfulMechanics::positiveSpellEffects(const Bonus *b)
  96. {
  97. if(b->source == Bonus::SPELL_EFFECT)
  98. {
  99. const CSpell * sp = SpellID(b->sid).toSpell();
  100. return sp && sp->isPositive();
  101. }
  102. return false; //not a spell effect
  103. }