Dispel.cpp 3.3 KB

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