Clone.cpp 3.7 KB

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