RemoveObstacle.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * RemoveObstacle.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 "RemoveObstacle.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/IBattleState.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../battle/CObstacleInstance.h"
  18. #include "../../serializer/JsonSerializeFormat.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. static const std::string EFFECT_NAME = "core:removeObstacle";
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. VCMI_REGISTER_SPELL_EFFECT(RemoveObstacle, EFFECT_NAME);
  26. RemoveObstacle::RemoveObstacle()
  27. : LocationEffect(),
  28. removeAbsolute(false),
  29. removeUsual(false),
  30. removeAllSpells(false)
  31. {
  32. }
  33. RemoveObstacle::~RemoveObstacle() = default;
  34. bool RemoveObstacle::applicable(Problem & problem, const Mechanics * m) const
  35. {
  36. if (getTargets(m, EffectTarget(), true).empty())
  37. {
  38. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  39. }
  40. return true;
  41. }
  42. bool RemoveObstacle::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  43. {
  44. return !getTargets(m, target, false).empty();
  45. }
  46. void RemoveObstacle::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  47. {
  48. BattleObstaclesChanged pack;
  49. for(const auto & obstacle : getTargets(m, target, false))
  50. pack.changes.emplace_back(obstacle->uniqueID, BattleChanges::EOperation::REMOVE);
  51. if(!pack.changes.empty())
  52. server->apply(&pack);
  53. }
  54. void RemoveObstacle::serializeJsonEffect(JsonSerializeFormat & handler)
  55. {
  56. handler.serializeBool("removeAbsolute", removeAbsolute, false);
  57. handler.serializeBool("removeUsual", removeUsual, false);
  58. handler.serializeBool("removeAllSpells", removeAllSpells, false);
  59. handler.serializeIdArray("removeSpells", removeSpells);
  60. }
  61. bool RemoveObstacle::canRemove(const CObstacleInstance * obstacle) const
  62. {
  63. if(removeAbsolute && obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  64. return true;
  65. if(removeUsual && obstacle->obstacleType == CObstacleInstance::USUAL)
  66. return true;
  67. auto spellObstacle = dynamic_cast<const SpellCreatedObstacle *>(obstacle);
  68. if(removeAllSpells && spellObstacle)
  69. return true;
  70. if(spellObstacle && !removeSpells.empty())
  71. {
  72. if(vstd::contains(removeSpells, SpellID(spellObstacle->ID)))
  73. return true;
  74. }
  75. return false;
  76. }
  77. std::set<const CObstacleInstance *> RemoveObstacle::getTargets(const Mechanics * m, const EffectTarget & target, bool alwaysMassive) const
  78. {
  79. std::set<const CObstacleInstance *> possibleTargets;
  80. if(m->isMassive() || alwaysMassive)
  81. {
  82. for(const auto & obstacle : m->battle()->battleGetAllObstacles())
  83. if(canRemove(obstacle.get()))
  84. possibleTargets.insert(obstacle.get());
  85. }
  86. else
  87. {
  88. for(const auto & destination : target)
  89. if(destination.hexValue.isValid())
  90. for(const auto & obstacle : m->battle()->battleGetAllObstaclesOnPos(destination.hexValue, false))
  91. if(canRemove(obstacle.get()))
  92. possibleTargets.insert(obstacle.get());
  93. }
  94. return possibleTargets;
  95. }
  96. }
  97. }
  98. VCMI_LIB_NAMESPACE_END