CCastleInterface.cpp 52 KB

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