CCastleInterface.cpp 59 KB

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