CCastleInterface.cpp 59 KB

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