CCastleInterface.cpp 59 KB

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