CCastleInterface.cpp 57 KB

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