CGarrisonInt.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../windows/CCreatureWindow.h"
  17. #include "../windows/GUIClasses.h"
  18. #include "../CGameInfo.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../../CCallback.h"
  21. #include "../../lib/ArtifactUtils.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. #include "../../lib/CCreatureHandler.h"
  24. #include "../../lib/mapObjects/CGHeroInstance.h"
  25. #include "../../lib/TextOperations.h"
  26. #include "../../lib/gameState/CGameState.h"
  27. void CGarrisonSlot::setHighlight(bool on)
  28. {
  29. if (on)
  30. selectionImage->enable(); //show
  31. else
  32. selectionImage->disable(); //hide
  33. }
  34. void CGarrisonSlot::hover (bool on)
  35. {
  36. ////Hoverable::hover(on);
  37. if(on)
  38. {
  39. std::string temp;
  40. if(creature)
  41. {
  42. if(owner->getSelection())
  43. {
  44. if(owner->getSelection() == this)
  45. {
  46. temp = CGI->generaltexth->tcommands[4]; //View %s
  47. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  48. }
  49. else if (owner->getSelection()->creature == creature)
  50. {
  51. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  52. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  53. }
  54. else if (owner->getSelection()->creature)
  55. {
  56. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  57. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  58. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  59. }
  60. else
  61. {
  62. logGlobal->warn("Warning - shouldn't be - highlighted void slot %d", owner->getSelection()->ID.getNum());
  63. logGlobal->warn("Highlighted set to nullptr");
  64. owner->selectSlot(nullptr);
  65. }
  66. }
  67. else
  68. {
  69. const bool isHeroOnMap = owner->armedObjs[0] // Hero is not a visitor and not a garrison defender
  70. && owner->armedObjs[0]->ID == Obj::HERO
  71. && (!owner->armedObjs[1] || owner->armedObjs[1]->ID == Obj::HERO) // one hero or we are in the Heroes exchange window
  72. && !(static_cast<const CGHeroInstance*>(owner->armedObjs[0]))->inTownGarrison;
  73. if(isHeroOnMap)
  74. {
  75. temp = CGI->generaltexth->allTexts[481]; //Select %s
  76. }
  77. else if(upg == EGarrisonType::UP)
  78. {
  79. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  80. }
  81. else // Hero is visiting some object (town, mine, etc)
  82. {
  83. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  84. }
  85. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  86. }
  87. }
  88. else
  89. {
  90. if(owner->getSelection())
  91. {
  92. const CArmedInstance *highl = owner->getSelection()->getObj();
  93. if( highl->needsLastStack() //we are moving stack from hero's
  94. && highl->stacksCount() == 1 //it's only stack
  95. && owner->getSelection()->upg != upg //we're moving it to the other garrison
  96. )
  97. {
  98. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  99. }
  100. else
  101. {
  102. temp = CGI->generaltexth->tcommands[6]; //Move %s
  103. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  104. }
  105. }
  106. else
  107. {
  108. temp = CGI->generaltexth->tcommands[11]; //Empty
  109. }
  110. }
  111. GH.statusbar()->write(temp);
  112. }
  113. else
  114. {
  115. GH.statusbar()->clear();
  116. }
  117. }
  118. const CArmedInstance * CGarrisonSlot::getObj() const
  119. {
  120. return owner->armedObjs[upg];
  121. }
  122. /// @return Whether the unit in the slot belongs to the current player.
  123. bool CGarrisonSlot::our() const
  124. {
  125. return owner->owned[upg];
  126. }
  127. /// @return Whether the unit in the slot belongs to an ally but not to the current player.
  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->p2 = ID; // store the second stack pos
  210. owner->pb = upg; // store the second stack owner (up or down army)
  211. owner->setSplittingMode(false);
  212. int minLeft=0, minRight=0;
  213. if(upg != selection->upg) // not splitting within same army
  214. {
  215. if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
  216. && selection->getObj()->needsLastStack() )
  217. {
  218. minLeft = 1;
  219. }
  220. // destination army can't be emptied, unless we're rebalancing two stacks of same creature
  221. if(getObj()->stacksCount() == 1
  222. && selection->creature == creature
  223. && getObj()->needsLastStack() )
  224. {
  225. minRight = 1;
  226. }
  227. }
  228. int countLeft = selection->myStack ? selection->myStack->count : 0;
  229. int countRight = myStack ? myStack->count : 0;
  230. GH.windows().createAndPushWindow<CSplitWindow>(selection->creature, std::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
  231. minLeft, minRight, countLeft, countRight);
  232. return true;
  233. }
  234. /// If certain creates cannot be moved, the selection should change
  235. /// Force reselection in these cases
  236. /// * When attempting to take creatures from ally
  237. /// * When attempting to swap creatures with an ally
  238. /// * When attempting to take unremovable units
  239. /// @return Whether reselection must be done
  240. bool CGarrisonSlot::mustForceReselection() const
  241. {
  242. const CGarrisonSlot * selection = owner->getSelection();
  243. bool withAlly = selection->our() ^ our();
  244. if (!creature || !selection->creature)
  245. return false;
  246. // Attempt to take creatures from ally (select theirs first)
  247. if (!selection->our())
  248. return true;
  249. // Attempt to swap creatures with ally (select ours first)
  250. if (selection->creature != creature && withAlly)
  251. return true;
  252. if (!owner->removableUnits)
  253. {
  254. if (selection->upg == EGarrisonType::UP)
  255. return true;
  256. else
  257. return creature || upg == EGarrisonType::UP;
  258. }
  259. return false;
  260. }
  261. void CGarrisonSlot::showPopupWindow(const Point & cursorPosition)
  262. {
  263. if(creature)
  264. {
  265. GH.windows().createAndPushWindow<CStackWindow>(myStack, true);
  266. }
  267. }
  268. void CGarrisonSlot::clickPressed(const Point & cursorPosition)
  269. {
  270. bool refr = false;
  271. const CGarrisonSlot * selection = owner->getSelection();
  272. if(!selection)
  273. {
  274. refr = highlightOrDropArtifact(); // Affects selection
  275. handleSplittingShortcuts();
  276. }
  277. else if(selection == this)
  278. {
  279. if(!handleSplittingShortcuts())
  280. refr = viewInfo(); // Affects selection
  281. }
  282. // Re-highlight if troops aren't removable or not ours.
  283. else if (mustForceReselection())
  284. {
  285. if(creature)
  286. owner->selectSlot(this);
  287. redraw();
  288. refr = true;
  289. }
  290. else
  291. {
  292. const CArmedInstance * selectedObj = owner->armedObjs[selection->upg];
  293. bool lastHeroStackSelected = false;
  294. if(selectedObj->stacksCount() == 1
  295. && owner->getSelection()->upg != upg
  296. && selectedObj->needsLastStack())
  297. {
  298. lastHeroStackSelected = true;
  299. }
  300. if((owner->getSplittingMode() || GH.isKeyboardShiftDown()) // split window
  301. && (!creature || creature == selection->creature))
  302. {
  303. refr = split();
  304. }
  305. else if(!creature && lastHeroStackSelected) // split all except last creature
  306. LOCPLINT->cb->splitStack(selectedObj, owner->armedObjs[upg], selection->ID, ID, selection->myStack->count - 1);
  307. else if(creature != selection->creature) // swap
  308. LOCPLINT->cb->swapCreatures(owner->armedObjs[upg], selectedObj, ID, selection->ID);
  309. else if(lastHeroStackSelected) // merge last stack to other hero stack
  310. refr = split();
  311. else // merge
  312. LOCPLINT->cb->mergeStacks(selectedObj, owner->armedObjs[upg], selection->ID, ID);
  313. }
  314. if(refr)
  315. {
  316. // Refresh Statusbar
  317. hover(false);
  318. hover(true);
  319. }
  320. }
  321. void CGarrisonSlot::update()
  322. {
  323. if(getObj() != nullptr)
  324. {
  325. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  326. myStack = getObj()->getStackPtr(ID);
  327. creature = myStack ? myStack->type : nullptr;
  328. }
  329. else
  330. {
  331. removeUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  332. myStack = nullptr;
  333. creature = nullptr;
  334. }
  335. if(creature)
  336. {
  337. creatureImage->enable();
  338. creatureImage->setFrame(creature->getIconIndex());
  339. stackCount->enable();
  340. stackCount->setText(TextOperations::formatMetric(myStack->count, 4));
  341. }
  342. else
  343. {
  344. creatureImage->disable();
  345. stackCount->disable();
  346. }
  347. }
  348. CGarrisonSlot::CGarrisonSlot(CGarrisonInt * Owner, int x, int y, SlotID IID, CGarrisonSlot::EGarrisonType Upg, const CStackInstance * creature_)
  349. : ID(IID),
  350. owner(Owner),
  351. myStack(creature_),
  352. creature(creature_ ? creature_->type : nullptr),
  353. upg(Upg)
  354. {
  355. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  356. pos.x += x;
  357. pos.y += y;
  358. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  359. creatureImage = std::make_shared<CAnimImage>(imgName, 0);
  360. creatureImage->disable();
  361. selectionImage = std::make_shared<CAnimImage>(imgName, 1);
  362. selectionImage->disable();
  363. if(Owner->smallIcons)
  364. {
  365. pos.w = 32;
  366. pos.h = 32;
  367. }
  368. else
  369. {
  370. pos.w = 58;
  371. pos.h = 64;
  372. }
  373. int labelPosW = pos.w;
  374. int labelPosH = pos.h;
  375. if(Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS) //labels under icon
  376. {
  377. labelPosW = pos.w / 2 + 1;
  378. labelPosH += 7;
  379. }
  380. ETextAlignment labelAlignment = Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS
  381. ? ETextAlignment::CENTER
  382. : ETextAlignment::BOTTOMRIGHT;
  383. stackCount = std::make_shared<CLabel>(labelPosW, labelPosH, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, labelAlignment, Colors::WHITE);
  384. update();
  385. }
  386. void CGarrisonSlot::splitIntoParts(CGarrisonSlot::EGarrisonType type, int amount)
  387. {
  388. auto empty = owner->getEmptySlot(type);
  389. if(empty == SlotID())
  390. return;
  391. owner->pb = type;
  392. owner->p2 = empty;
  393. owner->splitStacks(1, amount);
  394. }
  395. bool CGarrisonSlot::handleSplittingShortcuts()
  396. {
  397. const bool isAlt = GH.isKeyboardAltDown();
  398. const bool isLShift = GH.isKeyboardShiftDown();
  399. const bool isLCtrl = GH.isKeyboardCtrlDown();
  400. if(!isAlt && !isLShift && !isLCtrl)
  401. return false; // This is only case when return false
  402. auto selected = owner->getSelection();
  403. if(!selected)
  404. return true; // Some Shortcusts are pressed but there are no appropriate actions
  405. auto units = selected->myStack->count;
  406. if(units < 1)
  407. return true;
  408. if (isLShift && isLCtrl && isAlt)
  409. {
  410. owner->bulkMoveArmy(selected);
  411. }
  412. else if(isLCtrl && isAlt)
  413. {
  414. owner->moveStackToAnotherArmy(selected);
  415. }
  416. else if(isLShift && isAlt)
  417. {
  418. auto dismiss = getDismiss();
  419. if(dismiss)
  420. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], dismiss, nullptr);
  421. }
  422. else if(isAlt)
  423. {
  424. owner->bulkMergeStacks(selected);
  425. }
  426. else
  427. {
  428. if(units <= 1)
  429. return true;
  430. if(isLCtrl && isLShift)
  431. owner->bulkSplitStack(selected);
  432. else if(isLShift)
  433. owner->bulkSmartSplitStack(selected);
  434. else
  435. splitIntoParts(selected->upg, 1); // LCtrl
  436. }
  437. return true;
  438. }
  439. void CGarrisonInt::addSplitBtn(std::shared_ptr<CButton> button)
  440. {
  441. addChild(button.get());
  442. button->recActions &= ~DISPOSE;
  443. splitButtons.push_back(button);
  444. button->block(getSelection() == nullptr);
  445. }
  446. void CGarrisonInt::createSlots()
  447. {
  448. int distance = interx + (smallIcons ? 32 : 58);
  449. for(int i = 0; i < 2; i++)
  450. {
  451. std::vector<std::shared_ptr<CGarrisonSlot>> garrisonSlots;
  452. garrisonSlots.resize(7);
  453. if(armedObjs[i])
  454. {
  455. for(auto & elem : armedObjs[i]->Slots())
  456. {
  457. garrisonSlots[elem.first.getNum()] = std::make_shared<CGarrisonSlot>(this, i*garOffset.x + (elem.first.getNum()*distance), i*garOffset.y, elem.first, static_cast<CGarrisonSlot::EGarrisonType>(i), elem.second);
  458. }
  459. }
  460. for(int j = 0; j < 7; j++)
  461. {
  462. if(!garrisonSlots[j])
  463. garrisonSlots[j] = std::make_shared<CGarrisonSlot>(this, i*garOffset.x + (j*distance), i*garOffset.y, SlotID(j), static_cast<CGarrisonSlot::EGarrisonType>(i), nullptr);
  464. if(layout == ESlotsLayout::TWO_ROWS && j >= 4)
  465. {
  466. garrisonSlots[j]->moveBy(Point(-126, 37));
  467. }
  468. else if(layout == ESlotsLayout::REVERSED_TWO_ROWS)
  469. {
  470. if(j >= 3)
  471. {
  472. garrisonSlots[j]->moveBy(Point(-90, 49));
  473. }
  474. else
  475. {
  476. garrisonSlots[j]->moveBy(Point(36, 0));
  477. }
  478. }
  479. }
  480. vstd::concatenate(availableSlots, garrisonSlots);
  481. }
  482. }
  483. void CGarrisonInt::recreateSlots()
  484. {
  485. selectSlot(nullptr);
  486. setSplittingMode(false);
  487. for(auto & elem : splitButtons)
  488. elem->block(true);
  489. for(auto slot : availableSlots)
  490. slot->update();
  491. }
  492. void CGarrisonInt::splitClick()
  493. {
  494. if(!getSelection())
  495. return;
  496. setSplittingMode(!getSplittingMode());
  497. redraw();
  498. }
  499. void CGarrisonInt::splitStacks(int, int amountRight)
  500. {
  501. LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
  502. }
  503. bool CGarrisonInt::checkSelected(const CGarrisonSlot * selected, TQuantity min) const
  504. {
  505. return selected && selected->myStack && selected->myStack->count > min && selected->creature;
  506. }
  507. void CGarrisonInt::moveStackToAnotherArmy(const CGarrisonSlot * selected)
  508. {
  509. if(!checkSelected(selected))
  510. return;
  511. const auto srcArmyType = selected->upg;
  512. const auto destArmyType = srcArmyType == CGarrisonSlot::UP
  513. ? CGarrisonSlot::DOWN
  514. : CGarrisonSlot::UP;
  515. auto srcArmy = armedObjs[srcArmyType];
  516. auto destArmy = armedObjs[destArmyType];
  517. if(!destArmy)
  518. return;
  519. auto destSlot = destArmy->getSlotFor(selected->creature);
  520. if(destSlot == SlotID())
  521. return;
  522. const auto srcSlot = selected->ID;
  523. const bool isDestSlotEmpty = !destArmy->getStackCount(destSlot);
  524. if(isDestSlotEmpty && !destArmy->getStackCount(srcSlot))
  525. destSlot = srcSlot; // Same place is more preferable
  526. const bool isLastStack = srcArmy->stacksCount() == 1 && srcArmy->needsLastStack();
  527. auto srcAmount = selected->myStack->count - (isLastStack ? 1 : 0);
  528. if(!srcAmount)
  529. return;
  530. if(!isDestSlotEmpty || isLastStack)
  531. {
  532. srcAmount += destArmy->getStackCount(destSlot); // Due to 'split' implementation in the 'CGameHandler::arrangeStacks'
  533. LOCPLINT->cb->splitStack(srcArmy, destArmy, srcSlot, destSlot, srcAmount);
  534. }
  535. else
  536. {
  537. LOCPLINT->cb->swapCreatures(srcArmy, destArmy, srcSlot, destSlot);
  538. }
  539. }
  540. void CGarrisonInt::bulkMoveArmy(const CGarrisonSlot * selected)
  541. {
  542. if(!checkSelected(selected))
  543. return;
  544. const auto srcArmyType = selected->upg;
  545. const auto destArmyType = (srcArmyType == CGarrisonSlot::UP)
  546. ? CGarrisonSlot::DOWN
  547. : CGarrisonSlot::UP;
  548. auto srcArmy = armedObjs[srcArmyType];
  549. auto destArmy = armedObjs[destArmyType];
  550. if(!destArmy)
  551. return;
  552. const auto srcSlot = selected->ID;
  553. LOCPLINT->cb->bulkMoveArmy(srcArmy->id, destArmy->id, srcSlot);
  554. }
  555. void CGarrisonInt::bulkMergeStacks(const CGarrisonSlot * selected)
  556. {
  557. if(!checkSelected(selected))
  558. return;
  559. const auto type = selected->upg;
  560. if(!armedObjs[type]->hasCreatureSlots(selected->creature, selected->ID))
  561. return;
  562. LOCPLINT->cb->bulkMergeStacks(armedObjs[type]->id, selected->ID);
  563. }
  564. void CGarrisonInt::bulkSplitStack(const CGarrisonSlot * selected)
  565. {
  566. if(!checkSelected(selected, 1)) // check if > 1
  567. return;
  568. const auto type = selected->upg;
  569. if(!hasEmptySlot(type))
  570. return;
  571. LOCPLINT->cb->bulkSplitStack(armedObjs[type]->id, selected->ID);
  572. }
  573. void CGarrisonInt::bulkSmartSplitStack(const CGarrisonSlot * selected)
  574. {
  575. if(!checkSelected(selected, 1))
  576. return;
  577. const auto type = selected->upg;
  578. // Do not disturb the server if the creature is already balanced
  579. if(!hasEmptySlot(type) && armedObjs[type]->isCreatureBalanced(selected->creature))
  580. return;
  581. LOCPLINT->cb->bulkSmartSplitStack(armedObjs[type]->id, selected->ID);
  582. }
  583. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point & garsOffset,
  584. const CArmedInstance * s1, const CArmedInstance * s2,
  585. bool _removableUnits, bool smallImgs, ESlotsLayout _layout)
  586. : highlighted(nullptr),
  587. inSplittingMode(false),
  588. interx(inx),
  589. garOffset(garsOffset),
  590. pb(false),
  591. smallIcons(smallImgs),
  592. removableUnits(_removableUnits),
  593. layout(_layout)
  594. {
  595. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  596. setArmy(s1, false);
  597. setArmy(s2, true);
  598. pos.x += x;
  599. pos.y += y;
  600. createSlots();
  601. }
  602. const CGarrisonSlot * CGarrisonInt::getSelection() const
  603. {
  604. return highlighted;
  605. }
  606. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  607. {
  608. if(slot != highlighted)
  609. {
  610. if(highlighted)
  611. highlighted->setHighlight(false);
  612. highlighted = slot;
  613. for(auto button : splitButtons)
  614. button->block(highlighted == nullptr || !slot->our());
  615. if(highlighted)
  616. highlighted->setHighlight(true);
  617. }
  618. }
  619. void CGarrisonInt::setSplittingMode(bool on)
  620. {
  621. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  622. if(inSplittingMode || on)
  623. {
  624. for(auto slot : availableSlots)
  625. {
  626. if(slot.get() != getSelection())
  627. slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  628. }
  629. inSplittingMode = on;
  630. }
  631. }
  632. bool CGarrisonInt::getSplittingMode()
  633. {
  634. return inSplittingMode;
  635. }
  636. SlotID CGarrisonInt::getEmptySlot(CGarrisonSlot::EGarrisonType type) const
  637. {
  638. assert(armedObjs[type]);
  639. return armedObjs[type] ? armedObjs[type]->getFreeSlot() : SlotID();
  640. }
  641. bool CGarrisonInt::hasEmptySlot(CGarrisonSlot::EGarrisonType type) const
  642. {
  643. return getEmptySlot(type) != SlotID();
  644. }
  645. void CGarrisonInt::setArmy(const CArmedInstance * army, bool bottomGarrison)
  646. {
  647. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  648. armedObjs[bottomGarrison] = army;
  649. }