RemoveObstacle.cpp 3.0 KB

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