CCastleInterface.cpp 60 KB

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