CCastleInterface.cpp 58 KB

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