CCastleInterface.cpp 59 KB

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