SpellMechanics.cpp 4.1 KB

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