2
0

CExchangeController.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. GAME->interface()->cb->swapCreatures(left, right, leftIt->first, leftIt->first);
  57. for(; rightIt != rightSlots.end(); rightIt++)
  58. GAME->interface()->cb->swapCreatures(left, right, rightIt->first, rightIt->first);
  59. }
  60. void CExchangeController::moveArmy(bool leftToRight, std::optional<SlotID> heldSlot)
  61. {
  62. const auto source = leftToRight ? left : right;
  63. const auto target = leftToRight ? right : left;
  64. if(!heldSlot.has_value())
  65. {
  66. const auto & weakestSlot = vstd::minElementByFun(source->Slots(),
  67. [](const auto & s) -> int
  68. {
  69. return s.second->getCreatureID().toCreature()->getAIValue();
  70. });
  71. heldSlot = weakestSlot->first;
  72. }
  73. if (source->getCreature(heldSlot.value()) == nullptr)
  74. return;
  75. GAME->interface()->cb->bulkMoveArmy(source->id, target->id, heldSlot.value());
  76. }
  77. void CExchangeController::moveStack(bool leftToRight, SlotID sourceSlot)
  78. {
  79. const auto source = leftToRight ? left : right;
  80. const auto target = leftToRight ? right : left;
  81. auto creature = source->getCreature(sourceSlot);
  82. if(creature == nullptr)
  83. return;
  84. SlotID targetSlot = target->getSlotFor(creature);
  85. if(targetSlot.validSlot())
  86. {
  87. if(source->stacksCount() == 1 && source->needsLastStack())
  88. {
  89. GAME->interface()->cb->splitStack(source, target, sourceSlot, targetSlot,
  90. target->getStackCount(targetSlot) + source->getStackCount(sourceSlot) - 1);
  91. }
  92. else
  93. {
  94. GAME->interface()->cb->mergeOrSwapStacks(source, target, sourceSlot, targetSlot);
  95. }
  96. }
  97. }
  98. void CExchangeController::moveSingleStackCreature(bool leftToRight, SlotID sourceSlot, bool forceEmptySlotTarget)
  99. {
  100. const auto source = leftToRight ? left : right;
  101. const auto target = leftToRight ? right : left;
  102. auto creature = source->getCreature(sourceSlot);
  103. if(creature == nullptr || source->stacksCount() == 1)
  104. return;
  105. SlotID targetSlot = forceEmptySlotTarget ? target->getFreeSlot() : target->getSlotFor(creature);
  106. if(targetSlot.validSlot())
  107. {
  108. GAME->interface()->cb->splitStack(source, target, sourceSlot, targetSlot, target->getStackCount(targetSlot) + 1);
  109. }
  110. }
  111. void CExchangeController::swapArtifacts(bool equipped, bool baclpack)
  112. {
  113. GAME->interface()->cb->bulkMoveArtifacts(left->id, right->id, true, equipped, baclpack);
  114. }
  115. void CExchangeController::moveArtifacts(bool leftToRight, bool equipped, bool baclpack)
  116. {
  117. const auto source = leftToRight ? left : right;
  118. const auto target = leftToRight ? right : left;
  119. GAME->interface()->cb->bulkMoveArtifacts(source->id, target->id, false, equipped, baclpack);
  120. }