2
0

CGarrisonInt.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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)
  67. {
  68. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  69. }
  70. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == Obj::TOWN)
  71. {
  72. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  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 (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  114. }
  115. bool CGarrisonSlot::our() const
  116. {
  117. return upg?(owner->owned[1]):(owner->owned[0]);
  118. }
  119. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  120. {
  121. if(down && creature)
  122. {
  123. GH.pushInt(new CStackWindow(myStack, true));
  124. }
  125. }
  126. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  127. {
  128. if(down)
  129. {
  130. bool refr = false;
  131. if(owner->getSelection())
  132. {
  133. if(owner->getSelection() == this) //view info
  134. {
  135. UpgradeInfo pom;
  136. LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
  137. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  138. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  139. std::function<void(CreatureID)> upgr = nullptr;
  140. std::function<void()> dism = nullptr;
  141. if(canUpgrade) upgr = [=] (CreatureID newID) { LOCPLINT->cb->upgradeCreature(getObj(), ID, newID); };
  142. if(canDismiss) dism = [=] { LOCPLINT->cb->dismissCreature(getObj(), ID); };
  143. owner->selectSlot(nullptr);
  144. owner->setSplittingMode(false);
  145. for(auto & elem : owner->splitButtons)
  146. elem->block(true);
  147. redraw();
  148. refr = true;
  149. GH.pushInt(new CStackWindow(myStack, dism, pom, upgr));
  150. }
  151. else
  152. {
  153. // Only allow certain moves if troops aren't removable or not ours.
  154. if ( ( owner->getSelection()->our()//our creature is selected
  155. || owner->getSelection()->creature == creature )//or we are rebalancing army
  156. && ( owner->removableUnits
  157. || (upg == 0 && ( owner->getSelection()->upg == 1 && !creature ) )
  158. || (upg == 1 && owner->getSelection()->upg == 1 ) ) )
  159. {
  160. //we want to split
  161. if((owner->getSplittingMode() || LOCPLINT->shiftPressed())
  162. && (!creature
  163. || (creature == owner->getSelection()->creature)))
  164. {
  165. owner->p2 = ID; //store the second stack pos
  166. owner->pb = upg;//store the second stack owner (up or down army)
  167. owner->setSplittingMode(false);
  168. int minLeft=0, minRight=0;
  169. if(upg != owner->getSelection()->upg) //not splitting within same army
  170. {
  171. if(owner->getSelection()->getObj()->stacksCount() == 1 //we're splitting away the last stack
  172. && owner->getSelection()->getObj()->needsLastStack() )
  173. {
  174. minLeft = 1;
  175. }
  176. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  177. && owner->getSelection()->creature == creature
  178. && getObj()->needsLastStack() )
  179. {
  180. minRight = 1;
  181. }
  182. }
  183. int countLeft = owner->getSelection()->myStack ? owner->getSelection()->myStack->count : 0;
  184. int countRight = myStack ? myStack->count : 0;
  185. GH.pushInt(new CSplitWindow(owner->getSelection()->creature, boost::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
  186. minLeft, minRight, countLeft, countRight));
  187. refr = true;
  188. }
  189. else if(creature != owner->getSelection()->creature) //swap
  190. {
  191. LOCPLINT->cb->swapCreatures(
  192. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  193. (!owner->getSelection()->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  194. ID,owner->getSelection()->ID);
  195. }
  196. else //merge
  197. {
  198. LOCPLINT->cb->mergeStacks(
  199. (!owner->getSelection()->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  200. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  201. owner->getSelection()->ID,ID);
  202. }
  203. }
  204. else // Highlight
  205. {
  206. if(creature)
  207. owner->selectSlot(this);
  208. redraw();
  209. refr = true;
  210. }
  211. }
  212. }
  213. else //highlight or drop artifact
  214. {
  215. bool artSelected = false;
  216. if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
  217. {
  218. const CArtifactsOfHero::SCommonPart *commonInfo = chw->artSets.front()->commonInfo;
  219. if (const CArtifactInstance *art = commonInfo->src.art)
  220. {
  221. const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
  222. artSelected = true;
  223. ArtifactLocation src(srcHero, commonInfo->src.slotID);
  224. ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
  225. if (art->canBePutAt(dst, true))
  226. { //equip clicked stack
  227. if(dst.getArt())
  228. {
  229. //creature can wear only one active artifact
  230. //if we are placing a new one, the old one will be returned to the hero's backpack
  231. LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
  232. }
  233. LOCPLINT->cb->swapArtifacts(src, dst);
  234. }
  235. }
  236. }
  237. if (!artSelected && creature)
  238. {
  239. owner->selectSlot(this);
  240. if(creature)
  241. {
  242. for(auto & elem : owner->splitButtons)
  243. elem->block(false);
  244. }
  245. }
  246. redraw();
  247. refr = true;
  248. }
  249. if(refr) {hover(false); hover(true); } //to refresh statusbar
  250. }
  251. }
  252. void CGarrisonSlot::update()
  253. {
  254. if (getObj() != nullptr)
  255. {
  256. addUsedEvents(LCLICK | RCLICK | HOVER);
  257. myStack = getObj()->getStackPtr(ID);
  258. creature = myStack ? myStack->type : nullptr;
  259. }
  260. else
  261. {
  262. removeUsedEvents(LCLICK | RCLICK | HOVER);
  263. myStack = nullptr;
  264. creature = nullptr;
  265. }
  266. if (creature)
  267. {
  268. creatureImage->enable();
  269. creatureImage->setFrame(creature->iconIndex);
  270. stackCount->enable();
  271. stackCount->setText(boost::lexical_cast<std::string>(myStack->count));
  272. }
  273. else
  274. {
  275. creatureImage->disable();
  276. stackCount->disable();
  277. }
  278. }
  279. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, SlotID IID, int Upg, const CStackInstance * Creature):
  280. ID(IID),
  281. owner(Owner),
  282. myStack(Creature),
  283. creature(Creature ? Creature->type : nullptr),
  284. upg(Upg)
  285. {
  286. OBJ_CONSTRUCTION_CAPTURING_ALL;
  287. if (getObj())
  288. addUsedEvents(LCLICK | RCLICK | HOVER);
  289. pos.x += x;
  290. pos.y += y;
  291. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  292. creatureImage = new CAnimImage(imgName, creature ? creature->iconIndex : 0);
  293. if (!creature)
  294. creatureImage->disable();
  295. selectionImage = new CAnimImage(imgName, 1);
  296. selectionImage->disable();
  297. if(Owner->smallIcons)
  298. {
  299. pos.w = 32;
  300. pos.h = 32;
  301. }
  302. else
  303. {
  304. pos.w = 58;
  305. pos.h = 64;
  306. }
  307. stackCount = new CLabel(pos.w, pos.h, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
  308. if (!creature)
  309. stackCount->disable();
  310. else
  311. stackCount->setText(boost::lexical_cast<std::string>(myStack->count));
  312. }
  313. void CGarrisonInt::addSplitBtn(CButton * button)
  314. {
  315. addChild(button);
  316. button->recActions = defActions;
  317. splitButtons.push_back(button);
  318. button->block(getSelection() == nullptr);
  319. }
  320. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  321. {
  322. ret.resize(7);
  323. if (set)
  324. {
  325. for(auto & elem : set->Slots())
  326. {
  327. ret[elem.first.getNum()] = new CGarrisonSlot(this, posX + (elem.first.getNum()*distance), posY, elem.first, Upg, elem.second);
  328. }
  329. }
  330. for(int i=0; i<ret.size(); i++)
  331. if(!ret[i])
  332. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY, SlotID(i), Upg, nullptr);
  333. if (twoRows)
  334. for (int i=4; i<ret.size(); i++)
  335. {
  336. ret[i]->pos.x -= 126;
  337. ret[i]->pos.y += 37;
  338. };
  339. }
  340. void CGarrisonInt::createSlots()
  341. {
  342. OBJ_CONSTRUCTION_CAPTURING_ALL;
  343. int width = smallIcons? 32 : 58;
  344. createSet(slotsUp, armedObjs[0], 0, 0, width+interx, 0);
  345. createSet(slotsDown, armedObjs[1], garOffset.x, garOffset.y, width+interx, 1);
  346. }
  347. void CGarrisonInt::recreateSlots()
  348. {
  349. selectSlot(nullptr);
  350. setSplittingMode(false);
  351. for(auto & elem : splitButtons)
  352. elem->block(true);
  353. for(CGarrisonSlot * slot : slotsUp)
  354. slot->update();
  355. for(CGarrisonSlot * slot : slotsDown)
  356. slot->update();
  357. }
  358. void CGarrisonInt::splitClick()
  359. {
  360. if(!getSelection())
  361. return;
  362. setSplittingMode(!getSplittingMode());
  363. redraw();
  364. }
  365. void CGarrisonInt::splitStacks(int, int amountRight)
  366. {
  367. LOCPLINT->cb->splitStack(armedObjs[getSelection()->upg], armedObjs[pb], getSelection()->ID, p2, amountRight);
  368. }
  369. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  370. SDL_Surface *pomsur, const Point& SurOffset,
  371. const CArmedInstance *s1, const CArmedInstance *s2,
  372. bool _removableUnits, bool smallImgs, bool _twoRows ) :
  373. highlighted(nullptr),
  374. inSplittingMode(false),
  375. interx(inx),
  376. garOffset(garsOffset),
  377. smallIcons(smallImgs),
  378. removableUnits(_removableUnits),
  379. twoRows(_twoRows)
  380. {
  381. setArmy(s1, false);
  382. setArmy(s2, true);
  383. pos.x += x;
  384. pos.y += y;
  385. createSlots();
  386. }
  387. const CGarrisonSlot * CGarrisonInt::getSelection()
  388. {
  389. return highlighted;
  390. }
  391. void CGarrisonInt::selectSlot(CGarrisonSlot *slot)
  392. {
  393. if (slot != highlighted)
  394. {
  395. if (highlighted)
  396. highlighted->setHighlight(false);
  397. highlighted = slot;
  398. for (auto button : splitButtons)
  399. button->block(highlighted == nullptr);
  400. if (highlighted)
  401. highlighted->setHighlight(true);
  402. }
  403. }
  404. void CGarrisonInt::setSplittingMode(bool on)
  405. {
  406. assert(on == false || highlighted != nullptr); //can't be in splitting mode without selection
  407. if (inSplittingMode || on)
  408. {
  409. for(CGarrisonSlot * slot : slotsUp)
  410. slot->setHighlight( ( on && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  411. for(CGarrisonSlot * slot : slotsDown)
  412. slot->setHighlight( ( on && (slot->creature == nullptr || slot->creature == getSelection()->creature)));
  413. inSplittingMode = on;
  414. }
  415. }
  416. bool CGarrisonInt::getSplittingMode()
  417. {
  418. return inSplittingMode;
  419. }
  420. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  421. {
  422. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == PlayerColor::UNFLAGGABLE) : false;
  423. armedObjs[bottomGarrison] = army;
  424. }
  425. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits ):
  426. CWindowObject(PLAYER_COLORED, "GARRISON")
  427. {
  428. OBJ_CONSTRUCTION_CAPTURING_ALL;
  429. garr = new CGarrisonInt(92, 127, 4, Point(0,96), background->bg, Point(93,127), up, down, removableUnits);
  430. {
  431. CButton *split = new CButton(Point(88, 314), "IDV6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [&]{ garr->splitClick(); } );
  432. removeChild(split);
  433. garr->addSplitBtn(split);
  434. }
  435. quit = new CButton(Point(399, 314), "IOK6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [&]{ close(); }, SDLK_RETURN);
  436. std::string titleText;
  437. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  438. titleText = CGI->generaltexth->allTexts[709];
  439. else
  440. {
  441. titleText = CGI->generaltexth->allTexts[35];
  442. boost::algorithm::replace_first(titleText, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  443. }
  444. new CLabel(275, 30, FONT_BIG, CENTER, Colors::YELLOW, titleText);
  445. new CAnimImage("CREST58", garr->armedObjs[0]->getOwner().getNum(), 0, 28, 124);
  446. new CAnimImage("PortraitsLarge", dynamic_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait, 0, 29, 222);
  447. }
  448. CGarrisonHolder::CGarrisonHolder()
  449. {
  450. }
  451. void CWindowWithGarrison::updateGarrisons()
  452. {
  453. garr->recreateSlots();
  454. }