CGarrisonInt.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * CGarrisonInt.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 "CGarrisonInt.h"
  12. #include "Buttons.h"
  13. #include "TextControls.h"
  14. #include "RadialMenu.h"
  15. #include "../GameEngine.h"
  16. #include "../GameInstance.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../render/IImage.h"
  19. #include "../windows/CCreatureWindow.h"
  20. #include "../windows/CWindowWithArtifacts.h"
  21. #include "../windows/GUIClasses.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/ArtifactUtils.h"
  25. #include "../../lib/texts/CGeneralTextHandler.h"
  26. #include "../../lib/texts/TextOperations.h"
  27. #include "../../lib/CCreatureHandler.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../lib/mapObjects/CGHeroInstance.h"
  30. #include "../../lib/networkPacks/ArtifactLocation.h"
  31. #include "../../lib/gameState/CGameState.h"
  32. #include "../../lib/gameState/UpgradeInfo.h"
  33. void CGarrisonSlot::setHighlight(bool on)
  34. {
  35. if (on)
  36. selectionImage->enable(); //show
  37. else
  38. selectionImage->disable(); //hide
  39. }
  40. void CGarrisonSlot::hover (bool on)
  41. {
  42. ////Hoverable::hover(on);
  43. if(on)
  44. {
  45. std::string temp;
  46. if(creature)
  47. {
  48. if(owner->getSelection())
  49. {
  50. if(owner->getSelection() == this)
  51. {
  52. temp = LIBRARY->generaltexth->tcommands[4]; //View %s
  53. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  54. }
  55. else if (owner->getSelection()->creature == creature)
  56. {
  57. temp = LIBRARY->generaltexth->tcommands[2]; //Combine %s armies
  58. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  59. }
  60. else if (owner->getSelection()->creature)
  61. {
  62. temp = LIBRARY->generaltexth->tcommands[7]; //Exchange %s with %s
  63. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  64. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  65. }
  66. else
  67. {
  68. logGlobal->warn("Warning - shouldn't be - highlighted void slot %d", owner->getSelection()->ID.getNum());
  69. logGlobal->warn("Highlighted set to nullptr");
  70. owner->selectSlot(nullptr);
  71. }
  72. }
  73. else
  74. {
  75. const bool isHeroOnMap = owner->upperArmy() // Hero is not a visitor and not a garrison defender
  76. && owner->upperArmy()->ID == Obj::HERO
  77. && (!owner->lowerArmy() || owner->lowerArmy()->ID == Obj::HERO) // one hero or we are in the Heroes exchange window
  78. && !(static_cast<const CGHeroInstance*>(owner->upperArmy()))->inTownGarrison;
  79. if(isHeroOnMap)
  80. {
  81. temp = LIBRARY->generaltexth->allTexts[481]; //Select %s
  82. }
  83. else if(upg == EGarrisonType::UPPER)
  84. {
  85. temp = LIBRARY->generaltexth->tcommands[12]; //Select %s (in garrison)
  86. }
  87. else // Hero is visiting some object (town, mine, etc)
  88. {
  89. temp = LIBRARY->generaltexth->tcommands[32]; //Select %s (visiting)
  90. }
  91. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  92. }
  93. }
  94. else
  95. {
  96. if(owner->getSelection())
  97. {
  98. const CArmedInstance *highl = owner->getSelection()->getObj();
  99. if( highl->needsLastStack() //we are moving stack from hero's
  100. && highl->stacksCount() == 1 //it's only stack
  101. && owner->getSelection()->upg != upg //we're moving it to the other garrison
  102. )
  103. {
  104. temp = LIBRARY->generaltexth->tcommands[5]; //Cannot move last army to garrison
  105. }
  106. else
  107. {
  108. temp = LIBRARY->generaltexth->tcommands[6]; //Move %s
  109. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  110. }
  111. }
  112. else
  113. {
  114. temp = LIBRARY->generaltexth->tcommands[11]; //Empty
  115. }
  116. }
  117. ENGINE->statusbar()->write(temp);
  118. }
  119. else
  120. {
  121. ENGINE->statusbar()->clear();
  122. }
  123. }
  124. const CArmedInstance * CGarrisonSlot::getObj() const
  125. {
  126. return owner->army(upg);
  127. }
  128. bool CGarrisonSlot::our() const
  129. {
  130. return owner->isArmyOwned(upg);
  131. }
  132. bool CGarrisonSlot::ally() const
  133. {
  134. if(!getObj())
  135. return false;
  136. return PlayerRelations::ALLIES == GAME->interface()->cb->getPlayerRelations(GAME->interface()->playerID, getObj()->tempOwner);
  137. }
  138. std::function<void()> CGarrisonSlot::getDismiss() const
  139. {
  140. const bool canDismiss = getObj()->tempOwner == GAME->interface()->playerID
  141. && (getObj()->stacksCount() > 1 ||
  142. !getObj()->needsLastStack());
  143. return canDismiss ? [=]()
  144. {
  145. GAME->interface()->cb->dismissCreature(getObj(), ID);
  146. } : (std::function<void()>)nullptr;
  147. }
  148. /// The creature slot has been clicked twice, therefore the creature info should be shown
  149. /// @return Whether the view should be refreshed
  150. bool CGarrisonSlot::viewInfo()
  151. {
  152. UpgradeInfo pom(ID.getNum());
  153. GAME->interface()->cb->fillUpgradeInfo(getObj(), ID, pom);
  154. bool canUpgrade = getObj()->tempOwner == GAME->interface()->playerID && pom.canUpgrade(); //upgrade is possible
  155. std::function<void(CreatureID)> upgr = nullptr;
  156. auto dism = getDismiss();
  157. if(canUpgrade) upgr = [=] (CreatureID newID) { GAME->interface()->cb->upgradeCreature(getObj(), ID, newID); };
  158. owner->selectSlot(nullptr);
  159. owner->setSplittingMode(false);
  160. for(auto & elem : owner->splitButtons)
  161. elem->block(true);
  162. redraw();
  163. ENGINE->windows().createAndPushWindow<CStackWindow>(myStack, dism, pom, upgr);
  164. return true;
  165. }
  166. /// The selection is empty, therefore the creature should be moved
  167. /// @return Whether the view should be refreshed
  168. bool CGarrisonSlot::highlightOrDropArtifact()
  169. {
  170. bool artSelected = false;
  171. if (auto chw = ENGINE->windows().topWindow<CWindowWithArtifacts>()) //dirty solution
  172. {
  173. const auto pickedArtInst = chw->getPickedArtifact();
  174. if(pickedArtInst)
  175. {
  176. const auto * srcHero = chw->getHeroPickedArtifact();
  177. artSelected = true;
  178. if (myStack) // try dropping the artifact only if the slot isn't empty
  179. {
  180. ArtifactLocation src(srcHero->id, ArtifactPosition::TRANSITION_POS);
  181. ArtifactLocation dst(getObj()->id, ArtifactPosition::CREATURE_SLOT);
  182. dst.creature = getSlot();
  183. if(pickedArtInst->canBePutAt(myStack, ArtifactPosition::CREATURE_SLOT, true))
  184. { //equip clicked stack
  185. if(auto dstArt = myStack->getArt(ArtifactPosition::CREATURE_SLOT))
  186. {
  187. //creature can wear only one active artifact
  188. //if we are placing a new one, the old one will be returned to the hero's backpack
  189. GAME->interface()->cb->swapArtifacts(dst, ArtifactLocation(srcHero->id,
  190. ArtifactUtils::getArtBackpackPosition(srcHero, dstArt->getTypeId())));
  191. }
  192. GAME->interface()->cb->swapArtifacts(src, dst);
  193. }
  194. }
  195. }
  196. }
  197. if (!artSelected && creature)
  198. {
  199. owner->selectSlot(this);
  200. if(creature)
  201. {
  202. for(auto & elem : owner->splitButtons)
  203. elem->block(!our());
  204. }
  205. }
  206. redraw();
  207. return true;
  208. }
  209. /// The creature is only being partially moved
  210. /// @return Whether the view should be refreshed
  211. bool CGarrisonSlot::split()
  212. {
  213. const CGarrisonSlot * selection = owner->getSelection();
  214. owner->setSplittingMode(false);
  215. int minLeft=0;
  216. int minRight=0;
  217. if(upg != selection->upg) // not splitting within same army
  218. {
  219. if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
  220. && selection->getObj()->needsLastStack() )
  221. {
  222. minLeft = 1;
  223. }
  224. // destination army can't be emptied, unless we're rebalancing two stacks of same creature
  225. if(getObj()->stacksCount() == 1
  226. && selection->creature == creature
  227. && getObj()->needsLastStack() )
  228. {
  229. minRight = 1;
  230. }
  231. }
  232. int countLeft = selection->myStack ? selection->myStack->count : 0;
  233. int countRight = myStack ? myStack->count : 0;
  234. auto splitFunctor = [this, selection](int amountLeft, int amountRight)
  235. {
  236. owner->splitStacks(selection, owner->army(upg), ID, amountRight);
  237. };
  238. ENGINE->windows().createAndPushWindow<CSplitWindow>(selection->creature, splitFunctor, minLeft, minRight, countLeft, countRight);
  239. return true;
  240. }
  241. bool CGarrisonSlot::mustForceReselection() const
  242. {
  243. const CGarrisonSlot * selection = owner->getSelection();
  244. bool withAlly = selection->our() ^ our();
  245. // not our turn - actions are blocked
  246. if (!GAME->interface()->makingTurn)
  247. return true;
  248. // Attempt to take creatures from ally (select theirs first)
  249. if (!selection->our())
  250. return true;
  251. if (!creature || !selection->creature)
  252. return false;
  253. // Attempt to swap creatures with ally (select ours first)
  254. if (selection->creature != creature && withAlly)
  255. return true;
  256. if (!owner->removableUnits)
  257. {
  258. if (selection->upg == EGarrisonType::UPPER)
  259. return true;
  260. else
  261. return creature || upg == EGarrisonType::UPPER;
  262. }
  263. return false;
  264. }
  265. void CGarrisonSlot::showPopupWindow(const Point & cursorPosition)
  266. {
  267. if(creature)
  268. {
  269. ENGINE->windows().createAndPushWindow<CStackWindow>(myStack, true);
  270. }
  271. }
  272. void CGarrisonSlot::clickPressed(const Point & cursorPosition)
  273. {
  274. bool refr = false;
  275. const CGarrisonSlot * selection = owner->getSelection();
  276. if(!selection)
  277. {
  278. refr = highlightOrDropArtifact(); // Affects selection
  279. handleSplittingShortcuts();
  280. }
  281. else if(selection == this)
  282. {
  283. if(!handleSplittingShortcuts())
  284. refr = viewInfo(); // Affects selection
  285. }
  286. // Re-highlight if troops aren't removable or not ours.
  287. else if (mustForceReselection())
  288. {
  289. if(creature)
  290. owner->selectSlot(this);
  291. owner->redraw();
  292. refr = true;
  293. }
  294. else
  295. {
  296. const CArmedInstance * selectedObj = owner->army(selection->upg);
  297. bool lastHeroStackSelected = false;
  298. if(selectedObj->stacksCount() == 1
  299. && owner->getSelection()->upg != upg
  300. && selectedObj->needsLastStack())
  301. {
  302. lastHeroStackSelected = true;
  303. }
  304. if((owner->getSplittingMode() || ENGINE->isKeyboardShiftDown()) // split window
  305. && (!creature || creature == selection->creature))
  306. {
  307. refr = split();
  308. }
  309. else if(!creature && lastHeroStackSelected) // split all except last creature
  310. GAME->interface()->cb->splitStack(selectedObj, owner->army(upg), selection->ID, ID, selection->myStack->count - 1);
  311. else if(creature != selection->creature) // swap
  312. GAME->interface()->cb->swapCreatures(owner->army(upg), selectedObj, ID, selection->ID);
  313. else if(lastHeroStackSelected) // merge last stack to other hero stack
  314. refr = split();
  315. else // merge
  316. GAME->interface()->cb->mergeStacks(selectedObj, owner->army(upg), selection->ID, ID);
  317. }
  318. if(refr)
  319. {
  320. // Refresh Statusbar
  321. hover(false);
  322. hover(true);
  323. }
  324. }
  325. void CGarrisonSlot::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  326. {
  327. if(!on)
  328. return;
  329. if(!myStack)
  330. return;
  331. if (!settings["input"]["radialWheelGarrisonSwipe"].Bool())
  332. return;
  333. if(ENGINE->windows().topWindow<CIntObject>()->isPopupWindow())
  334. return;
  335. const auto * otherArmy = upg == EGarrisonType::UPPER ? owner->lowerArmy() : owner->upperArmy();
  336. bool stackExists = myStack != nullptr;
  337. bool hasSameUnit = stackExists && !owner->army(upg)->getCreatureSlots(myStack->getCreature(), ID).empty();
  338. bool hasOwnEmptySlots = stackExists && owner->army(upg)->getFreeSlot() != SlotID();
  339. bool exchangeMode = stackExists && owner->upperArmy() && owner->lowerArmy();
  340. bool hasOtherEmptySlots = exchangeMode && otherArmy->getFreeSlot() != SlotID();
  341. bool hasAnyEmptySlots = hasOtherEmptySlots || hasOwnEmptySlots;
  342. std::vector<RadialMenuConfig> menuElements = {
  343. { RadialMenuConfig::ITEM_NW, hasSameUnit, "stackMerge", "vcmi.radialWheel.mergeSameUnit", [this](){owner->bulkMergeStacks(this);} },
  344. { RadialMenuConfig::ITEM_NE, hasOwnEmptySlots, "stackFillOne", "vcmi.radialWheel.fillSingleUnit", [this](){owner->bulkSplitStack(this);} },
  345. { RadialMenuConfig::ITEM_WW, hasOwnEmptySlots, "stackSplitOne", "vcmi.radialWheel.splitSingleUnit", [this](){splitIntoParts(this->getGarrison(), 1); } },
  346. { RadialMenuConfig::ITEM_EE, hasOwnEmptySlots, "stackSplitEqual", "vcmi.radialWheel.splitUnitEqually", [this](){owner->bulkSmartSplitStack(this);} },
  347. { RadialMenuConfig::ITEM_SW, hasOtherEmptySlots, "heroMove", "vcmi.radialWheel.moveUnit", [this](){owner->moveStackToAnotherArmy(this);} },
  348. { RadialMenuConfig::ITEM_SE, hasAnyEmptySlots, "heroSwap", "vcmi.radialWheel.splitUnit", [this](){ owner->selectSlot(this); owner->splitClick();} },
  349. };
  350. if (hasAnyEmptySlots || hasSameUnit)
  351. ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements);
  352. }
  353. void CGarrisonSlot::update()
  354. {
  355. if(getObj() != nullptr)
  356. {
  357. addUsedEvents(LCLICK | SHOW_POPUP | GESTURE | HOVER);
  358. myStack = getObj()->getStackPtr(ID);
  359. creature = myStack ? myStack->getCreature() : nullptr;
  360. }
  361. else
  362. {
  363. removeUsedEvents(LCLICK | SHOW_POPUP | GESTURE | HOVER);
  364. myStack = nullptr;
  365. creature = nullptr;
  366. }
  367. if(creature)
  368. {
  369. creatureImage->enable();
  370. creatureImage->setFrame(creature->getIconIndex());
  371. stackCount->enable();
  372. stackCount->setText(TextOperations::formatMetric(myStack->count, 4));
  373. }
  374. else
  375. {
  376. creatureImage->disable();
  377. stackCount->disable();
  378. }
  379. }
  380. CGarrisonSlot::CGarrisonSlot(CGarrisonInt * Owner, int x, int y, SlotID IID, EGarrisonType Upg, const CStackInstance * creature_)
  381. : ID(IID),
  382. owner(Owner),
  383. myStack(creature_),
  384. creature(creature_ ? creature_->getCreature() : nullptr),
  385. upg(Upg)
  386. {
  387. OBJECT_CONSTRUCTION;
  388. pos.x += x;
  389. pos.y += y;
  390. AnimationPath imgName = AnimationPath::builtin(owner->smallIcons ? "cprsmall" : "TWCRPORT");
  391. creatureImage = std::make_shared<CAnimImage>(imgName, 0);
  392. creatureImage->disable();
  393. selectionImage = std::make_shared<CAnimImage>(imgName, 1);
  394. selectionImage->disable();
  395. selectionImage->center(creatureImage->pos.center());
  396. if(Owner->smallIcons)
  397. {
  398. pos.w = 32;
  399. pos.h = 32;
  400. }
  401. else
  402. {
  403. pos.w = 58;
  404. pos.h = 64;
  405. }
  406. int labelPosW = pos.w;
  407. int labelPosH = pos.h;
  408. if(Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS) //labels under icon
  409. {
  410. labelPosW = pos.w / 2 + 1;
  411. labelPosH += 7;
  412. }
  413. ETextAlignment labelAlignment = Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS
  414. ? ETextAlignment::CENTER
  415. : ETextAlignment::BOTTOMRIGHT;
  416. stackCount = std::make_shared<CLabel>(labelPosW, labelPosH, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, labelAlignment, Colors::WHITE);
  417. update();
  418. }
  419. void CGarrisonSlot::splitIntoParts(EGarrisonType type, int amount)
  420. {
  421. auto empty = owner->getEmptySlot(type);
  422. if(empty == SlotID())
  423. return;
  424. owner->splitStacks(this, owner->army(type), empty, amount);
  425. }
  426. bool CGarrisonSlot::handleSplittingShortcuts()
  427. {
  428. const bool isAlt = ENGINE->isKeyboardAltDown();
  429. const bool isLShift = ENGINE->isKeyboardShiftDown();
  430. const bool isLCtrl = ENGINE->isKeyboardCmdDown();
  431. if(!isAlt && !isLShift && !isLCtrl)
  432. return false; // This is only case when return false
  433. auto selected = owner->getSelection();
  434. if(!selected)
  435. return true; // Some Shortcusts are pressed but there are no appropriate actions
  436. auto units = selected->myStack->count;
  437. if(units < 1)
  438. return true;
  439. if (isLShift && isLCtrl && isAlt)
  440. {
  441. owner->bulkMoveArmy(selected);
  442. }
  443. else if(isLCtrl && isAlt)
  444. {
  445. owner->moveStackToAnotherArmy(selected);
  446. }
  447. else if(isLShift && isAlt)
  448. {
  449. auto dismiss = getDismiss();
  450. if(dismiss)
  451. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[12], dismiss, nullptr);
  452. }
  453. else if(isAlt)
  454. {
  455. owner->bulkMergeStacks(selected);
  456. }
  457. else
  458. {
  459. if(units <= 1)
  460. return true;
  461. if(isLCtrl && isLShift)
  462. owner->bulkSplitStack(selected);
  463. else if(isLShift)
  464. owner->bulkSmartSplitStack(selected);
  465. else
  466. splitIntoParts(selected->upg, 1); // LCtrl
  467. }
  468. return true;
  469. }
  470. void CGarrisonInt::addSplitBtn(std::shared_ptr<CButton> button)
  471. {
  472. addChild(button.get());
  473. splitButtons.push_back(button);
  474. button->block(getSelection() == nullptr);
  475. }
  476. void CGarrisonInt::createSlots()
  477. {
  478. availableSlots.clear();
  479. int distance = interx + (smallIcons ? 32 : 58);
  480. for(auto i : { EGarrisonType::UPPER, EGarrisonType::LOWER })
  481. {
  482. Point offset = garOffset * static_cast<int>(i);
  483. for(int j = 0; j < 7; j++)
  484. {
  485. Point position(offset.x + (j*distance), offset.y);
  486. if(layout == ESlotsLayout::TWO_ROWS && j >= 4)
  487. {
  488. position += Point(-126, 37);
  489. }
  490. else if(layout == ESlotsLayout::REVERSED_TWO_ROWS)
  491. {
  492. if(j >= 3)
  493. {
  494. position += Point(-90, 49);
  495. }
  496. else
  497. {
  498. position += Point(36, 0);
  499. }
  500. }
  501. SlotID slot(j);
  502. availableSlots.push_back(std::make_shared<CGarrisonSlot>(this, position.x, position.y, slot, i, army(i) ? army(i)->getStackPtr(slot) : nullptr));
  503. }
  504. }
  505. }
  506. void CGarrisonInt::recreateSlots()
  507. {
  508. selectSlot(nullptr);
  509. setSplittingMode(false);
  510. for(auto & elem : splitButtons)
  511. elem->block(true);
  512. for(auto slot : availableSlots)
  513. slot->update();
  514. }
  515. void CGarrisonInt::splitClick()
  516. {
  517. if(!getSelection())
  518. return;
  519. setSplittingMode(!getSplittingMode());
  520. redraw();
  521. }
  522. void CGarrisonInt::splitStacks(const CGarrisonSlot * from, const CArmedInstance * armyDest, SlotID slotDest, int amount )
  523. {
  524. GAME->interface()->cb->splitStack(armedObjs[from->upg], armyDest, from->ID, slotDest, amount);
  525. }
  526. bool CGarrisonInt::checkSelected(const CGarrisonSlot * selected, TQuantity min) const
  527. {
  528. return selected && selected->myStack && selected->myStack->count > min && selected->creature;
  529. }
  530. void CGarrisonInt::moveStackToAnotherArmy(const CGarrisonSlot * selected)
  531. {
  532. if(!checkSelected(selected))
  533. return;
  534. const auto srcArmyType = selected->upg;
  535. const auto destArmyType = srcArmyType == EGarrisonType::UPPER
  536. ? EGarrisonType::LOWER
  537. : EGarrisonType::UPPER;
  538. auto srcArmy = armedObjs[srcArmyType];
  539. auto destArmy = armedObjs[destArmyType];
  540. if(!destArmy)
  541. return;
  542. auto destSlot = destArmy->getSlotFor(selected->creature);
  543. if(destSlot == SlotID())
  544. return;
  545. const auto srcSlot = selected->ID;
  546. const bool isDestSlotEmpty = !destArmy->getStackCount(destSlot);
  547. if(isDestSlotEmpty && !destArmy->getStackCount(srcSlot))
  548. destSlot = srcSlot; // Same place is more preferable
  549. const bool isLastStack = srcArmy->stacksCount() == 1 && srcArmy->needsLastStack();
  550. auto srcAmount = selected->myStack->count - (isLastStack ? 1 : 0);
  551. if(!srcAmount)
  552. return;
  553. if(!isDestSlotEmpty || isLastStack)
  554. {
  555. srcAmount += destArmy->getStackCount(destSlot); // Due to 'split' implementation in the 'CGameHandler::arrangeStacks'
  556. GAME->interface()->cb->splitStack(srcArmy, destArmy, srcSlot, destSlot, srcAmount);
  557. }
  558. else
  559. {
  560. GAME->interface()->cb->swapCreatures(srcArmy, destArmy, srcSlot, destSlot);
  561. }
  562. }
  563. void CGarrisonInt::bulkMoveArmy(const CGarrisonSlot * selected)
  564. {
  565. if(!checkSelected(selected))
  566. return;
  567. const auto srcArmyType = selected->upg;
  568. const auto destArmyType = (srcArmyType == EGarrisonType::UPPER)
  569. ? EGarrisonType::LOWER
  570. : EGarrisonType::UPPER;
  571. auto srcArmy = armedObjs[srcArmyType];
  572. auto destArmy = armedObjs[destArmyType];
  573. if(!destArmy)
  574. return;
  575. const auto srcSlot = selected->ID;
  576. GAME->interface()->cb->bulkMoveArmy(srcArmy->id, destArmy->id, srcSlot);
  577. }
  578. void CGarrisonInt::bulkMergeStacks(const CGarrisonSlot * selected)
  579. {
  580. if(!checkSelected(selected))
  581. return;
  582. const auto type = selected->upg;
  583. if(!armedObjs[type]->hasCreatureSlots(selected->creature, selected->ID))
  584. return;
  585. GAME->interface()->cb->bulkMergeStacks(armedObjs[type]->id, selected->ID);
  586. }
  587. void CGarrisonInt::bulkSplitStack(const CGarrisonSlot * selected)
  588. {
  589. if(!checkSelected(selected, 1)) // check if > 1
  590. return;
  591. const auto type = selected->upg;
  592. if(!hasEmptySlot(type))
  593. return;
  594. GAME->interface()->cb->bulkSplitStack(armedObjs[type]->id, selected->ID);
  595. }
  596. void CGarrisonInt::bulkSmartSplitStack(const CGarrisonSlot * selected)
  597. {
  598. if(!checkSelected(selected, 1))
  599. return;
  600. const auto type = selected->upg;
  601. // Do not disturb the server if the creature is already balanced
  602. if(!hasEmptySlot(type) && armedObjs[type]->isCreatureBalanced(selected->creature))
  603. return;
  604. GAME->interface()->cb->bulkSmartSplitStack(armedObjs[type]->id, selected->ID);
  605. }
  606. CGarrisonInt::CGarrisonInt(const Point & position, int inx, const Point & garsOffset, const CArmedInstance * s1, const CArmedInstance * s2, bool _removableUnits, bool smallImgs, ESlotsLayout _layout)
  607. : highlighted(nullptr)
  608. , inSplittingMode(false)
  609. , interx(inx)
  610. , garOffset(garsOffset)
  611. , smallIcons(smallImgs)
  612. , removableUnits(_removableUnits)
  613. , layout(_layout)
  614. {
  615. OBJECT_CONSTRUCTION;
  616. setArmy(s1, EGarrisonType::UPPER);
  617. setArmy(s2, EGarrisonType::LOWER);
  618. pos += position;
  619. createSlots();
  620. }
  621. const CGarrisonSlot * CGarrisonInt::getSelection() const
  622. {
  623. return highlighted;
  624. }
  625. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  626. {
  627. if(slot != highlighted)
  628. {
  629. if(highlighted)
  630. highlighted->setHighlight(false);
  631. highlighted = slot;
  632. for(auto button : splitButtons)
  633. button->block(highlighted == nullptr || !slot->our());
  634. if(highlighted)
  635. highlighted->setHighlight(true);
  636. }
  637. }
  638. void CGarrisonInt::setSplittingMode(bool on)
  639. {
  640. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  641. if(inSplittingMode || on)
  642. {
  643. for(auto slot : availableSlots)
  644. {
  645. if(slot.get() != getSelection())
  646. slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  647. }
  648. inSplittingMode = on;
  649. }
  650. }
  651. bool CGarrisonInt::getSplittingMode()
  652. {
  653. return inSplittingMode;
  654. }
  655. SlotID CGarrisonInt::getEmptySlot(EGarrisonType type) const
  656. {
  657. assert(army(type));
  658. return army(type) ? army(type)->getFreeSlot() : SlotID();
  659. }
  660. bool CGarrisonInt::hasEmptySlot(EGarrisonType type) const
  661. {
  662. return getEmptySlot(type) != SlotID();
  663. }
  664. const CArmedInstance * CGarrisonInt::upperArmy() const
  665. {
  666. return army(EGarrisonType::UPPER);
  667. }
  668. const CArmedInstance * CGarrisonInt::lowerArmy() const
  669. {
  670. return army(EGarrisonType::LOWER);
  671. }
  672. const CArmedInstance * CGarrisonInt::army(EGarrisonType which) const
  673. {
  674. if(armedObjs.count(which))
  675. return armedObjs.at(which);
  676. return nullptr;
  677. }
  678. bool CGarrisonInt::isArmyOwned(EGarrisonType which) const
  679. {
  680. const auto * object = army(which);
  681. if (!object)
  682. return false;
  683. if (object->tempOwner == GAME->interface()->playerID)
  684. return true;
  685. if (object->tempOwner == PlayerColor::UNFLAGGABLE)
  686. return true;
  687. return false;
  688. }
  689. void CGarrisonInt::setArmy(const CArmedInstance * army, EGarrisonType type)
  690. {
  691. armedObjs[type] = army;
  692. }