CreatureSpellMechanics.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "../BattleState.h"
  14. ///AcidBreathDamageMechanics
  15. void AcidBreathDamageMechanics::applyBattleEffects(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
  16. {
  17. //calculating dmg to display
  18. ctx.sc.dmgToDisplay = parameters.usedSpellPower;
  19. for(auto & attackedCre : ctx.attackedCres) //no immunities
  20. {
  21. BattleStackAttacked bsa;
  22. bsa.flags |= BattleStackAttacked::SPELL_EFFECT;
  23. bsa.spellID = owner->id;
  24. bsa.damageAmount = parameters.usedSpellPower; //damage times the number of attackers
  25. bsa.stackAttacked = (attackedCre)->ID;
  26. bsa.attackerID = -1;
  27. (attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
  28. ctx.si.stacks.push_back(bsa);
  29. }
  30. }
  31. ///DeathStareMechanics
  32. void DeathStareMechanics::applyBattleEffects(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
  33. {
  34. //calculating dmg to display
  35. ctx.sc.dmgToDisplay = parameters.usedSpellPower;
  36. if(!ctx.attackedCres.empty())
  37. vstd::amin(ctx.sc.dmgToDisplay, (*ctx.attackedCres.begin())->count); //stack is already reduced after attack
  38. for(auto & attackedCre : ctx.attackedCres)
  39. {
  40. BattleStackAttacked bsa;
  41. bsa.flags |= BattleStackAttacked::SPELL_EFFECT;
  42. bsa.spellID = owner->id;
  43. bsa.damageAmount = parameters.usedSpellPower * (attackedCre)->valOfBonuses(Bonus::STACK_HEALTH);
  44. bsa.stackAttacked = (attackedCre)->ID;
  45. bsa.attackerID = -1;
  46. (attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
  47. ctx.si.stacks.push_back(bsa);
  48. }
  49. }
  50. ///DispellHelpfulMechanics
  51. void DispellHelpfulMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
  52. {
  53. DefaultSpellMechanics::applyBattle(battle, packet);
  54. doDispell(battle, packet, Selector::positiveSpellEffects);
  55. }
  56. ESpellCastProblem::ESpellCastProblem DispellHelpfulMechanics::isImmuneByStack(const CGHeroInstance * caster, const CStack * obj) const
  57. {
  58. TBonusListPtr spellBon = obj->getSpellBonuses();
  59. bool hasPositiveSpell = false;
  60. for(const Bonus * b : *spellBon)
  61. {
  62. if(SpellID(b->sid).toSpell()->isPositive())
  63. {
  64. hasPositiveSpell = true;
  65. break;
  66. }
  67. }
  68. if(!hasPositiveSpell)
  69. {
  70. return ESpellCastProblem::NO_SPELLS_TO_DISPEL;
  71. }
  72. //use default algorithm only if there is no mechanics-related problem
  73. return DefaultSpellMechanics::isImmuneByStack(caster,obj);
  74. }