CCastleInterface.cpp 69 KB

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