CCastleInterface.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*
  2. * CCastleInterface.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 "CCastleInterface.h"
  12. #include "CHeroWindow.h"
  13. #include "CTradeWindow.h"
  14. #include "InfoWindows.h"
  15. #include "GUIClasses.h"
  16. #include "QuickRecruitmentWindow.h"
  17. #include "../CGameInfo.h"
  18. #include "../CMusicHandler.h"
  19. #include "../CPlayerInterface.h"
  20. #include "../PlayerLocalState.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../widgets/MiscWidgets.h"
  25. #include "../widgets/CComponent.h"
  26. #include "../widgets/CGarrisonInt.h"
  27. #include "../widgets/Buttons.h"
  28. #include "../widgets/TextControls.h"
  29. #include "../render/Canvas.h"
  30. #include "../render/IImage.h"
  31. #include "../render/ColorFilter.h"
  32. #include "../adventureMap/AdventureMapInterface.h"
  33. #include "../adventureMap/CList.h"
  34. #include "../adventureMap/CResDataBar.h"
  35. #include "../../CCallback.h"
  36. #include "../../lib/CArtHandler.h"
  37. #include "../../lib/CBuildingHandler.h"
  38. #include "../../lib/CCreatureHandler.h"
  39. #include "../../lib/CGeneralTextHandler.h"
  40. #include "../../lib/GameSettings.h"
  41. #include "../../lib/spells/CSpellHandler.h"
  42. #include "../../lib/CTownHandler.h"
  43. #include "../../lib/GameConstants.h"
  44. #include "../../lib/StartInfo.h"
  45. #include "../../lib/campaign/CampaignState.h"
  46. #include "../../lib/mapObjects/CGHeroInstance.h"
  47. #include "../../lib/mapObjects/CGTownInstance.h"
  48. static bool useCompactCreatureBox()
  49. {
  50. return settings["gameTweaks"]["compactTownCreatureInfo"].Bool();
  51. }
  52. static bool useAvailableAmountAsCreatureLabel()
  53. {
  54. return settings["gameTweaks"]["availableCreaturesAsDwellingLabel"].Bool();
  55. }
  56. CBuildingRect::CBuildingRect(CCastleBuildings * Par, const CGTownInstance * Town, const CStructure * Str)
  57. : CShowableAnim(0, 0, Str->defName, CShowableAnim::BASE, BUILDING_FRAME_TIME),
  58. parent(Par),
  59. town(Town),
  60. str(Str),
  61. border(nullptr),
  62. area(nullptr),
  63. stateTimeCounter(BUILD_ANIMATION_FINISHED_TIMEPOINT)
  64. {
  65. addUsedEvents(LCLICK | SHOW_POPUP | MOVE | HOVER | TIME);
  66. pos.x += str->pos.x;
  67. pos.y += str->pos.y;
  68. // special animation frame manipulation for castle shipyard with and without ship
  69. // done due to .def used in special way, not to animate building - first image is for shipyard without citadel moat, 2nd image is for including moat
  70. if(Town->town->faction->getId() == FactionID::CASTLE && Str->building &&
  71. (Str->building->bid == BuildingID::SHIPYARD || Str->building->bid == BuildingID::SHIP))
  72. {
  73. if(Town->hasBuilt(BuildingID::CITADEL))
  74. {
  75. this->first = 1;
  76. this->frame = 1;
  77. }
  78. else
  79. this->last = 0;
  80. }
  81. if(!str->borderName.empty())
  82. border = IImage::createFromFile(str->borderName, EImageBlitMode::ALPHA);
  83. if(!str->areaName.empty())
  84. area = IImage::createFromFile(str->areaName, EImageBlitMode::ALPHA);
  85. }
  86. const CBuilding * CBuildingRect::getBuilding()
  87. {
  88. if (!str->building)
  89. return nullptr;
  90. if (str->hiddenUpgrade) // hidden upgrades, e.g. hordes - return base (dwelling for hordes)
  91. return town->town->buildings.at(str->building->getBase());
  92. return str->building;
  93. }
  94. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  95. {
  96. return (str->pos.z) < (p2.str->pos.z);
  97. }
  98. void CBuildingRect::hover(bool on)
  99. {
  100. if (!area)
  101. return;
  102. if(on)
  103. {
  104. if(! parent->selectedBuilding //no building hovered
  105. || (*parent->selectedBuilding)<(*this)) //or we are on top
  106. {
  107. parent->selectedBuilding = this;
  108. GH.statusbar()->write(getSubtitle());
  109. }
  110. }
  111. else
  112. {
  113. if(parent->selectedBuilding == this)
  114. {
  115. parent->selectedBuilding = nullptr;
  116. GH.statusbar()->clear();
  117. }
  118. }
  119. }
  120. void CBuildingRect::clickPressed(const Point & cursorPosition)
  121. {
  122. if(getBuilding() && area && (parent->selectedBuilding==this))
  123. {
  124. auto building = getBuilding();
  125. parent->buildingClicked(building->bid, building->subId, building->upgrade);
  126. }
  127. }
  128. void CBuildingRect::showPopupWindow(const Point & cursorPosition)
  129. {
  130. if((!area) || (this!=parent->selectedBuilding) || getBuilding() == nullptr)
  131. return;
  132. BuildingID bid = getBuilding()->bid;
  133. const CBuilding *bld = town->town->buildings.at(bid);
  134. if (bid < BuildingID::DWELL_FIRST)
  135. {
  136. CRClickPopup::createAndPush(CInfoWindow::genText(bld->getNameTranslated(), bld->getDescriptionTranslated()),
  137. std::make_shared<CComponent>(CComponent::building, bld->town->faction->getIndex(), bld->bid));
  138. }
  139. else
  140. {
  141. int level = ( bid - BuildingID::DWELL_FIRST ) % GameConstants::CREATURES_PER_TOWN;
  142. GH.windows().createAndPushWindow<CDwellingInfoBox>(parent->pos.x+parent->pos.w / 2, parent->pos.y+parent->pos.h /2, town, level);
  143. }
  144. }
  145. void CBuildingRect::show(Canvas & to)
  146. {
  147. uint32_t stageDelay = BUILDING_APPEAR_TIMEPOINT;
  148. if(stateTimeCounter < BUILDING_APPEAR_TIMEPOINT)
  149. {
  150. setAlpha(255 * stateTimeCounter / stageDelay);
  151. CShowableAnim::show(to);
  152. }
  153. else
  154. {
  155. setAlpha(255);
  156. CShowableAnim::show(to);
  157. }
  158. if(border && stateTimeCounter > BUILDING_APPEAR_TIMEPOINT)
  159. {
  160. if(stateTimeCounter >= BUILD_ANIMATION_FINISHED_TIMEPOINT)
  161. {
  162. if(parent->selectedBuilding == this)
  163. to.draw(border, pos.topLeft());
  164. return;
  165. }
  166. auto darkBorder = ColorFilter::genRangeShifter(0.f, 0.f, 0.f, 0.5f, 0.5f, 0.5f );
  167. auto lightBorder = ColorFilter::genRangeShifter(0.f, 0.f, 0.f, 2.0f, 2.0f, 2.0f );
  168. auto baseBorder = ColorFilter::genEmptyShifter();
  169. float progress = float(stateTimeCounter % stageDelay) / stageDelay;
  170. if (stateTimeCounter < BUILDING_WHITE_BORDER_TIMEPOINT)
  171. border->adjustPalette(ColorFilter::genInterpolated(lightBorder, darkBorder, progress), 0);
  172. else
  173. if (stateTimeCounter < BUILDING_YELLOW_BORDER_TIMEPOINT)
  174. border->adjustPalette(ColorFilter::genInterpolated(darkBorder, baseBorder, progress), 0);
  175. else
  176. border->adjustPalette(baseBorder, 0);
  177. to.draw(border, pos.topLeft());
  178. }
  179. }
  180. void CBuildingRect::tick(uint32_t msPassed)
  181. {
  182. CShowableAnim::tick(msPassed);
  183. stateTimeCounter += msPassed;
  184. }
  185. void CBuildingRect::showAll(Canvas & to)
  186. {
  187. if (stateTimeCounter == 0)
  188. return;
  189. CShowableAnim::showAll(to);
  190. if(!isActive() && parent->selectedBuilding == this && border)
  191. to.draw(border, pos.topLeft());
  192. }
  193. std::string CBuildingRect::getSubtitle()//hover text for building
  194. {
  195. if (!getBuilding())
  196. return "";
  197. int bid = getBuilding()->bid;
  198. if (bid<30)//non-dwellings - only buiding name
  199. return town->town->buildings.at(getBuilding()->bid)->getNameTranslated();
  200. else//dwellings - recruit %creature%
  201. {
  202. auto & availableCreatures = town->creatures[(bid-30)%GameConstants::CREATURES_PER_TOWN].second;
  203. if(availableCreatures.size())
  204. {
  205. int creaID = availableCreatures.back();//taking last of available creatures
  206. return CGI->generaltexth->allTexts[16] + " " + CGI->creh->objects.at(creaID)->getNamePluralTranslated();
  207. }
  208. else
  209. {
  210. logGlobal->warn("Dwelling with id %d offers no creatures!", bid);
  211. return "#ERROR#";
  212. }
  213. }
  214. }
  215. void CBuildingRect::mouseMoved (const Point & cursorPosition, const Point & lastUpdateDistance)
  216. {
  217. hover(true);
  218. }
  219. bool CBuildingRect::receiveEvent(const Point & position, int eventType) const
  220. {
  221. if (!pos.isInside(position.x, position.y))
  222. return false;
  223. if(area && area->isTransparent(position - pos.topLeft()))
  224. return false;
  225. return CIntObject::receiveEvent(position, eventType);
  226. }
  227. CDwellingInfoBox::CDwellingInfoBox(int centerX, int centerY, const CGTownInstance * Town, int level)
  228. : CWindowObject(RCLICK_POPUP, "CRTOINFO", Point(centerX, centerY))
  229. {
  230. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  231. background->colorize(Town->tempOwner);
  232. const CCreature * creature = CGI->creh->objects.at(Town->creatures.at(level).second.back());
  233. title = std::make_shared<CLabel>(80, 30, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, creature->getNamePluralTranslated());
  234. animation = std::make_shared<CCreaturePic>(30, 44, creature, true, true);
  235. std::string text = std::to_string(Town->creatures.at(level).first);
  236. available = std::make_shared<CLabel>(80,190, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[217] + text);
  237. costPerTroop = std::make_shared<CLabel>(80, 227, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[346]);
  238. for(int i = 0; i<GameConstants::RESOURCE_QUANTITY; i++)
  239. {
  240. auto res = static_cast<EGameResID>(i);
  241. if(creature->getRecruitCost(res))
  242. {
  243. resPicture.push_back(std::make_shared<CAnimImage>("RESOURCE", i, 0, 0, 0));
  244. resAmount.push_back(std::make_shared<CLabel>(0,0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(creature->getRecruitCost(res))));
  245. }
  246. }
  247. int posY = 238;
  248. int posX = pos.w/2 - (int)resAmount.size() * 25 + 5;
  249. for (size_t i=0; i<resAmount.size(); i++)
  250. {
  251. resPicture[i]->moveBy(Point(posX, posY));
  252. resAmount[i]->moveBy(Point(posX+16, posY+43));
  253. posX += 50;
  254. }
  255. }
  256. CDwellingInfoBox::~CDwellingInfoBox() = default;
  257. CHeroGSlot::CHeroGSlot(int x, int y, int updown, const CGHeroInstance * h, HeroSlots * Owner)
  258. {
  259. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  260. owner = Owner;
  261. pos.x += x;
  262. pos.y += y;
  263. pos.w = 58;
  264. pos.h = 64;
  265. upg = updown;
  266. portrait = std::make_shared<CAnimImage>("PortraitsLarge", 0, 0, 0, 0);
  267. portrait->visible = false;
  268. flag = std::make_shared<CAnimImage>("CREST58", 0, 0, 0, 0);
  269. flag->visible = false;
  270. selection = std::make_shared<CAnimImage>("TWCRPORT", 1, 0);
  271. selection->visible = false;
  272. set(h);
  273. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  274. }
  275. CHeroGSlot::~CHeroGSlot() = default;
  276. void CHeroGSlot::hover(bool on)
  277. {
  278. if(!on)
  279. {
  280. GH.statusbar()->clear();
  281. return;
  282. }
  283. std::shared_ptr<CHeroGSlot> other = upg ? owner->garrisonedHero : owner->visitingHero;
  284. std::string temp;
  285. if(hero)
  286. {
  287. if(isSelected())//view NNN
  288. {
  289. temp = CGI->generaltexth->tcommands[4];
  290. boost::algorithm::replace_first(temp,"%s",hero->getNameTranslated());
  291. }
  292. else if(other->hero && other->isSelected())//exchange
  293. {
  294. temp = CGI->generaltexth->tcommands[7];
  295. boost::algorithm::replace_first(temp,"%s",hero->getNameTranslated());
  296. boost::algorithm::replace_first(temp,"%s",other->hero->getNameTranslated());
  297. }
  298. else// select NNN (in ZZZ)
  299. {
  300. if(upg)//down - visiting
  301. {
  302. temp = CGI->generaltexth->tcommands[32];
  303. boost::algorithm::replace_first(temp,"%s",hero->getNameTranslated());
  304. }
  305. else //up - garrison
  306. {
  307. temp = CGI->generaltexth->tcommands[12];
  308. boost::algorithm::replace_first(temp,"%s",hero->getNameTranslated());
  309. }
  310. }
  311. }
  312. else //we are empty slot
  313. {
  314. if(other->isSelected() && other->hero) //move NNNN
  315. {
  316. temp = CGI->generaltexth->tcommands[6];
  317. boost::algorithm::replace_first(temp,"%s",other->hero->getNameTranslated());
  318. }
  319. else //empty
  320. {
  321. temp = CGI->generaltexth->allTexts[507];
  322. }
  323. }
  324. if(temp.size())
  325. GH.statusbar()->write(temp);
  326. }
  327. void CHeroGSlot::clickPressed(const Point & cursorPosition)
  328. {
  329. std::shared_ptr<CHeroGSlot> other = upg ? owner->garrisonedHero : owner->visitingHero;
  330. owner->garr->setSplittingMode(false);
  331. owner->garr->selectSlot(nullptr);
  332. if(hero && isSelected())
  333. {
  334. setHighlight(false);
  335. LOCPLINT->openHeroWindow(hero);
  336. }
  337. else if(other->hero && other->isSelected())
  338. {
  339. owner->swapArmies();
  340. }
  341. else if(hero)
  342. {
  343. setHighlight(true);
  344. owner->garr->selectSlot(nullptr);
  345. redraw();
  346. }
  347. //refresh statusbar
  348. hover(false);
  349. hover(true);
  350. }
  351. void CHeroGSlot::showPopupWindow(const Point & cursorPosition)
  352. {
  353. if(hero)
  354. {
  355. GH.windows().createAndPushWindow<CInfoBoxPopup>(Point(pos.x + 175, pos.y + 100), hero);
  356. }
  357. }
  358. void CHeroGSlot::deactivate()
  359. {
  360. selection->visible = false;
  361. CIntObject::deactivate();
  362. }
  363. bool CHeroGSlot::isSelected() const
  364. {
  365. return selection->visible;
  366. }
  367. void CHeroGSlot::setHighlight(bool on)
  368. {
  369. selection->visible = on;
  370. if(owner->garrisonedHero->hero && owner->visitingHero->hero) //two heroes in town
  371. {
  372. for(auto & elem : owner->garr->splitButtons) //splitting enabled when slot higlighted
  373. elem->block(!on);
  374. }
  375. }
  376. void CHeroGSlot::set(const CGHeroInstance * newHero)
  377. {
  378. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  379. hero = newHero;
  380. selection->visible = false;
  381. portrait->visible = false;
  382. flag->visible = false;
  383. if(newHero)
  384. {
  385. portrait->visible = true;
  386. portrait->setFrame(newHero->portrait);
  387. }
  388. else if(!upg && owner->showEmpty) //up garrison
  389. {
  390. flag->visible = true;
  391. flag->setFrame(LOCPLINT->castleInt->town->getOwner().getNum());
  392. }
  393. }
  394. HeroSlots::HeroSlots(const CGTownInstance * Town, Point garrPos, Point visitPos, std::shared_ptr<CGarrisonInt> Garrison, bool ShowEmpty):
  395. showEmpty(ShowEmpty),
  396. town(Town),
  397. garr(Garrison)
  398. {
  399. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  400. garrisonedHero = std::make_shared<CHeroGSlot>(garrPos.x, garrPos.y, 0, town->garrisonHero, this);
  401. visitingHero = std::make_shared<CHeroGSlot>(visitPos.x, visitPos.y, 1, town->visitingHero, this);
  402. }
  403. HeroSlots::~HeroSlots() = default;
  404. void HeroSlots::update()
  405. {
  406. garrisonedHero->set(town->garrisonHero);
  407. visitingHero->set(town->visitingHero);
  408. }
  409. void HeroSlots::splitClicked()
  410. {
  411. if(!!town->visitingHero && town->garrisonHero && (visitingHero->isSelected() || garrisonedHero->isSelected()))
  412. {
  413. LOCPLINT->showHeroExchange(town->visitingHero->id, town->garrisonHero->id);
  414. }
  415. }
  416. void HeroSlots::swapArmies()
  417. {
  418. bool allow = true;
  419. //moving hero out of town - check if it is allowed
  420. if (town->garrisonHero)
  421. {
  422. if (!town->visitingHero && LOCPLINT->cb->howManyHeroes(false) >= CGI->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_ON_MAP_CAP))
  423. {
  424. std::string text = CGI->generaltexth->translate("core.genrltxt.18"); //You already have %d adventuring heroes under your command.
  425. boost::algorithm::replace_first(text,"%d",std::to_string(LOCPLINT->cb->howManyHeroes(false)));
  426. LOCPLINT->showInfoDialog(text, std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  427. allow = false;
  428. }
  429. else if (town->garrisonHero->stacksCount() == 0)
  430. {
  431. //This hero has no creatures. A hero must have creatures before he can brave the dangers of the countryside.
  432. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.19"), {}, soundBase::sound_todo);
  433. allow = false;
  434. }
  435. }
  436. if(!town->garrisonHero && town->visitingHero) //visiting => garrison, merge armies: town army => hero army
  437. {
  438. if(!town->visitingHero->canBeMergedWith(*town))
  439. {
  440. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[275], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  441. allow = false;
  442. }
  443. }
  444. garrisonedHero->setHighlight(false);
  445. visitingHero->setHighlight(false);
  446. if (allow)
  447. LOCPLINT->cb->swapGarrisonHero(town);
  448. }
  449. class SORTHELP
  450. {
  451. public:
  452. bool operator() (const CIntObject * a, const CIntObject * b)
  453. {
  454. auto b1 = dynamic_cast<const CBuildingRect *>(a);
  455. auto b2 = dynamic_cast<const CBuildingRect *>(b);
  456. if(!b1 && !b2)
  457. return intptr_t(a) < intptr_t(b);
  458. if(b1 && !b2)
  459. return false;
  460. if(!b1 && b2)
  461. return true;
  462. return (*b1)<(*b2);
  463. }
  464. };
  465. SORTHELP buildSorter;
  466. CCastleBuildings::CCastleBuildings(const CGTownInstance* Town):
  467. town(Town),
  468. selectedBuilding(nullptr)
  469. {
  470. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  471. background = std::make_shared<CPicture>(town->town->clientInfo.townBackground);
  472. background->needRefresh = true;
  473. background->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  474. pos.w = background->pos.w;
  475. pos.h = background->pos.h;
  476. recreate();
  477. }
  478. CCastleBuildings::~CCastleBuildings() = default;
  479. void CCastleBuildings::recreate()
  480. {
  481. selectedBuilding = nullptr;
  482. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  483. buildings.clear();
  484. groups.clear();
  485. //Generate buildings list
  486. auto buildingsCopy = town->builtBuildings;// a bit modified copy of built buildings
  487. if(vstd::contains(town->builtBuildings, BuildingID::SHIPYARD))
  488. {
  489. auto bayPos = town->bestLocation();
  490. if(!bayPos.valid())
  491. logGlobal->warn("Shipyard in non-coastal town!");
  492. std::vector <const CGObjectInstance *> vobjs = LOCPLINT->cb->getVisitableObjs(bayPos, false);
  493. //there is visitable obj at shipyard output tile and it's a boat or hero (on boat)
  494. if(!vobjs.empty() && (vobjs.front()->ID == Obj::BOAT || vobjs.front()->ID == Obj::HERO))
  495. {
  496. buildingsCopy.insert(BuildingID::SHIP);
  497. }
  498. }
  499. for(const CStructure * structure : town->town->clientInfo.structures)
  500. {
  501. if(!structure->building)
  502. {
  503. buildings.push_back(std::make_shared<CBuildingRect>(this, town, structure));
  504. continue;
  505. }
  506. if(vstd::contains(buildingsCopy, structure->building->bid))
  507. {
  508. groups[structure->building->getBase()].push_back(structure);
  509. }
  510. }
  511. for(auto & entry : groups)
  512. {
  513. const CBuilding * build = town->town->buildings.at(entry.first);
  514. const CStructure * toAdd = *boost::max_element(entry.second, [=](const CStructure * a, const CStructure * b)
  515. {
  516. return build->getDistance(a->building->bid) < build->getDistance(b->building->bid);
  517. });
  518. buildings.push_back(std::make_shared<CBuildingRect>(this, town, toAdd));
  519. }
  520. boost::sort(children, buildSorter); //TODO: create building in blit order
  521. }
  522. void CCastleBuildings::addBuilding(BuildingID building)
  523. {
  524. //FIXME: implement faster method without complete recreation of town
  525. BuildingID base = town->town->buildings.at(building)->getBase();
  526. recreate();
  527. auto & structures = groups.at(base);
  528. for(auto buildingRect : buildings)
  529. {
  530. if(vstd::contains(structures, buildingRect->str))
  531. {
  532. //reset animation
  533. if(structures.size() == 1)
  534. buildingRect->stateTimeCounter = 0; // transparency -> fully visible stage
  535. else
  536. buildingRect->stateTimeCounter = CBuildingRect::BUILDING_APPEAR_TIMEPOINT; // already in fully visible stage
  537. break;
  538. }
  539. }
  540. }
  541. void CCastleBuildings::removeBuilding(BuildingID building)
  542. {
  543. //FIXME: implement faster method without complete recreation of town
  544. recreate();
  545. }
  546. const CGHeroInstance * CCastleBuildings::getHero()
  547. {
  548. if(town->visitingHero)
  549. return town->visitingHero;
  550. else
  551. return town->garrisonHero;
  552. }
  553. void CCastleBuildings::buildingClicked(BuildingID building, BuildingSubID::EBuildingSubID subID, BuildingID::EBuildingID upgrades)
  554. {
  555. logGlobal->trace("You've clicked on %d", (int)building.toEnum());
  556. const CBuilding *b = town->town->buildings.find(building)->second;
  557. if(building >= BuildingID::DWELL_FIRST)
  558. {
  559. enterDwelling((building-BuildingID::DWELL_FIRST)%GameConstants::CREATURES_PER_TOWN);
  560. }
  561. else
  562. {
  563. switch(building)
  564. {
  565. case BuildingID::MAGES_GUILD_1:
  566. case BuildingID::MAGES_GUILD_2:
  567. case BuildingID::MAGES_GUILD_3:
  568. case BuildingID::MAGES_GUILD_4:
  569. case BuildingID::MAGES_GUILD_5:
  570. enterMagesGuild();
  571. break;
  572. case BuildingID::TAVERN:
  573. LOCPLINT->showTavernWindow(town);
  574. break;
  575. case BuildingID::SHIPYARD:
  576. if(town->shipyardStatus() == IBoatGenerator::GOOD)
  577. LOCPLINT->showShipyardDialog(town);
  578. else if(town->shipyardStatus() == IBoatGenerator::BOAT_ALREADY_BUILT)
  579. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[51]);
  580. break;
  581. case BuildingID::FORT:
  582. case BuildingID::CITADEL:
  583. case BuildingID::CASTLE:
  584. GH.windows().createAndPushWindow<CFortScreen>(town);
  585. break;
  586. case BuildingID::VILLAGE_HALL:
  587. case BuildingID::CITY_HALL:
  588. case BuildingID::TOWN_HALL:
  589. case BuildingID::CAPITOL:
  590. enterTownHall();
  591. break;
  592. case BuildingID::MARKETPLACE:
  593. GH.windows().createAndPushWindow<CMarketplaceWindow>(town, town->visitingHero);
  594. break;
  595. case BuildingID::BLACKSMITH:
  596. enterBlacksmith(town->town->warMachine);
  597. break;
  598. case BuildingID::SHIP:
  599. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[51]); //Cannot build another boat
  600. break;
  601. case BuildingID::SPECIAL_1:
  602. case BuildingID::SPECIAL_2:
  603. case BuildingID::SPECIAL_3:
  604. switch(subID)
  605. {
  606. case BuildingSubID::NONE:
  607. enterBuilding(building);
  608. break;
  609. case BuildingSubID::MYSTIC_POND:
  610. enterFountain(building, subID, upgrades);
  611. break;
  612. case BuildingSubID::ARTIFACT_MERCHANT:
  613. if(town->visitingHero)
  614. GH.windows().createAndPushWindow<CMarketplaceWindow>(town, town->visitingHero, EMarketMode::RESOURCE_ARTIFACT);
  615. else
  616. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->getNameTranslated())); //Only visiting heroes may use the %s.
  617. break;
  618. case BuildingSubID::FOUNTAIN_OF_FORTUNE:
  619. enterFountain(building, subID, upgrades);
  620. break;
  621. case BuildingSubID::FREELANCERS_GUILD:
  622. if(getHero())
  623. GH.windows().createAndPushWindow<CMarketplaceWindow>(town, getHero(), EMarketMode::CREATURE_RESOURCE);
  624. else
  625. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->getNameTranslated())); //Only visiting heroes may use the %s.
  626. break;
  627. case BuildingSubID::MAGIC_UNIVERSITY:
  628. if (getHero())
  629. GH.windows().createAndPushWindow<CUniversityWindow>(getHero(), town);
  630. else
  631. enterBuilding(building);
  632. break;
  633. case BuildingSubID::BROTHERHOOD_OF_SWORD:
  634. if(upgrades == BuildingID::TAVERN)
  635. LOCPLINT->showTavernWindow(town);
  636. else
  637. enterBuilding(building);
  638. break;
  639. case BuildingSubID::CASTLE_GATE:
  640. enterCastleGate();
  641. break;
  642. case BuildingSubID::CREATURE_TRANSFORMER: //Skeleton Transformer
  643. GH.windows().createAndPushWindow<CTransformerWindow>(town, getHero());
  644. break;
  645. case BuildingSubID::PORTAL_OF_SUMMONING:
  646. if (town->creatures[GameConstants::CREATURES_PER_TOWN].second.empty())//No creatures
  647. LOCPLINT->showInfoDialog(CGI->generaltexth->tcommands[30]);
  648. else
  649. enterDwelling(GameConstants::CREATURES_PER_TOWN);
  650. break;
  651. case BuildingSubID::BALLISTA_YARD:
  652. enterBlacksmith(ArtifactID::BALLISTA);
  653. break;
  654. default:
  655. enterBuilding(building);
  656. break;
  657. }
  658. break;
  659. default:
  660. enterBuilding(building);
  661. break;
  662. }
  663. }
  664. }
  665. void CCastleBuildings::enterBlacksmith(ArtifactID artifactID)
  666. {
  667. const CGHeroInstance *hero = town->visitingHero;
  668. if(!hero)
  669. {
  670. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % town->town->buildings.find(BuildingID::BLACKSMITH)->second->getNameTranslated()));
  671. return;
  672. }
  673. auto art = artifactID.toArtifact();
  674. int price = art->getPrice();
  675. bool possible = LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) >= price;
  676. if(possible)
  677. {
  678. for(auto slot : art->getPossibleSlots().at(ArtBearer::HERO))
  679. {
  680. if(hero->getArt(slot) == nullptr)
  681. {
  682. possible = true;
  683. break;
  684. }
  685. else
  686. {
  687. possible = false;
  688. }
  689. }
  690. }
  691. CreatureID cre = art->getWarMachine();
  692. GH.windows().createAndPushWindow<CBlacksmithDialog>(possible, cre, artifactID, hero->id);
  693. }
  694. void CCastleBuildings::enterBuilding(BuildingID building)
  695. {
  696. std::vector<std::shared_ptr<CComponent>> comps(1, std::make_shared<CComponent>(CComponent::building, town->subID, building));
  697. LOCPLINT->showInfoDialog( town->town->buildings.find(building)->second->getDescriptionTranslated(), comps);
  698. }
  699. void CCastleBuildings::enterCastleGate()
  700. {
  701. if (!town->visitingHero)
  702. {
  703. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[126]);
  704. return;//only visiting hero can use castle gates
  705. }
  706. std::vector <int> availableTowns;
  707. std::vector <const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
  708. for(auto & Town : Towns)
  709. {
  710. const CGTownInstance *t = Town;
  711. if (t->id != this->town->id && t->visitingHero == nullptr && //another town, empty and this is
  712. t->town->faction->getId() == town->town->faction->getId() && //the town of the same faction
  713. t->hasBuilt(BuildingSubID::CASTLE_GATE)) //and the town has a castle gate
  714. {
  715. availableTowns.push_back(t->id.getNum());//add to the list
  716. }
  717. }
  718. auto gateIcon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, BuildingID::CASTLE_GATE);//will be deleted by selection window
  719. GH.windows().createAndPushWindow<CObjectListWindow>(availableTowns, gateIcon, CGI->generaltexth->jktexts[40],
  720. CGI->generaltexth->jktexts[41], std::bind (&CCastleInterface::castleTeleport, LOCPLINT->castleInt, _1));
  721. }
  722. void CCastleBuildings::enterDwelling(int level)
  723. {
  724. assert(level >= 0 && level < town->creatures.size());
  725. auto recruitCb = [=](CreatureID id, int count)
  726. {
  727. LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, level);
  728. };
  729. GH.windows().createAndPushWindow<CRecruitmentWindow>(town, level, town, recruitCb, -87);
  730. }
  731. void CCastleBuildings::enterToTheQuickRecruitmentWindow()
  732. {
  733. const auto beginIt = town->creatures.cbegin();
  734. const auto afterLastIt = town->creatures.size() > GameConstants::CREATURES_PER_TOWN
  735. ? std::next(beginIt, GameConstants::CREATURES_PER_TOWN)
  736. : town->creatures.cend();
  737. const auto hasSomeoneToRecruit = std::any_of(beginIt, afterLastIt,
  738. [](const auto & creatureInfo) { return creatureInfo.first > 0; });
  739. if(hasSomeoneToRecruit)
  740. GH.windows().createAndPushWindow<QuickRecruitmentWindow>(town, pos);
  741. else
  742. CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.townHall.noCreaturesToRecruit"), {});
  743. }
  744. void CCastleBuildings::enterFountain(const BuildingID & building, BuildingSubID::EBuildingSubID subID, BuildingID::EBuildingID upgrades)
  745. {
  746. std::vector<std::shared_ptr<CComponent>> comps(1, std::make_shared<CComponent>(CComponent::building,town->subID,building));
  747. std::string descr = town->town->buildings.find(building)->second->getDescriptionTranslated();
  748. std::string hasNotProduced;
  749. std::string hasProduced;
  750. if(this->town->town->faction->getIndex() == ETownType::RAMPART)
  751. {
  752. hasNotProduced = CGI->generaltexth->allTexts[677];
  753. hasProduced = CGI->generaltexth->allTexts[678];
  754. }
  755. else
  756. {
  757. auto buildingName = town->town->getSpecialBuilding(subID)->getNameTranslated();
  758. hasNotProduced = std::string(CGI->generaltexth->translate("vcmi.townHall.hasNotProduced"));
  759. hasProduced = std::string(CGI->generaltexth->translate("vcmi.townHall.hasProduced"));
  760. boost::algorithm::replace_first(hasNotProduced, "%s", buildingName);
  761. boost::algorithm::replace_first(hasProduced, "%s", buildingName);
  762. }
  763. bool isMysticPondOrItsUpgrade = subID == BuildingSubID::MYSTIC_POND
  764. || (upgrades != BuildingID::NONE
  765. && town->town->buildings.find(BuildingID(upgrades))->second->subId == BuildingSubID::MYSTIC_POND);
  766. if(upgrades != BuildingID::NONE)
  767. descr += "\n\n"+town->town->buildings.find(BuildingID(upgrades))->second->getDescriptionTranslated();
  768. if(isMysticPondOrItsUpgrade) //for vanila Rampart like towns
  769. {
  770. if(town->bonusValue.first == 0) //Mystic Pond produced nothing;
  771. descr += "\n\n" + hasNotProduced;
  772. else //Mystic Pond produced something;
  773. {
  774. descr += "\n\n" + hasProduced;
  775. boost::algorithm::replace_first(descr,"%s",CGI->generaltexth->restypes[town->bonusValue.first]);
  776. boost::algorithm::replace_first(descr,"%d",std::to_string(town->bonusValue.second));
  777. }
  778. }
  779. LOCPLINT->showInfoDialog(descr, comps);
  780. }
  781. void CCastleBuildings::enterMagesGuild()
  782. {
  783. const CGHeroInstance *hero = getHero();
  784. if(hero && !hero->hasSpellbook()) //hero doesn't have spellbok
  785. {
  786. const StartInfo *si = LOCPLINT->cb->getStartInfo();
  787. // it would be nice to find a way to move this hack to config/mapOverrides.json
  788. if(si && si->campState && si->campState && // We're in campaign,
  789. (si->campState->getFilename() == "DATA/YOG.H3C") && // which is "Birth of a Barbarian",
  790. (hero->subID == 45)) // and the hero is Yog (based on Solmyr)
  791. {
  792. // "Yog has given up magic in all its forms..."
  793. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[736]);
  794. }
  795. else if(LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) < 500) //not enough gold to buy spellbook
  796. {
  797. openMagesGuild();
  798. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213]);
  799. }
  800. else
  801. {
  802. CFunctionList<void()> onYes = [this](){ openMagesGuild(); };
  803. CFunctionList<void()> onNo = onYes;
  804. onYes += [hero](){ LOCPLINT->cb->buyArtifact(hero, ArtifactID::SPELLBOOK); };
  805. std::vector<std::shared_ptr<CComponent>> components(1, std::make_shared<CComponent>(CComponent::artifact,ArtifactID::SPELLBOOK,0));
  806. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214], onYes, onNo, components);
  807. }
  808. }
  809. else
  810. {
  811. openMagesGuild();
  812. }
  813. }
  814. void CCastleBuildings::enterTownHall()
  815. {
  816. if(town->visitingHero && town->visitingHero->hasArt(ArtifactID::GRAIL) &&
  817. !vstd::contains(town->builtBuildings, BuildingID::GRAIL)) //hero has grail, but town does not have it
  818. {
  819. if(!vstd::contains(town->forbiddenBuildings, BuildingID::GRAIL))
  820. {
  821. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[597], //Do you wish this to be the permanent home of the Grail?
  822. [&](){ LOCPLINT->cb->buildBuilding(town, BuildingID::GRAIL); },
  823. [&](){ openTownHall(); });
  824. }
  825. else
  826. {
  827. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[673]);
  828. assert(GH.windows().topWindow<CInfoWindow>() != nullptr);
  829. GH.windows().topWindow<CInfoWindow>()->buttons[0]->addCallback(std::bind(&CCastleBuildings::openTownHall, this));
  830. }
  831. }
  832. else
  833. {
  834. openTownHall();
  835. }
  836. }
  837. void CCastleBuildings::openMagesGuild()
  838. {
  839. std::string mageGuildBackground;
  840. mageGuildBackground = LOCPLINT->castleInt->town->town->clientInfo.guildBackground;
  841. GH.windows().createAndPushWindow<CMageGuildScreen>(LOCPLINT->castleInt,mageGuildBackground);
  842. }
  843. void CCastleBuildings::openTownHall()
  844. {
  845. GH.windows().createAndPushWindow<CHallInterface>(town);
  846. }
  847. CCreaInfo::CCreaInfo(Point position, const CGTownInstance * Town, int Level, bool compact, bool _showAvailable):
  848. town(Town),
  849. level(Level),
  850. showAvailable(_showAvailable)
  851. {
  852. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  853. pos += position;
  854. if(town->creatures.size() <= level || town->creatures[level].second.empty())
  855. {
  856. level = -1;
  857. return;//No creature
  858. }
  859. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  860. ui32 creatureID = town->creatures[level].second.back();
  861. creature = CGI->creh->objects[creatureID];
  862. picture = std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, 8, 0);
  863. std::string value;
  864. if(showAvailable)
  865. value = std::to_string(town->creatures[level].first);
  866. else
  867. value = std::string("+") + std::to_string(town->creatureGrowth(level));
  868. if(compact)
  869. {
  870. label = std::make_shared<CLabel>(40, 32, FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, value);
  871. pos.x += 8;
  872. pos.w = 32;
  873. pos.h = 32;
  874. }
  875. else
  876. {
  877. label = std::make_shared<CLabel>(24, 40, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, value);
  878. pos.w = 48;
  879. pos.h = 48;
  880. }
  881. }
  882. void CCreaInfo::update()
  883. {
  884. if(label)
  885. {
  886. std::string value;
  887. if(showAvailable)
  888. value = std::to_string(town->creatures[level].first);
  889. else
  890. value = std::string("+") + std::to_string(town->creatureGrowth(level));
  891. if(value != label->getText())
  892. label->setText(value);
  893. }
  894. }
  895. void CCreaInfo::hover(bool on)
  896. {
  897. std::string message = CGI->generaltexth->allTexts[588];
  898. boost::algorithm::replace_first(message, "%s", creature->getNamePluralTranslated());
  899. if(on)
  900. {
  901. GH.statusbar()->write(message);
  902. }
  903. else
  904. {
  905. GH.statusbar()->clearIfMatching(message);
  906. }
  907. }
  908. void CCreaInfo::clickPressed(const Point & cursorPosition)
  909. {
  910. int offset = LOCPLINT->castleInt? (-87) : 0;
  911. auto recruitCb = [=](CreatureID id, int count)
  912. {
  913. LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, level);
  914. };
  915. GH.windows().createAndPushWindow<CRecruitmentWindow>(town, level, town, recruitCb, offset);
  916. }
  917. std::string CCreaInfo::genGrowthText()
  918. {
  919. GrowthInfo gi = town->getGrowthInfo(level);
  920. std::string descr = boost::str(boost::format(CGI->generaltexth->allTexts[589]) % creature->getNameSingularTranslated() % gi.totalGrowth());
  921. for(const GrowthInfo::Entry & entry : gi.entries)
  922. descr +="\n" + entry.description;
  923. return descr;
  924. }
  925. void CCreaInfo::showPopupWindow(const Point & cursorPosition)
  926. {
  927. if (showAvailable)
  928. GH.windows().createAndPushWindow<CDwellingInfoBox>(GH.screenDimensions().x / 2, GH.screenDimensions().y / 2, town, level);
  929. else
  930. CRClickPopup::createAndPush(genGrowthText(), std::make_shared<CComponent>(CComponent::creature, creature->getId()));
  931. }
  932. bool CCreaInfo::getShowAvailable()
  933. {
  934. return showAvailable;
  935. }
  936. CTownInfo::CTownInfo(int posX, int posY, const CGTownInstance * Town, bool townHall)
  937. : town(Town),
  938. building(nullptr)
  939. {
  940. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  941. addUsedEvents(SHOW_POPUP | HOVER);
  942. pos.x += posX;
  943. pos.y += posY;
  944. int buildID;
  945. if(townHall)
  946. {
  947. buildID = 10 + town->hallLevel();
  948. picture = std::make_shared<CAnimImage>("ITMTL.DEF", town->hallLevel());
  949. }
  950. else
  951. {
  952. buildID = 6 + town->fortLevel();
  953. if(buildID == 6)
  954. return;//FIXME: suspicious statement, fix or comment
  955. picture = std::make_shared<CAnimImage>("ITMCL.DEF", town->fortLevel()-1);
  956. }
  957. building = town->town->buildings.at(BuildingID(buildID));
  958. pos = picture->pos;
  959. }
  960. void CTownInfo::hover(bool on)
  961. {
  962. if(on)
  963. {
  964. if(building )
  965. GH.statusbar()->write(building->getNameTranslated());
  966. }
  967. else
  968. {
  969. GH.statusbar()->clear();
  970. }
  971. }
  972. void CTownInfo::showPopupWindow(const Point & cursorPosition)
  973. {
  974. if(building)
  975. {
  976. auto c = std::make_shared<CComponent>(CComponent::building, building->town->faction->getIndex(), building->bid);
  977. CRClickPopup::createAndPush(CInfoWindow::genText(building->getNameTranslated(), building->getDescriptionTranslated()), c);
  978. }
  979. }
  980. CCastleInterface::CCastleInterface(const CGTownInstance * Town, const CGTownInstance * from):
  981. CStatusbarWindow(PLAYER_COLORED | BORDERED),
  982. town(Town)
  983. {
  984. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  985. LOCPLINT->castleInt = this;
  986. addUsedEvents(KEYBOARD);
  987. builds = std::make_shared<CCastleBuildings>(town);
  988. panel = std::make_shared<CPicture>("TOWNSCRN", 0, builds->pos.h);
  989. panel->colorize(LOCPLINT->playerID);
  990. pos.w = panel->pos.w;
  991. pos.h = builds->pos.h + panel->pos.h;
  992. center();
  993. updateShadow();
  994. garr = std::make_shared<CGarrisonInt>(Point(305, 387), 4, Point(0,96), town->getUpperArmy(), town->visitingHero);
  995. garr->setRedrawParent(true);
  996. heroes = std::make_shared<HeroSlots>(town, Point(241, 387), Point(241, 483), garr, true);
  997. title = std::make_shared<CLabel>(85, 387, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, town->getNameTranslated());
  998. income = std::make_shared<CLabel>(195, 443, FONT_SMALL, ETextAlignment::CENTER);
  999. icon = std::make_shared<CAnimImage>("ITPT", 0, 0, 15, 387);
  1000. exit = std::make_shared<CButton>(Point(744, 544), "TSBTNS", CButton::tooltip(CGI->generaltexth->tcommands[8]), [&](){close();}, EShortcut::GLOBAL_RETURN);
  1001. exit->setImageOrder(4, 5, 6, 7);
  1002. auto split = std::make_shared<CButton>(Point(744, 382), "TSBTNS", CButton::tooltip(CGI->generaltexth->tcommands[3]), [&]()
  1003. {
  1004. garr->splitClick();
  1005. heroes->splitClicked();
  1006. });
  1007. garr->addSplitBtn(split);
  1008. Rect barRect(9, 182, 732, 18);
  1009. auto statusbarBackground = std::make_shared<CPicture>(panel->getSurface(), barRect, 9, 555);
  1010. statusbar = CGStatusBar::create(statusbarBackground);
  1011. resdatabar = std::make_shared<CResDataBar>("ARESBAR", 3, 575, 37, 3, 84, 78);
  1012. townlist = std::make_shared<CTownList>(3, Rect(Point(743, 414), Point(48, 128)), Point(1,16), Point(0, 32), LOCPLINT->localState->getOwnedTowns().size() );
  1013. townlist->setScrollUpButton( std::make_shared<CButton>( Point(744, 414), "IAM014", CButton::tooltipLocalized("core.help.306")));
  1014. townlist->setScrollDownButton( std::make_shared<CButton>( Point(744, 526), "IAM015", CButton::tooltipLocalized("core.help.307")));
  1015. if(from)
  1016. townlist->select(from);
  1017. townlist->select(town); //this will scroll list to select current town
  1018. townlist->onSelect = std::bind(&CCastleInterface::townChange, this);
  1019. recreateIcons();
  1020. if (!from)
  1021. adventureInt->onAudioPaused();
  1022. CCS->musich->playMusic(town->town->clientInfo.musicTheme, true, false);
  1023. }
  1024. CCastleInterface::~CCastleInterface()
  1025. {
  1026. // resume map audio if:
  1027. // adventureInt exists (may happen on exiting client with open castle interface)
  1028. // castleInt has not been replaced (happens on switching between towns inside castle interface)
  1029. if (adventureInt && LOCPLINT->castleInt == this)
  1030. adventureInt->onAudioResumed();
  1031. if(LOCPLINT->castleInt == this)
  1032. LOCPLINT->castleInt = nullptr;
  1033. }
  1034. void CCastleInterface::updateGarrisons()
  1035. {
  1036. garr->recreateSlots();
  1037. }
  1038. void CCastleInterface::close()
  1039. {
  1040. if(town->tempOwner == LOCPLINT->playerID) //we may have opened window for an allied town
  1041. {
  1042. if(town->visitingHero && town->visitingHero->tempOwner == LOCPLINT->playerID)
  1043. LOCPLINT->localState->setSelection(town->visitingHero);
  1044. else
  1045. LOCPLINT->localState->setSelection(town);
  1046. }
  1047. CWindowObject::close();
  1048. }
  1049. void CCastleInterface::castleTeleport(int where)
  1050. {
  1051. const CGTownInstance * dest = LOCPLINT->cb->getTown(ObjectInstanceID(where));
  1052. LOCPLINT->localState->setSelection(town->visitingHero);//according to assert(ho == adventureInt->selection) in the eraseCurrentPathOf
  1053. LOCPLINT->cb->teleportHero(town->visitingHero, dest);
  1054. LOCPLINT->localState->erasePath(town->visitingHero);
  1055. }
  1056. void CCastleInterface::townChange()
  1057. {
  1058. //TODO: do not recreate window
  1059. const CGTownInstance * dest = LOCPLINT->localState->getOwnedTown(townlist->getSelectedIndex());
  1060. const CGTownInstance * town = this->town;// "this" is going to be deleted
  1061. if ( dest == town )
  1062. return;
  1063. close();
  1064. GH.windows().createAndPushWindow<CCastleInterface>(dest, town);
  1065. }
  1066. void CCastleInterface::addBuilding(BuildingID bid)
  1067. {
  1068. deactivate();
  1069. builds->addBuilding(bid);
  1070. recreateIcons();
  1071. activate();
  1072. redraw();
  1073. }
  1074. void CCastleInterface::removeBuilding(BuildingID bid)
  1075. {
  1076. deactivate();
  1077. builds->removeBuilding(bid);
  1078. recreateIcons();
  1079. activate();
  1080. redraw();
  1081. }
  1082. void CCastleInterface::recreateIcons()
  1083. {
  1084. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  1085. size_t iconIndex = town->town->clientInfo.icons[town->hasFort()][town->builded >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  1086. icon->setFrame(iconIndex);
  1087. TResources townIncome = town->dailyIncome();
  1088. income->setText(std::to_string(townIncome[EGameResID::GOLD]));
  1089. hall = std::make_shared<CTownInfo>(80, 413, town, true);
  1090. fort = std::make_shared<CTownInfo>(122, 413, town, false);
  1091. fastArmyPurchase = std::make_shared<CButton>(Point(122, 413), "itmcl.def", CButton::tooltip(), [&](){ builds->enterToTheQuickRecruitmentWindow(); });
  1092. fastArmyPurchase->setImageOrder(town->fortLevel() - 1, town->fortLevel() - 1, town->fortLevel() - 1, town->fortLevel() - 1);
  1093. fastArmyPurchase->setAnimateLonelyFrame(true);
  1094. creainfo.clear();
  1095. bool compactCreatureInfo = useCompactCreatureBox();
  1096. bool useAvailableCreaturesForLabel = useAvailableAmountAsCreatureLabel();
  1097. for(size_t i=0; i<4; i++)
  1098. creainfo.push_back(std::make_shared<CCreaInfo>(Point(14 + 55 * (int)i, 459), town, (int)i, compactCreatureInfo, useAvailableCreaturesForLabel));
  1099. for(size_t i=0; i<4; i++)
  1100. creainfo.push_back(std::make_shared<CCreaInfo>(Point(14 + 55 * (int)i, 507), town, (int)i + 4, compactCreatureInfo, useAvailableCreaturesForLabel));
  1101. }
  1102. void CCastleInterface::keyPressed(EShortcut key)
  1103. {
  1104. switch(key)
  1105. {
  1106. case EShortcut::MOVE_UP:
  1107. townlist->selectPrev();
  1108. break;
  1109. case EShortcut::MOVE_DOWN:
  1110. townlist->selectNext();
  1111. break;
  1112. case EShortcut::TOWN_SWAP_ARMIES:
  1113. heroes->swapArmies();
  1114. break;
  1115. case EShortcut::TOWN_OPEN_TAVERN:
  1116. if(town->hasBuilt(BuildingID::TAVERN))
  1117. LOCPLINT->showTavernWindow(town);
  1118. break;
  1119. default:
  1120. break;
  1121. }
  1122. }
  1123. void CCastleInterface::creaturesChangedEventHandler()
  1124. {
  1125. for(auto creatureInfoBox : creainfo)
  1126. {
  1127. if(creatureInfoBox->getShowAvailable())
  1128. {
  1129. creatureInfoBox->update();
  1130. }
  1131. }
  1132. }
  1133. CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance * Town, const CBuilding * Building):
  1134. town(Town),
  1135. building(Building)
  1136. {
  1137. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1138. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  1139. pos.x += x;
  1140. pos.y += y;
  1141. pos.w = 154;
  1142. pos.h = 92;
  1143. state = LOCPLINT->cb->canBuildStructure(town, building->bid);
  1144. static int panelIndex[12] =
  1145. {
  1146. 3, 3, 3, 0, 0, 2, 2, 1, 2, 2, 3, 3
  1147. };
  1148. static int iconIndex[12] =
  1149. {
  1150. -1, -1, -1, 0, 0, 1, 2, -1, 1, 1, -1, -1
  1151. };
  1152. icon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, building->bid, 0, 2, 2);
  1153. header = std::make_shared<CAnimImage>("TPTHBAR", panelIndex[state], 0, 1, 73);
  1154. if(iconIndex[state] >=0)
  1155. mark = std::make_shared<CAnimImage>("TPTHCHK", iconIndex[state], 0, 136, 56);
  1156. name = std::make_shared<CLabel>(75, 81, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, building->getNameTranslated());
  1157. //todo: add support for all possible states
  1158. if(state >= EBuildingState::BUILDING_ERROR)
  1159. state = EBuildingState::FORBIDDEN;
  1160. }
  1161. void CHallInterface::CBuildingBox::hover(bool on)
  1162. {
  1163. if(on)
  1164. {
  1165. std::string toPrint;
  1166. if(state==EBuildingState::PREREQUIRES || state == EBuildingState::MISSING_BASE)
  1167. toPrint = CGI->generaltexth->hcommands[5];
  1168. else if(state==EBuildingState::CANT_BUILD_TODAY)
  1169. toPrint = CGI->generaltexth->allTexts[223];
  1170. else
  1171. toPrint = CGI->generaltexth->hcommands[state];
  1172. boost::algorithm::replace_first(toPrint,"%s",building->getNameTranslated());
  1173. GH.statusbar()->write(toPrint);
  1174. }
  1175. else
  1176. {
  1177. GH.statusbar()->clear();
  1178. }
  1179. }
  1180. void CHallInterface::CBuildingBox::clickPressed(const Point & cursorPosition)
  1181. {
  1182. GH.windows().createAndPushWindow<CBuildWindow>(town,building,state,0);
  1183. }
  1184. void CHallInterface::CBuildingBox::showPopupWindow(const Point & cursorPosition)
  1185. {
  1186. GH.windows().createAndPushWindow<CBuildWindow>(town,building,state,1);
  1187. }
  1188. CHallInterface::CHallInterface(const CGTownInstance * Town):
  1189. CStatusbarWindow(PLAYER_COLORED | BORDERED, Town->town->clientInfo.hallBackground),
  1190. town(Town)
  1191. {
  1192. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1193. resdatabar = std::make_shared<CMinorResDataBar>();
  1194. resdatabar->moveBy(pos.topLeft(), true);
  1195. Rect barRect(5, 556, 740, 18);
  1196. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 5, 556);
  1197. statusbar = CGStatusBar::create(statusbarBackground);
  1198. title = std::make_shared<CLabel>(399, 12, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, town->town->buildings.at(BuildingID(town->hallLevel()+BuildingID::VILLAGE_HALL))->getNameTranslated());
  1199. exit = std::make_shared<CButton>(Point(748, 556), "TPMAGE1.DEF", CButton::tooltip(CGI->generaltexth->hcommands[8]), [&](){close();}, EShortcut::GLOBAL_RETURN);
  1200. auto & boxList = town->town->clientInfo.hallSlots;
  1201. boxes.resize(boxList.size());
  1202. for(size_t row=0; row<boxList.size(); row++) //for each row
  1203. {
  1204. for(size_t col=0; col<boxList[row].size(); col++) //for each box
  1205. {
  1206. const CBuilding * building = nullptr;
  1207. for(auto & buildingID : boxList[row][col])//we are looking for the first not built structure
  1208. {
  1209. const CBuilding * current = town->town->buildings.at(buildingID);
  1210. if(vstd::contains(town->builtBuildings, buildingID))
  1211. {
  1212. building = current;
  1213. }
  1214. else
  1215. {
  1216. if(current->mode == CBuilding::BUILD_NORMAL)
  1217. {
  1218. building = current;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. int posX = pos.w/2 - (int)boxList[row].size()*154/2 - ((int)boxList[row].size()-1)*20 + 194*(int)col,
  1224. posY = 35 + 104*(int)row;
  1225. if(building)
  1226. boxes[row].push_back(std::make_shared<CBuildingBox>(posX, posY, town, building));
  1227. }
  1228. }
  1229. }
  1230. CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, int state, bool rightClick):
  1231. CStatusbarWindow(PLAYER_COLORED | (rightClick ? RCLICK_POPUP : 0), "TPUBUILD"),
  1232. town(Town),
  1233. building(Building)
  1234. {
  1235. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1236. icon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, building->bid, 0, 125, 50);
  1237. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26);
  1238. statusbar = CGStatusBar::create(statusbarBackground);
  1239. name = std::make_shared<CLabel>(197, 30, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->hcommands[7]) % building->getNameTranslated()));
  1240. description = std::make_shared<CTextBox>(building->getDescriptionTranslated(), Rect(33, 135, 329, 67), 0, FONT_MEDIUM, ETextAlignment::CENTER);
  1241. stateText = std::make_shared<CTextBox>(getTextForState(state), Rect(33, 216, 329, 67), 0, FONT_SMALL, ETextAlignment::CENTER);
  1242. //Create components for all required resources
  1243. std::vector<std::shared_ptr<CComponent>> components;
  1244. for(int i = 0; i<GameConstants::RESOURCE_QUANTITY; i++)
  1245. {
  1246. if(building->resources[i])
  1247. components.push_back(std::make_shared<CComponent>(CComponent::resource, i, building->resources[i], CComponent::small));
  1248. }
  1249. cost = std::make_shared<CComponentBox>(components, Rect(25, 300, pos.w - 50, 130));
  1250. if(!rightClick)
  1251. { //normal window
  1252. std::string tooltipYes = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % building->getNameTranslated());
  1253. std::string tooltipNo = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % building->getNameTranslated());
  1254. buy = std::make_shared<CButton>(Point(45, 446), "IBUY30", CButton::tooltip(tooltipYes), [&](){ buyFunc(); }, EShortcut::GLOBAL_ACCEPT);
  1255. buy->setBorderColor(Colors::METALLIC_GOLD);
  1256. buy->block(state!=7 || LOCPLINT->playerID != town->tempOwner);
  1257. cancel = std::make_shared<CButton>(Point(290, 445), "ICANCEL", CButton::tooltip(tooltipNo), [&](){ close();}, EShortcut::GLOBAL_CANCEL);
  1258. cancel->setBorderColor(Colors::METALLIC_GOLD);
  1259. }
  1260. }
  1261. void CBuildWindow::buyFunc()
  1262. {
  1263. LOCPLINT->cb->buildBuilding(town,building->bid);
  1264. GH.windows().popWindows(2); //we - build window and hall screen
  1265. }
  1266. std::string CBuildWindow::getTextForState(int state)
  1267. {
  1268. std::string ret;
  1269. if(state < EBuildingState::ALLOWED)
  1270. ret = CGI->generaltexth->hcommands[state];
  1271. switch (state)
  1272. {
  1273. case EBuildingState::ALREADY_PRESENT:
  1274. case EBuildingState::CANT_BUILD_TODAY:
  1275. case EBuildingState::NO_RESOURCES:
  1276. ret.replace(ret.find_first_of("%s"), 2, building->getNameTranslated());
  1277. break;
  1278. case EBuildingState::ALLOWED:
  1279. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1280. case EBuildingState::PREREQUIRES:
  1281. {
  1282. auto toStr = [&](const BuildingID build) -> std::string
  1283. {
  1284. return town->town->buildings.at(build)->getNameTranslated();
  1285. };
  1286. ret = CGI->generaltexth->allTexts[52];
  1287. ret += "\n" + town->genBuildingRequirements(building->bid).toString(toStr);
  1288. break;
  1289. }
  1290. case EBuildingState::MISSING_BASE:
  1291. {
  1292. std::string msg = CGI->generaltexth->translate("vcmi.townHall.missingBase");
  1293. ret = boost::str(boost::format(msg) % town->town->buildings.at(building->upgrade)->getNameTranslated());
  1294. break;
  1295. }
  1296. }
  1297. return ret;
  1298. }
  1299. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int min, int max)
  1300. {
  1301. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1302. pos.x+=size.x;
  1303. pos.y+=size.y;
  1304. pos.w = size.w;
  1305. pos.h = size.h;
  1306. init(name, descr, min, max);
  1307. }
  1308. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int val)
  1309. {
  1310. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1311. pos.x+=size.x;
  1312. pos.y+=size.y;
  1313. pos.w = size.w;
  1314. pos.h = size.h;
  1315. init(name, descr, val, val);
  1316. }
  1317. void LabeledValue::init(std::string nameText, std::string descr, int min, int max)
  1318. {
  1319. addUsedEvents(HOVER);
  1320. hoverText = descr;
  1321. std::string valueText;
  1322. if(min && max)
  1323. {
  1324. valueText = std::to_string(min);
  1325. if(min != max)
  1326. valueText += '-' + std::to_string(max);
  1327. }
  1328. name = std::make_shared<CLabel>(3, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, nameText);
  1329. value = std::make_shared<CLabel>(pos.w-3, pos.h-2, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, valueText);
  1330. }
  1331. void LabeledValue::hover(bool on)
  1332. {
  1333. if(on)
  1334. {
  1335. GH.statusbar()->write(hoverText);
  1336. }
  1337. else
  1338. {
  1339. GH.statusbar()->clear();
  1340. }
  1341. }
  1342. CFortScreen::CFortScreen(const CGTownInstance * town):
  1343. CStatusbarWindow(PLAYER_COLORED | BORDERED, getBgName(town))
  1344. {
  1345. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1346. ui32 fortSize = static_cast<ui32>(town->creatures.size());
  1347. if(fortSize > GameConstants::CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1348. fortSize--;
  1349. const CBuilding * fortBuilding = town->town->buildings.at(BuildingID(town->fortLevel()+6));
  1350. title = std::make_shared<CLabel>(400, 12, FONT_BIG, ETextAlignment::CENTER, Colors::WHITE, fortBuilding->getNameTranslated());
  1351. std::string text = boost::str(boost::format(CGI->generaltexth->fcommands[6]) % fortBuilding->getNameTranslated());
  1352. exit = std::make_shared<CButton>(Point(748, 556), "TPMAGE1", CButton::tooltip(text), [&](){ close(); }, EShortcut::GLOBAL_RETURN);
  1353. std::vector<Point> positions =
  1354. {
  1355. Point(10, 22), Point(404, 22),
  1356. Point(10, 155), Point(404,155),
  1357. Point(10, 288), Point(404,288)
  1358. };
  1359. if(fortSize == GameConstants::CREATURES_PER_TOWN)
  1360. {
  1361. positions.push_back(Point(206,421));
  1362. }
  1363. else
  1364. {
  1365. positions.push_back(Point(10, 421));
  1366. positions.push_back(Point(404,421));
  1367. }
  1368. for(ui32 i=0; i<fortSize; i++)
  1369. {
  1370. BuildingID buildingID;
  1371. if(fortSize == GameConstants::CREATURES_PER_TOWN)
  1372. {
  1373. if(vstd::contains(town->builtBuildings, BuildingID::DWELL_UP_FIRST+i))
  1374. buildingID = BuildingID(BuildingID::DWELL_UP_FIRST+i);
  1375. else
  1376. buildingID = BuildingID(BuildingID::DWELL_FIRST+i);
  1377. }
  1378. else
  1379. {
  1380. buildingID = BuildingID::SPECIAL_3;
  1381. }
  1382. recAreas.push_back(std::make_shared<RecruitArea>(positions[i].x, positions[i].y, town, i));
  1383. }
  1384. resdatabar = std::make_shared<CMinorResDataBar>();
  1385. resdatabar->moveBy(pos.topLeft(), true);
  1386. Rect barRect(4, 554, 740, 18);
  1387. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 4, 554);
  1388. statusbar = CGStatusBar::create(statusbarBackground);
  1389. }
  1390. std::string CFortScreen::getBgName(const CGTownInstance * town)
  1391. {
  1392. ui32 fortSize = static_cast<ui32>(town->creatures.size());
  1393. if(fortSize > GameConstants::CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1394. fortSize--;
  1395. if(fortSize == GameConstants::CREATURES_PER_TOWN)
  1396. return "TPCASTL7";
  1397. else
  1398. return "TPCASTL8";
  1399. }
  1400. void CFortScreen::creaturesChangedEventHandler()
  1401. {
  1402. for(auto & elem : recAreas)
  1403. elem->creaturesChangedEventHandler();
  1404. LOCPLINT->castleInt->creaturesChangedEventHandler();
  1405. }
  1406. CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance * Town, int Level):
  1407. town(Town),
  1408. level(Level),
  1409. availableCount(nullptr)
  1410. {
  1411. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1412. pos.x +=posX;
  1413. pos.y +=posY;
  1414. pos.w = 386;
  1415. pos.h = 126;
  1416. if(!town->creatures[level].second.empty())
  1417. addUsedEvents(LCLICK | HOVER);//Activate only if dwelling is present
  1418. icons = std::make_shared<CPicture>("TPCAINFO", 261, 3);
  1419. if(getMyBuilding() != nullptr)
  1420. {
  1421. buildingIcon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, getMyBuilding()->bid, 0, 4, 21);
  1422. buildingName = std::make_shared<CLabel>(78, 101, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyBuilding()->getNameTranslated());
  1423. if(vstd::contains(town->builtBuildings, getMyBuilding()->bid))
  1424. {
  1425. ui32 available = town->creatures[level].first;
  1426. std::string availableText = CGI->generaltexth->allTexts[217]+ std::to_string(available);
  1427. availableCount = std::make_shared<CLabel>(78, 119, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, availableText);
  1428. }
  1429. }
  1430. if(getMyCreature() != nullptr)
  1431. {
  1432. hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % getMyCreature()->getNamePluralTranslated());
  1433. new CCreaturePic(159, 4, getMyCreature(), false);
  1434. new CLabel(78, 11, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyCreature()->getNamePluralTranslated());
  1435. Rect sizes(287, 4, 96, 18);
  1436. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], getMyCreature()->getAttack(false)));
  1437. sizes.y+=20;
  1438. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[191], CGI->generaltexth->fcommands[1], getMyCreature()->getDefense(false)));
  1439. sizes.y+=21;
  1440. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[199], CGI->generaltexth->fcommands[2], getMyCreature()->getMinDamage(false), getMyCreature()->getMaxDamage(false)));
  1441. sizes.y+=20;
  1442. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[388], CGI->generaltexth->fcommands[3], getMyCreature()->getMaxHealth()));
  1443. sizes.y+=21;
  1444. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[193], CGI->generaltexth->fcommands[4], getMyCreature()->valOfBonuses(BonusType::STACKS_SPEED)));
  1445. sizes.y+=20;
  1446. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[194], CGI->generaltexth->fcommands[5], town->creatureGrowth(level)));
  1447. }
  1448. }
  1449. const CCreature * CFortScreen::RecruitArea::getMyCreature()
  1450. {
  1451. if(!town->creatures.at(level).second.empty()) // built
  1452. return VLC->creh->objects[town->creatures.at(level).second.back()];
  1453. if(!town->town->creatures.at(level).empty()) // there are creatures on this level
  1454. return VLC->creh->objects[town->town->creatures.at(level).front()];
  1455. return nullptr;
  1456. }
  1457. const CBuilding * CFortScreen::RecruitArea::getMyBuilding()
  1458. {
  1459. BuildingID myID = BuildingID(BuildingID::DWELL_FIRST).advance(level);
  1460. if (level == GameConstants::CREATURES_PER_TOWN)
  1461. return town->town->getSpecialBuilding(BuildingSubID::PORTAL_OF_SUMMONING);
  1462. if (!town->town->buildings.count(myID))
  1463. return nullptr;
  1464. const CBuilding * build = town->town->buildings.at(myID);
  1465. while (town->town->buildings.count(myID))
  1466. {
  1467. if (town->hasBuilt(myID))
  1468. build = town->town->buildings.at(myID);
  1469. myID.advance(GameConstants::CREATURES_PER_TOWN);
  1470. }
  1471. return build;
  1472. }
  1473. void CFortScreen::RecruitArea::hover(bool on)
  1474. {
  1475. if(on)
  1476. GH.statusbar()->write(hoverText);
  1477. else
  1478. GH.statusbar()->clear();
  1479. }
  1480. void CFortScreen::RecruitArea::creaturesChangedEventHandler()
  1481. {
  1482. if(availableCount)
  1483. {
  1484. std::string availableText = CGI->generaltexth->allTexts[217] + std::to_string(town->creatures[level].first);
  1485. availableCount->setText(availableText);
  1486. }
  1487. }
  1488. void CFortScreen::RecruitArea::clickPressed(const Point & cursorPosition)
  1489. {
  1490. LOCPLINT->castleInt->builds->enterDwelling(level);
  1491. }
  1492. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner,std::string imagem)
  1493. : CStatusbarWindow(BORDERED, imagem)
  1494. {
  1495. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1496. window = std::make_shared<CPicture>(owner->town->town->clientInfo.guildWindow, 332, 76);
  1497. resdatabar = std::make_shared<CMinorResDataBar>();
  1498. resdatabar->moveBy(pos.topLeft(), true);
  1499. Rect barRect(7, 556, 737, 18);
  1500. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 7, 556);
  1501. statusbar = CGStatusBar::create(statusbarBackground);
  1502. exit = std::make_shared<CButton>(Point(748, 556), "TPMAGE1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[593]), [&](){ close(); }, EShortcut::GLOBAL_RETURN);
  1503. static const std::vector<std::vector<Point> > positions =
  1504. {
  1505. {Point(222,445), Point(312,445), Point(402,445), Point(520,445), Point(610,445), Point(700,445)},
  1506. {Point(48,53), Point(48,147), Point(48,241), Point(48,335), Point(48,429)},
  1507. {Point(570,82), Point(672,82), Point(570,157), Point(672,157)},
  1508. {Point(183,42), Point(183,148), Point(183,253)},
  1509. {Point(491,325), Point(591,325)}
  1510. };
  1511. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1512. {
  1513. size_t spellCount = owner->town->spellsAtLevel((int)i+1,false); //spell at level with -1 hmmm?
  1514. for(size_t j=0; j<spellCount; j++)
  1515. {
  1516. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1517. spells.push_back(std::make_shared<Scroll>(positions[i][j], CGI->spellh->objects[owner->town->spells[i][j]]));
  1518. else
  1519. emptyScrolls.push_back(std::make_shared<CAnimImage>("TPMAGES.DEF", 1, 0, positions[i][j].x, positions[i][j].y));
  1520. }
  1521. }
  1522. }
  1523. CMageGuildScreen::Scroll::Scroll(Point position, const CSpell *Spell)
  1524. : spell(Spell)
  1525. {
  1526. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1527. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  1528. pos += position;
  1529. image = std::make_shared<CAnimImage>("SPELLSCR", spell->id);
  1530. pos = image->pos;
  1531. }
  1532. void CMageGuildScreen::Scroll::clickPressed(const Point & cursorPosition)
  1533. {
  1534. LOCPLINT->showInfoDialog(spell->getDescriptionTranslated(0), std::make_shared<CComponent>(CComponent::spell, spell->id));
  1535. }
  1536. void CMageGuildScreen::Scroll::showPopupWindow(const Point & cursorPosition)
  1537. {
  1538. CRClickPopup::createAndPush(spell->getDescriptionTranslated(0), std::make_shared<CComponent>(CComponent::spell, spell->id));
  1539. }
  1540. void CMageGuildScreen::Scroll::hover(bool on)
  1541. {
  1542. if(on)
  1543. GH.statusbar()->write(spell->getNameTranslated());
  1544. else
  1545. GH.statusbar()->clear();
  1546. }
  1547. CBlacksmithDialog::CBlacksmithDialog(bool possible, CreatureID creMachineID, ArtifactID aid, ObjectInstanceID hid):
  1548. CStatusbarWindow(PLAYER_COLORED, "TPSMITH")
  1549. {
  1550. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1551. Rect barRect(8, pos.h - 26, pos.w - 16, 19);
  1552. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 8, pos.h - 26);
  1553. statusbar = CGStatusBar::create(statusbarBackground);
  1554. animBG = std::make_shared<CPicture>("TPSMITBK", 64, 50);
  1555. animBG->needRefresh = true;
  1556. const CCreature * creature = CGI->creh->objects[creMachineID];
  1557. anim = std::make_shared<CCreatureAnim>(64, 50, creature->animDefName);
  1558. anim->clipRect(113,125,200,150);
  1559. title = std::make_shared<CLabel>(165, 28, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW,
  1560. boost::str(boost::format(CGI->generaltexth->allTexts[274]) % creature->getNameSingularTranslated()));
  1561. costText = std::make_shared<CLabel>(165, 218, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[43]);
  1562. costValue = std::make_shared<CLabel>(165, 292, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE,
  1563. std::to_string(aid.toArtifact(CGI->artifacts())->getPrice()));
  1564. std::string text = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % creature->getNameSingularTranslated());
  1565. buy = std::make_shared<CButton>(Point(42, 312), "IBUY30.DEF", CButton::tooltip(text), [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  1566. text = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % creature->getNameSingularTranslated());
  1567. cancel = std::make_shared<CButton>(Point(224, 312), "ICANCEL.DEF", CButton::tooltip(text), [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
  1568. if(possible)
  1569. buy->addCallback([=](){ LOCPLINT->cb->buyArtifact(LOCPLINT->cb->getHero(hid),aid); });
  1570. else
  1571. buy->block(true);
  1572. costIcon = std::make_shared<CAnimImage>("RESOURCE", GameResID(EGameResID::GOLD), 0, 148, 244);
  1573. }