CCastleInterface.cpp 59 KB

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