CGarrisonInt.cpp 14 KB

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