CCastleInterface.cpp 29 KB

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