SpellMechanics.cpp 3.9 KB

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