CExchangeController.cpp 3.8 KB

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