CCastleInterface.cpp 56 KB

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