CCastleInterface.cpp 61 KB

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