CCastleInterface.cpp 45 KB

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