CCastleInterface.cpp 59 KB

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