CCastleInterface.cpp 61 KB

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