CCastleInterface.cpp 69 KB

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