CCastleInterface.cpp 52 KB

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