CCastleInterface.cpp 53 KB

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