CCastleInterface.cpp 57 KB

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