CCastleInterface.cpp 59 KB

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