CCastleInterface.cpp 46 KB

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