CCastleInterface.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. #include "../stdafx.h"
  2. #include "CCastleInterface.h"
  3. #include "AdventureMapButton.h"
  4. #include "CAdvmapInterface.h"
  5. #include "../CCallback.h"
  6. #include "CGameInfo.h"
  7. #include "CHeroWindow.h"
  8. #include "CMessage.h"
  9. #include "SDL_Extensions.h"
  10. #include "CCreatureAnimation.h"
  11. #include "Graphics.h"
  12. #include "../hch/CArtHandler.h"
  13. #include "../hch/CBuildingHandler.h"
  14. #include "../hch/CDefHandler.h"
  15. #include "../hch/CGeneralTextHandler.h"
  16. #include "../hch/CLodHandler.h"
  17. #include "../hch/CObjectHandler.h"
  18. #include "../hch/CSpellHandler.h"
  19. #include "../hch/CTownHandler.h"
  20. #include <boost/algorithm/string.hpp>
  21. #include <boost/algorithm/string/replace.hpp>
  22. #include <boost/assign/std/vector.hpp>
  23. #include <boost/lexical_cast.hpp>
  24. #include <cmath>
  25. #include <sstream>
  26. using namespace boost::assign;
  27. using namespace CSDL_Ext;
  28. /*
  29. * CCastleInterface.cpp, part of VCMI engine
  30. *
  31. * Authors: listed in file AUTHORS in main folder
  32. *
  33. * License: GNU General Public License v2.0 or later
  34. * Full text of license available in license.txt file, in main folder
  35. *
  36. */
  37. extern TTF_Font * GEOR16;
  38. CBuildingRect::CBuildingRect(Structure *Str)
  39. :moi(false), offset(0), str(Str)
  40. {
  41. def = CDefHandler::giveDef(Str->defName);
  42. max = def->ourImages.size();
  43. if(str->ID == 33 && str->townID == 4) //little 'hack' for estate in necropolis - background color is not always the first color in the palette
  44. {
  45. for(std::vector<Cimage>::iterator i=def->ourImages.begin();i!=def->ourImages.end();i++)
  46. {
  47. SDL_SetColorKey(i->bitmap,SDL_SRCCOLORKEY,*((char*)i->bitmap->pixels));
  48. }
  49. }
  50. pos.x = str->pos.x + LOCPLINT->castleInt->pos.x;
  51. pos.y = str->pos.y + LOCPLINT->castleInt->pos.y;
  52. pos.w = def->ourImages[0].bitmap->w;
  53. pos.h = def->ourImages[0].bitmap->h;
  54. if(Str->ID<0 || (Str->ID>=27 && Str->ID<=29))
  55. {
  56. area = border = NULL;
  57. return;
  58. }
  59. border = BitmapHandler::loadBitmap(str->borderName);
  60. if (border)
  61. {
  62. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  63. }
  64. else
  65. {
  66. tlog2 << "Warning: no border for "<<Str->ID<<std::endl;
  67. }
  68. area = BitmapHandler::loadBitmap(str->areaName); //FIXME look up
  69. if (area)
  70. {
  71. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  72. }
  73. else
  74. {
  75. tlog2 << "Warning: no area for "<<Str->ID<<std::endl;
  76. }
  77. }
  78. CBuildingRect::~CBuildingRect()
  79. {
  80. delete def;
  81. if(border)
  82. SDL_FreeSurface(border);
  83. if(area)
  84. SDL_FreeSurface(area);
  85. }
  86. void CBuildingRect::activate()
  87. {
  88. activateHover();
  89. activateLClick();
  90. activateRClick();
  91. }
  92. void CBuildingRect::deactivate()
  93. {
  94. deactivateHover();
  95. deactivateLClick();
  96. deactivateRClick();
  97. if(moi)
  98. deactivateMouseMove();
  99. moi=false;
  100. }
  101. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  102. {
  103. if(str->pos.z != p2.str->pos.z)
  104. return (str->pos.z) < (p2.str->pos.z);
  105. else
  106. return (str->ID) < (p2.str->ID);
  107. }
  108. void CBuildingRect::hover(bool on)
  109. {
  110. //Hoverable::hover(on);
  111. if(on)
  112. {
  113. if(!moi)
  114. activateMouseMove();
  115. moi = true;
  116. }
  117. else
  118. {
  119. if(moi)
  120. deactivateMouseMove();
  121. moi = false;
  122. if(LOCPLINT->castleInt->hBuild == this)
  123. {
  124. LOCPLINT->castleInt->hBuild = NULL;
  125. LOCPLINT->statusbar->clear();
  126. //call mouseMoved in other buildings, cursor might have been moved while they were inactive (eg. because of r-click popup)
  127. for(size_t i = 0; i < LOCPLINT->castleInt->buildings.size(); i++)
  128. LOCPLINT->castleInt->buildings[i]->mouseMoved(GH.current->motion);
  129. }
  130. }
  131. }
  132. void CBuildingRect::clickLeft(tribool down, bool previousState)
  133. {
  134. if(area && (LOCPLINT->castleInt->hBuild==this) && !(indeterminate(down)) && (CSDL_Ext::SDL_GetPixel(area,GH.current->motion.x-pos.x,GH.current->motion.y-pos.y) != 0)) //na polu
  135. {
  136. if(previousState && !down)
  137. LOCPLINT->castleInt->buildingClicked(str->ID);
  138. //ClickableL::clickLeft(down);
  139. }
  140. }
  141. void CBuildingRect::clickRight(tribool down, bool previousState)
  142. {
  143. if((!area) || (!((bool)down)) || (this!=LOCPLINT->castleInt->hBuild))
  144. return;
  145. if((CSDL_Ext::SDL_GetPixel(area,GH.current->motion.x-pos.x,GH.current->motion.y-pos.y) != 0)) //na polu
  146. {
  147. CBuilding *bld = CGI->buildh->buildings[str->townID][str->ID];
  148. assert(bld);
  149. CInfoPopup *vinya = new CInfoPopup();
  150. vinya->free = true;
  151. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  152. (LOCPLINT->playerID,
  153. bld->Description(),
  154. LOCPLINT->castleInt->bicons->ourImages[str->ID].bitmap,
  155. bld->Name());
  156. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  157. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  158. GH.pushInt(vinya);
  159. }
  160. }
  161. void CBuildingRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  162. {
  163. if(area && isItIn(&pos,sEvent.x, sEvent.y))
  164. {
  165. if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
  166. {
  167. if(LOCPLINT->castleInt->hBuild == this)
  168. {
  169. LOCPLINT->castleInt->hBuild = NULL;
  170. LOCPLINT->statusbar->clear();
  171. }
  172. }
  173. else //inside the area of this building
  174. {
  175. if(LOCPLINT->castleInt->hBuild) //a building is hovered
  176. {
  177. if((*LOCPLINT->castleInt->hBuild)<(*this)) //set if we are on top
  178. {
  179. LOCPLINT->castleInt->hBuild = this;
  180. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->Name().length())
  181. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  182. else
  183. LOCPLINT->statusbar->print(str->name);
  184. }
  185. }
  186. else //no building hovered
  187. {
  188. LOCPLINT->castleInt->hBuild = this;
  189. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->Name().length())
  190. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  191. else
  192. LOCPLINT->statusbar->print(str->name);
  193. }
  194. }
  195. }
  196. //if(border)
  197. // blitAt(border,pos.x,pos.y);
  198. }
  199. void CHeroGSlot::hover (bool on)
  200. {
  201. if(!on) return;
  202. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  203. std::string temp;
  204. if(hero)
  205. {
  206. if(highlight)//view NNN
  207. {
  208. temp = CGI->generaltexth->tcommands[4];
  209. boost::algorithm::replace_first(temp,"%s",hero->name);
  210. }
  211. else if(other->hero && other->highlight)//exchange
  212. {
  213. temp = CGI->generaltexth->tcommands[7];
  214. boost::algorithm::replace_first(temp,"%s",hero->name);
  215. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  216. }
  217. else// select NNN (in ZZZ)
  218. {
  219. if(upg)//down - visiting
  220. {
  221. temp = CGI->generaltexth->tcommands[32];
  222. boost::algorithm::replace_first(temp,"%s",hero->name);
  223. }
  224. else //up - garrison
  225. {
  226. temp = CGI->generaltexth->tcommands[12];
  227. boost::algorithm::replace_first(temp,"%s",hero->name);
  228. }
  229. }
  230. }
  231. else //we are empty slot
  232. {
  233. if(other->highlight && other->hero) //move NNNN
  234. {
  235. temp = CGI->generaltexth->tcommands[6];
  236. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  237. }
  238. else //empty
  239. {
  240. temp = CGI->generaltexth->allTexts[507];
  241. }
  242. }
  243. if(temp.size())
  244. LOCPLINT->statusbar->print(temp);
  245. }
  246. void CHeroGSlot::clickRight(tribool down, bool previousState)
  247. {
  248. }
  249. void CHeroGSlot::clickLeft(tribool down, bool previousState)
  250. {
  251. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  252. if(!down)
  253. {
  254. owner->garr->splitting = false;
  255. owner->garr->highlighted = NULL;
  256. if(hero && highlight)
  257. {
  258. setHighlight(false);
  259. LOCPLINT->openHeroWindow(hero);
  260. }
  261. else if(other->hero && other->highlight)
  262. {
  263. bool allow = true;
  264. if(upg) //moving hero out of town - check if it is allowed
  265. {
  266. if(!hero && LOCPLINT->cb->howManyHeroes(false) >= 8)
  267. {
  268. std::string tmp = CGI->generaltexth->allTexts[18]; //You already have %d adventuring heroes under your command.
  269. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  270. LOCPLINT->showInfoDialog(tmp,std::vector<SComponent*>(), soundBase::sound_todo);
  271. allow = false;
  272. }
  273. else if(!other->hero->army.slots.size()) //hero has no creatures - strange, but if we have appropriate error message...
  274. {
  275. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[19],std::vector<SComponent*>(), soundBase::sound_todo); //This hero has no creatures. A hero must have creatures before he can brave the dangers of the countryside.
  276. allow = false;
  277. }
  278. }
  279. setHighlight(false);
  280. other->setHighlight(false);
  281. if(allow)
  282. LOCPLINT->cb->swapGarrisonHero(owner->town);
  283. }
  284. else if(hero)
  285. {
  286. setHighlight(true);
  287. owner->garr->highlighted = NULL;
  288. show(screen2);
  289. }
  290. hover(false);hover(true); //refresh statusbar
  291. }
  292. //if(indeterminate(down) && !isItIn(&other->pos,GH.current->motion.x,GH.current->motion.y))
  293. //{
  294. // other->highlight = highlight = false;
  295. // show(screen2);
  296. //}
  297. }
  298. void CHeroGSlot::activate()
  299. {
  300. activateLClick();
  301. activateRClick();
  302. activateHover();
  303. }
  304. void CHeroGSlot::deactivate()
  305. {
  306. highlight = false;
  307. deactivateLClick();
  308. deactivateRClick();
  309. deactivateHover();
  310. }
  311. void CHeroGSlot::show(SDL_Surface * to)
  312. {
  313. if(hero) //there is hero
  314. blitAt(graphics->portraitLarge[hero->portrait],pos,to);
  315. else if(!upg) //up garrison
  316. blitAt(graphics->flags->ourImages[LOCPLINT->castleInt->town->getOwner()].bitmap,pos,to);
  317. if(highlight)
  318. blitAt(graphics->bigImgs[-1],pos,to);
  319. }
  320. CHeroGSlot::CHeroGSlot(int x, int y, int updown, const CGHeroInstance *h, CCastleInterface * Owner)
  321. {
  322. owner = Owner;
  323. pos.x = x;
  324. pos.y = y;
  325. pos.w = 58;
  326. pos.h = 64;
  327. hero = h;
  328. upg = updown;
  329. highlight = false;
  330. }
  331. CHeroGSlot::~CHeroGSlot()
  332. {
  333. }
  334. void CHeroGSlot::setHighlight( bool on )
  335. {
  336. highlight = on;
  337. if(owner->hslotup.hero && owner->hslotdown.hero) //two heroes in town
  338. {
  339. for(size_t i = 0; i<owner->garr->splitButtons.size(); i++) //splitting enabled when slot higlighted
  340. owner->garr->splitButtons[i]->block(!on);
  341. }
  342. }
  343. static std::string getBgName(int type) //TODO - co z tym zrobi�?
  344. {
  345. switch (type)
  346. {
  347. case 0:
  348. return "TBCSBACK.bmp";
  349. case 1:
  350. return "TBRMBACK.bmp";
  351. case 2:
  352. return "TBTWBACK.bmp";
  353. case 3:
  354. return "TBINBACK.bmp";
  355. case 4:
  356. return "TBNCBACK.bmp";
  357. case 5:
  358. return "TBDNBACK.bmp";
  359. case 6:
  360. return "TBSTBACK.bmp";
  361. case 7:
  362. return "TBFRBACK.bmp";
  363. case 8:
  364. return "TBELBACK.bmp";
  365. default:
  366. #ifndef __GNUC__
  367. throw new std::exception("std::string getBgName(int type): invalid type");
  368. #else
  369. throw new std::exception();
  370. #endif
  371. }
  372. }
  373. class SORTHELP
  374. {
  375. public:
  376. bool operator ()
  377. (const CBuildingRect *a ,
  378. const CBuildingRect *b)
  379. {
  380. return (*a)<(*b);
  381. }
  382. } srthlp ;
  383. CCastleInterface::CCastleInterface(const CGTownInstance * Town, int listPos)
  384. :hslotup(241,387,0,Town->garrisonHero,this),hslotdown(241,483,1,Town->visitingHero,this)
  385. {
  386. showing = false;
  387. bars = CDefHandler::giveDefEss("TPTHBAR.DEF");
  388. status = CDefHandler::giveDefEss("TPTHCHK.DEF");
  389. LOCPLINT->castleInt = this;
  390. hall = NULL;
  391. townInt = BitmapHandler::loadBitmap("TOWNSCRN.bmp");
  392. cityBg = BitmapHandler::loadBitmap(getBgName(Town->subID));
  393. pos.x = screen->w/2 - 400;
  394. pos.y = screen->h/2 - 300;
  395. hslotup.pos.x += pos.x;
  396. hslotup.pos.y += pos.y;
  397. hslotdown.pos.x += pos.x;
  398. hslotdown.pos.y += pos.y;
  399. hall = CDefHandler::giveDef("ITMTL.DEF");
  400. fort = CDefHandler::giveDef("ITMCL.DEF");
  401. hBuild = NULL;
  402. count=0;
  403. town = Town;
  404. animval = 0;
  405. //garrison
  406. garr = new CGarrisonInt(pos.x+305,pos.y+387,4,Point(0,96),townInt,Point(62,374),town,town->visitingHero);
  407. townlist = new CTownList(3,pos.x+744,pos.y+414,"IAM014.DEF","IAM015.DEF");//744,526);
  408. exit = new AdventureMapButton
  409. (CGI->generaltexth->tcommands[8],"",boost::bind(&CCastleInterface::close,this),pos.x+744,pos.y+544,"TSBTNS.DEF",SDLK_RETURN);
  410. exit->assignedKeys.insert(SDLK_ESCAPE);
  411. split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+744,pos.y+382,"TSBTNS.DEF");
  412. split->callback += boost::bind(&CCastleInterface::splitClicked,this);
  413. garr->splitButtons.push_back(split);
  414. statusbar = new CStatusBar(pos.x+7,pos.y+555,"TSTATBAR.bmp",732);
  415. resdatabar = new CResDataBar("ZRESBAR.bmp",pos.x+3,pos.y+575,32,2,85,85);
  416. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  417. townlist->fun = boost::bind(&CCastleInterface::townChange,this);
  418. townlist->genList();
  419. townlist->selected = vstd::findPos(townlist->items,Town);
  420. townlist->from = townlist->selected - listPos;
  421. amax(townlist->from, 0);
  422. amin(townlist->from, townlist->items.size() - townlist->SIZE);
  423. graphics->blueToPlayersAdv(townInt,LOCPLINT->playerID);
  424. exit->bitmapOffset = 4;
  425. //buildings
  426. recreateBuildings();
  427. std::string defname;
  428. switch (town->subID)
  429. {
  430. case 0:
  431. defname = "HALLCSTL.DEF";
  432. musicID = musicBase::castleTown;
  433. break;
  434. case 1:
  435. defname = "HALLRAMP.DEF";
  436. musicID = musicBase::rampartTown;
  437. break;
  438. case 2:
  439. defname = "HALLTOWR.DEF";
  440. musicID = musicBase::towerTown;
  441. break;
  442. case 3:
  443. defname = "HALLINFR.DEF";
  444. musicID = musicBase::infernoTown;
  445. break;
  446. case 4:
  447. defname = "HALLNECR.DEF";
  448. musicID = musicBase::necroTown;
  449. break;
  450. case 5:
  451. defname = "HALLDUNG.DEF";
  452. musicID = musicBase::dungeonTown;
  453. break;
  454. case 6:
  455. defname = "HALLSTRN.DEF";
  456. musicID = musicBase::strongHoldTown;
  457. break;
  458. case 7:
  459. defname = "HALLFORT.DEF";
  460. musicID = musicBase::fortressTown;
  461. break;
  462. case 8:
  463. defname = "HALLELEM.DEF";
  464. musicID = musicBase::elemTown;
  465. break;
  466. default:
  467. throw new std::string("Wrong town subID");
  468. }
  469. bicons = CDefHandler::giveDefEss(defname);
  470. CGI->musich->playMusic(musicID, -1);
  471. }
  472. CCastleInterface::~CCastleInterface()
  473. {
  474. delete bars;
  475. delete status;
  476. SDL_FreeSurface(townInt);
  477. SDL_FreeSurface(cityBg);
  478. delete exit;
  479. //delete split;
  480. delete hall;
  481. delete fort;
  482. delete garr;
  483. delete townlist;
  484. delete statusbar;
  485. delete resdatabar;
  486. for(size_t i=0;i<buildings.size();i++)
  487. {
  488. delete buildings[i];
  489. }
  490. delete bicons;
  491. }
  492. void CCastleInterface::close()
  493. {
  494. if(town->visitingHero)
  495. LOCPLINT->adventureInt->select(town->visitingHero);
  496. else
  497. LOCPLINT->adventureInt->select(town);
  498. LOCPLINT->castleInt = NULL;
  499. GH.popIntTotally(this);
  500. CGI->musich->stopMusic(5000);
  501. }
  502. void CCastleInterface::splitF()
  503. {
  504. }
  505. void CCastleInterface::buildingClicked(int building)
  506. {
  507. tlog5<<"You've clicked on "<<building<<std::endl;
  508. if(building==19 || building==18)
  509. {
  510. building = town->town->hordeLvl[0] + 30;
  511. }
  512. else if(building==24 || building==25)
  513. {
  514. building = town->town->hordeLvl[1] + 30;
  515. }
  516. if(building >= 30)
  517. {
  518. showRecruitmentWindow(building);
  519. }
  520. else
  521. {
  522. switch(building)
  523. {
  524. case 0: case 1: case 2: case 3: case 4: //mage guild
  525. {
  526. const CGHeroInstance *h = NULL; //hero that "enters" mage guild
  527. if(!town->garrisonHero && !town->visitingHero) //no heroes in town
  528. h = NULL;
  529. else if(!town->garrisonHero) //only visiting hero
  530. h = town->visitingHero;
  531. else if(!town->visitingHero || hslotup.highlight) //only garrisoned hero OR both heroes present, garrisoned hero selected
  532. h = town->garrisonHero;
  533. else //both heroes present, use the visiting one
  534. h = town->visitingHero;
  535. if(h && !vstd::contains(h->artifWorn,ui16(17))) //hero doesn't have spellbok
  536. {
  537. if(LOCPLINT->cb->getResourceAmount(6) < 500) //not enough gold to buy spellbook
  538. {
  539. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213],std::vector<SComponent*>(), soundBase::sound_todo);
  540. }
  541. else
  542. {
  543. CFunctionList<void()> fl = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,h,0);
  544. fl += boost::bind(&CCastleInterface::enterMageGuild,this);
  545. std::vector<SComponent*> vvv(1,new SComponent(SComponent::artifact,0,0));
  546. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214],vvv,fl,0,true);
  547. }
  548. }
  549. else
  550. {
  551. enterMageGuild();
  552. }
  553. break;
  554. }
  555. case 5: //tavern
  556. {
  557. enterTavern();
  558. break;
  559. }
  560. case 6: //shipyard
  561. {
  562. LOCPLINT->showShipyardDialog(town);
  563. break;
  564. }
  565. case 7: case 8: case 9: //fort/citadel/castle
  566. {
  567. CFortScreen *fs = new CFortScreen(this);
  568. GH.pushInt(fs);
  569. break;
  570. }
  571. case 10: case 11: case 12: case 13: //hall
  572. enterHall();
  573. break;
  574. case 14: //marketplace
  575. {
  576. CMarketplaceWindow *cmw = new CMarketplaceWindow(0);
  577. GH.pushInt(cmw);
  578. break;
  579. }
  580. case 15: //resource silo
  581. {
  582. LOCPLINT->showInfoDialog(CGI->buildh->buildings[town->subID][15]->Description(),std::vector<SComponent*>(), soundBase::sound_todo);
  583. break;
  584. }
  585. case 16: //blacksmith
  586. {
  587. const CGHeroInstance *hero = town->visitingHero;
  588. if(!hero)
  589. {
  590. std::string pom = CGI->generaltexth->allTexts[273];
  591. boost::algorithm::replace_first(pom,"%s",CGI->buildh->buildings[town->subID][16]->Name());
  592. LOCPLINT->showInfoDialog(pom,std::vector<SComponent*>(), soundBase::sound_todo);
  593. return;
  594. }
  595. int aid = town->town->warMachine;
  596. int price = CGI->arth->artifacts[aid].price;
  597. bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
  598. if(vstd::contains(hero->artifWorn,ui16(aid+9))) //hero already has machine
  599. possible = false;
  600. GH.pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(aid,false),aid,hero->id));
  601. break;
  602. }
  603. //TODO: case 17: //special 1
  604. //TODO: case 18: //basic horde 1
  605. //TODO: case 19: //upg horde 1
  606. case 20: //ship at shipyard
  607. //Do nothing.
  608. break;
  609. //TODO: case 21: //special 2
  610. case 22: //special 3
  611. {
  612. switch(town->subID)
  613. {
  614. case 0: //brotherhood of sword
  615. enterTavern();
  616. break;
  617. default:
  618. tlog4<<"This building isn't handled...\n";
  619. break;
  620. }
  621. break;
  622. }
  623. //TODO: case 23: //special 4
  624. //TODO: case 24: //basic horde 2
  625. //TODO: case 25: //upg horde 2
  626. //TODO: case 26: //grail
  627. default:
  628. tlog4<<"This building isn't handled...\n";
  629. }
  630. }
  631. }
  632. void CCastleInterface::enterHall()
  633. {
  634. CHallInterface *h = new CHallInterface(this);
  635. GH.pushInt(h);
  636. }
  637. void CCastleInterface::showAll( SDL_Surface * to/*=NULL*/)
  638. {
  639. blitAt(cityBg,pos,to);
  640. blitAt(townInt,pos.x,pos.y+374,to);
  641. LOCPLINT->adventureInt->resdatabar.draw(to);
  642. townlist->draw(to);
  643. statusbar->show(to);
  644. resdatabar->draw(to);
  645. garr->show(to);
  646. int pom;
  647. //draw fort icon
  648. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  649. pom = 2;
  650. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  651. pom = 1;
  652. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  653. pom = 0;
  654. else pom = 3;
  655. blitAt(fort->ourImages[pom].bitmap,pos.x+122,pos.y+413,to);
  656. //draw ((village/town/city) hall)/capitol icon
  657. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  658. pom = 3;
  659. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  660. pom = 2;
  661. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  662. pom = 1;
  663. else pom = 0;
  664. blitAt(hall->ourImages[pom].bitmap,pos.x+80,pos.y+413,to);
  665. //draw creatures icons and their growths
  666. for(int i=0;i<CREATURES_PER_TOWN;i++)
  667. {
  668. int cid = -1;
  669. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  670. {
  671. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  672. cid = town->town->upgradedCreatures[i];
  673. else
  674. cid = town->town->basicCreatures[i];
  675. }
  676. if (cid>=0)
  677. {
  678. int pomx, pomy;
  679. pomx = 22 + (55*((i>3)?(i-4):i));
  680. pomy = (i>3)?(507):(459);
  681. blitAt(graphics->smallImgs[cid],pos.x+pomx,pos.y+pomy,to);
  682. std::ostringstream oss;
  683. oss << '+' << town->creatureGrowth(i);
  684. CSDL_Ext::printAtMiddle(oss.str(),pos.x+pomx+16,pos.y+pomy+37,GEOR13,zwykly,to);
  685. }
  686. }
  687. //print name and income
  688. CSDL_Ext::printAt(town->name,pos.x+85,pos.y+389,GEOR13,zwykly,to);
  689. char temp[10];
  690. SDL_itoa(town->dailyIncome(),temp,10);
  691. CSDL_Ext::printAtMiddle(temp,pos.x+195,pos.y+442,GEOR13,zwykly,to);
  692. //blit town icon
  693. pom = town->subID*2;
  694. if (!town->hasFort())
  695. pom += F_NUMBER*2;
  696. if(town->builded >= MAX_BUILDING_PER_TURN)
  697. pom++;
  698. blitAt(graphics->bigTownPic->ourImages[pom].bitmap,pos.x+15,pos.y+387,to);
  699. hslotup.show(to);
  700. hslotdown.show(to);
  701. show(to);
  702. if(screen->w != 800 || screen->h !=600)
  703. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  704. exit->show(to);
  705. //split->show(to);
  706. }
  707. void CCastleInterface::townChange()
  708. {
  709. const CGTownInstance * nt = townlist->items[townlist->selected];
  710. int tpos = townlist->selected - townlist->from;
  711. GH.popIntTotally(this);
  712. GH.pushInt(new CCastleInterface(nt, tpos));
  713. }
  714. void CCastleInterface::show(SDL_Surface * to)
  715. {
  716. count++;
  717. if(count==8)
  718. {
  719. count=0;
  720. animval++;
  721. }
  722. blitAt(cityBg,pos,to);
  723. //blit buildings
  724. for(size_t i=0;i<buildings.size();i++)
  725. {
  726. int frame = ((animval)%(buildings[i]->max - buildings[i]->offset)) + buildings[i]->offset;
  727. if(frame)
  728. {
  729. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  730. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  731. }
  732. else
  733. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  734. if(hBuild==buildings[i] && hBuild->border) //if this this higlighted structure and has border we'll blit it
  735. blitAt(hBuild->border,hBuild->pos,to);
  736. }
  737. }
  738. void CCastleInterface::activate()
  739. {
  740. showing = true;
  741. townlist->activate();
  742. garr->activate();
  743. LOCPLINT->statusbar = statusbar;
  744. exit->activate();
  745. //split->activate();
  746. for(size_t i=0;i<buildings.size();i++) //XXX pls use iterators or at() but not []
  747. {
  748. buildings[i]->activate();
  749. }
  750. hslotdown.activate();
  751. hslotup.activate();
  752. activateKeys();
  753. }
  754. void CCastleInterface::deactivate()
  755. {
  756. showing = false;
  757. townlist->deactivate();
  758. garr->deactivate();
  759. exit->deactivate();
  760. //split->deactivate();
  761. for(size_t i=0;i<buildings.size();i++) //XXX iterators
  762. {
  763. buildings[i]->deactivate();
  764. }
  765. hslotdown.deactivate();
  766. hslotup.deactivate();
  767. deactivateKeys();
  768. }
  769. void CCastleInterface::addBuilding(int bid)
  770. {
  771. //TODO: lepiej by bylo tylko dodawac co trzeba pamietajac o grupach
  772. deactivate();
  773. recreateBuildings();
  774. activate();
  775. }
  776. void CCastleInterface::removeBuilding(int bid)
  777. {
  778. //TODO: lepiej by bylo tylko usuwac co trzeba pamietajac o grupach
  779. recreateBuildings();
  780. }
  781. void CCastleInterface::recreateBuildings()
  782. {
  783. for(size_t i=0;i<buildings.size();i++)
  784. {
  785. if(showing)
  786. buildings[i]->deactivate();
  787. delete buildings[i];
  788. }
  789. buildings.clear();
  790. hBuild = NULL;
  791. std::set< std::pair<int,int> > s; //group - id
  792. for (std::set<si32>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  793. {
  794. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  795. {
  796. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  797. {
  798. Structure * st = CGI->townh->structures[town->subID][*i];
  799. if(st->group<0) //no group - just add it
  800. {
  801. buildings.push_back(new CBuildingRect(st));
  802. }
  803. else
  804. {
  805. std::set< std::pair<int,int> >::iterator obecny=s.end();
  806. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  807. {
  808. if(seti->first == st->group)
  809. {
  810. obecny = seti;
  811. break;
  812. }
  813. }
  814. if(obecny != s.end())
  815. {
  816. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  817. {
  818. for(size_t itpb = 0; itpb<buildings.size(); itpb++)
  819. {
  820. if(buildings[itpb]->str->ID == obecny->second)
  821. {
  822. delete buildings[itpb];
  823. buildings.erase(buildings.begin() + itpb);
  824. #ifndef __GNUC__
  825. obecny->second = st->ID; //XXX look closer!
  826. #else
  827. *(const_cast<int*>(&(obecny->second))) = st->ID;
  828. #endif
  829. buildings.push_back(new CBuildingRect(st));
  830. }
  831. }
  832. }
  833. }
  834. else
  835. {
  836. buildings.push_back(new CBuildingRect(st));
  837. s.insert(std::pair<int,int>(st->group,st->ID));
  838. }
  839. }
  840. }
  841. else continue;
  842. }
  843. else
  844. break;
  845. }
  846. //ship in shipyard
  847. bool isThereShip = false;
  848. if(vstd::contains(town->builtBuildings,6))
  849. {
  850. std::vector <const CGObjectInstance *> vobjs = LOCPLINT->cb->getVisitableObjs(town->bestLocation());
  851. if(vobjs.size() && vobjs.front()->ID == 8) //there is visitable obj at shipyard output tile and it's a boat
  852. {
  853. Structure * st = CGI->townh->structures[town->subID][20];
  854. buildings.push_back(new CBuildingRect(st));
  855. s.insert(std::pair<int,int>(st->group,st->ID));
  856. isThereShip = true;
  857. }
  858. }
  859. std::sort(buildings.begin(),buildings.end(),srthlp);
  860. //code for Mana Vortex (there are two sets of animation frames - one without mage guild and one with
  861. if((town->subID == 5) && (town->builtBuildings.find(21)!=town->builtBuildings.end()))
  862. {
  863. CBuildingRect *vortex = NULL;
  864. for(size_t i=0;i<buildings.size();i++)
  865. {
  866. if(buildings[i]->str->ID==21)
  867. {
  868. vortex=buildings[i];
  869. break;
  870. }
  871. }
  872. if(town->builtBuildings.find(4)!=town->builtBuildings.end()) //there is mage Guild level 5
  873. {
  874. vortex->offset = 10;
  875. vortex->max = vortex->def->ourImages.size();
  876. }
  877. else
  878. {
  879. vortex->offset = 0;
  880. vortex->max = 10;
  881. }
  882. }
  883. //code for the shipyard in the Castle
  884. else if(town->subID == 0)
  885. {
  886. int shipID = 0;
  887. if(isThereShip)
  888. shipID = 20;
  889. else if(vstd::contains(town->builtBuildings, 6))
  890. shipID = 6;
  891. if(shipID)
  892. {
  893. CBuildingRect *shipyard = NULL;
  894. for(size_t i=0;i<buildings.size();i++)
  895. {
  896. if(buildings[i]->str->ID==shipID)
  897. {
  898. shipyard=buildings[i];
  899. break;
  900. }
  901. }
  902. if(town->builtBuildings.find(8)!=town->builtBuildings.end()) //there is citadel
  903. {
  904. shipyard->offset = 1;
  905. shipyard->max = shipyard->def->ourImages.size();
  906. }
  907. else
  908. {
  909. shipyard->offset = 0;
  910. shipyard->max = 1;
  911. }
  912. }
  913. }
  914. if(showing)
  915. for(size_t i=0;i<buildings.size();i++)
  916. buildings[i]->activate();
  917. }
  918. CRecruitmentWindow * CCastleInterface::showRecruitmentWindow( int building )
  919. {
  920. if(building>36) //upg dwelling
  921. building-=7;
  922. int level = building-30;
  923. assert(level >= 0 && level <= 6);
  924. //std::vector<std::pair<int,int > > crs;
  925. //int amount = town->creatures[level].first;
  926. //const std::vector<ui32> &cres = town->creatures[level].second;
  927. //for(size_t i = 0; i < cres.size(); i++)
  928. // crs.push_back(std::make_pair((int)cres[i],amount));
  929. //CRecruitmentWindow *rw = new CRecruitmentWindow(crs,boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  930. CRecruitmentWindow *rw = new CRecruitmentWindow(town, level, town, boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  931. GH.pushInt(rw);
  932. return rw;
  933. }
  934. void CCastleInterface::enterMageGuild()
  935. {
  936. GH.pushInt(new CMageGuildScreen(this));
  937. }
  938. void CCastleInterface::enterTavern()
  939. {
  940. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(town);
  941. CTavernWindow *tv = new CTavernWindow(h[0],h[1],"GOSSIP TEST");
  942. GH.pushInt(tv);
  943. }
  944. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  945. {
  946. if(key.state != SDL_PRESSED) return;
  947. switch(key.keysym.sym)
  948. {
  949. case SDLK_UP:
  950. if(townlist->selected)
  951. {
  952. townlist->selected--;
  953. townlist->from--;
  954. townChange();
  955. }
  956. break;
  957. case SDLK_DOWN:
  958. if(townlist->selected < townlist->items.size() - 1)
  959. {
  960. townlist->selected++;
  961. townlist->from++;
  962. townChange();
  963. }
  964. break;
  965. case SDLK_SPACE:
  966. if(town->visitingHero && town->garrisonHero)
  967. {
  968. LOCPLINT->cb->swapGarrisonHero(town);
  969. }
  970. break;
  971. default:
  972. break;
  973. }
  974. }
  975. void CCastleInterface::splitClicked()
  976. {
  977. if(town->visitingHero && town->garrisonHero && (hslotdown.highlight || hslotup.highlight))
  978. {
  979. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  980. }
  981. }
  982. void CHallInterface::CBuildingBox::hover(bool on)
  983. {
  984. //Hoverable::hover(on);
  985. if(on)
  986. {
  987. std::string toPrint;
  988. if(state==8)
  989. toPrint = CGI->generaltexth->hcommands[5];
  990. else
  991. toPrint = CGI->generaltexth->hcommands[state];
  992. std::vector<std::string> name;
  993. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->Name());
  994. LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
  995. }
  996. else
  997. LOCPLINT->statusbar->clear();
  998. }
  999. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1000. {
  1001. if(previousState && (!down))
  1002. {
  1003. GH.pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0));
  1004. }
  1005. //ClickableL::clickLeft(down);
  1006. }
  1007. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1008. {
  1009. if(down)
  1010. {
  1011. GH.pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1));
  1012. }
  1013. //ClickableR::clickRight(down);
  1014. }
  1015. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  1016. {
  1017. CCastleInterface *ci = LOCPLINT->castleInt;
  1018. blitAt(ci->bicons->ourImages[BID].bitmap,pos.x,pos.y,to);
  1019. int pom, pom2=-1;
  1020. switch (state)
  1021. {
  1022. case 4:
  1023. pom = 0;
  1024. pom2 = 0;
  1025. break;
  1026. case 7:
  1027. pom = 1;
  1028. break;
  1029. case 6:
  1030. pom2 = 2;
  1031. pom = 2;
  1032. break;
  1033. case 5: case 8:
  1034. pom2 = 1;
  1035. pom = 2;
  1036. break;
  1037. case 0: case 2: case 1: default:
  1038. pom = 3;
  1039. break;
  1040. }
  1041. blitAt(ci->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71,to);
  1042. if(pom2>=0)
  1043. blitAt(ci->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54,to);
  1044. CSDL_Ext::printAtMiddle(CGI->buildh->buildings[ci->town->subID][BID]->Name(),pos.x-1+ci->bars->ourImages[0].bitmap->w/2,pos.y+71+ci->bars->ourImages[0].bitmap->h/2, GEOR13,zwykly,to);
  1045. }
  1046. void CHallInterface::CBuildingBox::activate()
  1047. {
  1048. activateHover();
  1049. activateLClick();
  1050. activateRClick();
  1051. }
  1052. void CHallInterface::CBuildingBox::deactivate()
  1053. {
  1054. deactivateHover();
  1055. deactivateLClick();
  1056. deactivateRClick();
  1057. }
  1058. CHallInterface::CBuildingBox::~CBuildingBox()
  1059. {
  1060. }
  1061. CHallInterface::CBuildingBox::CBuildingBox(int id)
  1062. :BID(id)
  1063. {
  1064. pos.w = 150;
  1065. pos.h = 70;
  1066. }
  1067. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  1068. :BID(id)
  1069. {
  1070. pos.x = x;
  1071. pos.y = y;
  1072. pos.w = 150;
  1073. pos.h = 70;
  1074. }
  1075. CHallInterface::CHallInterface(CCastleInterface * owner)
  1076. {
  1077. resdatabar = new CMinorResDataBar;
  1078. pos = owner->pos;
  1079. resdatabar->pos.x += pos.x;
  1080. resdatabar->pos.y += pos.y;
  1081. bg = BitmapHandler::loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  1082. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1083. exit = new AdventureMapButton
  1084. (CGI->generaltexth->tcommands[8],"",boost::bind(&CHallInterface::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1085. exit->assignedKeys.insert(SDLK_ESCAPE);
  1086. //preparing boxes with buildings//
  1087. boxes.resize(5);
  1088. for(size_t i=0;i<5;i++) //for each row
  1089. {
  1090. std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[owner->town->subID].second;
  1091. for(size_t j=0; j<boxList[i].size();j++) //for each box
  1092. {
  1093. size_t k=0;
  1094. for(;k<boxList[i][j].size();k++)//we are looking for the first not build structure
  1095. {
  1096. int bid = boxList[i][j][k];
  1097. if(!vstd::contains(owner->town->builtBuildings,bid))
  1098. {
  1099. int x = 34 + 194*j,
  1100. y = 37 + 104*i,
  1101. ID = bid;
  1102. if(boxList[i].size() == 2) //only two boxes in this row
  1103. x+=194;
  1104. else if(boxList[i].size() == 3) //only three boxes in this row
  1105. x+=97;
  1106. boxes[i].push_back(new CBuildingBox(bid,pos.x+x,pos.y+y));
  1107. //if this is horde dwelling for upgraded creatures and we already have one for basic creatures
  1108. if((bid == 25 && vstd::contains(owner->town->builtBuildings,24))
  1109. || (bid == 19 && vstd::contains(owner->town->builtBuildings,18))
  1110. )
  1111. {
  1112. boxes[i].back()->state = 4; //already built
  1113. }
  1114. else
  1115. {
  1116. boxes[i].back()->state = LOCPLINT->cb->canBuildStructure(owner->town,ID);
  1117. }
  1118. break;
  1119. }
  1120. }
  1121. if(k == boxList[i][j].size()) //all buildings built - let's take the last one
  1122. {
  1123. int x = 34 + 194*j,
  1124. y = 37 + 104*i;
  1125. if(boxList[i].size() == 2)
  1126. x+=194;
  1127. else if(boxList[i].size() == 3)
  1128. x+=97;
  1129. boxes[i].push_back(new CBuildingBox(boxList[i][j][k-1],pos.x+x,pos.y+y));
  1130. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  1131. }
  1132. }
  1133. }
  1134. }
  1135. CHallInterface::~CHallInterface()
  1136. {
  1137. SDL_FreeSurface(bg);
  1138. for(size_t i=0;i<boxes.size();i++)
  1139. for(size_t j=0;j<boxes[i].size();j++)
  1140. delete boxes[i][j]; //TODO whats wrong with smartpointers?
  1141. delete exit;
  1142. delete resdatabar;
  1143. }
  1144. void CHallInterface::close()
  1145. {
  1146. GH.popIntTotally(this);
  1147. }
  1148. void CHallInterface::show(SDL_Surface * to)
  1149. {
  1150. blitAt(bg,pos,to);
  1151. LOCPLINT->castleInt->statusbar->show(to);
  1152. resdatabar->show(to);
  1153. exit->show(to);
  1154. for(int i=0; i<5; i++)
  1155. {
  1156. for(size_t j=0;j<boxes[i].size(); ++j)
  1157. boxes[i][j]->show(to);
  1158. }
  1159. }
  1160. void CHallInterface::activate()
  1161. {
  1162. for(int i=0;i<5;i++)
  1163. {
  1164. for(size_t j=0; j < boxes[i].size(); ++j)
  1165. {
  1166. boxes[i][j]->activate();
  1167. }
  1168. }
  1169. exit->activate();
  1170. }
  1171. void CHallInterface::deactivate()
  1172. {
  1173. for(int i=0;i<5;i++)
  1174. {
  1175. for(size_t j=0;j<boxes[i].size();++j)
  1176. {
  1177. boxes[i][j]->deactivate();
  1178. }
  1179. }
  1180. exit->deactivate();
  1181. }
  1182. void CHallInterface::CBuildWindow::activate()
  1183. {
  1184. activateRClick();
  1185. if(mode)
  1186. return;
  1187. if(state==7)
  1188. buy->activate();
  1189. cancel->activate();
  1190. }
  1191. void CHallInterface::CBuildWindow::deactivate()
  1192. {
  1193. deactivateRClick();
  1194. if(mode)
  1195. return;
  1196. if(state==7)
  1197. buy->deactivate();
  1198. cancel->deactivate();
  1199. }
  1200. void CHallInterface::CBuildWindow::Buy()
  1201. {
  1202. int building = bid;
  1203. GH.popInts(2); //we - build window and hall screen
  1204. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,building);
  1205. }
  1206. void CHallInterface::CBuildWindow::close()
  1207. {
  1208. GH.popIntTotally(this);
  1209. }
  1210. void CHallInterface::CBuildWindow::clickRight(tribool down, bool previousState)
  1211. {
  1212. if((!down || indeterminate(down)) && mode)
  1213. close();
  1214. }
  1215. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  1216. {
  1217. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  1218. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  1219. SDL_BlitSurface(bitmap,&poms,to,&pom);
  1220. if(!mode)
  1221. {
  1222. buy->show(to);
  1223. cancel->show(to);
  1224. }
  1225. }
  1226. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  1227. {
  1228. std::string ret;
  1229. if(state<7)
  1230. ret = CGI->generaltexth->hcommands[state];
  1231. switch (state)
  1232. {
  1233. case 4: case 5: case 6:
  1234. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->Name());
  1235. break;
  1236. case 7:
  1237. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1238. case 8:
  1239. {
  1240. ret = CGI->generaltexth->allTexts[52];
  1241. std::set<int> used;
  1242. used.insert(bid);
  1243. std::set<int> reqs;
  1244. for(std::set<int>::iterator i=CGI->townh->requirements[tid][bid].begin();
  1245. i!=CGI->townh->requirements[tid][bid].end();
  1246. i++
  1247. )
  1248. {
  1249. if (LOCPLINT->castleInt->town->builtBuildings.find(*i) == LOCPLINT->castleInt->town->builtBuildings.end())
  1250. reqs.insert(*i);
  1251. }
  1252. while(true)
  1253. {
  1254. size_t czystych=0;
  1255. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1256. {
  1257. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  1258. {
  1259. used.insert(*i);
  1260. for(
  1261. std::set<int>::iterator j=CGI->townh->requirements[tid][*i].begin();
  1262. j!=CGI->townh->requirements[tid][*i].end();
  1263. j++
  1264. )
  1265. {
  1266. if(LOCPLINT->castleInt->town->builtBuildings.find(*j) == //this building is not built
  1267. LOCPLINT->castleInt->town->builtBuildings.end())
  1268. reqs.insert(*j);
  1269. }
  1270. }
  1271. else
  1272. {
  1273. czystych++;
  1274. }
  1275. }
  1276. if(czystych==reqs.size())
  1277. break;
  1278. }
  1279. bool first=true;
  1280. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1281. {
  1282. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->Name());
  1283. first = false;
  1284. }
  1285. }
  1286. }
  1287. return ret;
  1288. }
  1289. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  1290. :tid(Tid), bid(Bid), state(State), mode(Mode)
  1291. {
  1292. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPUBUILD.bmp");
  1293. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1294. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  1295. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  1296. SDL_FreeSurface(hhlp);
  1297. pos.x = screen->w/2 - bitmap->w/2;
  1298. pos.y = screen->h/2 - bitmap->h/2;
  1299. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  1300. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->Name());
  1301. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->Description(),197,168,GEOR16,40,zwykly,bitmap);
  1302. CSDL_Ext::printAtMiddleWB(getTextForState(state),197,248,GEOR13,50,zwykly,bitmap);
  1303. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->generaltexth->hcommands[7],pom),197,30,GEOR16,tytulowy,bitmap);
  1304. int resamount=0;
  1305. for(int i=0;i<7;i++)
  1306. {
  1307. if(CGI->buildh->buildings[tid][bid]->resources[i])
  1308. {
  1309. resamount++;
  1310. }
  1311. }
  1312. int ah = (resamount>4) ? 304 : 341;
  1313. int cn=-1, it=0;
  1314. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  1315. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  1316. char buf[15];
  1317. while(++cn<7)
  1318. {
  1319. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  1320. continue;
  1321. SDL_itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  1322. if(it<4)
  1323. {
  1324. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+42,GEOR16,zwykly,bitmap);
  1325. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  1326. }
  1327. else
  1328. {
  1329. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+42,GEOR16,zwykly,bitmap);
  1330. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  1331. }
  1332. if(it==4)
  1333. ah+=75;
  1334. }
  1335. if(!mode)
  1336. {
  1337. buy = new AdventureMapButton
  1338. ("","",boost::bind(&CBuildWindow::Buy,this),pos.x+45,pos.y+446,"IBUY30.DEF",SDLK_RETURN);
  1339. cancel = new AdventureMapButton
  1340. ("","",boost::bind(&CBuildWindow::close,this),pos.x+290,pos.y+445,"ICANCEL.DEF",SDLK_ESCAPE);
  1341. if(state!=7)
  1342. buy->state=2;
  1343. }
  1344. }
  1345. CHallInterface::CBuildWindow::~CBuildWindow()
  1346. {
  1347. SDL_FreeSurface(bitmap);
  1348. if(!mode)
  1349. {
  1350. delete buy;
  1351. delete cancel;
  1352. }
  1353. }
  1354. CFortScreen::~CFortScreen()
  1355. {
  1356. for(size_t i=0;i<crePics.size();i++)
  1357. delete crePics[i];
  1358. for (size_t i=0;i<recAreas.size();i++)
  1359. delete recAreas[i];
  1360. SDL_FreeSurface(bg);
  1361. delete exit;
  1362. delete resdatabar;
  1363. }
  1364. void CFortScreen::show( SDL_Surface * to)
  1365. {
  1366. blitAt(bg,pos,to);
  1367. static unsigned char anim = 1;
  1368. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1369. {
  1370. crePics[i]->blitPic(to,pos.x+positions[i].x+159,pos.y+positions[i].y+4,!(anim%4));
  1371. }
  1372. anim++;
  1373. exit->show(to);
  1374. resdatabar->show(to);
  1375. LOCPLINT->statusbar->show(to);
  1376. }
  1377. void CFortScreen::activate()
  1378. {
  1379. LOCPLINT->statusbar = LOCPLINT->castleInt->statusbar;
  1380. exit->activate();
  1381. for (size_t i=0;i<recAreas.size(); ++i)
  1382. {
  1383. recAreas[i]->activate();
  1384. }
  1385. }
  1386. void CFortScreen::deactivate()
  1387. {
  1388. exit->deactivate();
  1389. for (size_t i=0;i<recAreas.size();i++)
  1390. {
  1391. recAreas[i]->deactivate();
  1392. }
  1393. }
  1394. void CFortScreen::close()
  1395. {
  1396. GH.popIntTotally(this);
  1397. }
  1398. CFortScreen::CFortScreen( CCastleInterface * owner )
  1399. {
  1400. resdatabar = new CMinorResDataBar;
  1401. pos = owner->pos;
  1402. bg = NULL;
  1403. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CFortScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1404. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1405. genRect(126,386,10,155),genRect(126,386,404,155),
  1406. genRect(126,386,10,288),genRect(126,386,404,288),
  1407. genRect(126,386,206,421);
  1408. draw(owner,true);
  1409. resdatabar->pos += pos;
  1410. }
  1411. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1412. {
  1413. if(bg)
  1414. SDL_FreeSurface(bg);
  1415. char buf[20];
  1416. memset(buf,0,20);
  1417. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1418. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1419. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1420. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1421. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1422. SDL_FreeSurface(bg2);
  1423. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name(),400,13,GEORXX,zwykly,bg);
  1424. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1425. {
  1426. bool upgraded = owner->town->creatureDwelling(i,true);
  1427. bool present = owner->town->creatureDwelling(i,false);
  1428. CCreature *c = &CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1429. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,GEOR13,zwykly,bg); //cr. name
  1430. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1431. printAtMiddle(CGI->buildh->buildings[owner->town->subID][30+i+upgraded*7]->Name(),positions[i].x+79,positions[i].y+100,GEOR13,zwykly,bg); //dwelling name
  1432. if(present) //if creature is present print avail able quantity
  1433. {
  1434. SDL_itoa(owner->town->creatures[i].first,buf,10);
  1435. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,GEOR13,zwykly,bg);
  1436. }
  1437. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1438. //attack
  1439. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,GEOR13,zwykly,bg);
  1440. SDL_itoa(c->attack,buf,10);
  1441. printToWR(buf,positions[i].x+381,positions[i].y+18,GEOR13,zwykly,bg);
  1442. //defense
  1443. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,GEOR13,zwykly,bg);
  1444. SDL_itoa(c->defence,buf,10);
  1445. printToWR(buf,positions[i].x+381,positions[i].y+38,GEOR13,zwykly,bg);
  1446. //damage
  1447. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,GEOR13,zwykly,bg);
  1448. SDL_itoa(c->damageMin,buf,10);
  1449. int hlp;
  1450. if(c->damageMin > 0)
  1451. hlp = log10f(c->damageMin)+2;
  1452. else
  1453. hlp = 2;
  1454. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1455. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1456. printToWR(buf,positions[i].x+381,positions[i].y+59,GEOR13,zwykly,bg);
  1457. //health
  1458. printAt(CGI->generaltexth->zelp[439].first,positions[i].x+288,positions[i].y+66,GEOR13,zwykly,bg);
  1459. SDL_itoa(c->hitPoints,buf,10);
  1460. printToWR(buf,positions[i].x+381,positions[i].y+79,GEOR13,zwykly,bg);
  1461. //speed
  1462. printAt(CGI->generaltexth->zelp[441].first,positions[i].x+288,positions[i].y+87,GEOR13,zwykly,bg);
  1463. SDL_itoa(c->speed,buf,10);
  1464. printToWR(buf,positions[i].x+381,positions[i].y+100,GEOR13,zwykly,bg);
  1465. if(present)//growth
  1466. {
  1467. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,GEOR13,zwykly,bg);
  1468. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1469. printToWR(buf,positions[i].x+381,positions[i].y+120,GEOR13,zwykly,bg);
  1470. }
  1471. if(first)
  1472. {
  1473. crePics.push_back(new CCreaturePic(c,false));
  1474. if(present)
  1475. {
  1476. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1477. recAreas.back()->pos = positions[i] + pos;
  1478. }
  1479. }
  1480. }
  1481. SDL_FreeSurface(icons);
  1482. }
  1483. void CFortScreen::RecArea::clickLeft(tribool down, bool previousState)
  1484. {
  1485. if(!down && previousState)
  1486. {
  1487. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1488. }
  1489. //ClickableL::clickLeft(down);
  1490. }
  1491. void CFortScreen::RecArea::activate()
  1492. {
  1493. activateLClick();
  1494. activateRClick();
  1495. }
  1496. void CFortScreen::RecArea::deactivate()
  1497. {
  1498. deactivateLClick();
  1499. deactivateRClick();
  1500. }
  1501. void CFortScreen::RecArea::clickRight(tribool down, bool previousState)
  1502. {
  1503. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1504. }
  1505. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1506. {
  1507. resdatabar = new CMinorResDataBar;
  1508. pos = owner->pos;
  1509. resdatabar->pos.x += pos.x;
  1510. resdatabar->pos.y += pos.y;
  1511. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1512. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CMageGuildScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1513. exit->assignedKeys.insert(SDLK_ESCAPE);
  1514. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1515. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1516. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1517. positions.resize(5);
  1518. positions[0] += genRect(61,83,222,445), genRect(61,83,312,445), genRect(61,83,402,445), genRect(61,83,520,445), genRect(61,83,610,445), genRect(61,83,700,445);
  1519. positions[1] += genRect(61,83,48,53), genRect(61,83,48,147), genRect(61,83,48,241), genRect(61,83,48,335), genRect(61,83,48,429);
  1520. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1521. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1522. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1523. blitAt(view,332,76,bg);
  1524. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1525. {
  1526. size_t sp = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1527. for(size_t j=0; j<sp; j++)
  1528. {
  1529. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1530. {
  1531. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1532. spells[spells.size()-1].pos = positions[i][j];
  1533. blitAt(graphics->spellscr->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1534. }
  1535. else
  1536. {
  1537. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1538. }
  1539. }
  1540. }
  1541. SDL_FreeSurface(view);
  1542. for(size_t i=0;i<spells.size();i++)
  1543. {
  1544. spells[i].pos.x += pos.x;
  1545. spells[i].pos.y += pos.y;
  1546. }
  1547. delete scrolls2;
  1548. }
  1549. CMageGuildScreen::~CMageGuildScreen()
  1550. {
  1551. delete exit;
  1552. SDL_FreeSurface(bg);
  1553. delete resdatabar;
  1554. }
  1555. void CMageGuildScreen::close()
  1556. {
  1557. GH.popIntTotally(this);
  1558. }
  1559. void CMageGuildScreen::show(SDL_Surface * to)
  1560. {
  1561. blitAt(bg,pos,to);
  1562. resdatabar->show(to);
  1563. LOCPLINT->statusbar->show(to);
  1564. exit->show(to);
  1565. }
  1566. void CMageGuildScreen::activate()
  1567. {
  1568. exit->activate();
  1569. for(size_t i=0;i<spells.size();i++)
  1570. {
  1571. spells[i].activate();
  1572. }
  1573. }
  1574. void CMageGuildScreen::deactivate()
  1575. {
  1576. exit->deactivate();
  1577. for(size_t i=0;i<spells.size();i++)
  1578. {
  1579. spells[i].deactivate();
  1580. }
  1581. }
  1582. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1583. {
  1584. if(down)
  1585. {
  1586. std::vector<SComponent*> comps(1,
  1587. new CCustomImgComponent(SComponent::spell,spell->id,0,graphics->spellscr->ourImages[spell->id].bitmap,false)
  1588. );
  1589. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1590. }
  1591. }
  1592. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1593. {
  1594. if(down)
  1595. {
  1596. CInfoPopup *vinya = new CInfoPopup();
  1597. vinya->free = true;
  1598. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1599. (LOCPLINT->playerID,
  1600. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1601. spell->name,30,30);
  1602. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1603. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1604. GH.pushInt(vinya);
  1605. }
  1606. }
  1607. void CMageGuildScreen::Scroll::hover(bool on)
  1608. {
  1609. //Hoverable::hover(on);
  1610. if(on)
  1611. LOCPLINT->statusbar->print(spell->name);
  1612. else
  1613. LOCPLINT->statusbar->clear();
  1614. }
  1615. void CMageGuildScreen::Scroll::activate()
  1616. {
  1617. activateLClick();
  1618. activateRClick();
  1619. activateHover();
  1620. }
  1621. void CMageGuildScreen::Scroll::deactivate()
  1622. {
  1623. deactivateLClick();
  1624. deactivateRClick();
  1625. deactivateHover();
  1626. }
  1627. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1628. {
  1629. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1630. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1631. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1632. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1633. SDL_FreeSurface(bg2);
  1634. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1635. blitAt(bg2,64,50,bmp);
  1636. SDL_FreeSurface(bg2);
  1637. CCreatureAnimation cra(CGI->creh->creatures[creMachineID].animDefName);
  1638. cra.nextFrameMiddle(bmp,170,120,true,0,false);
  1639. char pom[75];
  1640. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID].nameSing.c_str()); //build a new ...
  1641. printAtMiddle(pom,165,28,GEORXX,tytulowy,bmp);
  1642. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,GEOR16,zwykly,bmp); //resource cost
  1643. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1644. printAtMiddle(pom,165,290,GEOR13,zwykly,bmp);
  1645. pos.w = bmp->w;
  1646. pos.h = bmp->h;
  1647. pos.x = screen->w/2 - pos.w/2;
  1648. pos.y = screen->h/2 - pos.h/2;
  1649. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF",SDLK_RETURN);
  1650. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1651. if(possible)
  1652. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1653. else
  1654. buy->bitmapOffset = 2;
  1655. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1656. }
  1657. void CBlacksmithDialog::show( SDL_Surface * to )
  1658. {
  1659. blitAt(bmp,pos,to);
  1660. buy->show(to);
  1661. cancel->show(to);
  1662. }
  1663. void CBlacksmithDialog::activate()
  1664. {
  1665. if(!buy->bitmapOffset)
  1666. buy->activate();
  1667. cancel->activate();
  1668. }
  1669. void CBlacksmithDialog::deactivate()
  1670. {
  1671. if(!buy->bitmapOffset)
  1672. buy->deactivate();
  1673. cancel->deactivate();
  1674. }
  1675. CBlacksmithDialog::~CBlacksmithDialog()
  1676. {
  1677. SDL_FreeSurface(bmp);
  1678. delete cancel;
  1679. delete buy;
  1680. }
  1681. void CBlacksmithDialog::close()
  1682. {
  1683. GH.popIntTotally(this);
  1684. }