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