CreatureSpellMechanics.cpp 3.5 KB

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