CGarrisonInt.cpp 21 KB

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