CExchangeController.cpp 3.2 KB

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