Teleport.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "Registry.h"
  14. #include "../ISpellMechanics.h"
  15. #include "../../NetPacks.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../battle/Unit.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. //TODO: Teleport effect
  20. static const std::string EFFECT_NAME = "core:teleport";
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. VCMI_REGISTER_SPELL_EFFECT(Teleport, EFFECT_NAME);
  26. Teleport::Teleport()
  27. : UnitEffect()
  28. {
  29. }
  30. Teleport::~Teleport() = default;
  31. void Teleport::adjustTargetTypes(std::vector<TargetType> & types) const
  32. {
  33. if(!types.empty())
  34. {
  35. if(types[0] != AimType::CREATURE)
  36. {
  37. types.clear();
  38. return;
  39. }
  40. if(types.size() == 1)
  41. {
  42. types.push_back(AimType::LOCATION);
  43. }
  44. else if(types.size() > 1)
  45. {
  46. if(types[1] != AimType::LOCATION)
  47. types.clear();
  48. }
  49. }
  50. }
  51. bool Teleport::applicable(Problem & problem, const Mechanics * m) const
  52. {
  53. return UnitEffect::applicable(problem, m);
  54. }
  55. void Teleport::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  56. {
  57. if(target.size() != 2)
  58. {
  59. server->complain("Teleport requires 2 destinations.");
  60. return;
  61. }
  62. auto targetUnit = target[0].unitValue;
  63. if(nullptr == targetUnit)
  64. {
  65. server->complain("No unit to teleport");
  66. return;
  67. }
  68. const BattleHex destination = target[1].hexValue;
  69. if(!destination.isValid())
  70. {
  71. server->complain("Invalid teleport destination");
  72. return;
  73. }
  74. //TODO: move here all teleport checks
  75. if(!m->battle()->battleCanTeleportTo(targetUnit, destination, m->getEffectLevel()))
  76. {
  77. server->complain("Forbidden teleport.");
  78. return;
  79. }
  80. BattleStackMoved pack;
  81. pack.distance = 0;
  82. pack.stack = targetUnit->unitId();
  83. std::vector<BattleHex> tiles;
  84. tiles.push_back(destination);
  85. pack.tilesToMove = tiles;
  86. pack.teleporting = true;
  87. server->apply(&pack);
  88. }
  89. void Teleport::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  90. {
  91. //TODO: teleport options
  92. }
  93. EffectTarget Teleport::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  94. {
  95. //first transformed destination is unit to teleport, let base class handle immunity etc.
  96. //second spell destination is destination tile, use it directly
  97. EffectTarget transformed = UnitEffect::transformTarget(m, aimPoint, spellTarget);
  98. EffectTarget ret;
  99. if(!transformed.empty())
  100. ret.push_back(transformed.front());
  101. if(aimPoint.size() == 2)
  102. ret.push_back(aimPoint.back());
  103. return ret;
  104. }
  105. }
  106. }
  107. VCMI_LIB_NAMESPACE_END