2
0

CExchangeController.cpp 3.7 KB

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