CCastleInterface.cpp 58 KB

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