Dispel.cpp 3.1 KB

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