Teleport.cpp 3.2 KB

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