CCastleInterface.cpp 45 KB

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