Dispel.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Dispel.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 "Dispel.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../CSpellHandler.h"
  15. #include "../../NetPacks.h"
  16. #include "../../battle/IBattleState.h"
  17. #include "../../battle/Unit.h"
  18. #include "../../serializer/JsonSerializeFormat.h"
  19. static const std::string EFFECT_NAME = "core:dispel";
  20. namespace spells
  21. {
  22. namespace effects
  23. {
  24. VCMI_REGISTER_SPELL_EFFECT(Dispel, EFFECT_NAME);
  25. Dispel::Dispel()
  26. : UnitEffect()
  27. {
  28. }
  29. Dispel::~Dispel() = default;
  30. void Dispel::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  31. {
  32. SetStackEffect sse;
  33. prepareEffects(sse, rng, m, target, battleState->describe);
  34. if(!sse.toRemove.empty())
  35. battleState->apply(&sse);
  36. }
  37. bool Dispel::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  38. {
  39. if(getBonuses(m, unit)->empty())
  40. return false;
  41. return UnitEffect::isValidTarget(m, unit);
  42. }
  43. void Dispel::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  44. {
  45. handler.serializeBool("dispelPositive", positive);
  46. handler.serializeBool("dispelNegative", negative);
  47. handler.serializeBool("dispelNeutral", neutral);
  48. }
  49. std::shared_ptr<const BonusList> Dispel::getBonuses(const Mechanics * m, const battle::Unit * unit) const
  50. {
  51. auto addSelector = [=](const Bonus * bonus)
  52. {
  53. if(bonus->source == Bonus::SPELL_EFFECT)
  54. {
  55. const CSpell * sourceSpell = SpellID(bonus->sid).toSpell();
  56. if(!sourceSpell)
  57. return false;//error
  58. if(bonus->sid == m->getSpellIndex())
  59. return false;
  60. if(positive && sourceSpell->isPositive())
  61. return true;
  62. if(negative && sourceSpell->isNegative())
  63. return true;
  64. if(neutral && sourceSpell->isNeutral())
  65. return true;
  66. }
  67. return false;
  68. };
  69. CSelector selector = CSelector(mainSelector).And(CSelector(addSelector));
  70. return unit->getBonuses(selector);
  71. }
  72. bool Dispel::mainSelector(const Bonus * bonus)
  73. {
  74. if(bonus->source == Bonus::SPELL_EFFECT)
  75. {
  76. const CSpell * sourceSpell = SpellID(bonus->sid).toSpell();
  77. if(!sourceSpell)
  78. return false;//error
  79. //Special case: DISRUPTING_RAY and ACID_BREATH_DEFENSE are "immune" to dispell
  80. //Other even PERMANENT effects can be removed (f.e. BIND)
  81. if(sourceSpell->id == SpellID::DISRUPTING_RAY || sourceSpell->id == SpellID::ACID_BREATH_DEFENSE)
  82. return false;
  83. //Special case:do not remove lifetime marker
  84. if(sourceSpell->id == SpellID::CLONE)
  85. return false;
  86. //stack may have inherited effects
  87. return sourceSpell->isCombatSpell();
  88. }
  89. //not spell effect
  90. return false;
  91. }
  92. void Dispel::prepareEffects(SetStackEffect & pack, RNG & rng, const Mechanics * m, const EffectTarget & target, bool describe) const
  93. {
  94. for(auto & t : target)
  95. {
  96. const battle::Unit * unit = t.unitValue;
  97. if(unit)
  98. {
  99. //special case for DISPEL_HELPFUL_SPELLS
  100. if(describe && positive && !negative && !neutral)
  101. {
  102. MetaString line;
  103. unit->addText(line, MetaString::GENERAL_TXT, -555, true);
  104. unit->addNameReplacement(line, true);
  105. pack.battleLog.push_back(std::move(line));
  106. }
  107. std::vector<Bonus> buffer;
  108. auto bl = getBonuses(m, unit);
  109. for(auto item : *bl)
  110. buffer.emplace_back(*item);
  111. if(!buffer.empty())
  112. pack.toRemove.push_back(std::make_pair(unit->unitId(), buffer));
  113. }
  114. }
  115. }
  116. }
  117. }