Clone.cpp 3.5 KB

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