CCastleInterface.cpp 52 KB

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