Clone.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Clone.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 "Clone.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/CBattleInfoCallback.h"
  16. #include "../../battle/CUnitState.h"
  17. #include "../../serializer/JsonSerializeFormat.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. static const std::string EFFECT_NAME = "core:clone";
  20. namespace spells
  21. {
  22. namespace effects
  23. {
  24. VCMI_REGISTER_SPELL_EFFECT(Clone, EFFECT_NAME);
  25. Clone::Clone()
  26. : UnitEffect(),
  27. maxTier(0)
  28. {
  29. }
  30. Clone::~Clone() = default;
  31. void Clone::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  32. {
  33. for(const Destination & dest : target)
  34. {
  35. const battle::Unit * clonedStack = dest.unitValue;
  36. //we shall have all targets to be stacks
  37. if(!clonedStack)
  38. {
  39. server->complain("No target stack to clone! Invalid effect target transformation.");
  40. continue;
  41. }
  42. //should not happen, but in theory we might have stack took damage from other effects
  43. if(clonedStack->getCount() < 1)
  44. continue;
  45. auto hex = m->battle()->getAvaliableHex(clonedStack->creatureId(), m->casterSide, clonedStack->getPosition());
  46. if(!hex.isValid())
  47. {
  48. server->complain("No place to put new clone!");
  49. break;
  50. }
  51. auto unitId = m->battle()->battleNextUnitId();
  52. battle::UnitInfo info;
  53. info.id = unitId;
  54. info.count = clonedStack->getCount();
  55. info.type = clonedStack->creatureId();
  56. info.side = m->casterSide;
  57. info.position = hex;
  58. info.summoned = true;
  59. BattleUnitsChanged pack;
  60. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  61. info.save(pack.changedStacks.back().data);
  62. server->apply(&pack);
  63. //TODO: use BattleUnitsChanged with UPDATE operation
  64. BattleUnitsChanged cloneFlags;
  65. auto cloneUnit = m->battle()->battleGetUnitByID(unitId);
  66. if(!cloneUnit)
  67. {
  68. server->complain("[Internal error] Cloned unit missing.");
  69. continue;
  70. }
  71. auto cloneState = cloneUnit->acquireState();
  72. cloneState->cloned = true;
  73. cloneFlags.changedStacks.emplace_back(cloneState->unitId(), UnitChanges::EOperation::RESET_STATE);
  74. cloneState->save(cloneFlags.changedStacks.back().data);
  75. auto originalState = clonedStack->acquireState();
  76. originalState->cloneID = unitId;
  77. cloneFlags.changedStacks.emplace_back(originalState->unitId(), UnitChanges::EOperation::RESET_STATE);
  78. originalState->save(cloneFlags.changedStacks.back().data);
  79. server->apply(&cloneFlags);
  80. SetStackEffect sse;
  81. Bonus lifeTimeMarker(Bonus::N_TURNS, Bonus::NONE, Bonus::SPELL_EFFECT, 0, SpellID::CLONE); //TODO: use special bonus type
  82. lifeTimeMarker.turnsRemain = m->getEffectDuration();
  83. std::vector<Bonus> buffer;
  84. buffer.push_back(lifeTimeMarker);
  85. sse.toAdd.push_back(std::make_pair(unitId, buffer));
  86. server->apply(&sse);
  87. }
  88. }
  89. bool Clone::isReceptive(const Mechanics * m, const battle::Unit * s) const
  90. {
  91. int creLevel = s->creatureLevel();
  92. if(creLevel > maxTier)
  93. return false;
  94. //use default algorithm only if there is no mechanics-related problem
  95. return UnitEffect::isReceptive(m, s);
  96. }
  97. bool Clone::isValidTarget(const Mechanics * m, const battle::Unit * s) const
  98. {
  99. //can't clone already cloned creature
  100. if(s->isClone())
  101. return false;
  102. //can`t clone if old clone still alive
  103. if(s->hasClone())
  104. return false;
  105. return UnitEffect::isValidTarget(m, s);
  106. }
  107. void Clone::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  108. {
  109. handler.serializeInt("maxTier", maxTier);
  110. }
  111. }
  112. }
  113. VCMI_LIB_NAMESPACE_END