CCastleInterface.cpp 49 KB

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