CCastleInterface.cpp 59 KB

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