RemoveObstacle.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace spells
  21. {
  22. namespace effects
  23. {
  24. bool RemoveObstacle::applicable(Problem & problem, const Mechanics * m) const
  25. {
  26. if (getTargets(m, EffectTarget(), true).empty())
  27. {
  28. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  29. }
  30. return true;
  31. }
  32. bool RemoveObstacle::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  33. {
  34. return !getTargets(m, target, false).empty();
  35. }
  36. void RemoveObstacle::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  37. {
  38. BattleObstaclesChanged pack;
  39. pack.battleID = m->battle()->getBattle()->getBattleID();
  40. for(const auto & obstacle : getTargets(m, target, false))
  41. {
  42. auto * serializable = const_cast<CObstacleInstance*>(obstacle); //Workaround
  43. pack.changes.emplace_back(obstacle->uniqueID, BattleChanges::EOperation::REMOVE);
  44. serializable->toInfo(pack.changes.back(), BattleChanges::EOperation::REMOVE);
  45. }
  46. if(!pack.changes.empty())
  47. server->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. const auto *spellObstacle = dynamic_cast<const SpellCreatedObstacle *>(obstacle);
  63. if(removeAllSpells && obstacle->obstacleType == CObstacleInstance::SPELL_CREATED)
  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->battle()->battleGetAllObstacles(BattlePerspective::ALL_KNOWING))
  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->battle()->battleGetAllObstaclesOnPos(destination.hexValue, false))
  86. if(canRemove(obstacle.get()))
  87. possibleTargets.insert(obstacle.get());
  88. }
  89. return possibleTargets;
  90. }
  91. }
  92. }
  93. VCMI_LIB_NAMESPACE_END