CCastleInterface.cpp 59 KB

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