CCastleInterface.cpp 51 KB

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