Clone.cpp 3.6 KB

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