CGarrisonInt.cpp 19 KB

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