CGarrisonInt.cpp 19 KB

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