CCastleInterface.cpp 54 KB

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