Teleport.cpp 2.4 KB

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