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