2
0

CGarrisonInt.cpp 19 KB

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