CKingdomInterface.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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/CGeneralTextHandler.h"
  33. #include "../../lib/CHeroHandler.h"
  34. #include "../../lib/GameSettings.h"
  35. #include "../../lib/CSkillHandler.h"
  36. #include "../../lib/CTownHandler.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_CAPTURING(255-DISPOSE);
  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_CAPTURING(255-DISPOSE);
  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. addSetAndCallbacks(newHeroSet);
  503. });
  504. case 1:
  505. return std::make_shared<CKingdTownList>(size);
  506. default:
  507. return std::shared_ptr<CIntObject>();
  508. }
  509. }
  510. void CKingdomInterface::generateMinesList(const std::vector<const CGObjectInstance *> & ownedObjects)
  511. {
  512. ui32 footerPos = OVERVIEW_SIZE * 116;
  513. ResourceSet minesCount = ResourceSet();
  514. int totalIncome=0;
  515. for(const CGObjectInstance * object : ownedObjects)
  516. {
  517. //Mines
  518. if(object->ID == Obj::MINE || object->ID == Obj::ABANDONED_MINE)
  519. {
  520. const CGMine * mine = dynamic_cast<const CGMine *>(object);
  521. assert(mine);
  522. minesCount[mine->producedResource]++;
  523. if (mine->producedResource == EGameResID::GOLD)
  524. totalIncome += mine->producedQuantity;
  525. }
  526. }
  527. //Heroes can produce gold as well - skill, specialty or arts
  528. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  529. for(auto & hero : heroes)
  530. {
  531. totalIncome += hero->valOfBonuses(Selector::typeSubtype(BonusType::GENERATE_RESOURCE, BonusSubtypeID(GameResID(EGameResID::GOLD))));
  532. }
  533. //Add town income of all towns
  534. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  535. for(auto & town : towns)
  536. {
  537. totalIncome += town->dailyIncome()[EGameResID::GOLD];
  538. }
  539. //if player has some modded boosts we want to show that as well
  540. totalIncome += LOCPLINT->cb->getPlayerState(LOCPLINT->playerID)->valOfBonuses(BonusType::RESOURCES_CONSTANT_BOOST, BonusSubtypeID(GameResID(EGameResID::GOLD)));
  541. totalIncome += LOCPLINT->cb->getPlayerState(LOCPLINT->playerID)->valOfBonuses(BonusType::RESOURCES_TOWN_MULTIPLYING_BOOST, BonusSubtypeID(GameResID(EGameResID::GOLD))) * towns.size();
  542. for(int i=0; i<7; i++)
  543. {
  544. std::string value = std::to_string(minesCount[i]);
  545. auto data = std::make_shared<InfoBoxCustom>(value, "", AnimationPath::builtin("OVMINES"), i, CGI->generaltexth->translate("core.minename", i));
  546. minesBox[i] = std::make_shared<InfoBox>(Point(20+i*80, 31+footerPos), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data);
  547. minesBox[i]->removeUsedEvents(LCLICK|SHOW_POPUP); //fixes #890 - mines boxes ignore clicks
  548. }
  549. incomeArea = std::make_shared<CHoverableArea>();
  550. incomeArea->pos = Rect(pos.x+580, pos.y+31+footerPos, 136, 68);
  551. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  552. incomeAmount = std::make_shared<CLabel>(628, footerPos + 70, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, std::to_string(totalIncome));
  553. }
  554. void CKingdomInterface::generateButtons()
  555. {
  556. ui32 footerPos = OVERVIEW_SIZE * 116;
  557. //Main control buttons
  558. btnHeroes = std::make_shared<CButton>(Point(748, 28+footerPos), AnimationPath::builtin("OVBUTN1.DEF"), CButton::tooltip(CGI->generaltexth->overview[11], CGI->generaltexth->overview[6]),
  559. std::bind(&CKingdomInterface::activateTab, this, 0), EShortcut::KINGDOM_HEROES_TAB);
  560. btnHeroes->block(true);
  561. btnTowns = std::make_shared<CButton>(Point(748, 64+footerPos), AnimationPath::builtin("OVBUTN6.DEF"), CButton::tooltip(CGI->generaltexth->overview[12], CGI->generaltexth->overview[7]),
  562. std::bind(&CKingdomInterface::activateTab, this, 1), EShortcut::KINGDOM_TOWNS_TAB);
  563. btnExit = std::make_shared<CButton>(Point(748,99+footerPos), AnimationPath::builtin("OVBUTN1.DEF"), CButton::tooltip(CGI->generaltexth->allTexts[600]),
  564. std::bind(&CKingdomInterface::close, this), EShortcut::GLOBAL_RETURN);
  565. btnExit->setImageOrder(3, 4, 5, 6);
  566. //Object list control buttons
  567. dwellTop = std::make_shared<CButton>(Point(733, 4), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToPos(0);});
  568. dwellBottom = std::make_shared<CButton>(Point(733, footerPos+2), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToPos(-1); });
  569. dwellBottom->setImageOrder(2, 3, 4, 5);
  570. dwellUp = std::make_shared<CButton>(Point(733, 24), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToPrev(); });
  571. dwellUp->setImageOrder(4, 5, 6, 7);
  572. dwellDown = std::make_shared<CButton>(Point(733, footerPos-18), AnimationPath::builtin("OVBUTN4.DEF"), CButton::tooltip(), [&](){ dwellingsList->moveToNext(); });
  573. dwellDown->setImageOrder(6, 7, 8, 9);
  574. }
  575. void CKingdomInterface::activateTab(size_t which)
  576. {
  577. Settings s = persistentStorage.write["gui"]["lastKindomInterface"];
  578. s->Integer() = which;
  579. btnHeroes->block(which == 0);
  580. btnTowns->block(which == 1);
  581. tabArea->setActive(which);
  582. }
  583. void CKingdomInterface::buildChanged()
  584. {
  585. tabArea->reset();
  586. }
  587. void CKingdomInterface::townChanged(const CGTownInstance *town)
  588. {
  589. if(auto townList = std::dynamic_pointer_cast<CKingdTownList>(tabArea->getItem()))
  590. townList->townChanged(town);
  591. }
  592. void CKingdomInterface::heroRemoved()
  593. {
  594. tabArea->reset();
  595. }
  596. void CKingdomInterface::updateGarrisons()
  597. {
  598. if(auto garrison = std::dynamic_pointer_cast<IGarrisonHolder>(tabArea->getItem()))
  599. garrison->updateGarrisons();
  600. }
  601. bool CKingdomInterface::holdsGarrison(const CArmedInstance * army)
  602. {
  603. return army->getOwner() == LOCPLINT->playerID;
  604. }
  605. CKingdHeroList::CKingdHeroList(size_t maxSize, const CreateHeroItemFunctor & onCreateHeroItemCallback)
  606. {
  607. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  608. title = std::make_shared<CPicture>(ImagePath::builtin("OVTITLE"),16,0);
  609. title->colorize(LOCPLINT->playerID);
  610. heroLabel = std::make_shared<CLabel>(150, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[0]);
  611. skillsLabel = std::make_shared<CLabel>(500, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[1]);
  612. ui32 townCount = LOCPLINT->cb->howManyHeroes(false);
  613. ui32 size = OVERVIEW_SIZE*116 + 19;
  614. heroes = std::make_shared<CListBox>([onCreateHeroItemCallback](size_t idx) -> std::shared_ptr<CIntObject>
  615. {
  616. auto heroesList = LOCPLINT->localState->getWanderingHeroes();
  617. if(idx < heroesList.size())
  618. {
  619. auto hero = std::make_shared<CHeroItem>(heroesList[idx]);
  620. onCreateHeroItemCallback(hero->heroArts);
  621. return hero;
  622. }
  623. else
  624. {
  625. return std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), (idx - 2) % GameConstants::KINGDOM_WINDOW_HEROES_SLOTS);
  626. }
  627. }, Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size));
  628. }
  629. void CKingdHeroList::updateGarrisons()
  630. {
  631. for(std::shared_ptr<CIntObject> object : heroes->getItems())
  632. {
  633. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  634. garrison->updateGarrisons();
  635. }
  636. }
  637. bool CKingdHeroList::holdsGarrison(const CArmedInstance * army)
  638. {
  639. for(std::shared_ptr<CIntObject> object : heroes->getItems())
  640. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  641. if (garrison->holdsGarrison(army))
  642. return true;
  643. return false;
  644. }
  645. CKingdTownList::CKingdTownList(size_t maxSize)
  646. {
  647. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  648. title = std::make_shared<CPicture>(ImagePath::builtin("OVTITLE"), 16, 0);
  649. title->colorize(LOCPLINT->playerID);
  650. townLabel = std::make_shared<CLabel>(146, 10,FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[3]);
  651. garrHeroLabel = std::make_shared<CLabel>(375, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[4]);
  652. visitHeroLabel = std::make_shared<CLabel>(608, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[5]);
  653. ui32 townCount = LOCPLINT->cb->howManyTowns();
  654. ui32 size = OVERVIEW_SIZE*116 + 19;
  655. towns = std::make_shared<CListBox>(std::bind(&CKingdTownList::createTownItem, this, _1),
  656. Point(19,21), Point(0,116), maxSize, townCount, 0, 1, Rect(-19, -21, size, size));
  657. }
  658. void CKingdTownList::townChanged(const CGTownInstance * town)
  659. {
  660. for(std::shared_ptr<CIntObject> object : towns->getItems())
  661. {
  662. CTownItem * townItem = dynamic_cast<CTownItem *>(object.get());
  663. if(townItem && townItem->town == town)
  664. townItem->update();
  665. }
  666. }
  667. void CKingdTownList::updateGarrisons()
  668. {
  669. for(std::shared_ptr<CIntObject> object : towns->getItems())
  670. {
  671. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  672. garrison->updateGarrisons();
  673. }
  674. }
  675. bool CKingdTownList::holdsGarrison(const CArmedInstance * army)
  676. {
  677. for(std::shared_ptr<CIntObject> object : towns->getItems())
  678. if(IGarrisonHolder * garrison = dynamic_cast<IGarrisonHolder*>(object.get()))
  679. if (garrison->holdsGarrison(army))
  680. return true;
  681. return false;
  682. }
  683. std::shared_ptr<CIntObject> CKingdTownList::createTownItem(size_t index)
  684. {
  685. ui32 picCount = 4; // OVSLOT contains 4 images
  686. auto townsList = LOCPLINT->localState->getOwnedTowns();
  687. if(index < townsList.size())
  688. return std::make_shared<CTownItem>(townsList[index]);
  689. else
  690. return std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), (index-2) % picCount );
  691. }
  692. CTownItem::CTownItem(const CGTownInstance * Town)
  693. : town(Town)
  694. {
  695. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  696. background = std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), 6);
  697. name = std::make_shared<CLabel>(74, 8, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, town->getNameTranslated());
  698. income = std::make_shared<CLabel>( 190, 60, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(town->dailyIncome()[EGameResID::GOLD]));
  699. hall = std::make_shared<CTownInfo>( 69, 31, town, true);
  700. fort = std::make_shared<CTownInfo>(111, 31, town, false);
  701. garr = std::make_shared<CGarrisonInt>(Point(313, 3), 4, Point(232,0), town->getUpperArmy(), town->visitingHero, true, true, CGarrisonInt::ESlotsLayout::TWO_ROWS);
  702. heroes = std::make_shared<HeroSlots>(town, Point(244,6), Point(475,6), garr, false);
  703. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  704. picture = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPT"), iconIndex, 0, 5, 6);
  705. openTown = std::make_shared<LRClickableAreaOpenTown>(Rect(5, 6, 58, 64), town);
  706. for(size_t i=0; i<town->creatures.size(); i++)
  707. {
  708. growth.push_back(std::make_shared<CCreaInfo>(Point(401+37*(int)i, 78), town, (int)i, true, true));
  709. available.push_back(std::make_shared<CCreaInfo>(Point(48+37*(int)i, 78), town, (int)i, true, false));
  710. }
  711. fastTownHall = std::make_shared<CButton>(Point(69, 31), AnimationPath::builtin("castleInterfaceQuickAccess"), CButton::tooltip(), [this]() { std::make_shared<CCastleBuildings>(town)->enterTownHall(); });
  712. fastTownHall->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("ITMTL"), town->hallLevel()));
  713. int imageIndex = town->fortLevel() == CGTownInstance::EFortLevel::NONE ? 3 : town->fortLevel() - 1;
  714. fastArmyPurchase = std::make_shared<CButton>(Point(111, 31), AnimationPath::builtin("castleInterfaceQuickAccess"), CButton::tooltip(), [this]() { std::make_shared<CCastleBuildings>(town)->enterToTheQuickRecruitmentWindow(); });
  715. fastArmyPurchase->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("itmcl"), imageIndex));
  716. fastTavern = std::make_shared<LRClickableArea>(Rect(5, 6, 58, 64), [&]()
  717. {
  718. if(town->builtBuildings.count(BuildingID::TAVERN))
  719. LOCPLINT->showTavernWindow(town, nullptr, QueryID::NONE);
  720. }, [&]{
  721. if(!town->town->faction->getDescriptionTranslated().empty())
  722. CRClickPopup::createAndPush(town->town->faction->getDescriptionTranslated());
  723. });
  724. fastMarket = std::make_shared<LRClickableArea>(Rect(153, 6, 65, 64), []()
  725. {
  726. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  727. for(auto & town : towns)
  728. {
  729. if(town->builtBuildings.count(BuildingID::MARKETPLACE))
  730. {
  731. GH.windows().createAndPushWindow<CMarketWindow>(town, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  732. return;
  733. }
  734. }
  735. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  736. });
  737. fastTown = std::make_shared<LRClickableArea>(Rect(67, 6, 165, 20), [&]()
  738. {
  739. GH.windows().createAndPushWindow<CCastleInterface>(town);
  740. });
  741. }
  742. void CTownItem::updateGarrisons()
  743. {
  744. garr->selectSlot(nullptr);
  745. garr->setArmy(town->getUpperArmy(), EGarrisonType::UPPER);
  746. garr->setArmy(town->visitingHero, EGarrisonType::LOWER);
  747. garr->recreateSlots();
  748. }
  749. bool CTownItem::holdsGarrison(const CArmedInstance * army)
  750. {
  751. return army == town || army == town->getUpperArmy() || army == town->visitingHero;
  752. }
  753. void CTownItem::update()
  754. {
  755. std::string incomeVal = std::to_string(town->dailyIncome()[EGameResID::GOLD]);
  756. if (incomeVal != income->getText())
  757. income->setText(incomeVal);
  758. heroes->update();
  759. for (size_t i=0; i<town->creatures.size(); i++)
  760. {
  761. growth[i]->update();
  762. available[i]->update();
  763. }
  764. }
  765. class ArtSlotsTab : public CIntObject
  766. {
  767. public:
  768. std::shared_ptr<CAnimImage> background;
  769. std::vector<std::shared_ptr<CArtPlace>> arts;
  770. ArtSlotsTab()
  771. {
  772. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  773. background = std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), 4);
  774. pos = background->pos;
  775. for(int i=0; i<9; i++)
  776. arts.push_back(std::make_shared<CArtPlace>(Point(269+i*48, 66)));
  777. }
  778. };
  779. class BackpackTab : public CIntObject
  780. {
  781. public:
  782. std::shared_ptr<CAnimImage> background;
  783. std::vector<std::shared_ptr<CArtPlace>> arts;
  784. std::shared_ptr<CButton> btnLeft;
  785. std::shared_ptr<CButton> btnRight;
  786. BackpackTab()
  787. {
  788. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  789. background = std::make_shared<CAnimImage>(AnimationPath::builtin("OVSLOT"), 5);
  790. pos = background->pos;
  791. btnLeft = std::make_shared<CButton>(Point(269, 66), AnimationPath::builtin("HSBTNS3"), CButton::tooltip(), 0);
  792. btnRight = std::make_shared<CButton>(Point(675, 66), AnimationPath::builtin("HSBTNS5"), CButton::tooltip(), 0);
  793. for(int i=0; i<8; i++)
  794. arts.push_back(std::make_shared<CArtPlace>(Point(294+i*48, 66)));
  795. }
  796. };
  797. CHeroItem::CHeroItem(const CGHeroInstance * Hero)
  798. : hero(Hero)
  799. {
  800. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  801. artTabs.resize(3);
  802. auto arts1 = std::make_shared<ArtSlotsTab>();
  803. auto arts2 = std::make_shared<ArtSlotsTab>();
  804. auto backpack = std::make_shared<BackpackTab>();
  805. artTabs[0] = arts1;
  806. artTabs[1] = arts2;
  807. artTabs[2] = backpack;
  808. arts1->recActions = SHARE_POS;
  809. arts2->recActions = SHARE_POS;
  810. backpack->recActions = SHARE_POS;
  811. name = std::make_shared<CLabel>(75, 7, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero->getNameTranslated());
  812. //layout is not trivial: MACH4 - catapult - excluded, MISC[x] rearranged
  813. assert(arts1->arts.size() == 9);
  814. assert(arts2->arts.size() == 9);
  815. CArtifactsOfHeroMain::ArtPlaceMap arts =
  816. {
  817. {ArtifactPosition::HEAD, arts1->arts[0]},
  818. {ArtifactPosition::SHOULDERS,arts1->arts[1]},
  819. {ArtifactPosition::NECK,arts1->arts[2]},
  820. {ArtifactPosition::RIGHT_HAND,arts1->arts[3]},
  821. {ArtifactPosition::LEFT_HAND,arts1->arts[4]},
  822. {ArtifactPosition::TORSO, arts1->arts[5]},
  823. {ArtifactPosition::RIGHT_RING,arts1->arts[6]},
  824. {ArtifactPosition::LEFT_RING, arts1->arts[7]},
  825. {ArtifactPosition::FEET, arts1->arts[8]},
  826. {ArtifactPosition::MISC1, arts2->arts[0]},
  827. {ArtifactPosition::MISC2, arts2->arts[1]},
  828. {ArtifactPosition::MISC3, arts2->arts[2]},
  829. {ArtifactPosition::MISC4, arts2->arts[3]},
  830. {ArtifactPosition::MISC5, arts2->arts[4]},
  831. {ArtifactPosition::MACH1, arts2->arts[5]},
  832. {ArtifactPosition::MACH2, arts2->arts[6]},
  833. {ArtifactPosition::MACH3, arts2->arts[7]},
  834. {ArtifactPosition::SPELLBOOK, arts2->arts[8]}
  835. };
  836. heroArts = std::make_shared<CArtifactsOfHeroKingdom>(arts, backpack->arts, backpack->btnLeft, backpack->btnRight);
  837. heroArts->setHero(hero);
  838. artsTabs = std::make_shared<CTabbedInt>(std::bind(&CHeroItem::onTabSelected, this, _1));
  839. artButtons = std::make_shared<CToggleGroup>(0);
  840. for(size_t it = 0; it<3; it++)
  841. {
  842. int stringID[3] = {259, 261, 262};
  843. std::string hover = CGI->generaltexth->overview[13+it];
  844. std::string overlay = CGI->generaltexth->overview[8+it];
  845. auto button = std::make_shared<CToggleButton>(Point(364+(int)it*112, 46), AnimationPath::builtin("OVBUTN3"), CButton::tooltip(hover, overlay), 0);
  846. button->setTextOverlay(CGI->generaltexth->allTexts[stringID[it]], FONT_SMALL, Colors::YELLOW);
  847. artButtons->addToggle((int)it, button);
  848. }
  849. artButtons->addCallback(std::bind(&CTabbedInt::setActive, artsTabs, _1));
  850. artButtons->addCallback(std::bind(&CHeroItem::onArtChange, this, _1));
  851. artButtons->setSelected(0);
  852. garr = std::make_shared<CGarrisonInt>(Point(6, 78), 4, Point(), hero, nullptr, true, true);
  853. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), hero->getIconIndex(), 0, 5, 6);
  854. heroArea = std::make_shared<CHeroArea>(5, 6, hero);
  855. name = std::make_shared<CLabel>(73, 7, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero->getNameTranslated());
  856. artsText = std::make_shared<CLabel>(320, 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->overview[2]);
  857. for(size_t i=0; i<GameConstants::PRIMARY_SKILLS; i++)
  858. {
  859. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_PRIMARY_SKILL, hero, (int)i);
  860. heroInfo.push_back(std::make_shared<InfoBox>(Point(78+(int)i*36, 26), InfoBox::POS_DOWN, InfoBox::SIZE_SMALL, data));
  861. }
  862. for(size_t i=0; i<GameConstants::SKILL_PER_HERO; i++)
  863. {
  864. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_SECONDARY_SKILL, hero, (int)i);
  865. heroInfo.push_back(std::make_shared<InfoBox>(Point(410+(int)i*36, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL, data));
  866. }
  867. {
  868. auto data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_SPECIAL, hero);
  869. heroInfo.push_back(std::make_shared<InfoBox>(Point(375, 5), InfoBox::POS_NONE, InfoBox::SIZE_SMALL, data));
  870. data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_EXPERIENCE, hero);
  871. heroInfo.push_back(std::make_shared<InfoBox>(Point(330, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data));
  872. data = std::make_shared<InfoBoxHeroData>(IInfoBoxData::HERO_MANA, hero);
  873. heroInfo.push_back(std::make_shared<InfoBox>(Point(280, 5), InfoBox::POS_INSIDE, InfoBox::SIZE_SMALL, data));
  874. }
  875. morale = std::make_shared<MoraleLuckBox>(true, Rect(225, 53, 30, 22), true);
  876. luck = std::make_shared<MoraleLuckBox>(false, Rect(225, 28, 30, 22), true);
  877. morale->set(hero);
  878. luck->set(hero);
  879. }
  880. void CHeroItem::updateGarrisons()
  881. {
  882. garr->recreateSlots();
  883. }
  884. bool CHeroItem::holdsGarrison(const CArmedInstance * army)
  885. {
  886. return hero == army;
  887. }
  888. std::shared_ptr<CIntObject> CHeroItem::onTabSelected(size_t index)
  889. {
  890. return artTabs.at(index);
  891. }
  892. void CHeroItem::onArtChange(int tabIndex)
  893. {
  894. //redraw item after background change
  895. if(isActive())
  896. redraw();
  897. }