SpellMechanics.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SpellMechanics.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 "SpellMechanics.h"
  12. #include "mapObjects/CGHeroInstance.h"
  13. #include "BattleState.h"
  14. ///DefaultSpellMechanics
  15. ESpellCastProblem::ESpellCastProblem DefaultSpellMechanics::isImmuneByStack(const CGHeroInstance * caster, ECastingMode::ECastingMode mode, const CStack * obj)
  16. {
  17. //by default use general algorithm
  18. return owner->isImmuneBy(obj);
  19. }
  20. ///CloneMechanics
  21. ESpellCastProblem::ESpellCastProblem CloneMechnics::isImmuneByStack(const CGHeroInstance* caster, ECastingMode::ECastingMode mode, const CStack * obj)
  22. {
  23. //can't clone already cloned creature
  24. if (vstd::contains(obj->state, EBattleStackState::CLONED))
  25. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  26. //TODO: how about stacks casting Clone?
  27. //currently Clone casted by stack is assumed Expert level
  28. ui8 schoolLevel;
  29. if (caster)
  30. {
  31. schoolLevel = caster->getSpellSchoolLevel(owner);
  32. }
  33. else
  34. {
  35. schoolLevel = 3;
  36. }
  37. if (schoolLevel < 3)
  38. {
  39. int maxLevel = (std::max(schoolLevel, (ui8)1) + 4);
  40. int creLevel = obj->getCreature()->level;
  41. if (maxLevel < creLevel) //tier 1-5 for basic, 1-6 for advanced, any level for expert
  42. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  43. }
  44. //use default algorithm only if there is no mechanics-related problem
  45. return DefaultSpellMechanics::isImmuneByStack(caster,mode,obj);
  46. }
  47. ///DispellHelpfulMechanics
  48. ESpellCastProblem::ESpellCastProblem DispellHelpfulMechanics::isImmuneByStack(const CGHeroInstance* caster, ECastingMode::ECastingMode mode, const CStack* obj)
  49. {
  50. TBonusListPtr spellBon = obj->getSpellBonuses();
  51. bool hasPositiveSpell = false;
  52. for(const Bonus * b : *spellBon)
  53. {
  54. if(SpellID(b->sid).toSpell()->isPositive())
  55. {
  56. hasPositiveSpell = true;
  57. break;
  58. }
  59. }
  60. if(!hasPositiveSpell)
  61. {
  62. return ESpellCastProblem::NO_SPELLS_TO_DISPEL;
  63. }
  64. //use default algorithm only if there is no mechanics-related problem
  65. return DefaultSpellMechanics::isImmuneByStack(caster,mode,obj);
  66. }
  67. ///HypnotizeMechanics
  68. ESpellCastProblem::ESpellCastProblem HypnotizeMechanics::isImmuneByStack(const CGHeroInstance* caster, ECastingMode::ECastingMode mode, const CStack* obj)
  69. {
  70. if(nullptr != caster) //do not resist hypnotize casted after attack, for example
  71. {
  72. //TODO: what with other creatures casting hypnotize, Faerie Dragons style?
  73. ui64 subjectHealth = (obj->count - 1) * obj->MaxHealth() + obj->firstHPleft;
  74. //apply 'damage' bonus for hypnotize, including hero specialty
  75. ui64 maxHealth = owner->calculateBonus(caster->getPrimSkillLevel(PrimarySkill::SPELL_POWER)
  76. * owner->power + owner->getPower(caster->getSpellSchoolLevel(owner)), caster, obj);
  77. if (subjectHealth > maxHealth)
  78. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  79. }
  80. return DefaultSpellMechanics::isImmuneByStack(caster,mode,obj);
  81. }
  82. ///SpecialRisingSpellMechanics
  83. ESpellCastProblem::ESpellCastProblem SpecialRisingSpellMechanics::isImmuneByStack(const CGHeroInstance* caster, ECastingMode::ECastingMode mode, const CStack* obj)
  84. {
  85. // following does apply to resurrect and animate dead(?) only
  86. // for sacrifice health calculation and health limit check don't matter
  87. if(obj->count >= obj->baseAmount)
  88. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  89. if (caster) //FIXME: Archangels can cast immune stack
  90. {
  91. auto maxHealth = owner->calculateHealedHP (caster, obj);
  92. if (maxHealth < obj->MaxHealth()) //must be able to rise at least one full creature
  93. return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
  94. }
  95. return DefaultSpellMechanics::isImmuneByStack(caster,mode,obj);
  96. }