2
0

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