CCastleInterface.cpp 47 KB

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