CCastleInterface.cpp 52 KB

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