CGarrisonInt.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 "RadialMenu.h"
  15. #include "../GameEngine.h"
  16. #include "../GameInstance.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../render/IImage.h"
  19. #include "../windows/CCreatureWindow.h"
  20. #include "../windows/CWindowWithArtifacts.h"
  21. #include "../windows/GUIClasses.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/texts/CGeneralTextHandler.h"
  25. #include "../../lib/texts/TextOperations.h"
  26. #include "../../lib/CCreatureHandler.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/GameLibrary.h"
  29. #include "../../lib/entities/artifact/ArtifactUtils.h"
  30. #include "../../lib/mapObjects/CGHeroInstance.h"
  31. #include "../../lib/networkPacks/ArtifactLocation.h"
  32. #include "../../lib/gameState/CGameState.h"
  33. #include "../../lib/gameState/UpgradeInfo.h"
  34. void CGarrisonSlot::setHighlight(bool on)
  35. {
  36. if (on)
  37. selectionImage->enable(); //show
  38. else
  39. selectionImage->disable(); //hide
  40. }
  41. void CGarrisonSlot::hover (bool on)
  42. {
  43. ////Hoverable::hover(on);
  44. if(on)
  45. {
  46. std::string temp;
  47. if(creature)
  48. {
  49. if(owner->getSelection())
  50. {
  51. if(owner->getSelection() == this)
  52. {
  53. temp = LIBRARY->generaltexth->tcommands[4]; //View %s
  54. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  55. }
  56. else if (owner->getSelection()->creature == creature)
  57. {
  58. temp = LIBRARY->generaltexth->tcommands[2]; //Combine %s armies
  59. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  60. }
  61. else if (owner->getSelection()->creature)
  62. {
  63. temp = LIBRARY->generaltexth->tcommands[7]; //Exchange %s with %s
  64. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  65. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  66. }
  67. else
  68. {
  69. logGlobal->warn("Warning - shouldn't be - highlighted void slot %d", owner->getSelection()->ID.getNum());
  70. logGlobal->warn("Highlighted set to nullptr");
  71. owner->selectSlot(nullptr);
  72. }
  73. }
  74. else
  75. {
  76. const bool isHeroOnMap = owner->upperArmy() // Hero is not a visitor and not a garrison defender
  77. && owner->upperArmy()->ID == Obj::HERO
  78. && (!owner->lowerArmy() || owner->lowerArmy()->ID == Obj::HERO) // one hero or we are in the Heroes exchange window
  79. && !(static_cast<const CGHeroInstance*>(owner->upperArmy()))->isGarrisoned();
  80. if(isHeroOnMap)
  81. {
  82. temp = LIBRARY->generaltexth->allTexts[481]; //Select %s
  83. }
  84. else if(upg == EGarrisonType::UPPER)
  85. {
  86. temp = LIBRARY->generaltexth->tcommands[12]; //Select %s (in garrison)
  87. }
  88. else // Hero is visiting some object (town, mine, etc)
  89. {
  90. temp = LIBRARY->generaltexth->tcommands[32]; //Select %s (visiting)
  91. }
  92. boost::algorithm::replace_first(temp,"%s",creature->getNameSingularTranslated());
  93. }
  94. }
  95. else
  96. {
  97. if(owner->getSelection())
  98. {
  99. const CArmedInstance *highl = owner->getSelection()->getObj();
  100. if( highl->needsLastStack() //we are moving stack from hero's
  101. && highl->stacksCount() == 1 //it's only stack
  102. && owner->getSelection()->upg != upg //we're moving it to the other garrison
  103. )
  104. {
  105. temp = LIBRARY->generaltexth->tcommands[5]; //Cannot move last army to garrison
  106. }
  107. else
  108. {
  109. temp = LIBRARY->generaltexth->tcommands[6]; //Move %s
  110. boost::algorithm::replace_first(temp,"%s",owner->getSelection()->creature->getNameSingularTranslated());
  111. }
  112. }
  113. else
  114. {
  115. temp = LIBRARY->generaltexth->tcommands[11]; //Empty
  116. }
  117. }
  118. ENGINE->statusbar()->write(temp);
  119. }
  120. else
  121. {
  122. ENGINE->statusbar()->clear();
  123. }
  124. }
  125. const CArmedInstance * CGarrisonSlot::getObj() const
  126. {
  127. return owner->army(upg);
  128. }
  129. bool CGarrisonSlot::our() const
  130. {
  131. return owner->isArmyOwned(upg);
  132. }
  133. bool CGarrisonSlot::ally() const
  134. {
  135. if(!getObj())
  136. return false;
  137. return PlayerRelations::ALLIES == GAME->interface()->cb->getPlayerRelations(GAME->interface()->playerID, getObj()->tempOwner);
  138. }
  139. std::function<void()> CGarrisonSlot::getDismiss() const
  140. {
  141. const bool canDismiss = getObj()->tempOwner == GAME->interface()->playerID
  142. && (getObj()->stacksCount() > 1 ||
  143. !getObj()->needsLastStack());
  144. return canDismiss ? [this]()
  145. {
  146. GAME->interface()->cb->dismissCreature(getObj(), ID);
  147. } : (std::function<void()>)nullptr;
  148. }
  149. /// The creature slot has been clicked twice, therefore the creature info should be shown
  150. /// @return Whether the view should be refreshed
  151. bool CGarrisonSlot::viewInfo()
  152. {
  153. UpgradeInfo pom(ID.getNum());
  154. GAME->interface()->cb->fillUpgradeInfo(getObj(), ID, pom);
  155. bool canUpgrade = getObj()->tempOwner == GAME->interface()->playerID && pom.canUpgrade(); //upgrade is possible
  156. std::function<void(CreatureID)> upgr = nullptr;
  157. auto dism = getDismiss();
  158. if(canUpgrade) upgr = [this] (CreatureID newID) { GAME->interface()->cb->upgradeCreature(getObj(), ID, newID); };
  159. owner->selectSlot(nullptr);
  160. owner->setSplittingMode(false);
  161. for(auto & elem : owner->splitButtons)
  162. elem->block(true);
  163. redraw();
  164. ENGINE->windows().createAndPushWindow<CStackWindow>(myStack, dism, pom, upgr);
  165. return true;
  166. }
  167. /// The selection is empty, therefore the creature should be moved
  168. /// @return Whether the view should be refreshed
  169. bool CGarrisonSlot::highlightOrDropArtifact()
  170. {
  171. bool artSelected = false;
  172. if (auto chw = ENGINE->windows().topWindow<CWindowWithArtifacts>()) //dirty solution
  173. {
  174. const auto pickedArtInst = chw->getPickedArtifact();
  175. if(pickedArtInst)
  176. {
  177. const auto * srcHero = chw->getHeroPickedArtifact();
  178. artSelected = true;
  179. if (myStack) // try dropping the artifact only if the slot isn't empty
  180. {
  181. ArtifactLocation src(srcHero->id, ArtifactPosition::TRANSITION_POS);
  182. ArtifactLocation dst(getObj()->id, ArtifactPosition::CREATURE_SLOT);
  183. dst.creature = getSlot();
  184. if(pickedArtInst->canBePutAt(myStack, ArtifactPosition::CREATURE_SLOT, true))
  185. { //equip clicked stack
  186. if(auto dstArt = myStack->getArt(ArtifactPosition::CREATURE_SLOT))
  187. {
  188. //creature can wear only one active artifact
  189. //if we are placing a new one, the old one will be returned to the hero's backpack
  190. GAME->interface()->cb->swapArtifacts(dst, ArtifactLocation(srcHero->id,
  191. ArtifactUtils::getArtBackpackPosition(srcHero, dstArt->getTypeId())));
  192. }
  193. GAME->interface()->cb->swapArtifacts(src, dst);
  194. }
  195. }
  196. }
  197. }
  198. if (!artSelected && creature)
  199. {
  200. owner->selectSlot(this);
  201. if(creature)
  202. {
  203. for(auto & elem : owner->splitButtons)
  204. elem->block(!our());
  205. }
  206. }
  207. redraw();
  208. return true;
  209. }
  210. /// The creature is only being partially moved
  211. /// @return Whether the view should be refreshed
  212. bool CGarrisonSlot::split()
  213. {
  214. const CGarrisonSlot * selection = owner->getSelection();
  215. owner->setSplittingMode(false);
  216. int minLeft=0;
  217. int minRight=0;
  218. if(upg != selection->upg) // not splitting within same army
  219. {
  220. if(selection->getObj()->stacksCount() == 1 // we're splitting away the last stack
  221. && selection->getObj()->needsLastStack() )
  222. {
  223. minLeft = 1;
  224. }
  225. // destination army can't be emptied, unless we're rebalancing two stacks of same creature
  226. if(getObj()->stacksCount() == 1
  227. && selection->creature == creature
  228. && getObj()->needsLastStack() )
  229. {
  230. minRight = 1;
  231. }
  232. }
  233. int countLeft = selection->myStack ? selection->myStack->getCount() : 0;
  234. int countRight = myStack ? myStack->getCount() : 0;
  235. auto splitFunctor = [this, selection](int amountLeft, int amountRight)
  236. {
  237. owner->splitStacks(selection, owner->army(upg), ID, amountRight);
  238. };
  239. ENGINE->windows().createAndPushWindow<CSplitWindow>(selection->creature, splitFunctor, minLeft, minRight, countLeft, countRight);
  240. return true;
  241. }
  242. bool CGarrisonSlot::mustForceReselection() const
  243. {
  244. const CGarrisonSlot * selection = owner->getSelection();
  245. bool withAlly = selection->our() ^ our();
  246. // not our turn - actions are blocked
  247. if (!GAME->interface()->makingTurn)
  248. return true;
  249. // Attempt to take creatures from ally (select theirs first)
  250. if (!selection->our())
  251. return true;
  252. if (!creature || !selection->creature)
  253. return false;
  254. // Attempt to swap creatures with ally (select ours first)
  255. if (selection->creature != creature && withAlly)
  256. return true;
  257. if (!owner->removableUnits)
  258. {
  259. if (selection->upg == EGarrisonType::UPPER)
  260. return true;
  261. else
  262. return creature || upg == EGarrisonType::UPPER;
  263. }
  264. return false;
  265. }
  266. void CGarrisonSlot::showPopupWindow(const Point & cursorPosition)
  267. {
  268. if(creature)
  269. {
  270. ENGINE->windows().createAndPushWindow<CStackWindow>(myStack, true);
  271. }
  272. }
  273. void CGarrisonSlot::clickPressed(const Point & cursorPosition)
  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. owner->redraw();
  293. refr = true;
  294. }
  295. else
  296. {
  297. const CArmedInstance * selectedObj = owner->army(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() || ENGINE->isKeyboardShiftDown()) // split window
  306. && (!creature || creature == selection->creature))
  307. {
  308. refr = split();
  309. }
  310. else if(!creature && lastHeroStackSelected) // split all except last creature
  311. GAME->interface()->cb->splitStack(selectedObj, owner->army(upg), selection->ID, ID, selection->myStack->getCount() - 1);
  312. else if(creature != selection->creature) // swap
  313. GAME->interface()->cb->swapCreatures(owner->army(upg), selectedObj, ID, selection->ID);
  314. else if(lastHeroStackSelected) // merge last stack to other hero stack
  315. refr = split();
  316. else // merge
  317. GAME->interface()->cb->mergeStacks(selectedObj, owner->army(upg), selection->ID, ID);
  318. }
  319. if(refr)
  320. {
  321. // Refresh Statusbar
  322. hover(false);
  323. hover(true);
  324. }
  325. }
  326. void CGarrisonSlot::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  327. {
  328. if(!on)
  329. return;
  330. if(!myStack)
  331. return;
  332. if (!settings["input"]["radialWheelGarrisonSwipe"].Bool())
  333. return;
  334. if(ENGINE->windows().topWindow<CIntObject>()->isPopupWindow())
  335. return;
  336. const auto * otherArmy = upg == EGarrisonType::UPPER ? owner->lowerArmy() : owner->upperArmy();
  337. bool stackExists = myStack != nullptr;
  338. bool hasSameUnit = stackExists && !owner->army(upg)->getCreatureSlots(myStack->getCreature(), ID).empty();
  339. bool hasOwnEmptySlots = stackExists && owner->army(upg)->getFreeSlot() != SlotID();
  340. bool exchangeMode = stackExists && owner->upperArmy() && owner->lowerArmy();
  341. bool hasOtherEmptySlots = exchangeMode && otherArmy->getFreeSlot() != SlotID();
  342. bool hasAnyEmptySlots = hasOtherEmptySlots || hasOwnEmptySlots;
  343. std::vector<RadialMenuConfig> menuElements = {
  344. { RadialMenuConfig::ITEM_NW, hasSameUnit, "stackMerge", "vcmi.radialWheel.mergeSameUnit", [this](){owner->bulkMergeStacks(this);} },
  345. { RadialMenuConfig::ITEM_NE, hasOwnEmptySlots, "stackFillOne", "vcmi.radialWheel.fillSingleUnit", [this](){owner->bulkSplitStack(this);} },
  346. { RadialMenuConfig::ITEM_WW, hasOwnEmptySlots, "stackSplitOne", "vcmi.radialWheel.splitSingleUnit", [this](){splitIntoParts(this->getGarrison(), 1); } },
  347. { RadialMenuConfig::ITEM_EE, hasOwnEmptySlots, "stackSplitEqual", "vcmi.radialWheel.splitUnitEqually", [this](){owner->bulkSplitAndRebalanceStack(this);} },
  348. { RadialMenuConfig::ITEM_SW, hasOtherEmptySlots, "heroMove", "vcmi.radialWheel.moveUnit", [this](){owner->moveStackToAnotherArmy(this);} },
  349. { RadialMenuConfig::ITEM_SE, hasAnyEmptySlots, "heroSwap", "vcmi.radialWheel.splitUnit", [this](){ owner->selectSlot(this); owner->splitClick();} },
  350. };
  351. if (hasAnyEmptySlots || hasSameUnit)
  352. ENGINE->windows().createAndPushWindow<RadialMenu>(pos.center(), menuElements);
  353. }
  354. void CGarrisonSlot::update()
  355. {
  356. if(getObj() != nullptr)
  357. {
  358. addUsedEvents(LCLICK | SHOW_POPUP | GESTURE | HOVER);
  359. myStack = getObj()->getStackPtr(ID);
  360. creature = myStack ? myStack->getCreature() : nullptr;
  361. }
  362. else
  363. {
  364. removeUsedEvents(LCLICK | SHOW_POPUP | GESTURE | HOVER);
  365. myStack = nullptr;
  366. creature = nullptr;
  367. }
  368. if(creature)
  369. {
  370. creatureImage->enable();
  371. creatureImage->setFrame(creature->getIconIndex());
  372. stackCount->enable();
  373. stackCount->setText(TextOperations::formatMetric(myStack->getCount(), 4));
  374. }
  375. else
  376. {
  377. creatureImage->disable();
  378. stackCount->disable();
  379. }
  380. }
  381. CGarrisonSlot::CGarrisonSlot(CGarrisonInt * Owner, int x, int y, SlotID IID, EGarrisonType Upg, const CStackInstance * creature_)
  382. : ID(IID),
  383. owner(Owner),
  384. myStack(creature_),
  385. creature(creature_ ? creature_->getCreature() : nullptr),
  386. upg(Upg)
  387. {
  388. OBJECT_CONSTRUCTION;
  389. pos.x += x;
  390. pos.y += y;
  391. AnimationPath imgName = AnimationPath::builtin(owner->smallIcons ? "cprsmall" : "TWCRPORT");
  392. creatureImage = std::make_shared<CAnimImage>(imgName, 0);
  393. creatureImage->disable();
  394. selectionImage = std::make_shared<CAnimImage>(imgName, 1);
  395. selectionImage->disable();
  396. selectionImage->center(creatureImage->pos.center());
  397. if(Owner->smallIcons)
  398. {
  399. pos.w = 32;
  400. pos.h = 32;
  401. }
  402. else
  403. {
  404. pos.w = 58;
  405. pos.h = 64;
  406. }
  407. int labelPosW = pos.w;
  408. int labelPosH = pos.h;
  409. if(Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS) //labels under icon
  410. {
  411. labelPosW = pos.w / 2 + 1;
  412. labelPosH += 7;
  413. }
  414. ETextAlignment labelAlignment = Owner->layout == CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS
  415. ? ETextAlignment::CENTER
  416. : ETextAlignment::BOTTOMRIGHT;
  417. stackCount = std::make_shared<CLabel>(labelPosW, labelPosH, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, labelAlignment, Colors::WHITE);
  418. update();
  419. }
  420. void CGarrisonSlot::splitIntoParts(EGarrisonType type, int amount)
  421. {
  422. auto empty = owner->getEmptySlot(type);
  423. if(empty == SlotID())
  424. return;
  425. owner->splitStacks(this, owner->army(type), empty, amount);
  426. }
  427. bool CGarrisonSlot::handleSplittingShortcuts()
  428. {
  429. const bool isAlt = ENGINE->isKeyboardAltDown();
  430. const bool isLShift = ENGINE->isKeyboardShiftDown();
  431. const bool isLCtrl = ENGINE->isKeyboardCmdDown();
  432. if(!isAlt && !isLShift && !isLCtrl)
  433. return false; // This is only case when return false
  434. auto selected = owner->getSelection();
  435. if(!selected)
  436. return true; // Some Shortcusts are pressed but there are no appropriate actions
  437. auto units = selected->myStack->getCount();
  438. if(units < 1)
  439. return true;
  440. if (isLShift && isLCtrl && isAlt)
  441. {
  442. owner->bulkMoveArmy(selected);
  443. }
  444. else if(isLCtrl && isAlt)
  445. {
  446. owner->moveStackToAnotherArmy(selected);
  447. }
  448. else if(isLShift && isAlt)
  449. {
  450. auto dismiss = getDismiss();
  451. if(dismiss)
  452. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[12], dismiss, nullptr);
  453. }
  454. else if(isAlt)
  455. {
  456. owner->bulkMergeStacks(selected);
  457. }
  458. else
  459. {
  460. if(units <= 1)
  461. return true;
  462. if(isLCtrl && isLShift)
  463. owner->bulkSplitStack(selected);
  464. else if(isLShift)
  465. owner->bulkSplitAndRebalanceStack(selected);
  466. else
  467. splitIntoParts(selected->upg, 1); // LCtrl
  468. }
  469. return true;
  470. }
  471. void CGarrisonInt::addSplitBtn(std::shared_ptr<CButton> button)
  472. {
  473. addChild(button.get());
  474. splitButtons.push_back(button);
  475. button->block(getSelection() == nullptr);
  476. }
  477. void CGarrisonInt::createSlots()
  478. {
  479. availableSlots.clear();
  480. int distance = interx + (smallIcons ? 32 : 58);
  481. for(auto i : { EGarrisonType::UPPER, EGarrisonType::LOWER })
  482. {
  483. Point offset = garOffset * static_cast<int>(i);
  484. for(int j = 0; j < 7; j++)
  485. {
  486. Point position(offset.x + (j*distance), offset.y);
  487. if(layout == ESlotsLayout::TWO_ROWS && j >= 4)
  488. {
  489. position += Point(-126, 37);
  490. }
  491. else if(layout == ESlotsLayout::REVERSED_TWO_ROWS)
  492. {
  493. if(j >= 3)
  494. {
  495. position += Point(-90, 49);
  496. }
  497. else
  498. {
  499. position += Point(36, 0);
  500. }
  501. }
  502. SlotID slot(j);
  503. availableSlots.push_back(std::make_shared<CGarrisonSlot>(this, position.x, position.y, slot, i, army(i) ? army(i)->getStackPtr(slot) : nullptr));
  504. }
  505. }
  506. }
  507. void CGarrisonInt::recreateSlots()
  508. {
  509. selectSlot(nullptr);
  510. setSplittingMode(false);
  511. for(auto & elem : splitButtons)
  512. elem->block(true);
  513. for(auto slot : availableSlots)
  514. slot->update();
  515. }
  516. void CGarrisonInt::splitClick()
  517. {
  518. if(!getSelection())
  519. return;
  520. setSplittingMode(!getSplittingMode());
  521. redraw();
  522. }
  523. void CGarrisonInt::splitStacks(const CGarrisonSlot * from, const CArmedInstance * armyDest, SlotID slotDest, int amount )
  524. {
  525. GAME->interface()->cb->splitStack(armedObjs[from->upg], armyDest, from->ID, slotDest, amount);
  526. }
  527. bool CGarrisonInt::checkSelected(const CGarrisonSlot * selected, TQuantity min) const
  528. {
  529. return selected && selected->myStack && selected->myStack->getCount() > min && selected->creature;
  530. }
  531. void CGarrisonInt::moveStackToAnotherArmy(const CGarrisonSlot * selected)
  532. {
  533. if(!checkSelected(selected))
  534. return;
  535. const auto srcArmyType = selected->upg;
  536. const auto destArmyType = srcArmyType == EGarrisonType::UPPER
  537. ? EGarrisonType::LOWER
  538. : EGarrisonType::UPPER;
  539. auto srcArmy = armedObjs[srcArmyType];
  540. auto destArmy = armedObjs[destArmyType];
  541. if(!destArmy)
  542. return;
  543. auto destSlot = destArmy->getSlotFor(selected->creature);
  544. if(destSlot == SlotID())
  545. return;
  546. const auto srcSlot = selected->ID;
  547. const bool isDestSlotEmpty = !destArmy->getStackCount(destSlot);
  548. if(isDestSlotEmpty && !destArmy->getStackCount(srcSlot))
  549. destSlot = srcSlot; // Same place is more preferable
  550. const bool isLastStack = srcArmy->stacksCount() == 1 && srcArmy->needsLastStack();
  551. auto srcAmount = selected->myStack->getCount() - (isLastStack ? 1 : 0);
  552. if(!srcAmount)
  553. return;
  554. if(!isDestSlotEmpty || isLastStack)
  555. {
  556. srcAmount += destArmy->getStackCount(destSlot); // Due to 'split' implementation in the 'CGameHandler::arrangeStacks'
  557. GAME->interface()->cb->splitStack(srcArmy, destArmy, srcSlot, destSlot, srcAmount);
  558. }
  559. else
  560. {
  561. GAME->interface()->cb->swapCreatures(srcArmy, destArmy, srcSlot, destSlot);
  562. }
  563. }
  564. void CGarrisonInt::bulkMoveArmy(const CGarrisonSlot * selected)
  565. {
  566. if(!checkSelected(selected))
  567. return;
  568. const auto srcArmyType = selected->upg;
  569. const auto destArmyType = (srcArmyType == EGarrisonType::UPPER)
  570. ? EGarrisonType::LOWER
  571. : EGarrisonType::UPPER;
  572. auto srcArmy = armedObjs[srcArmyType];
  573. auto destArmy = armedObjs[destArmyType];
  574. if(!destArmy)
  575. return;
  576. const auto srcSlot = selected->ID;
  577. GAME->interface()->cb->bulkMoveArmy(srcArmy->id, destArmy->id, srcSlot);
  578. }
  579. void CGarrisonInt::bulkMergeStacks(const CGarrisonSlot * selected)
  580. {
  581. if(!checkSelected(selected))
  582. return;
  583. const auto type = selected->upg;
  584. if(!armedObjs[type]->hasCreatureSlots(selected->creature, selected->ID))
  585. return;
  586. GAME->interface()->cb->bulkMergeStacks(armedObjs[type]->id, selected->ID);
  587. }
  588. void CGarrisonInt::bulkSplitStack(const CGarrisonSlot * selected)
  589. {
  590. if(!checkSelected(selected, 1)) // check if > 1
  591. return;
  592. const auto type = selected->upg;
  593. if(!hasEmptySlot(type))
  594. return;
  595. GAME->interface()->cb->bulkSplitStack(armedObjs[type]->id, selected->ID);
  596. }
  597. void CGarrisonInt::bulkSplitAndRebalanceStack(const CGarrisonSlot * selected)
  598. {
  599. if(!checkSelected(selected, 1))
  600. return;
  601. const auto type = selected->upg;
  602. // Do not disturb the server if the creature is already balanced
  603. if(!hasEmptySlot(type) && armedObjs[type]->isCreatureBalanced(selected->creature))
  604. return;
  605. GAME->interface()->cb->bulkSplitAndRebalanceStack(armedObjs[type]->id, selected->ID);
  606. }
  607. CGarrisonInt::CGarrisonInt(const Point & position, int inx, const Point & garsOffset, const CArmedInstance * s1, const CArmedInstance * s2, bool _removableUnits, bool smallImgs, ESlotsLayout _layout)
  608. : highlighted(nullptr)
  609. , inSplittingMode(false)
  610. , interx(inx)
  611. , garOffset(garsOffset)
  612. , smallIcons(smallImgs)
  613. , removableUnits(_removableUnits)
  614. , layout(_layout)
  615. {
  616. OBJECT_CONSTRUCTION;
  617. setArmy(s1, EGarrisonType::UPPER);
  618. setArmy(s2, EGarrisonType::LOWER);
  619. pos += position;
  620. createSlots();
  621. }
  622. const CGarrisonSlot * CGarrisonInt::getSelection() const
  623. {
  624. return highlighted;
  625. }
  626. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  627. {
  628. if(slot != highlighted)
  629. {
  630. if(highlighted)
  631. highlighted->setHighlight(false);
  632. highlighted = slot;
  633. for(auto button : splitButtons)
  634. button->block(highlighted == nullptr || !slot->our());
  635. if(highlighted)
  636. highlighted->setHighlight(true);
  637. }
  638. }
  639. void CGarrisonInt::setSplittingMode(bool on)
  640. {
  641. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  642. if(inSplittingMode || on)
  643. {
  644. for(auto slot : availableSlots)
  645. {
  646. if(slot.get() != getSelection())
  647. slot->setHighlight( ( on && (slot->our() || slot->ally()) && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  648. }
  649. inSplittingMode = on;
  650. }
  651. }
  652. bool CGarrisonInt::getSplittingMode()
  653. {
  654. return inSplittingMode;
  655. }
  656. SlotID CGarrisonInt::getEmptySlot(EGarrisonType type) const
  657. {
  658. assert(army(type));
  659. return army(type) ? army(type)->getFreeSlot() : SlotID();
  660. }
  661. bool CGarrisonInt::hasEmptySlot(EGarrisonType type) const
  662. {
  663. return getEmptySlot(type) != SlotID();
  664. }
  665. const CArmedInstance * CGarrisonInt::upperArmy() const
  666. {
  667. return army(EGarrisonType::UPPER);
  668. }
  669. const CArmedInstance * CGarrisonInt::lowerArmy() const
  670. {
  671. return army(EGarrisonType::LOWER);
  672. }
  673. const CArmedInstance * CGarrisonInt::army(EGarrisonType which) const
  674. {
  675. if(armedObjs.count(which))
  676. return armedObjs.at(which);
  677. return nullptr;
  678. }
  679. bool CGarrisonInt::isArmyOwned(EGarrisonType which) const
  680. {
  681. const auto * object = army(which);
  682. if (!object)
  683. return false;
  684. if (object->tempOwner == GAME->interface()->playerID)
  685. return true;
  686. if (object->tempOwner == PlayerColor::UNFLAGGABLE)
  687. return true;
  688. return false;
  689. }
  690. void CGarrisonInt::setArmy(const CArmedInstance * army, EGarrisonType type)
  691. {
  692. armedObjs[type] = army;
  693. }