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