Dispel.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. //Spells that can't be dispelled
  82. if(sourceSpell->isPersistent())
  83. return false;
  84. //stack may have inherited effects
  85. if(sourceSpell->isAdventure())
  86. return false;
  87. //Don't remove bonuses that may have been added by this very spell
  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