CCastleInterface.cpp 59 KB

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