CCastleInterface.cpp 52 KB

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