CCastleInterface.cpp 55 KB

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