CGarrisonInt.cpp 15 KB

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