CGarrisonInt.cpp 21 KB

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