CCastleInterface.cpp 52 KB

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