RemoveObstacle.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return !getTargets(m, EffectTarget(), true).empty();
  37. }
  38. bool RemoveObstacle::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  39. {
  40. return !getTargets(m, target, false).empty();
  41. }
  42. void RemoveObstacle::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  43. {
  44. BattleObstaclesChanged pack;
  45. for(const auto & obstacle : getTargets(m, target, false))
  46. pack.changes.emplace_back(obstacle->uniqueID, BattleChanges::EOperation::REMOVE);
  47. if(!pack.changes.empty())
  48. server->apply(&pack);
  49. }
  50. void RemoveObstacle::serializeJsonEffect(JsonSerializeFormat & handler)
  51. {
  52. handler.serializeBool("removeAbsolute", removeAbsolute, false);
  53. handler.serializeBool("removeUsual", removeUsual, false);
  54. handler.serializeBool("removeAllSpells", removeAllSpells, false);
  55. handler.serializeIdArray("removeSpells", removeSpells);
  56. }
  57. bool RemoveObstacle::canRemove(const CObstacleInstance * obstacle) const
  58. {
  59. if(removeAbsolute && obstacle->obstacleType == CObstacleInstance::ABSOLUTE_OBSTACLE)
  60. return true;
  61. if(removeUsual && obstacle->obstacleType == CObstacleInstance::USUAL)
  62. return true;
  63. auto spellObstacle = dynamic_cast<const SpellCreatedObstacle *>(obstacle);
  64. if(removeAllSpells && spellObstacle)
  65. return true;
  66. if(spellObstacle && !removeSpells.empty())
  67. {
  68. if(vstd::contains(removeSpells, SpellID(spellObstacle->ID)))
  69. return true;
  70. }
  71. return false;
  72. }
  73. std::set<const CObstacleInstance *> RemoveObstacle::getTargets(const Mechanics * m, const EffectTarget & target, bool alwaysMassive) const
  74. {
  75. std::set<const CObstacleInstance *> possibleTargets;
  76. if(m->isMassive() || alwaysMassive)
  77. {
  78. for(const auto & obstacle : m->battle()->battleGetAllObstacles())
  79. if(canRemove(obstacle.get()))
  80. possibleTargets.insert(obstacle.get());
  81. }
  82. else
  83. {
  84. for(const auto & destination : target)
  85. if(destination.hexValue.isValid())
  86. for(const auto & obstacle : m->battle()->battleGetAllObstaclesOnPos(destination.hexValue, false))
  87. if(canRemove(obstacle.get()))
  88. possibleTargets.insert(obstacle.get());
  89. }
  90. return possibleTargets;
  91. }
  92. }
  93. }
  94. VCMI_LIB_NAMESPACE_END