CCastleInterface.cpp 58 KB

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