CCastleInterface.cpp 61 KB

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