CExchangeController.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * CExchangeController.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 "CExchangeController.h"
  12. #include "../CPlayerInterface.h"
  13. #include "../GameInstance.h"
  14. #include "../widgets/CGarrisonInt.h"
  15. #include "../lib/callback/CCallback.h"
  16. #include "../lib/mapObjects/CGHeroInstance.h"
  17. CExchangeController::CExchangeController(ObjectInstanceID hero1, ObjectInstanceID hero2)
  18. : left(GAME->interface()->cb->getHero(hero1))
  19. , right(GAME->interface()->cb->getHero(hero2))
  20. {
  21. }
  22. void CExchangeController::swapArmy()
  23. {
  24. const auto & leftSlots = left->Slots();
  25. const auto & rightSlots = right->Slots();
  26. auto leftIt = leftSlots.begin();
  27. auto rightIt = rightSlots.begin();
  28. // Swap slots that are full in both armies
  29. // [A] [B] => [B] [A]
  30. for (SlotID slotID(0); slotID < GameConstants::ARMY_SIZE; ++slotID)
  31. {
  32. if (left->hasStackAtSlot(slotID) && right->hasStackAtSlot(slotID))
  33. GAME->interface()->cb->swapCreatures(left, right, slotID, slotID);
  34. }
  35. // Swap pairs of stacks in different slots and correct their positions
  36. // [A] [ ] [B] [ ] [ ] [A]
  37. // => =>
  38. // [ ] [B] [ ] [A] [B] [ ]
  39. for (;;)
  40. {
  41. while (leftIt != leftSlots.end() && right->hasStackAtSlot(leftIt->first))
  42. leftIt++;
  43. while (rightIt != rightSlots.end() && left->hasStackAtSlot(rightIt->first))
  44. rightIt++;
  45. if (leftIt == leftSlots.end() || rightIt == rightSlots.end())
  46. break;
  47. GAME->interface()->cb->swapCreatures(left, right, leftIt->first, rightIt->first);
  48. GAME->interface()->cb->swapCreatures(left, left, leftIt->first, rightIt->first);
  49. GAME->interface()->cb->swapCreatures(right, right, rightIt->first, leftIt->first);
  50. leftIt++;
  51. rightIt++;
  52. }
  53. // Move remaining unpaired stacks (if armies size is different)
  54. // [A] [ ] => [ ] [A]
  55. for(; leftIt != leftSlots.end(); leftIt++)
  56. if (!right->hasStackAtSlot(leftIt->first))
  57. GAME->interface()->cb->swapCreatures(left, right, leftIt->first, leftIt->first);
  58. for(; rightIt != rightSlots.end(); rightIt++)
  59. if (!left->hasStackAtSlot(rightIt->first))
  60. GAME->interface()->cb->swapCreatures(left, right, rightIt->first, rightIt->first);
  61. }
  62. void CExchangeController::moveArmy(bool leftToRight, std::optional<SlotID> heldSlot)
  63. {
  64. const auto source = leftToRight ? left : right;
  65. const auto target = leftToRight ? right : left;
  66. if(!heldSlot.has_value())
  67. {
  68. const auto & weakestSlot = vstd::minElementByFun(source->Slots(),
  69. [](const auto & s) -> int
  70. {
  71. return s.second->getCreatureID().toCreature()->getAIValue();
  72. });
  73. heldSlot = weakestSlot->first;
  74. }
  75. if (source->getCreature(heldSlot.value()) == nullptr)
  76. return;
  77. GAME->interface()->cb->bulkMoveArmy(source->id, target->id, heldSlot.value());
  78. }
  79. void CExchangeController::moveStack(bool leftToRight, SlotID sourceSlot)
  80. {
  81. const auto source = leftToRight ? left : right;
  82. const auto target = leftToRight ? right : left;
  83. auto creature = source->getCreature(sourceSlot);
  84. if(creature == nullptr)
  85. return;
  86. SlotID targetSlot = target->getSlotFor(creature);
  87. if(targetSlot.validSlot())
  88. {
  89. if(source->stacksCount() == 1 && source->needsLastStack())
  90. {
  91. GAME->interface()->cb->splitStack(source, target, sourceSlot, targetSlot,
  92. target->getStackCount(targetSlot) + source->getStackCount(sourceSlot) - 1);
  93. }
  94. else
  95. {
  96. GAME->interface()->cb->mergeOrSwapStacks(source, target, sourceSlot, targetSlot);
  97. }
  98. }
  99. }
  100. void CExchangeController::moveSingleStackCreature(bool leftToRight, SlotID sourceSlot, bool forceEmptySlotTarget)
  101. {
  102. const auto source = leftToRight ? left : right;
  103. const auto target = leftToRight ? right : left;
  104. auto creature = source->getCreature(sourceSlot);
  105. if(creature == nullptr || source->stacksCount() == 1)
  106. return;
  107. SlotID targetSlot = forceEmptySlotTarget ? target->getFreeSlot() : target->getSlotFor(creature);
  108. if(targetSlot.validSlot())
  109. {
  110. GAME->interface()->cb->splitStack(source, target, sourceSlot, targetSlot, target->getStackCount(targetSlot) + 1);
  111. }
  112. }
  113. void CExchangeController::swapArtifacts(bool equipped, bool baclpack)
  114. {
  115. GAME->interface()->cb->bulkMoveArtifacts(left->id, right->id, true, equipped, baclpack);
  116. }
  117. void CExchangeController::moveArtifacts(bool leftToRight, bool equipped, bool baclpack)
  118. {
  119. const auto source = leftToRight ? left : right;
  120. const auto target = leftToRight ? right : left;
  121. GAME->interface()->cb->bulkMoveArtifacts(source->id, target->id, false, equipped, baclpack);
  122. }