CreatureSpellMechanics.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. void AcidBreathDamageMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
  17. {
  18. //todo: this should be effectValue
  19. //calculating dmg to display
  20. ctx.setDamageToDisplay(parameters.effectPower);
  21. for(auto & attackedCre : ctx.attackedCres)
  22. {
  23. BattleStackAttacked bsa;
  24. bsa.flags |= BattleStackAttacked::SPELL_EFFECT;
  25. bsa.spellID = owner->id;
  26. bsa.damageAmount = parameters.effectPower; //damage times the number of attackers
  27. bsa.stackAttacked = (attackedCre)->ID;
  28. bsa.attackerID = -1;
  29. (attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
  30. ctx.si.stacks.push_back(bsa);
  31. }
  32. }
  33. ESpellCastProblem::ESpellCastProblem AcidBreathDamageMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
  34. {
  35. //just in case
  36. if(!obj->alive())
  37. return ESpellCastProblem::WRONG_SPELL_TARGET;
  38. //there should be no immunities by design
  39. //but make it a bit configurable
  40. //ignore all immunities, except specific absolute immunity
  41. {
  42. //SPELL_IMMUNITY absolute case
  43. std::stringstream cachingStr;
  44. cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << owner->id.toEnum() << "addInfo_1";
  45. if(obj->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, owner->id.toEnum(), 1), cachingStr.str()))
  46. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  47. }
  48. return ESpellCastProblem::OK;
  49. }
  50. ///DeathStareMechanics
  51. void DeathStareMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
  52. {
  53. //calculating dmg to display
  54. si32 damageToDisplay = parameters.effectPower;
  55. if(!ctx.attackedCres.empty())
  56. vstd::amin(damageToDisplay, (*ctx.attackedCres.begin())->count); //stack is already reduced after attack
  57. ctx.setDamageToDisplay(damageToDisplay);
  58. for(auto & attackedCre : ctx.attackedCres)
  59. {
  60. BattleStackAttacked bsa;
  61. bsa.flags |= BattleStackAttacked::SPELL_EFFECT;
  62. bsa.spellID = owner->id;
  63. bsa.damageAmount = parameters.effectPower * (attackedCre)->valOfBonuses(Bonus::STACK_HEALTH);//todo: move here all DeathStare calculation
  64. bsa.stackAttacked = (attackedCre)->ID;
  65. bsa.attackerID = -1;
  66. (attackedCre)->prepareAttacked(bsa, env->getRandomGenerator());
  67. ctx.si.stacks.push_back(bsa);
  68. }
  69. }
  70. ///DispellHelpfulMechanics
  71. void DispellHelpfulMechanics::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
  72. {
  73. DefaultSpellMechanics::applyBattle(battle, packet);
  74. doDispell(battle, packet, positiveSpellEffects);
  75. }
  76. ESpellCastProblem::ESpellCastProblem DispellHelpfulMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
  77. {
  78. if(!canDispell(obj, positiveSpellEffects, "DispellHelpfulMechanics::positiveSpellEffects"))
  79. return ESpellCastProblem::NO_SPELLS_TO_DISPEL;
  80. //use default algorithm only if there is no mechanics-related problem
  81. return DefaultSpellMechanics::isImmuneByStack(caster,obj);
  82. }
  83. bool DispellHelpfulMechanics::positiveSpellEffects(const Bonus *b)
  84. {
  85. if(b->source == Bonus::SPELL_EFFECT)
  86. {
  87. const CSpell * sp = SpellID(b->sid).toSpell();
  88. return sp && sp->isPositive();
  89. }
  90. return false; //not a spell effect
  91. }