CCastleInterface.cpp 60 KB

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