CCastleInterface.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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. 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 Point & cursorPosition)
  214. {
  215. if(area && pos.isInside(cursorPosition.x, cursorPosition.y))
  216. {
  217. if(area->isTransparent(cursorPosition - 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>(GH.screenDimensions().x / 2, GH.screenDimensions().y / 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_Keycode & key)
  1083. {
  1084. switch(key)
  1085. {
  1086. case SDLK_UP:
  1087. townlist->selectPrev();
  1088. break;
  1089. case SDLK_DOWN:
  1090. townlist->selectNext();
  1091. break;
  1092. case SDLK_SPACE:
  1093. heroes->swapArmies();
  1094. break;
  1095. case SDLK_t:
  1096. if(town->hasBuilt(BuildingID::TAVERN))
  1097. LOCPLINT->showTavernWindow(town);
  1098. break;
  1099. default:
  1100. break;
  1101. }
  1102. }
  1103. CHallInterface::CBuildingBox::CBuildingBox(int x, int y, const CGTownInstance * Town, const CBuilding * Building):
  1104. town(Town),
  1105. building(Building)
  1106. {
  1107. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1108. addUsedEvents(LCLICK | RCLICK | HOVER);
  1109. pos.x += x;
  1110. pos.y += y;
  1111. pos.w = 154;
  1112. pos.h = 92;
  1113. state = LOCPLINT->cb->canBuildStructure(town, building->bid);
  1114. static int panelIndex[12] =
  1115. {
  1116. 3, 3, 3, 0, 0, 2, 2, 1, 2, 2, 3, 3
  1117. };
  1118. static int iconIndex[12] =
  1119. {
  1120. -1, -1, -1, 0, 0, 1, 2, -1, 1, 1, -1, -1
  1121. };
  1122. icon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, building->bid, 0, 2, 2);
  1123. header = std::make_shared<CAnimImage>("TPTHBAR", panelIndex[state], 0, 1, 73);
  1124. if(iconIndex[state] >=0)
  1125. mark = std::make_shared<CAnimImage>("TPTHCHK", iconIndex[state], 0, 136, 56);
  1126. name = std::make_shared<CLabel>(75, 81, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, building->getNameTranslated());
  1127. //todo: add support for all possible states
  1128. if(state >= EBuildingState::BUILDING_ERROR)
  1129. state = EBuildingState::FORBIDDEN;
  1130. }
  1131. void CHallInterface::CBuildingBox::hover(bool on)
  1132. {
  1133. if(on)
  1134. {
  1135. std::string toPrint;
  1136. if(state==EBuildingState::PREREQUIRES || state == EBuildingState::MISSING_BASE)
  1137. toPrint = CGI->generaltexth->hcommands[5];
  1138. else if(state==EBuildingState::CANT_BUILD_TODAY)
  1139. toPrint = CGI->generaltexth->allTexts[223];
  1140. else
  1141. toPrint = CGI->generaltexth->hcommands[state];
  1142. boost::algorithm::replace_first(toPrint,"%s",building->getNameTranslated());
  1143. GH.statusbar->write(toPrint);
  1144. }
  1145. else
  1146. {
  1147. GH.statusbar->clear();
  1148. }
  1149. }
  1150. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1151. {
  1152. if(previousState && (!down))
  1153. GH.pushIntT<CBuildWindow>(town,building,state,0);
  1154. }
  1155. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1156. {
  1157. if(down)
  1158. GH.pushIntT<CBuildWindow>(town,building,state,1);
  1159. }
  1160. CHallInterface::CHallInterface(const CGTownInstance * Town):
  1161. CStatusbarWindow(PLAYER_COLORED | BORDERED, Town->town->clientInfo.hallBackground),
  1162. town(Town)
  1163. {
  1164. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1165. resdatabar = std::make_shared<CMinorResDataBar>();
  1166. resdatabar->moveBy(pos.topLeft(), true);
  1167. Rect barRect(5, 556, 740, 18);
  1168. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 5, 556);
  1169. statusbar = CGStatusBar::create(statusbarBackground);
  1170. title = std::make_shared<CLabel>(399, 12, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, town->town->buildings.at(BuildingID(town->hallLevel()+BuildingID::VILLAGE_HALL))->getNameTranslated());
  1171. exit = std::make_shared<CButton>(Point(748, 556), "TPMAGE1.DEF", CButton::tooltip(CGI->generaltexth->hcommands[8]), [&](){close();}, SDLK_RETURN);
  1172. exit->assignedKeys.insert(SDLK_ESCAPE);
  1173. auto & boxList = town->town->clientInfo.hallSlots;
  1174. boxes.resize(boxList.size());
  1175. for(size_t row=0; row<boxList.size(); row++) //for each row
  1176. {
  1177. for(size_t col=0; col<boxList[row].size(); col++) //for each box
  1178. {
  1179. const CBuilding * building = nullptr;
  1180. for(auto & buildingID : boxList[row][col])//we are looking for the first not built structure
  1181. {
  1182. const CBuilding * current = town->town->buildings.at(buildingID);
  1183. if(vstd::contains(town->builtBuildings, buildingID))
  1184. {
  1185. building = current;
  1186. }
  1187. else
  1188. {
  1189. if(current->mode == CBuilding::BUILD_NORMAL)
  1190. {
  1191. building = current;
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. int posX = pos.w/2 - (int)boxList[row].size()*154/2 - ((int)boxList[row].size()-1)*20 + 194*(int)col,
  1197. posY = 35 + 104*(int)row;
  1198. if(building)
  1199. boxes[row].push_back(std::make_shared<CBuildingBox>(posX, posY, town, building));
  1200. }
  1201. }
  1202. }
  1203. CBuildWindow::CBuildWindow(const CGTownInstance *Town, const CBuilding * Building, int state, bool rightClick):
  1204. CStatusbarWindow(PLAYER_COLORED | (rightClick ? RCLICK_POPUP : 0), "TPUBUILD"),
  1205. town(Town),
  1206. building(Building)
  1207. {
  1208. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1209. icon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, building->bid, 0, 125, 50);
  1210. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26);
  1211. statusbar = CGStatusBar::create(statusbarBackground);
  1212. name = std::make_shared<CLabel>(197, 30, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->hcommands[7]) % building->getNameTranslated()));
  1213. description = std::make_shared<CTextBox>(building->getDescriptionTranslated(), Rect(33, 135, 329, 67), 0, FONT_MEDIUM, ETextAlignment::CENTER);
  1214. stateText = std::make_shared<CTextBox>(getTextForState(state), Rect(33, 216, 329, 67), 0, FONT_SMALL, ETextAlignment::CENTER);
  1215. //Create components for all required resources
  1216. std::vector<std::shared_ptr<CComponent>> components;
  1217. for(int i = 0; i<GameConstants::RESOURCE_QUANTITY; i++)
  1218. {
  1219. if(building->resources[i])
  1220. components.push_back(std::make_shared<CComponent>(CComponent::resource, i, building->resources[i], CComponent::small));
  1221. }
  1222. cost = std::make_shared<CComponentBox>(components, Rect(25, 300, pos.w - 50, 130));
  1223. if(!rightClick)
  1224. { //normal window
  1225. std::string tooltipYes = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % building->getNameTranslated());
  1226. std::string tooltipNo = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % building->getNameTranslated());
  1227. buy = std::make_shared<CButton>(Point(45, 446), "IBUY30", CButton::tooltip(tooltipYes), [&](){ buyFunc(); }, SDLK_RETURN);
  1228. buy->setBorderColor(Colors::METALLIC_GOLD);
  1229. buy->block(state!=7 || LOCPLINT->playerID != town->tempOwner);
  1230. cancel = std::make_shared<CButton>(Point(290, 445), "ICANCEL", CButton::tooltip(tooltipNo), [&](){ close();}, SDLK_ESCAPE);
  1231. cancel->setBorderColor(Colors::METALLIC_GOLD);
  1232. }
  1233. }
  1234. void CBuildWindow::buyFunc()
  1235. {
  1236. LOCPLINT->cb->buildBuilding(town,building->bid);
  1237. GH.popInts(2); //we - build window and hall screen
  1238. }
  1239. std::string CBuildWindow::getTextForState(int state)
  1240. {
  1241. std::string ret;
  1242. if(state < EBuildingState::ALLOWED)
  1243. ret = CGI->generaltexth->hcommands[state];
  1244. switch (state)
  1245. {
  1246. case EBuildingState::ALREADY_PRESENT:
  1247. case EBuildingState::CANT_BUILD_TODAY:
  1248. case EBuildingState::NO_RESOURCES:
  1249. ret.replace(ret.find_first_of("%s"), 2, building->getNameTranslated());
  1250. break;
  1251. case EBuildingState::ALLOWED:
  1252. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1253. case EBuildingState::PREREQUIRES:
  1254. {
  1255. auto toStr = [&](const BuildingID build) -> std::string
  1256. {
  1257. return town->town->buildings.at(build)->getNameTranslated();
  1258. };
  1259. ret = CGI->generaltexth->allTexts[52];
  1260. ret += "\n" + town->genBuildingRequirements(building->bid).toString(toStr);
  1261. break;
  1262. }
  1263. case EBuildingState::MISSING_BASE:
  1264. {
  1265. std::string msg = CGI->generaltexth->translate("vcmi.townHall.missingBase");
  1266. ret = boost::str(boost::format(msg) % town->town->buildings.at(building->upgrade)->getNameTranslated());
  1267. break;
  1268. }
  1269. }
  1270. return ret;
  1271. }
  1272. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int min, int max)
  1273. {
  1274. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1275. pos.x+=size.x;
  1276. pos.y+=size.y;
  1277. pos.w = size.w;
  1278. pos.h = size.h;
  1279. init(name, descr, min, max);
  1280. }
  1281. LabeledValue::LabeledValue(Rect size, std::string name, std::string descr, int val)
  1282. {
  1283. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1284. pos.x+=size.x;
  1285. pos.y+=size.y;
  1286. pos.w = size.w;
  1287. pos.h = size.h;
  1288. init(name, descr, val, val);
  1289. }
  1290. void LabeledValue::init(std::string nameText, std::string descr, int min, int max)
  1291. {
  1292. addUsedEvents(HOVER);
  1293. hoverText = descr;
  1294. std::string valueText;
  1295. if(min && max)
  1296. {
  1297. valueText = boost::lexical_cast<std::string>(min);
  1298. if(min != max)
  1299. valueText += '-' + boost::lexical_cast<std::string>(max);
  1300. }
  1301. name = std::make_shared<CLabel>(3, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, nameText);
  1302. value = std::make_shared<CLabel>(pos.w-3, pos.h-2, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, valueText);
  1303. }
  1304. void LabeledValue::hover(bool on)
  1305. {
  1306. if(on)
  1307. {
  1308. GH.statusbar->write(hoverText);
  1309. }
  1310. else
  1311. {
  1312. GH.statusbar->clear();
  1313. parent->hovered = false;
  1314. }
  1315. }
  1316. CFortScreen::CFortScreen(const CGTownInstance * town):
  1317. CStatusbarWindow(PLAYER_COLORED | BORDERED, getBgName(town))
  1318. {
  1319. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1320. ui32 fortSize = static_cast<ui32>(town->creatures.size());
  1321. if(fortSize > GameConstants::CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1322. fortSize--;
  1323. const CBuilding * fortBuilding = town->town->buildings.at(BuildingID(town->fortLevel()+6));
  1324. title = std::make_shared<CLabel>(400, 12, FONT_BIG, ETextAlignment::CENTER, Colors::WHITE, fortBuilding->getNameTranslated());
  1325. std::string text = boost::str(boost::format(CGI->generaltexth->fcommands[6]) % fortBuilding->getNameTranslated());
  1326. exit = std::make_shared<CButton>(Point(748, 556), "TPMAGE1", CButton::tooltip(text), [&](){ close(); }, SDLK_RETURN);
  1327. exit->assignedKeys.insert(SDLK_ESCAPE);
  1328. std::vector<Point> positions =
  1329. {
  1330. Point(10, 22), Point(404, 22),
  1331. Point(10, 155), Point(404,155),
  1332. Point(10, 288), Point(404,288)
  1333. };
  1334. if(fortSize == GameConstants::CREATURES_PER_TOWN)
  1335. {
  1336. positions.push_back(Point(206,421));
  1337. }
  1338. else
  1339. {
  1340. positions.push_back(Point(10, 421));
  1341. positions.push_back(Point(404,421));
  1342. }
  1343. for(ui32 i=0; i<fortSize; i++)
  1344. {
  1345. BuildingID buildingID;
  1346. if(fortSize == GameConstants::CREATURES_PER_TOWN)
  1347. {
  1348. if(vstd::contains(town->builtBuildings, BuildingID::DWELL_UP_FIRST+i))
  1349. buildingID = BuildingID(BuildingID::DWELL_UP_FIRST+i);
  1350. else
  1351. buildingID = BuildingID(BuildingID::DWELL_FIRST+i);
  1352. }
  1353. else
  1354. {
  1355. buildingID = BuildingID::SPECIAL_3;
  1356. }
  1357. recAreas.push_back(std::make_shared<RecruitArea>(positions[i].x, positions[i].y, town, i));
  1358. }
  1359. resdatabar = std::make_shared<CMinorResDataBar>();
  1360. resdatabar->moveBy(pos.topLeft(), true);
  1361. Rect barRect(4, 554, 740, 18);
  1362. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 4, 554);
  1363. statusbar = CGStatusBar::create(statusbarBackground);
  1364. }
  1365. std::string CFortScreen::getBgName(const CGTownInstance * town)
  1366. {
  1367. ui32 fortSize = static_cast<ui32>(town->creatures.size());
  1368. if(fortSize > GameConstants::CREATURES_PER_TOWN && town->creatures.back().second.empty())
  1369. fortSize--;
  1370. if(fortSize == GameConstants::CREATURES_PER_TOWN)
  1371. return "TPCASTL7";
  1372. else
  1373. return "TPCASTL8";
  1374. }
  1375. void CFortScreen::creaturesChanged()
  1376. {
  1377. for(auto & elem : recAreas)
  1378. elem->creaturesChanged();
  1379. }
  1380. CFortScreen::RecruitArea::RecruitArea(int posX, int posY, const CGTownInstance * Town, int Level):
  1381. town(Town),
  1382. level(Level),
  1383. availableCount(nullptr)
  1384. {
  1385. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1386. pos.x +=posX;
  1387. pos.y +=posY;
  1388. pos.w = 386;
  1389. pos.h = 126;
  1390. if(!town->creatures[level].second.empty())
  1391. addUsedEvents(LCLICK | RCLICK | HOVER);//Activate only if dwelling is present
  1392. icons = std::make_shared<CPicture>("TPCAINFO", 261, 3);
  1393. if(getMyBuilding() != nullptr)
  1394. {
  1395. buildingIcon = std::make_shared<CAnimImage>(town->town->clientInfo.buildingsIcons, getMyBuilding()->bid, 0, 4, 21);
  1396. buildingName = std::make_shared<CLabel>(78, 101, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyBuilding()->getNameTranslated());
  1397. if(vstd::contains(town->builtBuildings, getMyBuilding()->bid))
  1398. {
  1399. ui32 available = town->creatures[level].first;
  1400. std::string availableText = CGI->generaltexth->allTexts[217]+ boost::lexical_cast<std::string>(available);
  1401. availableCount = std::make_shared<CLabel>(78, 119, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, availableText);
  1402. }
  1403. }
  1404. if(getMyCreature() != nullptr)
  1405. {
  1406. hoverText = boost::str(boost::format(CGI->generaltexth->tcommands[21]) % getMyCreature()->getNamePluralTranslated());
  1407. new CCreaturePic(159, 4, getMyCreature(), false);
  1408. new CLabel(78, 11, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getMyCreature()->getNamePluralTranslated());
  1409. Rect sizes(287, 4, 96, 18);
  1410. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[190], CGI->generaltexth->fcommands[0], getMyCreature()->getAttack(false)));
  1411. sizes.y+=20;
  1412. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[191], CGI->generaltexth->fcommands[1], getMyCreature()->getDefense(false)));
  1413. sizes.y+=21;
  1414. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[199], CGI->generaltexth->fcommands[2], getMyCreature()->getMinDamage(false), getMyCreature()->getMaxDamage(false)));
  1415. sizes.y+=20;
  1416. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[388], CGI->generaltexth->fcommands[3], getMyCreature()->MaxHealth()));
  1417. sizes.y+=21;
  1418. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[193], CGI->generaltexth->fcommands[4], getMyCreature()->valOfBonuses(Bonus::STACKS_SPEED)));
  1419. sizes.y+=20;
  1420. values.push_back(std::make_shared<LabeledValue>(sizes, CGI->generaltexth->allTexts[194], CGI->generaltexth->fcommands[5], town->creatureGrowth(level)));
  1421. }
  1422. }
  1423. const CCreature * CFortScreen::RecruitArea::getMyCreature()
  1424. {
  1425. if(!town->creatures.at(level).second.empty()) // built
  1426. return VLC->creh->objects[town->creatures.at(level).second.back()];
  1427. if(!town->town->creatures.at(level).empty()) // there are creatures on this level
  1428. return VLC->creh->objects[town->town->creatures.at(level).front()];
  1429. return nullptr;
  1430. }
  1431. const CBuilding * CFortScreen::RecruitArea::getMyBuilding()
  1432. {
  1433. BuildingID myID = BuildingID(BuildingID::DWELL_FIRST).advance(level);
  1434. if (level == GameConstants::CREATURES_PER_TOWN)
  1435. return town->town->getSpecialBuilding(BuildingSubID::PORTAL_OF_SUMMONING);
  1436. if (!town->town->buildings.count(myID))
  1437. return nullptr;
  1438. const CBuilding * build = town->town->buildings.at(myID);
  1439. while (town->town->buildings.count(myID))
  1440. {
  1441. if (town->hasBuilt(myID))
  1442. build = town->town->buildings.at(myID);
  1443. myID.advance(GameConstants::CREATURES_PER_TOWN);
  1444. }
  1445. return build;
  1446. }
  1447. void CFortScreen::RecruitArea::hover(bool on)
  1448. {
  1449. if(on)
  1450. GH.statusbar->write(hoverText);
  1451. else
  1452. GH.statusbar->clear();
  1453. }
  1454. void CFortScreen::RecruitArea::creaturesChanged()
  1455. {
  1456. if(availableCount)
  1457. {
  1458. std::string availableText = CGI->generaltexth->allTexts[217] + boost::lexical_cast<std::string>(town->creatures[level].first);
  1459. availableCount->setText(availableText);
  1460. }
  1461. }
  1462. void CFortScreen::RecruitArea::clickLeft(tribool down, bool previousState)
  1463. {
  1464. if(!down && previousState)
  1465. LOCPLINT->castleInt->builds->enterDwelling(level);
  1466. }
  1467. void CFortScreen::RecruitArea::clickRight(tribool down, bool previousState)
  1468. {
  1469. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1470. }
  1471. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner,std::string imagem)
  1472. : CStatusbarWindow(BORDERED, imagem)
  1473. {
  1474. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1475. window = std::make_shared<CPicture>(owner->town->town->clientInfo.guildWindow, 332, 76);
  1476. resdatabar = std::make_shared<CMinorResDataBar>();
  1477. resdatabar->moveBy(pos.topLeft(), true);
  1478. Rect barRect(7, 556, 737, 18);
  1479. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 7, 556);
  1480. statusbar = CGStatusBar::create(statusbarBackground);
  1481. exit = std::make_shared<CButton>(Point(748, 556), "TPMAGE1.DEF", CButton::tooltip(CGI->generaltexth->allTexts[593]), [&](){ close(); }, SDLK_RETURN);
  1482. exit->assignedKeys.insert(SDLK_ESCAPE);
  1483. static const std::vector<std::vector<Point> > positions =
  1484. {
  1485. {Point(222,445), Point(312,445), Point(402,445), Point(520,445), Point(610,445), Point(700,445)},
  1486. {Point(48,53), Point(48,147), Point(48,241), Point(48,335), Point(48,429)},
  1487. {Point(570,82), Point(672,82), Point(570,157), Point(672,157)},
  1488. {Point(183,42), Point(183,148), Point(183,253)},
  1489. {Point(491,325), Point(591,325)}
  1490. };
  1491. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1492. {
  1493. size_t spellCount = owner->town->spellsAtLevel((int)i+1,false); //spell at level with -1 hmmm?
  1494. for(size_t j=0; j<spellCount; j++)
  1495. {
  1496. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1497. spells.push_back(std::make_shared<Scroll>(positions[i][j], CGI->spellh->objects[owner->town->spells[i][j]]));
  1498. else
  1499. emptyScrolls.push_back(std::make_shared<CAnimImage>("TPMAGES.DEF", 1, 0, positions[i][j].x, positions[i][j].y));
  1500. }
  1501. }
  1502. }
  1503. CMageGuildScreen::Scroll::Scroll(Point position, const CSpell *Spell)
  1504. : spell(Spell)
  1505. {
  1506. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1507. addUsedEvents(LCLICK | RCLICK | HOVER);
  1508. pos += position;
  1509. image = std::make_shared<CAnimImage>("SPELLSCR", spell->id);
  1510. pos = image->pos;
  1511. }
  1512. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1513. {
  1514. if(down)
  1515. LOCPLINT->showInfoDialog(spell->getDescriptionTranslated(0), std::make_shared<CComponent>(CComponent::spell, spell->id));
  1516. }
  1517. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1518. {
  1519. if(down)
  1520. CRClickPopup::createAndPush(spell->getDescriptionTranslated(0), std::make_shared<CComponent>(CComponent::spell, spell->id));
  1521. }
  1522. void CMageGuildScreen::Scroll::hover(bool on)
  1523. {
  1524. if(on)
  1525. GH.statusbar->write(spell->getNameTranslated());
  1526. else
  1527. GH.statusbar->clear();
  1528. }
  1529. CBlacksmithDialog::CBlacksmithDialog(bool possible, CreatureID creMachineID, ArtifactID aid, ObjectInstanceID hid):
  1530. CStatusbarWindow(PLAYER_COLORED, "TPSMITH")
  1531. {
  1532. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1533. Rect barRect(8, pos.h - 26, pos.w - 16, 19);
  1534. auto statusbarBackground = std::make_shared<CPicture>(background->getSurface(), barRect, 8, pos.h - 26);
  1535. statusbar = CGStatusBar::create(statusbarBackground);
  1536. animBG = std::make_shared<CPicture>("TPSMITBK", 64, 50);
  1537. animBG->needRefresh = true;
  1538. const CCreature * creature = CGI->creh->objects[creMachineID];
  1539. anim = std::make_shared<CCreatureAnim>(64, 50, creature->animDefName);
  1540. anim->clipRect(113,125,200,150);
  1541. title = std::make_shared<CLabel>(165, 28, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW,
  1542. boost::str(boost::format(CGI->generaltexth->allTexts[274]) % creature->getNameSingularTranslated()));
  1543. costText = std::make_shared<CLabel>(165, 218, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[43]);
  1544. costValue = std::make_shared<CLabel>(165, 290, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE,
  1545. boost::lexical_cast<std::string>(aid.toArtifact(CGI->artifacts())->getPrice()));
  1546. std::string text = boost::str(boost::format(CGI->generaltexth->allTexts[595]) % creature->getNameSingularTranslated());
  1547. buy = std::make_shared<CButton>(Point(42, 312), "IBUY30.DEF", CButton::tooltip(text), [&](){ close(); }, SDLK_RETURN);
  1548. text = boost::str(boost::format(CGI->generaltexth->allTexts[596]) % creature->getNameSingularTranslated());
  1549. cancel = std::make_shared<CButton>(Point(224, 312), "ICANCEL.DEF", CButton::tooltip(text), [&](){ close(); }, SDLK_ESCAPE);
  1550. if(possible)
  1551. buy->addCallback([=](){ LOCPLINT->cb->buyArtifact(LOCPLINT->cb->getHero(hid),aid); });
  1552. else
  1553. buy->block(true);
  1554. costIcon = std::make_shared<CAnimImage>("RESOURCE", Res::GOLD, 0, 148, 244);
  1555. }