CCastleInterface.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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]);
  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. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[51]); //Cannot build another boat
  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 || vobjs.front()->ID == HEROI_TYPE)) //there is visitable obj at shipyard output tile and it's a boat or hero (on 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. }