CCastleInterface.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. #include "CCallback.h"
  15. extern TTF_Font * GEOR16;
  16. CBuildingRect::CBuildingRect(Structure *Str)
  17. :str(Str), moi(false)
  18. {
  19. def = CGI->spriteh->giveDef(Str->defName);
  20. pos.x = str->pos.x;
  21. pos.y = str->pos.y;
  22. pos.w = def->ourImages[0].bitmap->w;
  23. pos.h = def->ourImages[0].bitmap->h;
  24. if(Str->ID<0 || (Str->ID>=27 && Str->ID<=29))
  25. {
  26. area = border = NULL;
  27. return;
  28. }
  29. if (border = CGI->bitmaph->loadBitmap(str->borderName))
  30. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  31. else
  32. std::cout << "Warning: no border for "<<Str->ID<<std::endl;
  33. if (area = CGI->bitmaph->loadBitmap(str->areaName))
  34. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  35. else
  36. std::cout << "Warning: no area for "<<Str->ID<<std::endl;
  37. }
  38. CBuildingRect::~CBuildingRect()
  39. {
  40. delete def;
  41. if(border)
  42. SDL_FreeSurface(border);
  43. if(area)
  44. SDL_FreeSurface(area);
  45. }
  46. void CBuildingRect::activate()
  47. {
  48. Hoverable::activate();
  49. ClickableL::activate();
  50. ClickableR::activate();
  51. }
  52. void CBuildingRect::deactivate()
  53. {
  54. Hoverable::deactivate();
  55. ClickableL::deactivate();
  56. ClickableR::deactivate();
  57. if(moi)
  58. MotionInterested::deactivate();
  59. moi=false;
  60. }
  61. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  62. {
  63. if(str->pos.z != p2.str->pos.z)
  64. return (str->pos.z) < (p2.str->pos.z);
  65. else
  66. return (str->ID) < (p2.str->ID);
  67. }
  68. void CBuildingRect::hover(bool on)
  69. {
  70. Hoverable::hover(on);
  71. if(on)
  72. {
  73. if(!moi)
  74. MotionInterested::activate();
  75. moi = true;
  76. }
  77. else
  78. {
  79. if(moi)
  80. MotionInterested::deactivate();
  81. moi = false;
  82. if(LOCPLINT->castleInt->hBuild == this)
  83. {
  84. LOCPLINT->castleInt->hBuild = NULL;
  85. LOCPLINT->statusbar->clear();
  86. }
  87. }
  88. }
  89. void CBuildingRect::clickLeft (tribool down)
  90. {
  91. 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
  92. {
  93. if(pressedL && !down)
  94. LOCPLINT->castleInt->buildingClicked(str->ID);
  95. ClickableL::clickLeft(down);
  96. }
  97. //todo - handle
  98. }
  99. void CBuildingRect::clickRight (tribool down)
  100. {
  101. if((!area) || (!((bool)down)) || (this!=LOCPLINT->castleInt->hBuild))
  102. return;
  103. if((CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->current->motion.y-pos.y) != 0)) //na polu
  104. {
  105. CInfoPopup *vinya = new CInfoPopup();
  106. vinya->free = true;
  107. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  108. (LOCPLINT->playerID,
  109. CGI->buildh->buildings[str->townID][str->ID]->description,
  110. LOCPLINT->castleInt->bicons->ourImages[str->ID].bitmap,
  111. CGI->buildh->buildings[str->townID][str->ID]->name);
  112. vinya->pos.x = ekran->w/2 - vinya->bitmap->w/2;
  113. vinya->pos.y = ekran->h/2 - vinya->bitmap->h/2;
  114. vinya->activate();
  115. }
  116. }
  117. void CBuildingRect::mouseMoved (SDL_MouseMotionEvent & sEvent)
  118. {
  119. if(area)
  120. {
  121. if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
  122. {
  123. if(LOCPLINT->castleInt->hBuild == this)
  124. {
  125. LOCPLINT->castleInt->hBuild = NULL;
  126. LOCPLINT->statusbar->clear();
  127. }
  128. }
  129. else //inside the area of this building
  130. {
  131. if(LOCPLINT->castleInt->hBuild) //a building is hovered
  132. {
  133. if((*LOCPLINT->castleInt->hBuild)<(*this)) //set if we are on top
  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. else //no building hovered
  143. {
  144. LOCPLINT->castleInt->hBuild = this;
  145. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->name.length())
  146. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->name);
  147. else
  148. LOCPLINT->statusbar->print(str->name);
  149. }
  150. }
  151. }
  152. //if(border)
  153. // blitAt(border,pos.x,pos.y);
  154. }
  155. std::string getBgName(int type) //TODO - co z tym zrobiæ?
  156. {
  157. switch (type)
  158. {
  159. case 0:
  160. return "TBCSBACK.bmp";
  161. case 1:
  162. return "TBRMBACK.bmp";
  163. case 2:
  164. return "TBTWBACK.bmp";
  165. case 3:
  166. return "TBINBACK.bmp";
  167. case 4:
  168. return "TBNCBACK.bmp";
  169. case 5:
  170. return "TBDNBACK.bmp";
  171. case 6:
  172. return "TBSTBACK.bmp";
  173. case 7:
  174. return "TBFRBACK.bmp";
  175. case 8:
  176. return "TBELBACK.bmp";
  177. default:
  178. throw new std::exception("std::string getBgName(int type): invalid type");
  179. }
  180. }
  181. class SORTHELP
  182. {
  183. public:
  184. bool operator ()
  185. (const CBuildingRect *a ,
  186. const CBuildingRect *b)
  187. {
  188. return (*a)<(*b);
  189. }
  190. } srthlp ;
  191. CCastleInterface::CCastleInterface(const CGTownInstance * Town, bool Activate)
  192. {
  193. hall = NULL;
  194. townInt = CGI->bitmaph->loadBitmap("TOWNSCRN.bmp");
  195. cityBg = CGI->bitmaph->loadBitmap(getBgName(Town->subID));
  196. hall = CGI->spriteh->giveDef("ITMTL.DEF");
  197. fort = CGI->spriteh->giveDef("ITMCL.DEF");
  198. flag = CGI->spriteh->giveDef("CREST58.DEF");
  199. hBuild = NULL;
  200. count=0;
  201. town = Town;
  202. //garrison
  203. garr = new CGarrisonInt(305,387,4,32,townInt,243,13,town,town->visitingHero);
  204. townlist = new CTownList<CCastleInterface>(3,&genRect(128,48,744,414),744,414,744,526);
  205. exit = new AdventureMapButton<CCastleInterface>
  206. (CGI->townh->tcommands[8],"",&CCastleInterface::close,744,544,"TSBTNS.DEF",this,false,NULL,false);
  207. split = new AdventureMapButton<CGarrisonInt>
  208. (CGI->townh->tcommands[3],"",&CGarrisonInt::splitClick,744,382,"TSBTNS.DEF",garr,false,NULL,false);
  209. statusbar = new CStatusBar(8,555,"TSTATBAR.bmp",732);
  210. townlist->owner = this;
  211. townlist->fun = &CCastleInterface::townChange;
  212. townlist->genList();
  213. townlist->selected = getIndexOf(townlist->items,Town);
  214. if((townlist->selected+1) > townlist->SIZE)
  215. townlist->from = townlist->selected - townlist->SIZE + 1;
  216. CSDL_Ext::blueToPlayersAdv(townInt,LOCPLINT->playerID);
  217. exit->bitmapOffset = 4;
  218. //buildings
  219. recreateBuildings();
  220. if(Activate)
  221. {
  222. LOCPLINT->objsToBlit.push_back(this);
  223. activate();
  224. showAll();
  225. }
  226. std::string defname;
  227. switch (town->subID)
  228. {
  229. case 0:
  230. defname = "HALLCSTL.DEF";
  231. break;
  232. case 1:
  233. defname = "HALLRAMP.DEF";
  234. break;
  235. case 2:
  236. defname = "HALLTOWR.DEF";
  237. break;
  238. case 3:
  239. defname = "HALLINFR.DEF";
  240. break;
  241. case 4:
  242. defname = "HALLNECR.DEF";
  243. break;
  244. case 5:
  245. defname = "HALLDUNG.DEF";
  246. break;
  247. case 6:
  248. defname = "HALLSTRN.DEF";
  249. break;
  250. case 7:
  251. defname = "HALLFORT.DEF";
  252. break;
  253. case 8:
  254. defname = "HALLELEM.DEF";
  255. break;
  256. default:
  257. throw new std::exception("Bad town subID");
  258. }
  259. bicons = CGI->spriteh->giveDefEss(defname);
  260. //blit buildings on bg
  261. //for(int i=0;i<buildings.size();i++)
  262. //{
  263. // blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,cityBg);
  264. //}
  265. }
  266. CCastleInterface::~CCastleInterface()
  267. {
  268. SDL_FreeSurface(townInt);
  269. SDL_FreeSurface(cityBg);
  270. delete exit;
  271. delete split;
  272. delete hall;
  273. delete fort;
  274. delete flag;
  275. delete garr;
  276. delete townlist;
  277. delete statusbar;
  278. for(int i=0;i<buildings.size();i++)
  279. {
  280. delete buildings[i];
  281. }
  282. delete bicons;
  283. }
  284. void CCastleInterface::close()
  285. {
  286. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  287. deactivate();
  288. LOCPLINT->castleInt = NULL;
  289. LOCPLINT->adventureInt->activate();
  290. delete this;
  291. }
  292. void CCastleInterface::splitF()
  293. {
  294. }
  295. void CCastleInterface::buildingClicked(int building)
  296. {
  297. std::cout<<"You've clicked on "<<building<<std::endl;
  298. if(building >= 30)
  299. {
  300. if(building>36)
  301. building-=7;
  302. std::vector<std::pair<int,int > > crs;
  303. int amount = (const_cast<CGTownInstance*>(town))->strInfo.creatures[building-30]; //trzeba odconstowac, bo inaczej operator [] by sypal :(
  304. crs.push_back(std::make_pair(town->town->basicCreatures[building-30],amount));
  305. if(town->builtBuildings.find(building+7) != town->builtBuildings.end()) //check if there is an upgraded building
  306. crs.push_back(std::make_pair(town->town->upgradedCreatures[building-30],amount));
  307. CRecrutationWindow *rw = new CRecrutationWindow(crs,this);
  308. rw->activate();
  309. }
  310. switch(building)
  311. {
  312. case 10: case 11: case 12: case 13:
  313. enterHall();
  314. break;
  315. default:
  316. std::cout<<"This building isn't handled...\n";
  317. }
  318. }
  319. void CCastleInterface::enterHall()
  320. {
  321. deactivate();
  322. hallInt = new CHallInterface(this);
  323. hallInt->activate();
  324. hallInt->show();
  325. }
  326. void CCastleInterface::showAll(SDL_Surface * to)
  327. {
  328. if (!to)
  329. to=ekran;
  330. blitAt(cityBg,0,0,to);
  331. blitAt(townInt,0,374,to);
  332. LOCPLINT->adventureInt->resdatabar.draw();
  333. townlist->draw();
  334. statusbar->show();
  335. garr->show();
  336. int pom;
  337. //draw fort icon
  338. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  339. pom = 2;
  340. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  341. pom = 1;
  342. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  343. pom = 0;
  344. else pom = 3;
  345. blitAt(fort->ourImages[pom].bitmap,122,413,to);
  346. //draw ((village/town/city) hall)/capitol icon
  347. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  348. pom = 3;
  349. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  350. pom = 2;
  351. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  352. pom = 1;
  353. else pom = 0;
  354. blitAt(hall->ourImages[pom].bitmap,80,413,to);
  355. //draw creatures icons and their growths
  356. for(int i=0;i<CREATURES_PER_TOWN;i++)
  357. {
  358. int cid = -1;
  359. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  360. {
  361. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  362. cid = town->town->upgradedCreatures[i];
  363. else
  364. cid = town->town->basicCreatures[i];
  365. }
  366. if (cid>=0)
  367. {
  368. int pomx, pomy;
  369. pomx = 22 + (55*((i>3)?(i-4):i));
  370. pomy = (i>3)?(507):(459);
  371. blitAt(CGI->creh->smallImgs[cid],pomx,pomy,to);
  372. std::ostringstream oss;
  373. oss << '+' << town->creatureGrowth(i);
  374. CSDL_Ext::printAtMiddle(oss.str(),pomx+16,pomy+37,GEOR13,zwykly,to);
  375. }
  376. }
  377. //print name and income
  378. CSDL_Ext::printAt(town->name,85,389,GEOR13,zwykly,to);
  379. char temp[10];
  380. itoa(town->dailyIncome(),temp,10);
  381. CSDL_Ext::printAtMiddle(temp,195,442,GEOR13,zwykly,to);
  382. //blit town icon
  383. pom = town->subID*2;
  384. if (!town->hasFort())
  385. pom += F_NUMBER*2;
  386. if(town->builded >= MAX_BUILDING_PER_TURN)
  387. pom++;
  388. blitAt(LOCPLINT->bigTownPic->ourImages[pom].bitmap,15,387,to);
  389. //flag
  390. if(town->getOwner()<PLAYER_LIMIT)
  391. blitAt(flag->ourImages[town->getOwner()].bitmap,241,387,to);
  392. show();
  393. }
  394. void CCastleInterface::townChange()
  395. {
  396. const CGTownInstance * nt = townlist->items[townlist->selected];
  397. deactivate();
  398. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  399. delete this;
  400. LOCPLINT->castleInt = new CCastleInterface(nt,true);
  401. }
  402. void CCastleInterface::show(SDL_Surface * to)
  403. {
  404. if(!showing)
  405. return;
  406. if (!to)
  407. to=ekran;
  408. count++;
  409. if(count==4)
  410. {
  411. count=0;
  412. animval++;
  413. }
  414. blitAt(cityBg,0,0,to);
  415. //blit buildings
  416. for(int i=0;i<buildings.size();i++)
  417. {
  418. if((animval)%(buildings[i]->def->ourImages.size()))
  419. {
  420. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  421. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  422. }
  423. else
  424. blitAt(buildings[i]->def->ourImages[(animval)%(buildings[i]->def->ourImages.size())].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  425. //if(buildings[i]->hovered && buildings[i]->border)
  426. // blitAt(buildings[i]->border,buildings[i]->pos.x,buildings[i]->pos.y);
  427. if(hBuild==buildings[i] && hBuild->border)
  428. blitAt(hBuild->border,hBuild->pos,to);
  429. }
  430. }
  431. void CCastleInterface::activate()
  432. {
  433. showing = true;
  434. townlist->activate();
  435. garr->activate();
  436. LOCPLINT->curint = this;
  437. LOCPLINT->statusbar = statusbar;
  438. exit->activate();
  439. split->activate();
  440. for(int i=0;i<buildings.size();i++)
  441. buildings[i]->activate();
  442. }
  443. void CCastleInterface::deactivate()
  444. {
  445. showing = false;
  446. townlist->deactivate();
  447. garr->deactivate();
  448. exit->deactivate();
  449. split->deactivate();
  450. for(int i=0;i<buildings.size();i++)
  451. buildings[i]->deactivate();
  452. }
  453. void CCastleInterface::addBuilding(int bid)
  454. {
  455. //TODO: lepiej by bylo tylko dodawac/usuwac co trzeba pamietajac o grupach
  456. recreateBuildings();
  457. }
  458. void CCastleInterface::removeBuilding(int bid)
  459. {
  460. //TODO: lepiej by bylo tylko dodawac/usuwac co trzeba pamietajac o grupach
  461. recreateBuildings();
  462. }
  463. void CCastleInterface::recreateBuildings()
  464. {
  465. for(int i=0;i<buildings.size();i++)
  466. {
  467. if(showing)
  468. buildings[i]->deactivate();
  469. delete buildings[i];
  470. }
  471. buildings.clear();
  472. hBuild = NULL;
  473. std::set< std::pair<int,int> > s; //group - id
  474. for (std::set<int>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  475. {
  476. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  477. {
  478. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  479. {
  480. Structure * st = CGI->townh->structures[town->subID][*i];
  481. if(st->group<0) //no group - just add it
  482. {
  483. buildings.push_back(new CBuildingRect(st));
  484. }
  485. else
  486. {
  487. std::set< std::pair<int,int> >::iterator obecny=s.end();
  488. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  489. {
  490. if(seti->first == st->group)
  491. {
  492. obecny = seti;
  493. break;
  494. }
  495. }
  496. if(obecny != s.end())
  497. {
  498. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  499. {
  500. for(int itpb = 0; itpb<buildings.size(); itpb++)
  501. {
  502. if(buildings[itpb]->str->ID == obecny->second)
  503. {
  504. delete buildings[itpb];
  505. buildings.erase(buildings.begin() + itpb);
  506. obecny->second = st->ID;
  507. buildings.push_back(new CBuildingRect(st));
  508. }
  509. }
  510. }
  511. }
  512. else
  513. {
  514. buildings.push_back(new CBuildingRect(st));
  515. s.insert(std::pair<int,int>(st->group,st->ID));
  516. }
  517. }
  518. }
  519. else continue;
  520. }
  521. else
  522. break;
  523. }
  524. std::sort(buildings.begin(),buildings.end(),srthlp);
  525. }
  526. void CCastleInterface::recruit(int ID, int amount)
  527. {
  528. LOCPLINT->cb->recruitCreatures(town,ID,amount);
  529. }
  530. void CHallInterface::CResDataBar::show(SDL_Surface * to)
  531. {
  532. blitAt(bg,pos.x,pos.y);
  533. char * buf = new char[15];
  534. for (int i=0;i<7;i++)
  535. {
  536. itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  537. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly);
  538. }
  539. std::vector<std::string> temp;
  540. itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  541. itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  542. itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  543. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  544. CGI->generaltexth->allTexts[62]
  545. +": %s, "
  546. + CGI->generaltexth->allTexts[63]
  547. + ": %s, "
  548. + CGI->generaltexth->allTexts[64]
  549. + ": %s",temp)
  550. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly);
  551. temp.clear();
  552. //updateRect(&pos,ekran);
  553. delete[] buf;
  554. }
  555. CHallInterface::CResDataBar::CResDataBar()
  556. {
  557. bg = CGI->bitmaph->loadBitmap("Z2ESBAR.bmp");
  558. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  559. CSDL_Ext::blueToPlayersAdv(bg,LOCPLINT->playerID);
  560. pos.x = 7;
  561. pos.y = 575;
  562. pos.w = bg->w;
  563. pos.h = bg->h;
  564. }
  565. CHallInterface::CResDataBar::~CResDataBar()
  566. {
  567. SDL_FreeSurface(bg);
  568. }
  569. void CHallInterface::CBuildingBox::hover(bool on)
  570. {
  571. Hoverable::hover(on);
  572. if(on)
  573. {
  574. std::string toPrint = CGI->townh->hcommands[state];
  575. std::vector<std::string> name;
  576. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->name);
  577. LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
  578. }
  579. else
  580. LOCPLINT->statusbar->clear();
  581. }
  582. void CHallInterface::CBuildingBox::clickLeft (tribool down)
  583. {
  584. if(pressedL && (!down))
  585. {
  586. LOCPLINT->castleInt->hallInt->deactivate();
  587. new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0);
  588. }
  589. ClickableL::clickLeft(down);
  590. }
  591. void CHallInterface::CBuildingBox::clickRight (tribool down)
  592. {
  593. if(down)
  594. {
  595. LOCPLINT->castleInt->hallInt->deactivate();
  596. new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1);
  597. }
  598. ClickableR::clickRight(down);
  599. }
  600. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  601. {
  602. blitAt(LOCPLINT->castleInt->bicons->ourImages[BID].bitmap,pos.x,pos.y);
  603. int pom, pom2=-1;
  604. switch (state)
  605. {
  606. case 4:
  607. pom = 0;
  608. pom2 = 0;
  609. break;
  610. case 7:
  611. pom = 1;
  612. break;
  613. case 6:
  614. pom2 = 2;
  615. pom = 2;
  616. break;
  617. case 0: case 5: case 8:
  618. pom2 = 1;
  619. pom = 2;
  620. break;
  621. case 2: case 1: default:
  622. pom = 3;
  623. break;
  624. }
  625. blitAt(LOCPLINT->castleInt->hallInt->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71);
  626. if(pom2>=0)
  627. blitAt(LOCPLINT->castleInt->hallInt->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54);
  628. CSDL_Ext::printAtMiddle(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->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);
  629. }
  630. void CHallInterface::CBuildingBox::activate()
  631. {
  632. Hoverable::activate();
  633. ClickableL::activate();
  634. ClickableR::activate();
  635. }
  636. void CHallInterface::CBuildingBox::deactivate()
  637. {
  638. Hoverable::deactivate();
  639. ClickableL::deactivate();
  640. ClickableR::deactivate();
  641. }
  642. CHallInterface::CBuildingBox::~CBuildingBox()
  643. {
  644. }
  645. CHallInterface::CBuildingBox::CBuildingBox(int id)
  646. :BID(id)
  647. {
  648. pos.w = 150;
  649. pos.h = 70;
  650. }
  651. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  652. :BID(id)
  653. {
  654. pos.x = x;
  655. pos.y = y;
  656. pos.w = 150;
  657. pos.h = 70;
  658. }
  659. CHallInterface::CHallInterface(CCastleInterface * owner)
  660. {
  661. bg = CGI->bitmaph->loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  662. CSDL_Ext::blueToPlayersAdv(bg,LOCPLINT->playerID);
  663. bars = CGI->spriteh->giveDefEss("TPTHBAR.DEF");
  664. status = CGI->spriteh->giveDefEss("TPTHCHK.DEF");
  665. exit = new AdventureMapButton<CHallInterface>
  666. (CGI->townh->tcommands[8],"",&CHallInterface::close,748,556,"TPMAGE1.DEF",this,false,NULL,false);
  667. //preparing boxes with buildings//
  668. boxes.resize(5);
  669. for(int i=0;i<5;i++) //for each row
  670. {
  671. for(int j=0; j<CGI->buildh->hall[owner->town->subID].second[i].size();j++) //for each box
  672. {
  673. int k=0;
  674. for(;k<CGI->buildh->hall[owner->town->subID].second[i][j].size();k++)//we are looking for the first not build structure
  675. {
  676. if(
  677. (owner->town->builtBuildings.find(CGI->buildh->hall[owner->town->subID].second[i][j][k]))
  678. ==
  679. (owner->town->builtBuildings.end()) )
  680. {
  681. int x = 34 + 194*j,
  682. y = 37 + 104*i,
  683. ID = CGI->buildh->hall[owner->town->subID].second[i][j][k];
  684. if(CGI->buildh->hall[owner->town->subID].second[i].size() == 2) //only two boxes in this row
  685. x+=194;
  686. else if(CGI->buildh->hall[owner->town->subID].second[i].size() == 3) //only three boxes in this row
  687. x+=97;
  688. boxes[i].push_back(new CBuildingBox(CGI->buildh->hall[owner->town->subID].second[i][j][k],x,y));
  689. boxes[i][boxes[i].size()-1]->state = 7; //allowed by default
  690. //can we build it?
  691. if(owner->town->forbiddenBuildings.find(CGI->buildh->hall[owner->town->subID].second[i][j][k])!=owner->town->forbiddenBuildings.end())
  692. boxes[i][boxes[i].size()-1]->state = 2; //forbidden
  693. else if(owner->town->builded >= MAX_BUILDING_PER_TURN)
  694. boxes[i][boxes[i].size()-1]->state = 5; //building limit
  695. //checking resources
  696. CBuilding * pom = CGI->buildh->buildings[owner->town->subID][CGI->buildh->hall[owner->town->subID].second[i][j][k]];
  697. for(int res=0;res<7;res++) //TODO: support custom amount of resources
  698. {
  699. if(pom->resources[res]>LOCPLINT->cb->getResourceAmount(res))
  700. boxes[i][boxes[i].size()-1]->state = 6; //lack of res
  701. }
  702. //checking for requirements
  703. for( std::set<int>::iterator ri = CGI->townh->requirements[owner->town->subID][ID].begin();
  704. ri != CGI->townh->requirements[owner->town->subID][ID].end();
  705. ri++ )
  706. {
  707. if(owner->town->builtBuildings.find(*ri)==owner->town->builtBuildings.end())
  708. boxes[i][boxes[i].size()-1]->state = 8; //lack of requirements - cannot build
  709. }
  710. //TODO: check if capital is already built, check if there is water for shipyard
  711. break;
  712. }
  713. }
  714. if(k==CGI->buildh->hall[owner->town->subID].second[i][j].size()) //all buildings built - let's take the last one
  715. {
  716. int x = 34 + 194*j,
  717. y = 37 + 104*i;
  718. if(CGI->buildh->hall[owner->town->subID].second[i].size() == 2)
  719. x+=194;
  720. else if(CGI->buildh->hall[owner->town->subID].second[i].size() == 3)
  721. x+=97;
  722. boxes[i].push_back(new CBuildingBox(CGI->buildh->hall[owner->town->subID].second[i][j][k-1],x,y));
  723. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  724. }
  725. }
  726. }
  727. }
  728. CHallInterface::~CHallInterface()
  729. {
  730. delete bars;
  731. delete status;
  732. SDL_FreeSurface(bg);
  733. for(int i=0;i<boxes.size();i++)
  734. for(int j=0;j<boxes[i].size();j++)
  735. delete boxes[i][j];
  736. delete exit;
  737. }
  738. void CHallInterface::close()
  739. {
  740. deactivate();
  741. delete this;
  742. LOCPLINT->castleInt->activate();
  743. LOCPLINT->castleInt->showAll();
  744. }
  745. void CHallInterface::show(SDL_Surface * to)
  746. {
  747. blitAt(bg,0,0);
  748. resdatabar.show();
  749. exit->show();
  750. for(int i=0; i<5; i++)
  751. {
  752. for(int j=0;j<boxes[i].size();j++)
  753. boxes[i][j]->show();
  754. }
  755. }
  756. void CHallInterface::activate()
  757. {
  758. for(int i=0;i<5;i++)
  759. for(int j=0;j<boxes[i].size();j++)
  760. boxes[i][j]->activate();
  761. exit->activate();
  762. }
  763. void CHallInterface::deactivate()
  764. {
  765. for(int i=0;i<5;i++)
  766. {
  767. for(int j=0;j<boxes[i].size();j++)
  768. {
  769. boxes[i][j]->deactivate();
  770. }
  771. }
  772. exit->deactivate();
  773. }
  774. void CHallInterface::CBuildWindow::activate()
  775. {
  776. LOCPLINT->objsToBlit.push_back(this);
  777. ClickableR::activate();
  778. if(mode)
  779. return;
  780. if(state==7)
  781. buy->activate();
  782. cancel->activate();
  783. }
  784. void CHallInterface::CBuildWindow::deactivate()
  785. {
  786. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  787. ClickableR::deactivate();
  788. if(mode)
  789. return;
  790. if(state==7)
  791. buy->deactivate();
  792. cancel->deactivate();
  793. }
  794. void CHallInterface::CBuildWindow::Buy()
  795. {
  796. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,bid);
  797. deactivate();
  798. delete this;
  799. delete LOCPLINT->castleInt->hallInt;
  800. LOCPLINT->castleInt->hallInt = NULL;
  801. LOCPLINT->castleInt->activate();
  802. LOCPLINT->castleInt->showAll();
  803. }
  804. void CHallInterface::CBuildWindow::close()
  805. {
  806. deactivate();
  807. delete this;
  808. LOCPLINT->castleInt->hallInt->activate();
  809. LOCPLINT->castleInt->hallInt->show();
  810. }
  811. void CHallInterface::CBuildWindow::clickRight (tribool down)
  812. {
  813. if((!down || indeterminate(down)) && mode)
  814. close();
  815. }
  816. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  817. {
  818. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  819. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  820. SDL_BlitSurface(bitmap,&poms,to?to:ekran,&pom);
  821. if(!mode)
  822. {
  823. buy->show();
  824. cancel->show();
  825. }
  826. }
  827. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  828. {
  829. std::string ret;
  830. if(state<7)
  831. ret = CGI->townh->hcommands[state];
  832. switch (state)
  833. {
  834. case 4:
  835. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->name);
  836. break;
  837. case 7:
  838. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  839. case 8:
  840. {
  841. ret = CGI->generaltexth->allTexts[52];
  842. std::set<int> used;
  843. used.insert(bid);
  844. std::set<int> reqs;
  845. for(std::set<int>::iterator i=CGI->townh->requirements[tid][bid].begin();i!=CGI->townh->requirements[tid][bid].end();i++)
  846. if (LOCPLINT->castleInt->town->builtBuildings.find(*i) == LOCPLINT->castleInt->town->builtBuildings.end())
  847. reqs.insert(*i);
  848. while(true)
  849. {
  850. int czystych=0;
  851. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  852. {
  853. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  854. {
  855. used.insert(*i);
  856. for(
  857. std::set<int>::iterator j=CGI->townh->requirements[tid][*i].begin();
  858. j!=CGI->townh->requirements[tid][*i].end();
  859. j++
  860. )
  861. {
  862. if(LOCPLINT->castleInt->town->builtBuildings.find(*j) == //this building is not built
  863. LOCPLINT->castleInt->town->builtBuildings.end())
  864. reqs.insert(*j);
  865. }
  866. }
  867. else
  868. {
  869. czystych++;
  870. }
  871. }
  872. if(czystych==reqs.size())
  873. break;
  874. }
  875. bool first=true;
  876. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  877. {
  878. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->name);
  879. first = false;
  880. }
  881. }
  882. }
  883. return ret;
  884. }
  885. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  886. :tid(Tid),bid(Bid),mode(Mode), state(State)
  887. {
  888. SDL_Surface *hhlp = CGI->bitmaph->loadBitmap("TPUBUILD.bmp");
  889. bitmap = SDL_ConvertSurface(hhlp,ekran->format,0); //na 8bitowej mapie by sie psulo
  890. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  891. SDL_FreeSurface(hhlp);
  892. pos.x = ekran->w/2 - bitmap->w/2;
  893. pos.y = ekran->h/2 - bitmap->h/2;
  894. CSDL_Ext::blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  895. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  896. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->name);
  897. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->description,197,168,GEOR16,40,zwykly,bitmap);
  898. CSDL_Ext::printAtMiddleWB(getTextForState(state),197,248,GEOR13,50,zwykly,bitmap);
  899. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->townh->hcommands[7],pom),197,30,GEOR16,tytulowy,bitmap);
  900. int resamount=0; for(int i=0;i<7;i++) if(CGI->buildh->buildings[tid][bid]->resources[i]) resamount++;
  901. int ah = (resamount>4) ? 304 : 341;
  902. int cn=-1, it=0;
  903. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  904. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  905. char buf[15];
  906. while(++cn<7)
  907. {
  908. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  909. continue;
  910. itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  911. if(it<4)
  912. {
  913. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+42,GEOR16,zwykly,bitmap);
  914. blitAt(CGI->townh->resources->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  915. }
  916. else
  917. {
  918. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+42,GEOR16,zwykly,bitmap);
  919. blitAt(CGI->townh->resources->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  920. }
  921. if(it==4)
  922. ah+=75;
  923. }
  924. if(!mode)
  925. {
  926. buy = new AdventureMapButton<CBuildWindow>
  927. ("","",&CBuildWindow::Buy,pos.x+45,pos.y+446,"IBUY30.DEF",this,false,NULL,false);
  928. cancel = new AdventureMapButton<CBuildWindow>
  929. ("","",&CBuildWindow::close,pos.x+290,pos.y+445,"ICANCEL.DEF",this,false,NULL,false);
  930. if(state!=7)
  931. buy->state=2;
  932. }
  933. activate();
  934. }
  935. CHallInterface::CBuildWindow::~CBuildWindow()
  936. {
  937. SDL_FreeSurface(bitmap);
  938. if(!mode)
  939. {
  940. delete buy;
  941. delete cancel;
  942. }
  943. }