RemoveObstacle.cpp 3.0 KB

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