Teleport.cpp 3.1 KB

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