CGarrisonInt.cpp 20 KB

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