2
0

CKingdomInterface.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * CKingdomInterface.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 "CKingdomInterface.h"
  12. #include "CCastleInterface.h"
  13. #include "CPlayerState.h"
  14. #include "InfoWindows.h"
  15. #include "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../PlayerLocalState.h"
  18. #include "../adventureMap/CResDataBar.h"
  19. #include "../gui/CGuiHandler.h"
  20. #include "../gui/Shortcut.h"
  21. #include "../gui/WindowHandler.h"
  22. #include "../widgets/CComponent.h"
  23. #include "../widgets/CGarrisonInt.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../widgets/MiscWidgets.h"
  26. #include "../widgets/Buttons.h"
  27. #include "../widgets/ObjectLists.h"
  28. #include "../windows/CMarketWindow.h"
  29. #include "../../CCallback.h"
  30. #include "../../lib/CConfigHandler.h"
  31. #include "../../lib/CCreatureHandler.h"
  32. #include "../../lib/texts/CGeneralTextHandler.h"
  33. #include "../../lib/CHeroHandler.h"
  34. #include "../../lib/GameSettings.h"
  35. #include "../../lib/CSkillHandler.h"
  36. #include "../../lib/StartInfo.h"
  37. #include "../../lib/mapObjects/CGHeroInstance.h"
  38. #include "../../lib/mapObjects/CGTownInstance.h"
  39. #include "../../lib/mapObjects/MiscObjects.h"
  40. static const std::string OVERVIEW_BACKGROUND = "OvCast.pcx";
  41. static const size_t OVERVIEW_SIZE = 4;
  42. InfoBox::InfoBox(Point position, InfoPos Pos, InfoSize Size, std::shared_ptr<IInfoBoxData> Data):
  43. size(Size),
  44. infoPos(Pos),
  45. data(Data),
  46. value(nullptr),
  47. name(nullptr)
  48. {
  49. assert(data);
  50. addUsedEvents(LCLICK | SHOW_POPUP);
  51. EFonts font = (size < SIZE_MEDIUM)? FONT_SMALL: FONT_MEDIUM;
  52. OBJECT_CONSTRUCTION;
  53. pos+=position;
  54. image = std::make_shared<CAnimImage>(data->getImageName(size), data->getImageIndex());
  55. pos = image->pos;
  56. switch(infoPos)
  57. {
  58. case POS_CORNER:
  59. value = std::make_shared<CLabel>(pos.w, pos.h, font, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, data->getValueText());
  60. break;
  61. case POS_INSIDE:
  62. value = std::make_shared<CLabel>(pos.w/2, pos.h-6, font, ETextAlignment::CENTER, Colors::WHITE, data->getValueText());
  63. break;
  64. case POS_UP_DOWN:
  65. name = std::make_shared<CLabel>(pos.w/2, -12, font, ETextAlignment::CENTER, Colors::WHITE, data->getNameText());
  66. [[fallthrough]];
  67. case POS_DOWN:
  68. value = std::make_shared<CLabel>(pos.w/2, pos.h+8, font, ETextAlignment::CENTER, Colors::WHITE, data->getValueText());
  69. break;
  70. case POS_RIGHT:
  71. name = std::make_shared<CLabel>(pos.w+6, 6, font, ETextAlignment::TOPLEFT, Colors::WHITE, data->getNameText());
  72. value = std::make_shared<CLabel>(pos.w+6, pos.h-16, font, ETextAlignment::TOPLEFT, Colors::WHITE, data->getValueText());
  73. break;
  74. }
  75. if(name)
  76. pos = pos.include(name->pos);
  77. if(value)
  78. pos = pos.include(value->pos);
  79. hover = std::make_shared<CHoverableArea>();
  80. hover->hoverText = data->getHoverText();
  81. hover->pos = pos;
  82. }
  83. InfoBox::~InfoBox() = default;
  84. void InfoBox::showPopupWindow(const Point & cursorPosition)
  85. {
  86. std::shared_ptr<CComponent> comp;
  87. std::string text;
  88. data->prepareMessage(text, comp);
  89. if (comp)
  90. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  91. else if (!text.empty())
  92. CRClickPopup::createAndPush(text);
  93. }
  94. void InfoBox::clickPressed(const Point & cursorPosition)
  95. {
  96. std::shared_ptr<CComponent> comp;
  97. std::string text;
  98. data->prepareMessage(text, comp);
  99. if(comp)
  100. LOCPLINT->showInfoDialog(text, CInfoWindow::TCompsInfo(1, comp));
  101. }
  102. IInfoBoxData::IInfoBoxData(InfoType Type)
  103. : type(Type)
  104. {
  105. }
  106. InfoBoxAbstractHeroData::InfoBoxAbstractHeroData(InfoType Type)
  107. : IInfoBoxData(Type)
  108. {
  109. }
  110. std::string InfoBoxAbstractHeroData::getValueText()
  111. {
  112. switch (type)
  113. {
  114. case HERO_MANA:
  115. case HERO_EXPERIENCE:
  116. case HERO_PRIMARY_SKILL:
  117. return std::to_string(getValue());
  118. case HERO_SPECIAL:
  119. return CGI->generaltexth->jktexts[5];
  120. case HERO_SECONDARY_SKILL:
  121. {
  122. si64 value = getValue();
  123. if (value)
  124. return CGI->generaltexth->levels[value];
  125. else
  126. return "";
  127. }
  128. default:
  129. logGlobal->error("Invalid InfoBox info type");
  130. }
  131. return "";
  132. }
  133. std::string InfoBoxAbstractHeroData::getNameText()
  134. {
  135. switch (type)
  136. {
  137. case HERO_PRIMARY_SKILL:
  138. return CGI->generaltexth->primarySkillNames[getSubID()];
  139. case HERO_MANA:
  140. return CGI->generaltexth->allTexts[387];
  141. case HERO_EXPERIENCE:
  142. return CGI->generaltexth->jktexts[6];
  143. case HERO_SPECIAL:
  144. return CGI->heroh->objects[getSubID()]->getSpecialtyNameTranslated();
  145. case HERO_SECONDARY_SKILL:
  146. if (getValue())
  147. return CGI->skillh->getByIndex(getSubID())->getNameTranslated();
  148. else
  149. return "";
  150. default:
  151. logGlobal->error("Invalid InfoBox info type");
  152. }
  153. return "";
  154. }
  155. AnimationPath InfoBoxAbstractHeroData::getImageName(InfoBox::InfoSize size)
  156. {
  157. //TODO: sizes
  158. switch(size)
  159. {
  160. case InfoBox::SIZE_SMALL:
  161. {
  162. switch(type)
  163. {
  164. case HERO_PRIMARY_SKILL:
  165. case HERO_MANA:
  166. case HERO_EXPERIENCE:
  167. return AnimationPath::builtin("PSKIL32");
  168. case HERO_SPECIAL:
  169. return AnimationPath::builtin("UN32");
  170. case HERO_SECONDARY_SKILL:
  171. return AnimationPath::builtin("SECSK32");
  172. default:
  173. assert(0);
  174. }
  175. }
  176. case InfoBox::SIZE_BIG:
  177. {
  178. switch(type)
  179. {
  180. case HERO_PRIMARY_SKILL:
  181. case HERO_MANA:
  182. case HERO_EXPERIENCE:
  183. return AnimationPath::builtin("PSKIL42");
  184. case HERO_SPECIAL:
  185. return AnimationPath::builtin("UN44");
  186. case HERO_SECONDARY_SKILL:
  187. return AnimationPath::builtin("SECSKILL");
  188. default:
  189. assert(0);
  190. }
  191. }
  192. default:
  193. assert(0);
  194. }
  195. return {};
  196. }
  197. std::string InfoBoxAbstractHeroData::getHoverText()
  198. {
  199. //TODO: any texts here?
  200. return "";
  201. }
  202. size_t InfoBoxAbstractHeroData::getImageIndex()
  203. {
  204. switch (type)
  205. {
  206. case HERO_SPECIAL:
  207. return CGI->heroh->objects[getSubID()]->imageIndex;
  208. case HERO_PRIMARY_SKILL:
  209. return getSubID();
  210. case HERO_MANA:
  211. return 5;
  212. case HERO_EXPERIENCE:
  213. return 4;
  214. case HERO_SECONDARY_SKILL:
  215. {
  216. si64 value = getValue();
  217. if (value)
  218. return getSubID()*3 + value + 2;
  219. else
  220. return 0;//FIXME: Should be transparent instead of empty
  221. }
  222. default:
  223. assert(0);
  224. return 0;
  225. }
  226. }
  227. void InfoBoxAbstractHeroData::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  228. {
  229. comp.reset();
  230. switch (type)
  231. {
  232. case HERO_SPECIAL:
  233. text = CGI->heroh->objects[getSubID()]->getSpecialtyDescriptionTranslated();
  234. break;
  235. case HERO_PRIMARY_SKILL:
  236. text = CGI->generaltexth->arraytxt[2+getSubID()];
  237. comp = std::make_shared<CComponent>(ComponentType::PRIM_SKILL, PrimarySkill(getSubID()), getValue());
  238. break;
  239. case HERO_MANA:
  240. text = CGI->generaltexth->allTexts[149];
  241. break;
  242. case HERO_EXPERIENCE:
  243. text = CGI->generaltexth->allTexts[241];
  244. break;
  245. case HERO_SECONDARY_SKILL:
  246. {
  247. si64 value = getValue();
  248. int subID = getSubID();
  249. if(value)
  250. {
  251. text = CGI->skillh->getByIndex(subID)->getDescriptionTranslated((int)value);
  252. comp = std::make_shared<CComponent>(ComponentType::SEC_SKILL, SecondarySkill(subID), (int)value);
  253. }
  254. break;
  255. }
  256. default:
  257. break;
  258. }
  259. }
  260. InfoBoxHeroData::InfoBoxHeroData(InfoType Type, const CGHeroInstance * Hero, int Index):
  261. InfoBoxAbstractHeroData(Type),
  262. hero(Hero),
  263. index(Index)
  264. {
  265. }
  266. int InfoBoxHeroData::getSubID()
  267. {
  268. switch(type)
  269. {
  270. case HERO_PRIMARY_SKILL:
  271. return index;
  272. case HERO_SECONDARY_SKILL:
  273. if(hero->secSkills.size() > index)
  274. return hero->secSkills[index].first;
  275. else
  276. return 0;
  277. case HERO_SPECIAL:
  278. return hero->type->getIndex();
  279. case HERO_MANA:
  280. case HERO_EXPERIENCE:
  281. return 0;
  282. default:
  283. assert(0);
  284. return 0;
  285. }
  286. }
  287. si64 InfoBoxHeroData::getValue()
  288. {
  289. if(!hero)
  290. return 0;
  291. switch(type)
  292. {
  293. case HERO_PRIMARY_SKILL:
  294. return hero->getPrimSkillLevel(static_cast<PrimarySkill>(index));
  295. case HERO_MANA:
  296. return hero->mana;
  297. case HERO_EXPERIENCE:
  298. return hero->exp;
  299. case HERO_SECONDARY_SKILL:
  300. if(hero->secSkills.size() > index)
  301. return hero->secSkills[index].second;
  302. else
  303. return 0;
  304. case HERO_SPECIAL:
  305. return 0;
  306. default:
  307. assert(0);
  308. return 0;
  309. }
  310. }
  311. std::string InfoBoxHeroData::getHoverText()
  312. {
  313. switch (type)
  314. {
  315. case HERO_PRIMARY_SKILL:
  316. return boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[index]);
  317. case HERO_MANA:
  318. return CGI->generaltexth->heroscrn[22];
  319. case HERO_EXPERIENCE:
  320. return CGI->generaltexth->heroscrn[9];
  321. case HERO_SPECIAL:
  322. return CGI->generaltexth->heroscrn[27];
  323. case HERO_SECONDARY_SKILL:
  324. if (hero->secSkills.size() > index)
  325. {
  326. std::string level = CGI->generaltexth->levels[hero->secSkills[index].second-1];
  327. std::string skill = CGI->skillh->getByIndex(hero->secSkills[index].first)->getNameTranslated();
  328. return boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % level % skill);
  329. }
  330. else
  331. {
  332. return "";
  333. }
  334. default:
  335. return InfoBoxAbstractHeroData::getHoverText();
  336. }
  337. }
  338. std::string InfoBoxHeroData::getValueText()
  339. {
  340. if (hero)
  341. {
  342. switch (type)
  343. {
  344. case HERO_MANA:
  345. return std::to_string(hero->mana) + '/' +
  346. std::to_string(hero->manaLimit());
  347. case HERO_EXPERIENCE:
  348. return std::to_string(hero->exp);
  349. }
  350. }
  351. return InfoBoxAbstractHeroData::getValueText();
  352. }
  353. void InfoBoxHeroData::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  354. {
  355. comp.reset();
  356. switch(type)
  357. {
  358. case HERO_MANA:
  359. text = CGI->generaltexth->allTexts[205];
  360. boost::replace_first(text, "%s", hero->getNameTranslated());
  361. boost::replace_first(text, "%d", std::to_string(hero->mana));
  362. boost::replace_first(text, "%d", std::to_string(hero->manaLimit()));
  363. break;
  364. case HERO_EXPERIENCE:
  365. text = CGI->generaltexth->allTexts[2];
  366. boost::replace_first(text, "%d", std::to_string(hero->level));
  367. boost::replace_first(text, "%d", std::to_string(CGI->heroh->reqExp(hero->level+1)));
  368. boost::replace_first(text, "%d", std::to_string(hero->exp));
  369. break;
  370. default:
  371. InfoBoxAbstractHeroData::prepareMessage(text, comp);
  372. break;
  373. }
  374. }
  375. InfoBoxCustomHeroData::InfoBoxCustomHeroData(InfoType Type, int SubID, si64 Value):
  376. InfoBoxAbstractHeroData(Type),
  377. subID(SubID),
  378. value(Value)
  379. {
  380. }
  381. int InfoBoxCustomHeroData::getSubID()
  382. {
  383. return subID;
  384. }
  385. si64 InfoBoxCustomHeroData::getValue()
  386. {
  387. return value;
  388. }
  389. InfoBoxCustom::InfoBoxCustom(std::string ValueText, std::string NameText, const AnimationPath & ImageName, size_t ImageIndex, std::string HoverText):
  390. IInfoBoxData(CUSTOM),
  391. valueText(ValueText),
  392. nameText(NameText),
  393. imageName(ImageName),
  394. hoverText(HoverText),
  395. imageIndex(ImageIndex)
  396. {
  397. }
  398. std::string InfoBoxCustom::getHoverText()
  399. {
  400. return hoverText;
  401. }
  402. size_t InfoBoxCustom::getImageIndex()
  403. {
  404. return imageIndex;
  405. }
  406. AnimationPath InfoBoxCustom::getImageName(InfoBox::InfoSize size)
  407. {
  408. return imageName;
  409. }
  410. std::string InfoBoxCustom::getNameText()
  411. {
  412. return nameText;
  413. }
  414. std::string InfoBoxCustom::getValueText()
  415. {
  416. return valueText;
  417. }
  418. void InfoBoxCustom::prepareMessage(std::string & text, std::shared_ptr<CComponent> & comp)
  419. {
  420. }
  421. CKingdomInterface::CKingdomInterface()
  422. : CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin(OVERVIEW_BACKGROUND))
  423. {
  424. OBJECT_CONSTRUCTION;
  425. ui32 footerPos = OVERVIEW_SIZE * 116;
  426. tabArea = std::make_shared<CTabbedInt>(std::bind(&CKingdomInterface::createMainTab, this, _1), Point(4,4));
  427. std::vector<const CGObjectInstance * > ownedObjects = LOCPLINT->cb->getMyObjects();
  428. generateObjectsList(ownedObjects);
  429. generateMinesList(ownedObjects);
  430. generateButtons();
  431. statusbar = CGStatusBar::create(std::make_shared<CPicture>(ImagePath::builtin("KSTATBAR"), 10,pos.h - 45));
  432. resdatabar = std::make_shared<CResDataBar>(ImagePath::builtin("KRESBAR"), 7, 111+footerPos, 29, 5, 76, 81);
  433. activateTab(persistentStorage["gui"]["lastKindomInterface"].Integer());
  434. }
  435. void CKingdomInterface::generateObjectsList(const std::vector<const CGObjectInstance * > &ownedObjects)
  436. {
  437. ui32 footerPos = OVERVIEW_SIZE * 116;
  438. size_t dwellSize = (footerPos - 64)/57;
  439. //Map used to determine image number for several objects
  440. std::map<std::pair<int,int>,int> idToImage;
  441. idToImage[std::make_pair( 20, 1)] = 81;//Golem factory
  442. idToImage[std::make_pair( 42, 0)] = 82;//Lighthouse
  443. idToImage[std::make_pair( 33, 0)] = 83;//Garrison
  444. idToImage[std::make_pair(219, 0)] = 83;//Garrison
  445. idToImage[std::make_pair( 33, 1)] = 84;//Anti-magic Garrison
  446. idToImage[std::make_pair(219, 1)] = 84;//Anti-magic Garrison
  447. idToImage[std::make_pair( 53, 7)] = 85;//Abandoned mine
  448. idToImage[std::make_pair( 20, 0)] = 86;//Conflux
  449. idToImage[std::make_pair( 87, 0)] = 87;//Harbor
  450. std::map<int, OwnedObjectInfo> visibleObjects;
  451. for(const CGObjectInstance * object : ownedObjects)
  452. {
  453. //Dwellings
  454. if(object->ID == Obj::CREATURE_GENERATOR1)
  455. {
  456. OwnedObjectInfo & info = visibleObjects[object->subID];
  457. if(info.count++ == 0)
  458. {
  459. info.hoverText = object->getObjectName();
  460. info.imageID = object->subID;
  461. }
  462. }
  463. //Special objects from idToImage map that should be displayed in objects list
  464. auto iter = idToImage.find(std::make_pair(object->ID, object->subID));
  465. if(iter != idToImage.end())
  466. {
  467. OwnedObjectInfo & info = visibleObjects[iter->second];
  468. if(info.count++ == 0)
  469. {
  470. info.hoverText = object->getObjectName();
  471. info.imageID = iter->second;
  472. }
  473. }
  474. }
  475. objects.reserve(visibleObjects.size());
  476. for(auto & element : visibleObjects)
  477. {
  478. objects.push_back(element.second);
  479. }
  480. dwellingsList = std::make_shared<CListBox>(std::bind(&CKingdomInterface::createOwnedObject, this, _1),
  481. Point(740,44), Point(0,57), dwellSize, visibleObjects.size());
  482. }
  483. std::shared_ptr<CIntObject> CKingdomInterface::createOwnedObject(size_t index)
  484. {
  485. if(index < objects.size())
  486. {
  487. OwnedObjectInfo & obj = objects[index];
  488. std::string value = std::to_string(obj.count);
  489. auto data = std::make_shared<InfoBoxCustom>(value, "", AnimationPath::builtin("FLAGPORT"), obj.imageID, obj.hoverText);
  490. return std::make_shared<InfoBox>(Point(), InfoBox::POS_CORNER, InfoBox::SIZE_SMALL, data);
  491. }
  492. return std::shared_ptr<CIntObject>();
  493. }
  494. std::shared_ptr<CIntObject> CKingdomInterface::createMainTab(size_t index)
  495. {
  496. size_t size = OVERVIEW_SIZE;
  497. switch(index)
  498. {
  499. case 0:
  500. return std::make_shared<CKingdHeroList>(size, [this](const CWindowWithArtifacts::CArtifactsOfHeroPtr & newHeroSet)
  501. {
  502. newHeroSet->clickPressedCallback = [this, newHeroSet](const CArtPlace & artPlace, const Point & cursorPosition)
  503. {
  504. clickPressedOnArtPlace(newHeroSet->getHero(), artPlace.slot, false, false, false);
  505. };
  506. newHeroSet->showPopupCallback = [this, newHeroSet](CArtPlace & artPlace, const Point & cursorPosition)
  507. {
  508. showArtifactAssembling(*newHeroSet, artPlace, cursorPosition);
  509. };
  510. newHeroSet->gestureCallback = [this, newHeroSet](const CArtPlace & artPlace, const Point & cursorPosition)
  511. {
  512. showQuickBackpackWindow(newHeroSet->getHero(), artPlace.slot, cursorPosition);
  513. };
  514. addSet(newHeroSet);
  515. });
  516. case 1:
  517. return std::make_shared<CKingdTownList>(size);
  518. default:
  519. return std::shared_ptr<CIntObject>();
  520. }
  521. }
  522. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance *> & ownedObjects)
  523. {
  524. ui32 footerPos = OVERVIEW_SIZE * 116;
  525. ResourceSet minesCount = ResourceSet();
  526. int totalIncome=0;
  527. for(const CGObjectInstance * object : ownedObjects)
  528. {
  529. //Mines
  530. if(object->ID == Obj::MINE || object->ID == Obj::ABANDONED_MINE)
  531. {
  532. const CGMine * mine = dynamic_cast<const CGMine *>(object);
  533. assert(mine);
  534. minesCount[mine->producedResource]++;
  535. if (mine->producedResource == EGameResID::GOLD)
  536. totalIncome += mine->getProducedQuantity();
  537. }
  538. }
  539. //Heroes can produce gold as well - skill, specialty or arts
  540. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  541. auto * playerSettings = LOCPLINT->cb->getPlayerSettings(LOCPLINT->playerID);
  542. for(auto & hero : heroes)
  543. {
  544. totalIncome += hero->valOfBonuses(Selector::typeSubtype(BonusType::GENERATE_RESOURCE, BonusSubtypeID(GameResID(EGameResID::GOLD)))) * playerSettings->handicap.percentIncome / 100;
  545. }
  546. //Add town income of all towns
  547. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  548. for(auto & town : towns)
  549. {
  550. totalIncome += town->dailyIncome()[EGameResID::GOLD];
  551. }
  552. //if player has some modded boosts we want to show that as well
  553. totalIncome += LOCPLINT->cb->getPlayerState(LOCPLINT->playerID)->valOfBonuses(BonusType::RESOURCES_CONSTANT_BOOST, BonusSubtypeID(GameResID(EGameResID::GOLD))) * playerSettings->handicap.percentIncome / 100;
  554. totalIncome += LOCPLINT->cb->getPlayerState(LOCPLINT->playerID)->valOfBonuses(BonusType::RESOURCES_TOWN_MULTIPLYING_BOOST, BonusSubtypeID(GameResID(EGameResID::GOLD))) * towns.size() * playerSettings->handicap.percentIncome / 100;
  555. for(int i=0; i<7; i++)
  556. {
  557. std::string value = std::to_string(minesCount[i]);
  558. auto data = std::make_shared<InfoBoxCustom>(value, "", AnimationPath::builtin("OVMINES"), i, CGI->generaltexth->translate("core.minename", i));
  559. minesBox[i] = std::make_shared<InfoBox>(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data);
  560. minesBox[i]->removeUsedEvents(LCLICK|SHOW_POPUP); //fixes #890 - mines boxes ignore clicks
  561. }
  562. incomeArea = std::make_shared<CHoverableArea>();
  563. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  564. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  565. incomeAmount = std::make_shared<CLabel>(628, footerPos + 70, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, std::to_string(totalIncome));
  566. }
  567. void CKingdomInterface::generateButtons()
  568. {
  569. ui32 footerPos = OVERVIEW_SIZE * 116;
  570. //Main control buttons
  571. btnHeroes = std::make_shared<CButton>(Point(748, 28+footerPos), AnimationPath::builtin("OVBUTN1.DEF"), CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  572. std::bind(&CKingdomInterface::activateTab, this, 0), EShortcut::KINGDOM_HEROES_TAB);
  573. btnHeroes->block(true);
  574. btnTowns = std::make_shared<CButton>(Point(748, 64+footerPos), AnimationPath::builtin("OVBUTN6.DEF"), CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  575. std::bind(&CKingdomInterface::activateTab, this, 1), EShortcut::KINGDOM_TOWNS_TAB);
  576. btnExit = std::make_shared<CButton>(Point(748,99+footerPos), AnimationPath::builtin("OVBUTN1.DEF"), CButton::tooltip(CGI->generaltexth->allTexts[600]),
  577. std::bind(&CKingdomInterface::close, this), EShortcut::GLOBAL_RETURN);
  578. btnExit->setImageOrder(3, 4, 5, 6);
  579. //Object list control buttons
  580. dwellTop = std::make_shared<CButton>(Point(733, 4), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToPos(0);});
  581. dwellBottom = std::make_shared<CButton>(Point(733, footerPos+2), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToPos(-1); });
  582. dwellBottom->setImageOrder(2, 3, 4, 5);
  583. dwellUp = std::make_shared<CButton>(Point(733, 24), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToPrev(); });
  584. dwellUp->setImageOrder(4, 5, 6, 7);
  585. dwellDown = std::make_shared<CButton>(Point(733, footerPos-18), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToNext(); });
  586. dwellDown->setImageOrder(6, 7, 8, 9);
  587. }
  588. void CKingdomInterface::activateTab(size_t which)
  589. {
  590. Settings s = persistentStorage.write["gui"]["lastKindomInterface"];
  591. s->Integer() = which;
  592. btnHeroes->block(which == 0);
  593. btnTowns->block(which == 1);
  594. tabArea->setActive(which);
  595. }
  596. void CKingdomInterface::buildChanged()
  597. {
  598. tabArea->reset();
  599. }
  600. void CKingdomInterface::townChanged(const CGTownInstance *town)
  601. {
  602. if(auto townList = std::dynamic_pointer_cast<CKingdTownList>(tabArea->getItem()))
  603. townList->townChanged(town);
  604. }
  605. void CKingdomInterface::heroRemoved()
  606. {
  607. tabArea->reset();
  608. }
  609. void CKingdomInterface::updateGarrisons()
  610. {
  611. if(auto garrison = std::dynamic_pointer_cast<IGarrisonHolder>(tabArea->getItem()))
  612. garrison->updateGarrisons();
  613. }
  614. bool CKingdomInterface::holdsGarrison(const CArmedInstance * army)
  615. {
  616. return army->getOwner() == LOCPLINT->playerID;
  617. }
  618. CKingdHeroList::CKingdHeroList(size_t maxSize, const CreateHeroItemFunctor & onCreateHeroItemCallback)
  619. {
  620. OBJECT_CONSTRUCTION;
  621. title = std::make_shared<CPicture>(ImagePath::builtin("OVTITLE"),16,0);
  622. title->setPlayerColor(LOCPLINT->playerID);
  623. heroLabel = std::make_shared<CLabel>(150, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[0]);
  624. skillsLabel = std::make_shared<CLabel>(500, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[1]);
  625. ui32 townCount = LOCPLINT->cb->howManyHeroes(false);
  626. ui32 size = OVERVIEW_SIZE*116 + 19;
  627. heroes = std::make_shared<CListBox>([onCreateHeroItemCallback](size_t idx) -> std::shared_ptr<CIntObject>
  628. {
  629. auto heroesList = LOCPLINT->localState->getWanderingHeroes();
  630. if(idx < heroesList.size())
  631. {
  632. auto hero = std::make_shared<CHeroItem>(heroesList[idx]);
  633. onCreateHeroItemCallback(hero->heroArts);
  634. return hero;
  635. }
  636. else
  637. {
  638. return std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), (idx - 2) % GameConstants::KINGDOM_WINDOW_HEROES_SLOTS);
  639. }
  640. }, Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size));
  641. }
  642. void CKingdHeroList::updateGarrisons()
  643. {
  644. for(std::shared_ptr<CIntObject> object : heroes->getItems())
  645. {
  646. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  647. garrison->updateGarrisons();
  648. }
  649. }
  650. bool CKingdHeroList::holdsGarrison(const CArmedInstance * army)
  651. {
  652. for(std::shared_ptr<CIntObject> object : heroes->getItems())
  653. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  654. if (garrison->holdsGarrison(army))
  655. return true;
  656. return false;
  657. }
  658. CKingdTownList::CKingdTownList(size_t maxSize)
  659. {
  660. OBJECT_CONSTRUCTION;
  661. title = std::make_shared<CPicture>(ImagePath::builtin("OVTITLE"), 16, 0);
  662. title->setPlayerColor(LOCPLINT->playerID);
  663. townLabel = std::make_shared<CLabel>(146, 10,FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[3]);
  664. garrHeroLabel = std::make_shared<CLabel>(375, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[4]);
  665. visitHeroLabel = std::make_shared<CLabel>(608, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[5]);
  666. ui32 townCount = LOCPLINT->cb->howManyTowns();
  667. ui32 size = OVERVIEW_SIZE*116 + 19;
  668. towns = std::make_shared<CListBox>(std::bind(&CKingdTownList::createTownItem, this, _1),
  669. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size));
  670. }
  671. void CKingdTownList::townChanged(const CGTownInstance * town)
  672. {
  673. for(std::shared_ptr<CIntObject> object : towns->getItems())
  674. {
  675. CTownItem * townItem = dynamic_cast<CTownItem *>(object.get());
  676. if(townItem && townItem->town == town)
  677. townItem->update();
  678. }
  679. }
  680. void CKingdTownList::updateGarrisons()
  681. {
  682. for(std::shared_ptr<CIntObject> object : towns->getItems())
  683. {
  684. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  685. garrison->updateGarrisons();
  686. }
  687. }
  688. bool CKingdTownList::holdsGarrison(const CArmedInstance * army)
  689. {
  690. for(std::shared_ptr<CIntObject> object : towns->getItems())
  691. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  692. if (garrison->holdsGarrison(army))
  693. return true;
  694. return false;
  695. }
  696. std::shared_ptr<CIntObject> CKingdTownList::createTownItem(size_t index)
  697. {
  698. ui32 picCount = 4; // OVSLOT contains 4 images
  699. auto townsList = LOCPLINT->localState->getOwnedTowns();
  700. if(index < townsList.size())
  701. return std::make_shared<CTownItem>(townsList[index]);
  702. else
  703. return std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), (index-2) % picCount );
  704. }
  705. CTownItem::CTownItem(const CGTownInstance * Town)
  706. : town(Town)
  707. {
  708. OBJECT_CONSTRUCTION;
  709. background = std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), 6);
  710. name = std::make_shared<CLabel>(74, 8, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, town->getNameTranslated());
  711. income = std::make_shared<CLabel>( 190, 60, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(town->dailyIncome()[EGameResID::GOLD]));
  712. hall = std::make_shared<CTownInfo>( 69, 31, town, true);
  713. fort = std::make_shared<CTownInfo>(111, 31, town, false);
  714. garr = std::make_shared<CGarrisonInt>(Point(313, 3), 4, Point(232,0), town->getUpperArmy(), town->visitingHero, true, true, CGarrisonInt::ESlotsLayout::TWO_ROWS);
  715. heroes = std::make_shared<HeroSlots>(town, Point(244,6), Point(475,6), garr, false);
  716. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->built >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  717. picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPT"), iconIndex, 0, 5, 6);
  718. openTown = std::make_shared<LRClickableAreaOpenTown>(Rect(5, 6, 58, 64), town);
  719. for(size_t i=0; i<town->creatures.size(); i++)
  720. {
  721. growth.push_back(std::make_shared<CCreaInfo>(Point(401+37*(int)i, 78), town, (int)i, true, true));
  722. available.push_back(std::make_shared<CCreaInfo>(Point(48+37*(int)i, 78), town, (int)i, true, false));
  723. }
  724. fastTownHall = std::make_shared<CButton>(Point(69, 31), AnimationPath::builtin("castleInterfaceQuickAccess"), CButton::tooltip(), [this]() { std::make_shared<CCastleBuildings>(town)->enterTownHall(); });
  725. fastTownHall->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("ITMTL"), town->hallLevel()));
  726. int imageIndex = town->fortLevel() == CGTownInstance::EFortLevel::NONE ? 3 : town->fortLevel() - 1;
  727. fastArmyPurchase = std::make_shared<CButton>(Point(111, 31), AnimationPath::builtin("castleInterfaceQuickAccess"), CButton::tooltip(), [this]() { std::make_shared<CCastleBuildings>(town)->enterToTheQuickRecruitmentWindow(); });
  728. fastArmyPurchase->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("itmcl"), imageIndex));
  729. fastTavern = std::make_shared<LRClickableArea>(Rect(5, 6, 58, 64), [&]()
  730. {
  731. if(town->builtBuildings.count(BuildingID::TAVERN))
  732. LOCPLINT->showTavernWindow(town, nullptr, QueryID::NONE);
  733. }, [&]{
  734. if(!town->town->faction->getDescriptionTranslated().empty())
  735. CRClickPopup::createAndPush(town->town->faction->getDescriptionTranslated());
  736. });
  737. fastMarket = std::make_shared<LRClickableArea>(Rect(153, 6, 65, 64), []()
  738. {
  739. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  740. for(auto & town : towns)
  741. {
  742. if(town->builtBuildings.count(BuildingID::MARKETPLACE))
  743. {
  744. GH.windows().createAndPushWindow<CMarketWindow>(town, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  745. return;
  746. }
  747. }
  748. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  749. });
  750. fastTown = std::make_shared<LRClickableArea>(Rect(67, 6, 165, 20), [&]()
  751. {
  752. GH.windows().createAndPushWindow<CCastleInterface>(town);
  753. });
  754. }
  755. void CTownItem::updateGarrisons()
  756. {
  757. garr->selectSlot(nullptr);
  758. garr->setArmy(town->getUpperArmy(), EGarrisonType::UPPER);
  759. garr->setArmy(town->visitingHero, EGarrisonType::LOWER);
  760. garr->recreateSlots();
  761. }
  762. bool CTownItem::holdsGarrison(const CArmedInstance * army)
  763. {
  764. return army == town || army == town->getUpperArmy() || army == town->visitingHero;
  765. }
  766. void CTownItem::update()
  767. {
  768. std::string incomeVal = std::to_string(town->dailyIncome()[EGameResID::GOLD]);
  769. if (incomeVal != income->getText())
  770. income->setText(incomeVal);
  771. heroes->update();
  772. for (size_t i=0; i<town->creatures.size(); i++)
  773. {
  774. growth[i]->update();
  775. available[i]->update();
  776. }
  777. }
  778. class ArtSlotsTab : public CIntObject
  779. {
  780. public:
  781. std::shared_ptr<CAnimImage> background;
  782. std::vector<std::shared_ptr<CArtPlace>> arts;
  783. ArtSlotsTab()
  784. {
  785. OBJECT_CONSTRUCTION;
  786. background = std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), 4);
  787. pos = background->pos;
  788. for(int i=0; i<9; i++)
  789. arts.push_back(std::make_shared<CArtPlace>(Point(269+i*48, 66)));
  790. }
  791. };
  792. class BackpackTab : public CIntObject
  793. {
  794. public:
  795. std::shared_ptr<CAnimImage> background;
  796. std::vector<std::shared_ptr<CArtPlace>> arts;
  797. std::shared_ptr<CButton> btnLeft;
  798. std::shared_ptr<CButton> btnRight;
  799. BackpackTab()
  800. {
  801. OBJECT_CONSTRUCTION;
  802. background = std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), 5);
  803. pos = background->pos;
  804. btnLeft = std::make_shared<CButton>(Point(269, 66), AnimationPath::builtin("HSBTNS3"), CButton::tooltip(), 0);
  805. btnRight = std::make_shared<CButton>(Point(675, 66), AnimationPath::builtin("HSBTNS5"), CButton::tooltip(), 0);
  806. for(int i=0; i<8; i++)
  807. arts.push_back(std::make_shared<CArtPlace>(Point(294+i*48, 66)));
  808. }
  809. };
  810. CHeroItem::CHeroItem(const CGHeroInstance * Hero)
  811. : hero(Hero)
  812. {
  813. OBJECT_CONSTRUCTION;
  814. artTabs.resize(3);
  815. auto arts1 = std::make_shared<ArtSlotsTab>();
  816. auto arts2 = std::make_shared<ArtSlotsTab>();
  817. auto backpack = std::make_shared<BackpackTab>();
  818. artTabs[0] = arts1;
  819. artTabs[1] = arts2;
  820. artTabs[2] = backpack;
  821. arts1->recActions = SHARE_POS;
  822. arts2->recActions = SHARE_POS;
  823. backpack->recActions = SHARE_POS;
  824. name = std::make_shared<CLabel>(75, 7, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero->getNameTranslated());
  825. //layout is not trivial: MACH4 - catapult - excluded, MISC[x] rearranged
  826. assert(arts1->arts.size() == 9);
  827. assert(arts2->arts.size() == 9);
  828. CArtifactsOfHeroMain::ArtPlaceMap arts =
  829. {
  830. {ArtifactPosition::HEAD, arts1->arts[0]},
  831. {ArtifactPosition::SHOULDERS,arts1->arts[1]},
  832. {ArtifactPosition::NECK,arts1->arts[2]},
  833. {ArtifactPosition::RIGHT_HAND,arts1->arts[3]},
  834. {ArtifactPosition::LEFT_HAND,arts1->arts[4]},
  835. {ArtifactPosition::TORSO, arts1->arts[5]},
  836. {ArtifactPosition::RIGHT_RING,arts1->arts[6]},
  837. {ArtifactPosition::LEFT_RING, arts1->arts[7]},
  838. {ArtifactPosition::FEET, arts1->arts[8]},
  839. {ArtifactPosition::MISC1, arts2->arts[0]},
  840. {ArtifactPosition::MISC2, arts2->arts[1]},
  841. {ArtifactPosition::MISC3, arts2->arts[2]},
  842. {ArtifactPosition::MISC4, arts2->arts[3]},
  843. {ArtifactPosition::MISC5, arts2->arts[4]},
  844. {ArtifactPosition::MACH1, arts2->arts[5]},
  845. {ArtifactPosition::MACH2, arts2->arts[6]},
  846. {ArtifactPosition::MACH3, arts2->arts[7]},
  847. {ArtifactPosition::SPELLBOOK, arts2->arts[8]}
  848. };
  849. heroArts = std::make_shared<CArtifactsOfHeroKingdom>(arts, backpack->arts, backpack->btnLeft, backpack->btnRight);
  850. heroArts->setHero(hero);
  851. artsTabs = std::make_shared<CTabbedInt>(std::bind(&CHeroItem::onTabSelected, this, _1));
  852. artButtons = std::make_shared<CToggleGroup>(0);
  853. for(size_t it = 0; it<3; it++)
  854. {
  855. int stringID[3] = {259, 261, 262};
  856. std::string hover = CGI->generaltexth->overview[13+it];
  857. std::string overlay = CGI->generaltexth->overview[8+it];
  858. auto button = std::make_shared<CToggleButton>(Point(364+(int)it*112, 46), AnimationPath::builtin("OVBUTN3"), CButton::tooltip(hover, overlay), 0);
  859. button->setTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  860. artButtons->addToggle((int)it, button);
  861. }
  862. artButtons->addCallback(std::bind(&CTabbedInt::setActive, artsTabs, _1));
  863. artButtons->addCallback(std::bind(&CHeroItem::onArtChange, this, _1));
  864. artButtons->setSelected(0);
  865. garr = std::make_shared<CGarrisonInt>(Point(6, 78), 4, Point(), hero, nullptr, true, true);
  866. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), hero->getIconIndex(), 0, 5, 6);
  867. heroArea = std::make_shared<CHeroArea>(5, 6, hero);
  868. name = std::make_shared<CLabel>(73, 7, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero->getNameTranslated());
  869. artsText = std::make_shared<CLabel>(320, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[2]);
  870. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  871. {
  872. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_PRIMARY_SKILL, hero, (int)i);
  873. heroInfo.push_back(std::make_shared<InfoBox>(Point(78+(int)i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL, data));
  874. }
  875. for(size_t i=0; i<GameConstants::SKILL_PER_HERO; i++)
  876. {
  877. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_SECONDARY_SKILL, hero, (int)i);
  878. heroInfo.push_back(std::make_shared<InfoBox>(Point(410+(int)i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL, data));
  879. }
  880. {
  881. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_SPECIAL, hero);
  882. heroInfo.push_back(std::make_shared<InfoBox>(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL, data));
  883. data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_EXPERIENCE, hero);
  884. heroInfo.push_back(std::make_shared<InfoBox>(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data));
  885. data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_MANA, hero);
  886. heroInfo.push_back(std::make_shared<InfoBox>(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data));
  887. }
  888. morale = std::make_shared<MoraleLuckBox>(true, Rect(225, 53, 30, 22), true);
  889. luck = std::make_shared<MoraleLuckBox>(false, Rect(225, 28, 30, 22), true);
  890. morale->set(hero);
  891. luck->set(hero);
  892. }
  893. void CHeroItem::updateGarrisons()
  894. {
  895. garr->recreateSlots();
  896. }
  897. bool CHeroItem::holdsGarrison(const CArmedInstance * army)
  898. {
  899. return hero == army;
  900. }
  901. std::shared_ptr<CIntObject> CHeroItem::onTabSelected(size_t index)
  902. {
  903. return artTabs.at(index);
  904. }
  905. void CHeroItem::onArtChange(int tabIndex)
  906. {
  907. //redraw item after background change
  908. if(isActive())
  909. redraw();
  910. }