Dispel.cpp 3.2 KB

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