Teleport.cpp 2.6 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 "../../NetPacks.h"
  15. #include "../../battle/CBattleInfoCallback.h"
  16. #include "../../battle/Unit.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. //TODO: Teleport effect
  19. static const std::string EFFECT_NAME = "core:teleport";
  20. namespace spells
  21. {
  22. namespace effects
  23. {
  24. VCMI_REGISTER_SPELL_EFFECT(Teleport, EFFECT_NAME);
  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
  46. {
  47. return UnitEffect::applicable(problem, m);
  48. }
  49. void Teleport::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  50. {
  51. if(target.size() != 2)
  52. {
  53. server->complain("Teleport requires 2 destinations.");
  54. return;
  55. }
  56. const auto *targetUnit = target[0].unitValue;
  57. if(nullptr == targetUnit)
  58. {
  59. server->complain("No unit to teleport");
  60. return;
  61. }
  62. const BattleHex destination = target[1].hexValue;
  63. if(!destination.isValid())
  64. {
  65. server->complain("Invalid teleport destination");
  66. return;
  67. }
  68. //TODO: move here all teleport checks
  69. if(!m->battle()->battleCanTeleportTo(targetUnit, destination, m->getEffectLevel()))
  70. {
  71. server->complain("Forbidden teleport.");
  72. return;
  73. }
  74. BattleStackMoved pack;
  75. pack.distance = 0;
  76. pack.stack = targetUnit->unitId();
  77. std::vector<BattleHex> tiles;
  78. tiles.push_back(destination);
  79. pack.tilesToMove = tiles;
  80. pack.teleporting = true;
  81. server->apply(&pack);
  82. }
  83. void Teleport::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  84. {
  85. //TODO: teleport options
  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