Teleport.cpp 2.9 KB

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