CGarrisonInt.cpp 22 KB

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