CGarrisonInt.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. stackCount = std::make_shared<CLabel>(pos.w, pos.h, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, ETextAlignment::BOTTOMRIGHT, Colors::WHITE);
  374. update();
  375. }
  376. void CGarrisonSlot::splitIntoParts(CGarrisonSlot::EGarrisonType type, int amount)
  377. {
  378. auto empty = owner->getEmptySlot(type);
  379. if(empty == SlotID())
  380. return;
  381. owner->pb = type;
  382. owner->p2 = empty;
  383. owner->splitStacks(1, amount);
  384. }
  385. bool CGarrisonSlot::handleSplittingShortcuts()
  386. {
  387. const bool isAlt = GH.isKeyboardAltDown();
  388. const bool isLShift = GH.isKeyboardShiftDown();
  389. const bool isLCtrl = GH.isKeyboardCtrlDown();
  390. if(!isAlt && !isLShift && !isLCtrl)
  391. return false; // This is only case when return false
  392. auto selected = owner->getSelection();
  393. if(!selected)
  394. return true; // Some Shortcusts are pressed but there are no appropriate actions
  395. auto units = selected->myStack->count;
  396. if(units < 1)
  397. return true;
  398. if (isLShift && isLCtrl && isAlt)
  399. {
  400. owner->bulkMoveArmy(selected);
  401. }
  402. else if(isLCtrl && isAlt)
  403. {
  404. owner->moveStackToAnotherArmy(selected);
  405. }
  406. else if(isLShift && isAlt)
  407. {
  408. auto dismiss = getDismiss();
  409. if(dismiss)
  410. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], dismiss, nullptr);
  411. }
  412. else if(isAlt)
  413. {
  414. owner->bulkMergeStacks(selected);
  415. }
  416. else
  417. {
  418. if(units <= 1)
  419. return true;
  420. if(isLCtrl && isLShift)
  421. owner->bulkSplitStack(selected);
  422. else if(isLShift)
  423. owner->bulkSmartSplitStack(selected);
  424. else
  425. splitIntoParts(selected->upg, 1); // LCtrl
  426. }
  427. return true;
  428. }
  429. void CGarrisonInt::addSplitBtn(std::shared_ptr<CButton> button)
  430. {
  431. addChild(button.get());
  432. button->recActions &= ~DISPOSE;
  433. splitButtons.push_back(button);
  434. button->block(getSelection() == nullptr);
  435. }
  436. void CGarrisonInt::createSlots()
  437. {
  438. int distance = interx + (smallIcons ? 32 : 58);
  439. for(int i=0; i<2; i++)
  440. {
  441. std::vector<std::shared_ptr<CGarrisonSlot>> garrisonSlots;
  442. garrisonSlots.resize(7);
  443. if(armedObjs[i])
  444. {
  445. for(auto & elem : armedObjs[i]->Slots())
  446. {
  447. 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);
  448. }
  449. }
  450. for(int j=0; j<7; j++)
  451. {
  452. if(!garrisonSlots[j])
  453. garrisonSlots[j] = std::make_shared<CGarrisonSlot>(this, i*garOffset.x + (j*distance), i*garOffset.y, SlotID(j), static_cast<CGarrisonSlot::EGarrisonType>(i), nullptr);
  454. if(twoRows && j>=4)
  455. {
  456. garrisonSlots[j]->moveBy(Point(-126, 37));
  457. }
  458. }
  459. vstd::concatenate(availableSlots, garrisonSlots);
  460. }
  461. }
  462. void CGarrisonInt::recreateSlots()
  463. {
  464. selectSlot(nullptr);
  465. setSplittingMode(false);
  466. for(auto & elem : splitButtons)
  467. elem->block(true);
  468. for(auto slot : availableSlots)
  469. slot->update();
  470. }
  471. void CGarrisonInt::splitClick()
  472. {
  473. if(!getSelection())
  474. return;
  475. setSplittingMode(!getSplittingMode());
  476. redraw();
  477. }
  478. void CGarrisonInt::splitStacks(int, int amountRight)
  479. {
  480. LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
  481. }
  482. bool CGarrisonInt::checkSelected(const CGarrisonSlot * selected, TQuantity min) const
  483. {
  484. return selected && selected->myStack && selected->myStack->count > min && selected->creature;
  485. }
  486. void CGarrisonInt::moveStackToAnotherArmy(const CGarrisonSlot * selected)
  487. {
  488. if(!checkSelected(selected))
  489. return;
  490. const auto srcArmyType = selected->upg;
  491. const auto destArmyType = srcArmyType == CGarrisonSlot::UP
  492. ? CGarrisonSlot::DOWN
  493. : CGarrisonSlot::UP;
  494. auto srcArmy = armedObjs[srcArmyType];
  495. auto destArmy = armedObjs[destArmyType];
  496. if(!destArmy)
  497. return;
  498. auto destSlot = destArmy->getSlotFor(selected->creature);
  499. if(destSlot == SlotID())
  500. return;
  501. const auto srcSlot = selected->ID;
  502. const bool isDestSlotEmpty = !destArmy->getStackCount(destSlot);
  503. if(isDestSlotEmpty && !destArmy->getStackCount(srcSlot))
  504. destSlot = srcSlot; // Same place is more preferable
  505. const bool isLastStack = srcArmy->stacksCount() == 1 && srcArmy->needsLastStack();
  506. auto srcAmount = selected->myStack->count - (isLastStack ? 1 : 0);
  507. if(!srcAmount)
  508. return;
  509. if(!isDestSlotEmpty || isLastStack)
  510. {
  511. srcAmount += destArmy->getStackCount(destSlot); // Due to 'split' implementation in the 'CGameHandler::arrangeStacks'
  512. LOCPLINT->cb->splitStack(srcArmy, destArmy, srcSlot, destSlot, srcAmount);
  513. }
  514. else
  515. {
  516. LOCPLINT->cb->swapCreatures(srcArmy, destArmy, srcSlot, destSlot);
  517. }
  518. }
  519. void CGarrisonInt::bulkMoveArmy(const CGarrisonSlot * selected)
  520. {
  521. if(!checkSelected(selected))
  522. return;
  523. const auto srcArmyType = selected->upg;
  524. const auto destArmyType = (srcArmyType == CGarrisonSlot::UP)
  525. ? CGarrisonSlot::DOWN
  526. : CGarrisonSlot::UP;
  527. auto srcArmy = armedObjs[srcArmyType];
  528. auto destArmy = armedObjs[destArmyType];
  529. if(!destArmy)
  530. return;
  531. const auto srcSlot = selected->ID;
  532. LOCPLINT->cb->bulkMoveArmy(srcArmy->id, destArmy->id, srcSlot);
  533. }
  534. void CGarrisonInt::bulkMergeStacks(const CGarrisonSlot * selected)
  535. {
  536. if(!checkSelected(selected))
  537. return;
  538. const auto type = selected->upg;
  539. if(!armedObjs[type]->hasCreatureSlots(selected->creature, selected->ID))
  540. return;
  541. LOCPLINT->cb->bulkMergeStacks(armedObjs[type]->id, selected->ID);
  542. }
  543. void CGarrisonInt::bulkSplitStack(const CGarrisonSlot * selected)
  544. {
  545. if(!checkSelected(selected, 1)) // check if > 1
  546. return;
  547. const auto type = selected->upg;
  548. if(!hasEmptySlot(type))
  549. return;
  550. LOCPLINT->cb->bulkSplitStack(armedObjs[type]->id, selected->ID);
  551. }
  552. void CGarrisonInt::bulkSmartSplitStack(const CGarrisonSlot * selected)
  553. {
  554. if(!checkSelected(selected, 1))
  555. return;
  556. const auto type = selected->upg;
  557. // Do not disturb the server if the creature is already balanced
  558. if(!hasEmptySlot(type) && armedObjs[type]->isCreatureBalanced(selected->creature))
  559. return;
  560. LOCPLINT->cb->bulkSmartSplitStack(armedObjs[type]->id, selected->ID);
  561. }
  562. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point & garsOffset,
  563. const CArmedInstance * s1, const CArmedInstance * s2,
  564. bool _removableUnits, bool smallImgs, bool _twoRows)
  565. : highlighted(nullptr),
  566. inSplittingMode(false),
  567. interx(inx),
  568. garOffset(garsOffset),
  569. pb(false),
  570. smallIcons(smallImgs),
  571. removableUnits(_removableUnits),
  572. twoRows(_twoRows)
  573. {
  574. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  575. setArmy(s1, false);
  576. setArmy(s2, true);
  577. pos.x += x;
  578. pos.y += y;
  579. createSlots();
  580. }
  581. const CGarrisonSlot * CGarrisonInt::getSelection() const
  582. {
  583. return highlighted;
  584. }
  585. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  586. {
  587. if(slot != highlighted)
  588. {
  589. if(highlighted)
  590. highlighted->setHighlight(false);
  591. highlighted = slot;
  592. for(auto button : splitButtons)
  593. button->block(highlighted == nullptr || !slot->our());
  594. if(highlighted)
  595. highlighted->setHighlight(true);
  596. }
  597. }
  598. void CGarrisonInt::setSplittingMode(bool on)
  599. {
  600. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  601. if(inSplittingMode || on)
  602. {
  603. for(auto slot : availableSlots)
  604. {
  605. if(slot.get() != getSelection())
  606. slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  607. }
  608. inSplittingMode = on;
  609. }
  610. }
  611. bool CGarrisonInt::getSplittingMode()
  612. {
  613. return inSplittingMode;
  614. }
  615. SlotID CGarrisonInt::getEmptySlot(CGarrisonSlot::EGarrisonType type) const
  616. {
  617. assert(armedObjs[type]);
  618. return armedObjs[type] ? armedObjs[type]->getFreeSlot() : SlotID();
  619. }
  620. bool CGarrisonInt::hasEmptySlot(CGarrisonSlot::EGarrisonType type) const
  621. {
  622. return getEmptySlot(type) != SlotID();
  623. }
  624. void CGarrisonInt::setArmy(const CArmedInstance * army, bool bottomGarrison)
  625. {
  626. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  627. armedObjs[bottomGarrison] = army;
  628. }