Teleport.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Teleport.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 "Teleport.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../battle/IBattleState.h"
  15. #include "../../battle/CBattleInfoCallback.h"
  16. #include "../../battle/Unit.h"
  17. #include "../../entities/building/TownFortifications.h"
  18. #include "../../networkPacks/PacksForClientBattle.h"
  19. #include "../../serializer/JsonSerializeFormat.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. void Teleport::adjustTargetTypes(std::vector<TargetType> & types) const
  26. {
  27. if(!types.empty())
  28. {
  29. if(types[0] != AimType::CREATURE)
  30. {
  31. types.clear();
  32. return;
  33. }
  34. if(types.size() == 1)
  35. {
  36. types.push_back(AimType::LOCATION);
  37. }
  38. else if(types.size() > 1)
  39. {
  40. if(types[1] != AimType::LOCATION)
  41. types.clear();
  42. }
  43. }
  44. }
  45. bool Teleport::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  46. {
  47. if(target.size() == 1) //Assume, this is check only for selecting a unit
  48. return UnitEffect::applicable(problem, m, target);
  49. if(target.size() != 2)
  50. return m->adaptProblem(ESpellCastProblem::WRONG_SPELL_TARGET, problem);
  51. const auto *targetUnit = target[0].unitValue;
  52. const auto & targetHex = target[1].hexValue;
  53. if(!targetUnit)
  54. return m->adaptProblem(ESpellCastProblem::WRONG_SPELL_TARGET, problem);
  55. if(!targetHex.isValid() || !m->battle()->getAccessibility(targetUnit).accessible(targetHex, targetUnit))
  56. return m->adaptProblem(ESpellCastProblem::WRONG_SPELL_TARGET, problem);
  57. if(m->battle()->battleGetFortifications().wallsHealth > 0 && !(isWallPassable && isMoatPassable))
  58. {
  59. return !m->battle()->battleHasPenaltyOnLine(target[0].hexValue, target[1].hexValue, !isWallPassable, !isMoatPassable);
  60. }
  61. return true;
  62. }
  63. void Teleport::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  64. {
  65. const auto *targetUnit = target[0].unitValue;
  66. const auto destination = target[1].hexValue;
  67. BattleStackMoved pack;
  68. pack.battleID = m->battle()->getBattle()->getBattleID();
  69. pack.distance = 0;
  70. pack.stack = targetUnit->unitId();
  71. std::vector<BattleHex> tiles;
  72. tiles.push_back(destination);
  73. pack.tilesToMove = tiles;
  74. pack.teleporting = true;
  75. server->apply(&pack);
  76. if(triggerObstacles)
  77. {
  78. auto spellEnv = dynamic_cast<SpellCastEnvironment*>(server);
  79. m->battle()->handleObstacleTriggersForUnit(*spellEnv, *targetUnit);
  80. }
  81. }
  82. void Teleport::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  83. {
  84. handler.serializeBool("triggerObstacles", triggerObstacles);
  85. handler.serializeBool("isWallPassable", isWallPassable);
  86. handler.serializeBool("isMoatPassable", isMoatPassable);
  87. }
  88. EffectTarget Teleport::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  89. {
  90. //first transformed destination is unit to teleport, let base class handle immunity etc.
  91. //second spell destination is destination tile, use it directly
  92. EffectTarget transformed = UnitEffect::transformTarget(m, aimPoint, spellTarget);
  93. EffectTarget ret;
  94. if(!transformed.empty())
  95. ret.push_back(transformed.front());
  96. if(aimPoint.size() == 2)
  97. ret.push_back(aimPoint.back());
  98. return ret;
  99. }
  100. }
  101. }
  102. VCMI_LIB_NAMESPACE_END