CCastleInterface.cpp 58 KB

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