CGarrisonInt.cpp 16 KB

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