CExchangeController.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "../../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. auto getStacks = [](const CArmedInstance * source) -> std::vector<std::pair<SlotID, CStackInstance*>>
  25. {
  26. auto slots = source->Slots();
  27. return std::vector<std::pair<SlotID, CStackInstance*>>(slots.begin(), slots.end());
  28. };
  29. auto leftSlots = getStacks(left);
  30. auto rightSlots = getStacks(right);
  31. auto i = leftSlots.begin();
  32. auto j = rightSlots.begin();
  33. for(; i != leftSlots.end() && j != rightSlots.end(); i++, j++)
  34. {
  35. GAME->interface()->cb->swapCreatures(left, right, i->first, j->first);
  36. }
  37. if(i != leftSlots.end())
  38. {
  39. auto freeSlots = right->getFreeSlots();
  40. auto slot = freeSlots.begin();
  41. for(; i != leftSlots.end() && slot != freeSlots.end(); i++, slot++)
  42. {
  43. GAME->interface()->cb->swapCreatures(left, right, i->first, *slot);
  44. }
  45. }
  46. else if(j != rightSlots.end())
  47. {
  48. auto freeSlots = left->getFreeSlots();
  49. auto slot = freeSlots.begin();
  50. for(; j != rightSlots.end() && slot != freeSlots.end(); j++, slot++)
  51. {
  52. GAME->interface()->cb->swapCreatures(left, right, *slot, j->first);
  53. }
  54. }
  55. }
  56. void CExchangeController::moveArmy(bool leftToRight, std::optional<SlotID> heldSlot)
  57. {
  58. const auto source = leftToRight ? left : right;
  59. const auto target = leftToRight ? right : left;
  60. if(!heldSlot.has_value())
  61. {
  62. auto weakestSlot = vstd::minElementByFun(source->Slots(),
  63. [](const std::pair<SlotID, CStackInstance*> & s) -> int
  64. {
  65. return s.second->getCreatureID().toCreature()->getAIValue();
  66. });
  67. heldSlot = weakestSlot->first;
  68. }
  69. if (source->getCreature(heldSlot.value()) == nullptr)
  70. return;
  71. GAME->interface()->cb->bulkMoveArmy(source->id, target->id, heldSlot.value());
  72. }
  73. void CExchangeController::moveStack(bool leftToRight, SlotID sourceSlot)
  74. {
  75. const auto source = leftToRight ? left : right;
  76. const auto target = leftToRight ? right : left;
  77. auto creature = source->getCreature(sourceSlot);
  78. if(creature == nullptr)
  79. return;
  80. SlotID targetSlot = target->getSlotFor(creature);
  81. if(targetSlot.validSlot())
  82. {
  83. if(source->stacksCount() == 1 && source->needsLastStack())
  84. {
  85. GAME->interface()->cb->splitStack(source, target, sourceSlot, targetSlot,
  86. target->getStackCount(targetSlot) + source->getStackCount(sourceSlot) - 1);
  87. }
  88. else
  89. {
  90. GAME->interface()->cb->mergeOrSwapStacks(source, target, sourceSlot, targetSlot);
  91. }
  92. }
  93. }
  94. void CExchangeController::moveSingleStackCreature(bool leftToRight, SlotID sourceSlot, bool forceEmptySlotTarget)
  95. {
  96. const auto source = leftToRight ? left : right;
  97. const auto target = leftToRight ? right : left;
  98. auto creature = source->getCreature(sourceSlot);
  99. if(creature == nullptr || source->stacksCount() == 1)
  100. return;
  101. SlotID targetSlot = forceEmptySlotTarget ? target->getFreeSlot() : target->getSlotFor(creature);
  102. if(targetSlot.validSlot())
  103. {
  104. GAME->interface()->cb->splitStack(source, target, sourceSlot, targetSlot, target->getStackCount(targetSlot) + 1);
  105. }
  106. }
  107. void CExchangeController::swapArtifacts(bool equipped, bool baclpack)
  108. {
  109. GAME->interface()->cb->bulkMoveArtifacts(left->id, right->id, true, equipped, baclpack);
  110. }
  111. void CExchangeController::moveArtifacts(bool leftToRight, bool equipped, bool baclpack)
  112. {
  113. const auto source = leftToRight ? left : right;
  114. const auto target = leftToRight ? right : left;
  115. GAME->interface()->cb->bulkMoveArtifacts(source->id, target->id, false, equipped, baclpack);
  116. }