CCastleInterface.cpp 41 KB

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