CCastleInterface.cpp 57 KB

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