CCastleInterface.cpp 52 KB

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