CCastleInterface.cpp 54 KB

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