CCastleInterface.cpp 52 KB

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