CCastleInterface.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #include "stdafx.h"
  2. #include "CCastleInterface.h"
  3. #include "hch/CObjectHandler.h"
  4. #include "CGameInfo.h"
  5. #include "hch/CLodHandler.h"
  6. #include "SDL_Extensions.h"
  7. #include "CAdvmapInterface.h"
  8. #include "hch/CTownHandler.h"
  9. #include "AdventureMapButton.h"
  10. #include "hch/CBuildingHandler.h"
  11. #include <sstream>
  12. #include "CMessage.h"
  13. #include "hch/CGeneralTextHandler.h"
  14. CBuildingRect::CBuildingRect(Structure *Str)
  15. :str(Str)
  16. {
  17. def = CGI->spriteh->giveDef(Str->defName);
  18. pos.x = str->pos.x;
  19. pos.y = str->pos.y;
  20. pos.w = def->ourImages[0].bitmap->w;
  21. pos.h = def->ourImages[0].bitmap->h;
  22. if(Str->ID<0 || (Str->ID>=27 && Str->ID<=29))
  23. {
  24. area = border = NULL;
  25. return;
  26. }
  27. if (border = CGI->bitmaph->loadBitmap(str->borderName))
  28. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  29. else
  30. std::cout << "Warning: no border for "<<Str->ID<<std::endl;
  31. if (area = CGI->bitmaph->loadBitmap(str->areaName))
  32. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  33. else
  34. std::cout << "Warning: no area for "<<Str->ID<<std::endl;
  35. }
  36. CBuildingRect::~CBuildingRect()
  37. {
  38. delete def;
  39. if(border)
  40. SDL_FreeSurface(border);
  41. if(area)
  42. SDL_FreeSurface(area);
  43. }
  44. void CBuildingRect::activate()
  45. {
  46. Hoverable::activate();
  47. ClickableL::activate();
  48. ClickableR::activate();
  49. }
  50. void CBuildingRect::deactivate()
  51. {
  52. Hoverable::deactivate();
  53. ClickableL::deactivate();
  54. ClickableR::deactivate();
  55. }
  56. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  57. {
  58. if(str->pos.z != p2.str->pos.z)
  59. return (str->pos.z) < (p2.str->pos.z);
  60. else
  61. return (str->ID) < (p2.str->ID);
  62. }
  63. void CBuildingRect::hover(bool on)
  64. {
  65. Hoverable::hover(on);
  66. if(on)
  67. {
  68. MotionInterested::activate();
  69. }
  70. else
  71. {
  72. MotionInterested::deactivate();
  73. if(LOCPLINT->castleInt->hBuild == this)
  74. {
  75. LOCPLINT->castleInt->hBuild = NULL;
  76. LOCPLINT->statusbar->clear();
  77. }
  78. }
  79. }
  80. void CBuildingRect::clickLeft (tribool down)
  81. {
  82. 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
  83. {
  84. if(pressedL && !down)
  85. LOCPLINT->castleInt->buildingClicked(str->ID);
  86. ClickableL::clickLeft(down);
  87. }
  88. //todo - handle
  89. }
  90. void CBuildingRect::clickRight (tribool down)
  91. {
  92. if((!area) || (!((bool)down)) || (this!=LOCPLINT->castleInt->hBuild))
  93. return;
  94. if((CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->current->motion.y-pos.y) != 0)) //na polu
  95. {
  96. CInfoPopup *vinya = new CInfoPopup();
  97. vinya->free = true;
  98. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  99. (LOCPLINT->playerID,
  100. CGI->buildh->buildings[str->townID][str->ID]->description,
  101. LOCPLINT->castleInt->bicons->ourImages[str->ID].bitmap,
  102. CGI->buildh->buildings[str->townID][str->ID]->name);
  103. vinya->pos.x = ekran->w/2 - vinya->bitmap->w/2;
  104. vinya->pos.y = ekran->h/2 - vinya->bitmap->h/2;
  105. vinya->activate();
  106. }
  107. }
  108. void CBuildingRect::mouseMoved (SDL_MouseMotionEvent & sEvent)
  109. {
  110. if(area)
  111. {
  112. if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
  113. {
  114. if(LOCPLINT->castleInt->hBuild == this)
  115. {
  116. LOCPLINT->castleInt->hBuild = NULL;
  117. LOCPLINT->statusbar->clear();
  118. }
  119. }
  120. else //inside the area of this building
  121. {
  122. if(LOCPLINT->castleInt->hBuild) //a building is hovered
  123. {
  124. if((*LOCPLINT->castleInt->hBuild)<(*this)) //set if we are on top
  125. {
  126. LOCPLINT->castleInt->hBuild = this;
  127. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->name.length())
  128. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->name);
  129. else
  130. LOCPLINT->statusbar->print(str->name);
  131. }
  132. }
  133. else //no building hovered
  134. {
  135. LOCPLINT->castleInt->hBuild = this;
  136. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->name.length())
  137. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->name);
  138. else
  139. LOCPLINT->statusbar->print(str->name);
  140. }
  141. }
  142. }
  143. //if(border)
  144. // blitAt(border,pos.x,pos.y);
  145. }
  146. std::string getBgName(int type) //TODO - co z tym zrobiæ?
  147. {
  148. switch (type)
  149. {
  150. case 0:
  151. return "TBCSBACK.bmp";
  152. case 1:
  153. return "TBRMBACK.bmp";
  154. case 2:
  155. return "TBTWBACK.bmp";
  156. case 3:
  157. return "TBINBACK.bmp";
  158. case 4:
  159. return "TBNCBACK.bmp";
  160. case 5:
  161. return "TBDNBACK.bmp";
  162. case 6:
  163. return "TBSTBACK.bmp";
  164. case 7:
  165. return "TBFRBACK.bmp";
  166. case 8:
  167. return "TBELBACK.bmp";
  168. default:
  169. throw new std::exception("std::string getBgName(int type): invalid type");
  170. }
  171. }
  172. class SORTHELP
  173. {
  174. public:
  175. bool operator ()
  176. (const CBuildingRect *a ,
  177. const CBuildingRect *b)
  178. {
  179. return (*a)<(*b);
  180. }
  181. } srthlp ;
  182. CCastleInterface::CCastleInterface(const CGTownInstance * Town, bool Activate)
  183. {
  184. hall = NULL;
  185. townInt = CGI->bitmaph->loadBitmap("TOWNSCRN.bmp");
  186. cityBg = CGI->bitmaph->loadBitmap(getBgName(Town->subID));
  187. hall = CGI->spriteh->giveDef("ITMTL.DEF");
  188. fort = CGI->spriteh->giveDef("ITMCL.DEF");
  189. flag = CGI->spriteh->giveDef("CREST58.DEF");
  190. townlist = new CTownList<CCastleInterface>(3,&genRect(128,48,744,414),744,414,744,526);
  191. exit = new AdventureMapButton<CCastleInterface>
  192. (CGI->townh->tcommands[8],"",&CCastleInterface::close,744,544,"TSBTNS.DEF",this,false,NULL,false);
  193. split = new AdventureMapButton<CCastleInterface>
  194. (CGI->townh->tcommands[3],"",&CCastleInterface::splitF,744,382,"TSBTNS.DEF",this,false,NULL,false);
  195. statusbar = new CStatusBar(8,555,"TSTATBAR.bmp",732);
  196. townlist->owner = this;
  197. townlist->fun = &CCastleInterface::townChange;
  198. townlist->genList();
  199. townlist->selected = getIndexOf(townlist->items,Town);
  200. if((townlist->selected+1) > townlist->SIZE)
  201. townlist->from = townlist->selected - townlist->SIZE + 1;
  202. hBuild = NULL;
  203. count=0;
  204. town = Town;
  205. CSDL_Ext::blueToPlayersAdv(townInt,LOCPLINT->playerID);
  206. exit->bitmapOffset = 4;
  207. std::set< std::pair<int,int> > s; //group - id
  208. //buildings
  209. for (std::set<int>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  210. {
  211. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  212. {
  213. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  214. {
  215. Structure * st = CGI->townh->structures[town->subID][*i];
  216. if(st->group<0) //no group - just add it
  217. {
  218. buildings.push_back(new CBuildingRect(st));
  219. }
  220. else
  221. {
  222. std::set< std::pair<int,int> >::iterator obecny=s.end();
  223. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  224. {
  225. if(seti->first == st->group)
  226. {
  227. obecny = seti;
  228. break;
  229. }
  230. }
  231. if(obecny != s.end())
  232. {
  233. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  234. {
  235. for(int itpb = 0; itpb<buildings.size(); itpb++)
  236. {
  237. if(buildings[itpb]->str->ID == obecny->second)
  238. {
  239. delete buildings[itpb];
  240. buildings.erase(buildings.begin() + itpb);
  241. obecny->second = st->ID;
  242. buildings.push_back(new CBuildingRect(st));
  243. }
  244. }
  245. }
  246. }
  247. else
  248. {
  249. buildings.push_back(new CBuildingRect(st));
  250. s.insert(std::pair<int,int>(st->group,st->ID));
  251. }
  252. }
  253. }
  254. else continue;
  255. }
  256. else
  257. break;
  258. }
  259. //garrison
  260. std::sort(buildings.begin(),buildings.end(),srthlp);
  261. garr = new CGarrisonInt(305,387,4,32,townInt,243,13,town,town->visitingHero);
  262. if(Activate)
  263. {
  264. LOCPLINT->objsToBlit.push_back(this);
  265. activate();
  266. showAll();
  267. }
  268. std::string defname;
  269. switch (town->subID)
  270. {
  271. case 0:
  272. defname = "HALLCSTL.DEF";
  273. break;
  274. case 1:
  275. defname = "HALLRAMP.DEF";
  276. break;
  277. case 2:
  278. defname = "HALLTOWR.DEF";
  279. break;
  280. case 3:
  281. defname = "HALLINFR.DEF";
  282. break;
  283. case 4:
  284. defname = "HALLNECR.DEF";
  285. break;
  286. case 5:
  287. defname = "HALLDUNG.DEF";
  288. break;
  289. case 6:
  290. defname = "HALLSTRN.DEF";
  291. break;
  292. case 7:
  293. defname = "HALLFORT.DEF";
  294. break;
  295. case 8:
  296. defname = "HALLELEM.DEF";
  297. break;
  298. default:
  299. throw new std::exception("Bad town subID");
  300. }
  301. bicons = CGI->spriteh->giveDefEss(defname);
  302. //blit buildings on bg
  303. //for(int i=0;i<buildings.size();i++)
  304. //{
  305. // blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,cityBg);
  306. //}
  307. }
  308. CCastleInterface::~CCastleInterface()
  309. {
  310. SDL_FreeSurface(townInt);
  311. SDL_FreeSurface(cityBg);
  312. delete exit;
  313. delete split;
  314. delete hall;
  315. delete fort;
  316. delete flag;
  317. delete garr;
  318. delete townlist;
  319. delete statusbar;
  320. for(int i=0;i<buildings.size();i++)
  321. {
  322. delete buildings[i];
  323. }
  324. delete bicons;
  325. }
  326. void CCastleInterface::close()
  327. {
  328. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  329. deactivate();
  330. LOCPLINT->castleInt = NULL;
  331. LOCPLINT->adventureInt->activate();
  332. delete this;
  333. }
  334. void CCastleInterface::splitF()
  335. {
  336. }
  337. void CCastleInterface::buildingClicked(int building)
  338. {
  339. std::cout<<"You've clicked on "<<building<<std::endl;
  340. switch(building)
  341. {
  342. case 10: case 11: case 12: case 13:
  343. enterHall();
  344. break;
  345. default:
  346. std::cout<<"This building isn't handled...\n";
  347. }
  348. }
  349. void CCastleInterface::enterHall()
  350. {
  351. deactivate();
  352. hallInt = new CHallInterface(this);
  353. hallInt->activate();
  354. hallInt->show();
  355. }
  356. void CCastleInterface::showAll(SDL_Surface * to)
  357. {
  358. if (!to)
  359. to=ekran;
  360. blitAt(cityBg,0,0,to);
  361. blitAt(townInt,0,374,to);
  362. LOCPLINT->adventureInt->resdatabar.draw();
  363. townlist->draw();
  364. statusbar->show();
  365. garr->show();
  366. int pom;
  367. //draw fort icon
  368. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  369. pom = 2;
  370. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  371. pom = 1;
  372. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  373. pom = 0;
  374. else pom = 3;
  375. blitAt(fort->ourImages[pom].bitmap,122,413,to);
  376. //draw ((village/town/city) hall)/capitol icon
  377. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  378. pom = 3;
  379. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  380. pom = 2;
  381. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  382. pom = 1;
  383. else pom = 0;
  384. blitAt(hall->ourImages[pom].bitmap,80,413,to);
  385. //draw creatures icons and their growths
  386. for(int i=0;i<CREATURES_PER_TOWN;i++)
  387. {
  388. int cid = -1;
  389. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  390. {
  391. cid = (14*town->subID)+(i*2);
  392. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  393. {
  394. cid++;
  395. }
  396. }
  397. if (cid>=0)
  398. {
  399. int pomx, pomy;
  400. pomx = 22 + (55*((i>3)?(i-4):i));
  401. pomy = (i>3)?(507):(459);
  402. blitAt(CGI->creh->smallImgs[cid],pomx,pomy,to);
  403. std::ostringstream oss;
  404. oss << '+' << (CGI->creh->creatures[cid].growth + town->creatureIncome[i]);
  405. CSDL_Ext::printAtMiddle(oss.str(),pomx+16,pomy+37,GEOR13,zwykly,to);
  406. }
  407. }
  408. //print name and income
  409. CSDL_Ext::printAt(town->name,85,389,GEOR13,zwykly,to);
  410. char temp[10];
  411. itoa(town->dailyIncome(),temp,10);
  412. CSDL_Ext::printAtMiddle(temp,195,442,GEOR13,zwykly,to);
  413. //blit town icon
  414. pom = town->subID*2;
  415. if (!town->hasFort())
  416. pom += F_NUMBER*2;
  417. if(town->builded >= MAX_BUILDING_PER_TURN)
  418. pom++;
  419. blitAt(LOCPLINT->bigTownPic->ourImages[pom].bitmap,15,387,to);
  420. //flag
  421. if(town->getOwner()<PLAYER_LIMIT)
  422. blitAt(flag->ourImages[town->getOwner()].bitmap,241,387,to);
  423. //print garrison
  424. //for(
  425. // std::map<int,std::pair<CCreature*,int> >::const_iterator i=town->garrison.slots.begin();
  426. // i!=town->garrison.slots.end();
  427. // i++
  428. // )
  429. //{
  430. // blitAt(CGI->creh->bigImgs[i->second.first->idNumber],305+(62*(i->first)),387,to);
  431. // itoa(i->second.second,temp,10);
  432. // CSDL_Ext::printTo(temp,305+(62*(i->first))+57,387+61,GEOR13,zwykly,to);
  433. //}
  434. show();
  435. }
  436. void CCastleInterface::townChange()
  437. {
  438. const CGTownInstance * nt = townlist->items[townlist->selected];
  439. deactivate();
  440. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  441. delete this;
  442. LOCPLINT->castleInt = new CCastleInterface(nt,true);
  443. }
  444. void CCastleInterface::show(SDL_Surface * to)
  445. {
  446. if(!showing)
  447. return;
  448. if (!to)
  449. to=ekran;
  450. count++;
  451. if(count==4)
  452. {
  453. count=0;
  454. animval++;
  455. }
  456. blitAt(cityBg,0,0,to);
  457. //blit buildings
  458. for(int i=0;i<buildings.size();i++)
  459. {
  460. if((animval)%(buildings[i]->def->ourImages.size()))
  461. {
  462. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  463. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  464. }
  465. else
  466. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  467. //if(buildings[i]->hovered && buildings[i]->border)
  468. // blitAt(buildings[i]->border,buildings[i]->pos.x,buildings[i]->pos.y);
  469. if(hBuild==buildings[i] && hBuild->border)
  470. blitAt(hBuild->border,hBuild->pos,to);
  471. }
  472. }
  473. void CCastleInterface::activate()
  474. {
  475. showing = true;
  476. townlist->activate();
  477. garr->activate();
  478. LOCPLINT->curint = this;
  479. LOCPLINT->statusbar = statusbar;
  480. exit->activate();
  481. split->activate();
  482. for(int i=0;i<buildings.size();i++)
  483. buildings[i]->activate();
  484. }
  485. void CCastleInterface::deactivate()
  486. {
  487. showing = false;
  488. townlist->deactivate();
  489. garr->deactivate();
  490. exit->deactivate();
  491. split->deactivate();
  492. for(int i=0;i<buildings.size();i++)
  493. buildings[i]->deactivate();
  494. }
  495. void CHallInterface::CResDataBar::show(SDL_Surface * to)
  496. {
  497. blitAt(bg,pos.x,pos.y);
  498. char * buf = new char[15];
  499. for (int i=0;i<7;i++)
  500. {
  501. itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  502. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly);
  503. }
  504. std::vector<std::string> temp;
  505. itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  506. itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  507. itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  508. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  509. CGI->generaltexth->allTexts[62]
  510. +": %s, "
  511. + CGI->generaltexth->allTexts[63]
  512. + ": %s, "
  513. + CGI->generaltexth->allTexts[64]
  514. + ": %s",temp)
  515. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly);
  516. temp.clear();
  517. //updateRect(&pos,ekran);
  518. delete[] buf;
  519. }
  520. CHallInterface::CResDataBar::CResDataBar()
  521. {
  522. bg = CGI->bitmaph->loadBitmap("Z2ESBAR.bmp");
  523. CSDL_Ext::blueToPlayers(bg,LOCPLINT->playerID);
  524. pos.x = 7;
  525. pos.y = 575;
  526. pos.w = bg->w;
  527. pos.h = bg->h;
  528. }
  529. CHallInterface::CResDataBar::~CResDataBar()
  530. {
  531. SDL_FreeSurface(bg);
  532. }
  533. void CHallInterface::CBuildingBox::hover(bool on)
  534. {
  535. }
  536. void CHallInterface::CBuildingBox::clickLeft (tribool down)
  537. {
  538. }
  539. void CHallInterface::CBuildingBox::clickRight (tribool down)
  540. {
  541. }
  542. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  543. {
  544. blitAt(LOCPLINT->castleInt->bicons->ourImages[ID].bitmap,pos.x,pos.y);
  545. int pom;
  546. switch (state)
  547. {
  548. case 3:
  549. pom = 0;
  550. break;
  551. case 0:
  552. pom = 1;
  553. break;
  554. case 1: case 2:
  555. pom = 2;
  556. break;
  557. default:
  558. pom = 3;
  559. }
  560. blitAt(LOCPLINT->castleInt->hallInt->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71);
  561. CSDL_Ext::printAtMiddle(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][ID]->name,pos.x-1+LOCPLINT->castleInt->hallInt->bars->ourImages[0].bitmap->w/2,pos.y+71+LOCPLINT->castleInt->hallInt->bars->ourImages[0].bitmap->h/2, GEOR13,zwykly);
  562. }
  563. void CHallInterface::CBuildingBox::activate()
  564. {
  565. }
  566. void CHallInterface::CBuildingBox::deactivate()
  567. {
  568. }
  569. CHallInterface::CBuildingBox::~CBuildingBox()
  570. {
  571. }
  572. CHallInterface::CBuildingBox::CBuildingBox(int id)
  573. :ID(id)
  574. {
  575. pos.w = 150;
  576. pos.h = 70;
  577. }
  578. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  579. :ID(id)
  580. {
  581. pos.x = x;
  582. pos.y = y;
  583. }
  584. CHallInterface::CHallInterface(CCastleInterface * owner)
  585. {
  586. bg = CGI->bitmaph->loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  587. CSDL_Ext::blueToPlayers(bg,LOCPLINT->playerID);
  588. bars = CGI->spriteh->giveDefEss("TPTHBAR.DEF");
  589. status = CGI->spriteh->giveDefEss("TPTHCHK.DEF");
  590. exit = new AdventureMapButton<CHallInterface>
  591. (CGI->townh->tcommands[8],"",&CHallInterface::close,748,556,"TPMAGE1.DEF",this,false,NULL,false);
  592. for(int i=0;i<5;i++) //for each row
  593. {
  594. for(int j=0; j<CGI->buildh->hall[owner->town->subID].second[i].size();j++) //for each box
  595. {
  596. int k=0;
  597. for(;k<CGI->buildh->hall[owner->town->subID].second[i][j].size();k++)//we are looking for the first not build structure
  598. {
  599. if(
  600. (owner->town->builtBuildings.find(CGI->buildh->hall[owner->town->subID].second[i][j][k]))
  601. ==
  602. (owner->town->builtBuildings.end()) )
  603. {
  604. int x = 34 + 194*j,
  605. y = 37 + 104*i;
  606. if(CGI->buildh->hall[owner->town->subID].second[i].size() == 2)
  607. x+=194;
  608. else if(CGI->buildh->hall[owner->town->subID].second[i].size() == 3)
  609. x+=97;
  610. boxes[i].push_back(new CBuildingBox(CGI->buildh->hall[owner->town->subID].second[i][j][k],x,y));
  611. //can we build it?
  612. if(owner->town->possibleBuildings.find(CGI->buildh->hall[owner->town->subID].second[i][j][k])==owner->town->possibleBuildings.end())
  613. boxes[i][boxes[i].size()-1]->state = -1; //forbidden
  614. else if(owner->town->builded >= MAX_BUILDING_PER_TURN)
  615. boxes[i][boxes[i].size()-1]->state = 2; //forbidden
  616. //TODO: check requirements
  617. //else if(owner->town->builded >= MAX_BUILDING_PER_TURN)
  618. // boxes[i][boxes[i].size()-1]->state = 2; //forbidden
  619. else
  620. {
  621. CBuilding * pom = CGI->buildh->buildings[owner->town->subID][CGI->buildh->hall[owner->town->subID].second[i][j][k]];
  622. boxes[i][boxes[i].size()-1]->state = 0; //allowed
  623. for(int res=0;res<7;res++) //TODO: support custom amount of resources
  624. {
  625. if(pom->resources[res]>LOCPLINT->cb->getResourceAmount(res))
  626. boxes[i][boxes[i].size()-1]->state = 1; //lack of res
  627. }
  628. }
  629. break;
  630. }
  631. }
  632. if(k==CGI->buildh->hall[owner->town->subID].second[i][j].size()) //all buildings built - let's take the last one
  633. {
  634. int x = 34 + 194*j,
  635. y = 37 + 104*i;
  636. if(CGI->buildh->hall[owner->town->subID].second[i].size() == 2)
  637. x+=194;
  638. else if(CGI->buildh->hall[owner->town->subID].second[i].size() == 3)
  639. x+=97;
  640. boxes[i].push_back(new CBuildingBox(CGI->buildh->hall[owner->town->subID].second[i][j][k-1],x,y));
  641. boxes[i][boxes[i].size()-1]->state = 3; //already exists
  642. }
  643. }
  644. }
  645. }
  646. CHallInterface::~CHallInterface()
  647. {
  648. }
  649. void CHallInterface::close()
  650. {
  651. deactivate();
  652. LOCPLINT->castleInt->activate();
  653. LOCPLINT->castleInt->showAll();
  654. }
  655. void CHallInterface::show(SDL_Surface * to)
  656. {
  657. blitAt(bg,0,0);
  658. resdatabar.show();
  659. exit->show();
  660. for(int i=0; i<5; i++)
  661. {
  662. for(int j=0;j<boxes[i].size();j++)
  663. boxes[i][j]->show();
  664. }
  665. }
  666. void CHallInterface::activate()
  667. {
  668. for(int i=0;i<5;i++)
  669. for(int j=0;j<boxes[i].size();j++)
  670. boxes[i][j]->activate();
  671. exit->activate();
  672. }
  673. void CHallInterface::deactivate()
  674. {
  675. for(int i=0;i<5;i++)
  676. {
  677. for(int j=0;j<boxes[i].size();j++)
  678. {
  679. boxes[i][j]->deactivate();
  680. delete boxes[i][j];
  681. }
  682. }
  683. exit->deactivate();
  684. }