2
0

CGarrisonInt.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../windows/CCreatureWindow.h"
  18. #include "../windows/GUIClasses.h"
  19. #include "../../CCallback.h"
  20. #include "../../lib/CGeneralTextHandler.h"
  21. #include "../../lib/CCreatureHandler.h"
  22. #include "../../lib/mapObjects/CGHeroInstance.h"
  23. #include "../../lib/CGameState.h"
  24. #ifdef VCMI_MAC
  25. #define SDL_SCANCODE_LCTRL SDL_SCANCODE_LGUI
  26. #endif
  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->nameSing);
  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->nameSing);
  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->nameSing);
  58. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  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->nameSing);
  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->nameSing);
  104. }
  105. }
  106. else
  107. {
  108. temp = CGI->generaltexth->tcommands[11]; //Empty
  109. }
  110. }
  111. GH.statusbar->setText(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->getUpgradeInfo(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.pushIntT<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 (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt().get())) //dirty solution
  168. {
  169. std::shared_ptr<CArtifactsOfHero::SCommonPart> commonInfo = chw->getCommonPart();
  170. const CArtifactInstance * art = nullptr;
  171. if(commonInfo)
  172. art = commonInfo->src.art;
  173. if(art)
  174. {
  175. const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
  176. artSelected = true;
  177. if (myStack) // try dropping the artifact only if the slot isn't empty
  178. {
  179. ArtifactLocation src(srcHero, commonInfo->src.slotID);
  180. ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
  181. if (art->canBePutAt(dst, true))
  182. { //equip clicked stack
  183. if(dst.getArt())
  184. {
  185. //creature can wear only one active artifact
  186. //if we are placing a new one, the old one will be returned to the hero's backpack
  187. LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
  188. }
  189. LOCPLINT->cb->swapArtifacts(src, dst);
  190. }
  191. }
  192. }
  193. }
  194. if (!artSelected && creature)
  195. {
  196. owner->selectSlot(this);
  197. if(creature)
  198. {
  199. for(auto & elem : owner->splitButtons)
  200. elem->block(!our());
  201. }
  202. }
  203. redraw();
  204. return true;
  205. }
  206. /// The creature is only being partially moved
  207. /// @return Whether the view should be refreshed
  208. bool CGarrisonSlot::split()
  209. {
  210. const CGarrisonSlot * selection = owner->getSelection();
  211. owner->p2 = ID; // store the second stack pos
  212. owner->pb = upg; // store the second stack owner (up or down army)
  213. owner->setSplittingMode(false);
  214. int minLeft=0, minRight=0;
  215. if(upg != selection->upg) // not splitting within same army
  216. {
  217. if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
  218. && selection->getObj()->needsLastStack() )
  219. {
  220. minLeft = 1;
  221. }
  222. // destination army can't be emptied, unless we're rebalancing two stacks of same creature
  223. if(getObj()->stacksCount() == 1
  224. && selection->creature == creature
  225. && getObj()->needsLastStack() )
  226. {
  227. minRight = 1;
  228. }
  229. }
  230. int countLeft = selection->myStack ? selection->myStack->count : 0;
  231. int countRight = myStack ? myStack->count : 0;
  232. GH.pushIntT<CSplitWindow>(selection->creature, std::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
  233. minLeft, minRight, countLeft, countRight);
  234. return true;
  235. }
  236. /// If certain creates cannot be moved, the selection should change
  237. /// Force reselection in these cases
  238. /// * When attempting to take creatures from ally
  239. /// * When attempting to swap creatures with an ally
  240. /// * When attempting to take unremovable units
  241. /// @return Whether reselection must be done
  242. bool CGarrisonSlot::mustForceReselection() const
  243. {
  244. const CGarrisonSlot * selection = owner->getSelection();
  245. bool withAlly = selection->our() ^ our();
  246. if (!creature || !selection->creature)
  247. return false;
  248. // Attempt to take creatures from ally (select theirs first)
  249. if (!selection->our())
  250. return true;
  251. // Attempt to swap creatures with ally (select ours first)
  252. if (selection->creature != creature && withAlly)
  253. return true;
  254. if (!owner->removableUnits)
  255. {
  256. if (selection->upg == EGarrisonType::UP)
  257. return true;
  258. else
  259. return creature || upg == EGarrisonType::UP;
  260. }
  261. return false;
  262. }
  263. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  264. {
  265. if(creature && down)
  266. {
  267. GH.pushIntT<CStackWindow>(myStack, true);
  268. }
  269. }
  270. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  271. {
  272. if(down)
  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. redraw();
  292. refr = true;
  293. }
  294. else
  295. {
  296. const CArmedInstance * selectedObj = owner->armedObjs[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() || LOCPLINT->shiftPressed()) // split window
  305. && (!creature || creature == selection->creature))
  306. {
  307. refr = split();
  308. }
  309. else if(!creature && lastHeroStackSelected) // split all except last creature
  310. LOCPLINT->cb->splitStack(selectedObj, owner->armedObjs[upg], selection->ID, ID, selection->myStack->count - 1);
  311. else if(creature != selection->creature) // swap
  312. LOCPLINT->cb->swapCreatures(owner->armedObjs[upg], selectedObj, ID, selection->ID);
  313. else if(lastHeroStackSelected) // merge last stack to other hero stack
  314. refr = split();
  315. else // merge
  316. LOCPLINT->cb->mergeStacks(selectedObj, owner->armedObjs[upg], selection->ID, ID);
  317. }
  318. if(refr)
  319. {
  320. // Refresh Statusbar
  321. hover(false);
  322. hover(true);
  323. }
  324. }
  325. }
  326. void CGarrisonSlot::update()
  327. {
  328. if(getObj() != nullptr)
  329. {
  330. addUsedEvents(LCLICK | RCLICK | HOVER);
  331. myStack = getObj()->getStackPtr(ID);
  332. creature = myStack ? myStack->type : nullptr;
  333. }
  334. else
  335. {
  336. removeUsedEvents(LCLICK | RCLICK | HOVER);
  337. myStack = nullptr;
  338. creature = nullptr;
  339. }
  340. if(creature)
  341. {
  342. creatureImage->enable();
  343. creatureImage->setFrame(creature->getIconIndex());
  344. stackCount->enable();
  345. stackCount->setText(boost::lexical_cast<std::string>(myStack->count));
  346. }
  347. else
  348. {
  349. creatureImage->disable();
  350. stackCount->disable();
  351. }
  352. }
  353. CGarrisonSlot::CGarrisonSlot(CGarrisonInt * Owner, int x, int y, SlotID IID, CGarrisonSlot::EGarrisonType Upg, const CStackInstance * creature_)
  354. : ID(IID),
  355. owner(Owner),
  356. myStack(creature_),
  357. creature(creature_ ? creature_->type : nullptr),
  358. upg(Upg)
  359. {
  360. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  361. pos.x += x;
  362. pos.y += y;
  363. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  364. creatureImage = std::make_shared<CAnimImage>(imgName, 0);
  365. creatureImage->disable();
  366. selectionImage = std::make_shared<CAnimImage>(imgName, 1);
  367. selectionImage->disable();
  368. if(Owner->smallIcons)
  369. {
  370. pos.w = 32;
  371. pos.h = 32;
  372. }
  373. else
  374. {
  375. pos.w = 58;
  376. pos.h = 64;
  377. }
  378. stackCount = std::make_shared<CLabel>(pos.w, pos.h, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
  379. update();
  380. }
  381. void CGarrisonSlot::splitIntoParts(CGarrisonSlot::EGarrisonType type, int amount)
  382. {
  383. auto empty = owner->getEmptySlot(type);
  384. if(empty == SlotID())
  385. return;
  386. owner->pb = type;
  387. owner->p2 = empty;
  388. owner->splitStacks(1, amount);
  389. }
  390. bool CGarrisonSlot::handleSplittingShortcuts()
  391. {
  392. const Uint8 * state = SDL_GetKeyboardState(NULL);
  393. const bool isAlt = !!state[SDL_SCANCODE_LALT];
  394. const bool isLShift = !!state[SDL_SCANCODE_LSHIFT];
  395. const bool isLCtrl = !!state[SDL_SCANCODE_LCTRL];
  396. if(!isAlt && !isLShift && !isLCtrl)
  397. return false; // This is only case when return false
  398. auto selected = owner->getSelection();
  399. if(!selected)
  400. return true; // Some Shortcusts are pressed but there are no appropriate actions
  401. auto units = selected->myStack->count;
  402. if(units < 1)
  403. return true;
  404. if (isLShift && isLCtrl && isAlt)
  405. {
  406. owner->bulkMoveArmy(selected);
  407. }
  408. else if(isLCtrl && isAlt)
  409. {
  410. owner->moveStackToAnotherArmy(selected);
  411. }
  412. else if(isLShift && isAlt)
  413. {
  414. auto dismiss = getDismiss();
  415. if(dismiss)
  416. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], dismiss, nullptr);
  417. }
  418. else if(isAlt)
  419. {
  420. owner->bulkMergeStacks(selected);
  421. }
  422. else
  423. {
  424. if(units <= 1)
  425. return true;
  426. if(isLCtrl && isLShift)
  427. owner->bulkSplitStack(selected);
  428. else if(isLShift)
  429. owner->bulkSmartSplitStack(selected);
  430. else
  431. splitIntoParts(selected->upg, 1); // LCtrl
  432. }
  433. return true;
  434. }
  435. void CGarrisonInt::addSplitBtn(std::shared_ptr<CButton> button)
  436. {
  437. addChild(button.get());
  438. button->recActions &= ~DISPOSE;
  439. splitButtons.push_back(button);
  440. button->block(getSelection() == nullptr);
  441. }
  442. void CGarrisonInt::createSlots()
  443. {
  444. int distance = interx + (smallIcons ? 32 : 58);
  445. for(int i=0; i<2; i++)
  446. {
  447. std::vector<std::shared_ptr<CGarrisonSlot>> garrisonSlots;
  448. garrisonSlots.resize(7);
  449. if(armedObjs[i])
  450. {
  451. for(auto & elem : armedObjs[i]->Slots())
  452. {
  453. 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);
  454. }
  455. }
  456. for(int j=0; j<7; j++)
  457. {
  458. if(!garrisonSlots[j])
  459. garrisonSlots[j] = std::make_shared<CGarrisonSlot>(this, i*garOffset.x + (j*distance), i*garOffset.y, SlotID(j), static_cast<CGarrisonSlot::EGarrisonType>(i), nullptr);
  460. if(twoRows && j>=4)
  461. {
  462. garrisonSlots[j]->moveBy(Point(-126, 37));
  463. }
  464. }
  465. vstd::concatenate(availableSlots, garrisonSlots);
  466. }
  467. }
  468. void CGarrisonInt::recreateSlots()
  469. {
  470. selectSlot(nullptr);
  471. setSplittingMode(false);
  472. for(auto & elem : splitButtons)
  473. elem->block(true);
  474. for(auto slot : availableSlots)
  475. slot->update();
  476. }
  477. void CGarrisonInt::splitClick()
  478. {
  479. if(!getSelection())
  480. return;
  481. setSplittingMode(!getSplittingMode());
  482. redraw();
  483. }
  484. void CGarrisonInt::splitStacks(int, int amountRight)
  485. {
  486. LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
  487. }
  488. bool CGarrisonInt::checkSelected(const CGarrisonSlot * selected, TQuantity min) const
  489. {
  490. return selected && selected->myStack && selected->myStack->count > min && selected->creature;
  491. }
  492. void CGarrisonInt::moveStackToAnotherArmy(const CGarrisonSlot * selected)
  493. {
  494. if(!checkSelected(selected))
  495. return;
  496. const auto srcArmyType = selected->upg;
  497. const auto destArmyType = srcArmyType == CGarrisonSlot::UP
  498. ? CGarrisonSlot::DOWN
  499. : CGarrisonSlot::UP;
  500. auto srcArmy = armedObjs[srcArmyType];
  501. auto destArmy = armedObjs[destArmyType];
  502. if(!destArmy)
  503. return;
  504. auto destSlot = destArmy->getSlotFor(selected->creature);
  505. if(destSlot == SlotID())
  506. return;
  507. const auto srcSlot = selected->ID;
  508. const bool isDestSlotEmpty = !destArmy->getStackCount(destSlot);
  509. if(isDestSlotEmpty && !destArmy->getStackCount(srcSlot))
  510. destSlot = srcSlot; // Same place is more preferable
  511. const bool isLastStack = srcArmy->stacksCount() == 1 && srcArmy->needsLastStack();
  512. auto srcAmount = selected->myStack->count - (isLastStack ? 1 : 0);
  513. if(!srcAmount)
  514. return;
  515. if(!isDestSlotEmpty || isLastStack)
  516. {
  517. srcAmount += destArmy->getStackCount(destSlot); // Due to 'split' implementation in the 'CGameHandler::arrangeStacks'
  518. LOCPLINT->cb->splitStack(srcArmy, destArmy, srcSlot, destSlot, srcAmount);
  519. }
  520. else
  521. {
  522. LOCPLINT->cb->swapCreatures(srcArmy, destArmy, srcSlot, destSlot);
  523. }
  524. }
  525. void CGarrisonInt::bulkMoveArmy(const CGarrisonSlot * selected)
  526. {
  527. if(!checkSelected(selected))
  528. return;
  529. const auto srcArmyType = selected->upg;
  530. const auto destArmyType = (srcArmyType == CGarrisonSlot::UP)
  531. ? CGarrisonSlot::DOWN
  532. : CGarrisonSlot::UP;
  533. auto srcArmy = armedObjs[srcArmyType];
  534. auto destArmy = armedObjs[destArmyType];
  535. if(!destArmy)
  536. return;
  537. const auto srcSlot = selected->ID;
  538. LOCPLINT->cb->bulkMoveArmy(srcArmy->id, destArmy->id, srcSlot);
  539. }
  540. void CGarrisonInt::bulkMergeStacks(const CGarrisonSlot * selected)
  541. {
  542. if(!checkSelected(selected))
  543. return;
  544. const auto type = selected->upg;
  545. if(!armedObjs[type]->hasCreatureSlots(selected->creature, selected->ID))
  546. return;
  547. LOCPLINT->cb->bulkMergeStacks(armedObjs[type]->id, selected->ID);
  548. }
  549. void CGarrisonInt::bulkSplitStack(const CGarrisonSlot * selected)
  550. {
  551. if(!checkSelected(selected, 1)) // check if > 1
  552. return;
  553. const auto type = selected->upg;
  554. if(!hasEmptySlot(type))
  555. return;
  556. LOCPLINT->cb->bulkSplitStack(armedObjs[type]->id, selected->ID);
  557. }
  558. void CGarrisonInt::bulkSmartSplitStack(const CGarrisonSlot * selected)
  559. {
  560. if(!checkSelected(selected, 1))
  561. return;
  562. const auto type = selected->upg;
  563. // Do not disturb the server if the creature is already balanced
  564. if(!hasEmptySlot(type) && armedObjs[type]->isCreatureBalanced(selected->creature))
  565. return;
  566. LOCPLINT->cb->bulkSmartSplitStack(armedObjs[type]->id, selected->ID);
  567. }
  568. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point & garsOffset,
  569. const CArmedInstance * s1, const CArmedInstance * s2,
  570. bool _removableUnits, bool smallImgs, bool _twoRows)
  571. : highlighted(nullptr),
  572. inSplittingMode(false),
  573. interx(inx),
  574. garOffset(garsOffset),
  575. pb(false),
  576. smallIcons(smallImgs),
  577. removableUnits(_removableUnits),
  578. twoRows(_twoRows)
  579. {
  580. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  581. setArmy(s1, false);
  582. setArmy(s2, true);
  583. pos.x += x;
  584. pos.y += y;
  585. createSlots();
  586. }
  587. const CGarrisonSlot * CGarrisonInt::getSelection() const
  588. {
  589. return highlighted;
  590. }
  591. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  592. {
  593. if(slot != highlighted)
  594. {
  595. if(highlighted)
  596. highlighted->setHighlight(false);
  597. highlighted = slot;
  598. for(auto button : splitButtons)
  599. button->block(highlighted == nullptr || !slot->our());
  600. if(highlighted)
  601. highlighted->setHighlight(true);
  602. }
  603. }
  604. void CGarrisonInt::setSplittingMode(bool on)
  605. {
  606. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  607. if(inSplittingMode || on)
  608. {
  609. for(auto slot : availableSlots)
  610. {
  611. if(slot.get() != getSelection())
  612. slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  613. }
  614. inSplittingMode = on;
  615. }
  616. }
  617. bool CGarrisonInt::getSplittingMode()
  618. {
  619. return inSplittingMode;
  620. }
  621. SlotID CGarrisonInt::getEmptySlot(CGarrisonSlot::EGarrisonType type) const
  622. {
  623. assert(armedObjs[type]);
  624. return armedObjs[type] ? armedObjs[type]->getFreeSlot() : SlotID();
  625. }
  626. bool CGarrisonInt::hasEmptySlot(CGarrisonSlot::EGarrisonType type) const
  627. {
  628. return getEmptySlot(type) != SlotID();
  629. }
  630. void CGarrisonInt::setArmy(const CArmedInstance * army, bool bottomGarrison)
  631. {
  632. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  633. armedObjs[bottomGarrison] = army;
  634. }