Clone.cpp 3.4 KB

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