CCastleInterface.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. #include "../stdafx.h"
  2. #include "CCastleInterface.h"
  3. #include "AdventureMapButton.h"
  4. #include "CAdvmapInterface.h"
  5. #include "../CCallback.h"
  6. #include "CGameInfo.h"
  7. #include "CHeroWindow.h"
  8. #include "CMessage.h"
  9. #include "SDL_Extensions.h"
  10. #include "CCreatureAnimation.h"
  11. #include "Graphics.h"
  12. #include "../hch/CArtHandler.h"
  13. #include "../hch/CBuildingHandler.h"
  14. #include "../hch/CDefHandler.h"
  15. #include "../hch/CGeneralTextHandler.h"
  16. #include "../hch/CLodHandler.h"
  17. #include "../hch/CObjectHandler.h"
  18. #include "../hch/CSpellHandler.h"
  19. #include "../hch/CTownHandler.h"
  20. #include <boost/algorithm/string.hpp>
  21. #include <boost/algorithm/string/replace.hpp>
  22. #include <boost/assign/std/vector.hpp>
  23. #include <boost/lexical_cast.hpp>
  24. #include <cmath>
  25. #include <sstream>
  26. using namespace boost::assign;
  27. using namespace CSDL_Ext;
  28. /*
  29. * CCastleInterface.cpp, part of VCMI engine
  30. *
  31. * Authors: listed in file AUTHORS in main folder
  32. *
  33. * License: GNU General Public License v2.0 or later
  34. * Full text of license available in license.txt file, in main folder
  35. *
  36. */
  37. extern TTF_Font * GEOR16;
  38. CBuildingRect::CBuildingRect(Structure *Str)
  39. :moi(false), offset(0), str(Str)
  40. {
  41. def = CDefHandler::giveDef(Str->defName);
  42. max = def->ourImages.size();
  43. if(str->ID == 33 && str->townID == 4) //little 'hack' for estate in necropolis - background color is not always the first color in the palette
  44. {
  45. for(std::vector<Cimage>::iterator i=def->ourImages.begin();i!=def->ourImages.end();i++)
  46. {
  47. SDL_SetColorKey(i->bitmap,SDL_SRCCOLORKEY,*((char*)i->bitmap->pixels));
  48. }
  49. }
  50. pos.x = str->pos.x + LOCPLINT->castleInt->pos.x;
  51. pos.y = str->pos.y + LOCPLINT->castleInt->pos.y;
  52. pos.w = def->ourImages[0].bitmap->w;
  53. pos.h = def->ourImages[0].bitmap->h;
  54. if(Str->ID<0 || (Str->ID>=27 && Str->ID<=29))
  55. {
  56. area = border = NULL;
  57. return;
  58. }
  59. if (border = BitmapHandler::loadBitmap(str->borderName)) //FIXME hmmm if '=' use () else '==' fatal mistake
  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. if (area = BitmapHandler::loadBitmap(str->areaName))//FIXME look up
  68. {
  69. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  70. }
  71. else
  72. {
  73. tlog2 << "Warning: no area for "<<Str->ID<<std::endl;
  74. }
  75. }
  76. CBuildingRect::~CBuildingRect()
  77. {
  78. delete def;
  79. if(border)
  80. SDL_FreeSurface(border);
  81. if(area)
  82. SDL_FreeSurface(area);
  83. }
  84. void CBuildingRect::activate()
  85. {
  86. Hoverable::activate();
  87. ClickableL::activate();
  88. ClickableR::activate();
  89. }
  90. void CBuildingRect::deactivate()
  91. {
  92. Hoverable::deactivate();
  93. ClickableL::deactivate();
  94. ClickableR::deactivate();
  95. if(moi)
  96. MotionInterested::deactivate();
  97. moi=false;
  98. }
  99. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  100. {
  101. if(str->pos.z != p2.str->pos.z)
  102. return (str->pos.z) < (p2.str->pos.z);
  103. else
  104. return (str->ID) < (p2.str->ID);
  105. }
  106. void CBuildingRect::hover(bool on)
  107. {
  108. Hoverable::hover(on);
  109. if(on)
  110. {
  111. if(!moi)
  112. MotionInterested::activate();
  113. moi = true;
  114. }
  115. else
  116. {
  117. if(moi)
  118. MotionInterested::deactivate();
  119. moi = false;
  120. if(LOCPLINT->castleInt->hBuild == this)
  121. {
  122. LOCPLINT->castleInt->hBuild = NULL;
  123. LOCPLINT->statusbar->clear();
  124. //call mouseMoved in other buildings, cursor might have been moved while they were inactive (eg. because of r-click popup)
  125. for(size_t i = 0; i < LOCPLINT->castleInt->buildings.size(); i++)
  126. LOCPLINT->castleInt->buildings[i]->mouseMoved(LOCPLINT->current->motion);
  127. }
  128. }
  129. }
  130. void CBuildingRect::clickLeft (tribool down)
  131. {
  132. if(area && (LOCPLINT->castleInt->hBuild==this) && !(indeterminate(down)) && (CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->current->motion.y-pos.y) != 0)) //na polu
  133. {
  134. if(pressedL && !down)
  135. LOCPLINT->castleInt->buildingClicked(str->ID);
  136. ClickableL::clickLeft(down);
  137. }
  138. //todo - handle
  139. }
  140. void CBuildingRect::clickRight (tribool down)
  141. {
  142. if((!area) || (!((bool)down)) || (this!=LOCPLINT->castleInt->hBuild))
  143. return;
  144. if((CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->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. LOCPLINT->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. LOCPLINT->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. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  181. else
  182. LOCPLINT->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. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  190. else
  191. LOCPLINT->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) return;
  201. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  202. std::string temp;
  203. if(hero)
  204. {
  205. if(highlight)//view NNN
  206. {
  207. temp = CGI->generaltexth->tcommands[4];
  208. boost::algorithm::replace_first(temp,"%s",hero->name);
  209. }
  210. else if(other->hero && other->highlight)//exchange
  211. {
  212. temp = CGI->generaltexth->tcommands[7];
  213. boost::algorithm::replace_first(temp,"%s",hero->name);
  214. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  215. }
  216. else// select NNN (in ZZZ)
  217. {
  218. if(upg)//down - visiting
  219. {
  220. temp = CGI->generaltexth->tcommands[32];
  221. boost::algorithm::replace_first(temp,"%s",hero->name);
  222. }
  223. else //up - garrison
  224. {
  225. temp = CGI->generaltexth->tcommands[12];
  226. boost::algorithm::replace_first(temp,"%s",hero->name);
  227. }
  228. }
  229. }
  230. else //we are empty slot
  231. {
  232. if(other->highlight && other->hero) //move NNNN
  233. {
  234. temp = CGI->generaltexth->tcommands[6];
  235. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  236. }
  237. else //empty
  238. {
  239. temp = CGI->generaltexth->allTexts[507];
  240. }
  241. }
  242. if(temp.size())
  243. LOCPLINT->statusbar->print(temp);
  244. }
  245. void CHeroGSlot::clickRight (boost::logic::tribool down)
  246. {
  247. }
  248. void CHeroGSlot::clickLeft(boost::logic::tribool down)
  249. {
  250. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  251. if(!down)
  252. {
  253. owner->garr->splitting = false;
  254. owner->garr->highlighted = NULL;
  255. if(hero && highlight)
  256. {
  257. highlight = false;
  258. LOCPLINT->openHeroWindow(hero);
  259. }
  260. else if(other->hero && other->highlight)
  261. {
  262. bool allow = true;
  263. if(upg) //moving hero out of town - check if it is allowed
  264. {
  265. if(!hero && LOCPLINT->cb->howManyHeroes(false) >= 8)
  266. {
  267. std::string tmp = CGI->generaltexth->allTexts[18]; //You already have %d adventuring heroes under your command.
  268. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  269. LOCPLINT->showInfoDialog(tmp,std::vector<SComponent*>(), soundBase::sound_todo);
  270. allow = false;
  271. }
  272. else if(!other->hero->army.slots.size()) //hero has no creatures - strange, but if we have appropriate error message...
  273. {
  274. 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.
  275. allow = false;
  276. }
  277. }
  278. other->highlight = highlight = false;
  279. if(allow)
  280. LOCPLINT->cb->swapGarrisonHero(owner->town);
  281. }
  282. else if(hero)
  283. {
  284. highlight = true;
  285. owner->garr->highlighted = NULL;
  286. show(screen2);
  287. }
  288. hover(false);hover(true); //refresh statusbar
  289. }
  290. if(indeterminate(down) && !isItIn(&other->pos,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  291. {
  292. other->highlight = highlight = false;
  293. show(screen2);
  294. }
  295. }
  296. void CHeroGSlot::activate()
  297. {
  298. ClickableL::activate();
  299. ClickableR::activate();
  300. Hoverable::activate();
  301. }
  302. void CHeroGSlot::deactivate()
  303. {
  304. highlight = false;
  305. ClickableL::deactivate();
  306. ClickableR::deactivate();
  307. Hoverable::deactivate();
  308. }
  309. void CHeroGSlot::show(SDL_Surface * to)
  310. {
  311. if(hero) //there is hero
  312. blitAt(graphics->portraitLarge[hero->portrait],pos,to);
  313. else if(!upg) //up garrison
  314. blitAt(graphics->flags->ourImages[LOCPLINT->castleInt->town->getOwner()].bitmap,pos,to);
  315. if(highlight)
  316. blitAt(graphics->bigImgs[-1],pos,to);
  317. }
  318. CHeroGSlot::CHeroGSlot(int x, int y, int updown, const CGHeroInstance *h, CCastleInterface * Owner)
  319. {
  320. owner = Owner;
  321. pos.x = x;
  322. pos.y = y;
  323. pos.w = 58;
  324. pos.h = 64;
  325. hero = h;
  326. upg = updown;
  327. highlight = false;
  328. }
  329. CHeroGSlot::~CHeroGSlot()
  330. {
  331. }
  332. static std::string getBgName(int type) //TODO - co z tym zrobi�?
  333. {
  334. switch (type)
  335. {
  336. case 0:
  337. return "TBCSBACK.bmp";
  338. case 1:
  339. return "TBRMBACK.bmp";
  340. case 2:
  341. return "TBTWBACK.bmp";
  342. case 3:
  343. return "TBINBACK.bmp";
  344. case 4:
  345. return "TBNCBACK.bmp";
  346. case 5:
  347. return "TBDNBACK.bmp";
  348. case 6:
  349. return "TBSTBACK.bmp";
  350. case 7:
  351. return "TBFRBACK.bmp";
  352. case 8:
  353. return "TBELBACK.bmp";
  354. default:
  355. #ifndef __GNUC__
  356. throw new std::exception("std::string getBgName(int type): invalid type");
  357. #else
  358. throw new std::exception();
  359. #endif
  360. }
  361. }
  362. class SORTHELP
  363. {
  364. public:
  365. bool operator ()
  366. (const CBuildingRect *a ,
  367. const CBuildingRect *b)
  368. {
  369. return (*a)<(*b);
  370. }
  371. } srthlp ;
  372. CCastleInterface::CCastleInterface(const CGTownInstance * Town)
  373. :hslotup(241,387,0,Town->garrisonHero,this),hslotdown(241,483,1,Town->visitingHero,this)
  374. {
  375. bars = CDefHandler::giveDefEss("TPTHBAR.DEF");
  376. status = CDefHandler::giveDefEss("TPTHCHK.DEF");
  377. LOCPLINT->castleInt = this;
  378. hall = NULL;
  379. townInt = BitmapHandler::loadBitmap("TOWNSCRN.bmp");
  380. cityBg = BitmapHandler::loadBitmap(getBgName(Town->subID));
  381. pos.x = screen->w/2 - 400;
  382. pos.y = screen->h/2 - 300;
  383. hslotup.pos.x += pos.x;
  384. hslotup.pos.y += pos.y;
  385. hslotdown.pos.x += pos.x;
  386. hslotdown.pos.y += pos.y;
  387. hall = CDefHandler::giveDef("ITMTL.DEF");
  388. fort = CDefHandler::giveDef("ITMCL.DEF");
  389. hBuild = NULL;
  390. count=0;
  391. town = Town;
  392. animval = 0;
  393. //garrison
  394. garr = new CGarrisonInt(pos.x+305,pos.y+387,4,32,townInt,243,13,town,town->visitingHero);
  395. townlist = new CTownList(3,pos.x+744,pos.y+414,"IAM014.DEF","IAM015.DEF");//744,526);
  396. exit = new AdventureMapButton
  397. (CGI->generaltexth->tcommands[8],"",boost::bind(&CCastleInterface::close,this),pos.x+744,pos.y+544,"TSBTNS.DEF",SDLK_RETURN);
  398. split = new AdventureMapButton
  399. (CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+744,pos.y+382,"TSBTNS.DEF");
  400. statusbar = new CStatusBar(pos.x+7,pos.y+555,"TSTATBAR.bmp",732);
  401. resdatabar = new CResDataBar("ZRESBAR.bmp",pos.x+3,pos.y+575,32,2,85,85);
  402. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  403. townlist->fun = boost::bind(&CCastleInterface::townChange,this);
  404. townlist->genList();
  405. townlist->selected = vstd::findPos(townlist->items,Town);
  406. if((townlist->selected+1) > townlist->SIZE)
  407. townlist->from = townlist->selected - townlist->SIZE + 2;
  408. graphics->blueToPlayersAdv(townInt,LOCPLINT->playerID);
  409. exit->bitmapOffset = 4;
  410. //buildings
  411. recreateBuildings();
  412. std::string defname;
  413. switch (town->subID)
  414. {
  415. case 0:
  416. defname = "HALLCSTL.DEF";
  417. musicID = musicBase::castleTown;
  418. break;
  419. case 1:
  420. defname = "HALLRAMP.DEF";
  421. musicID = musicBase::rampartTown;
  422. break;
  423. case 2:
  424. defname = "HALLTOWR.DEF";
  425. musicID = musicBase::towerTown;
  426. break;
  427. case 3:
  428. defname = "HALLINFR.DEF";
  429. musicID = musicBase::infernoTown;
  430. break;
  431. case 4:
  432. defname = "HALLNECR.DEF";
  433. musicID = musicBase::necroTown;
  434. break;
  435. case 5:
  436. defname = "HALLDUNG.DEF";
  437. musicID = musicBase::dungeonTown;
  438. break;
  439. case 6:
  440. defname = "HALLSTRN.DEF";
  441. musicID = musicBase::strongHoldTown;
  442. break;
  443. case 7:
  444. defname = "HALLFORT.DEF";
  445. musicID = musicBase::fortressTown;
  446. break;
  447. case 8:
  448. defname = "HALLELEM.DEF";
  449. musicID = musicBase::elemTown;
  450. break;
  451. default:
  452. throw new std::string("Wrong town subID");
  453. }
  454. bicons = CDefHandler::giveDefEss(defname);
  455. }
  456. CCastleInterface::~CCastleInterface()
  457. {
  458. delete bars;
  459. delete status;
  460. SDL_FreeSurface(townInt);
  461. SDL_FreeSurface(cityBg);
  462. delete exit;
  463. delete split;
  464. delete hall;
  465. delete fort;
  466. delete garr;
  467. delete townlist;
  468. delete statusbar;
  469. delete resdatabar;
  470. for(size_t i=0;i<buildings.size();i++)
  471. {
  472. delete buildings[i];
  473. }
  474. delete bicons;
  475. }
  476. void CCastleInterface::close()
  477. {
  478. if(town->visitingHero)
  479. LOCPLINT->adventureInt->select(town->visitingHero);
  480. LOCPLINT->castleInt = NULL;
  481. LOCPLINT->popIntTotally(this);
  482. CGI->musich->stopMusic(5000);
  483. }
  484. void CCastleInterface::splitF()
  485. {
  486. }
  487. void CCastleInterface::buildingClicked(int building)
  488. {
  489. tlog5<<"You've clicked on "<<building<<std::endl;
  490. if(building==19 || building==18)
  491. {
  492. building = town->town->hordeLvl[0] + 30;
  493. }
  494. else if(building==24 || building==25)
  495. {
  496. building = town->town->hordeLvl[1] + 30;
  497. }
  498. if(building >= 30)
  499. {
  500. showRecruitmentWindow(building);
  501. }
  502. else
  503. {
  504. switch(building)
  505. {
  506. case 0: case 1: case 2: case 3: case 4: //mage guild
  507. {
  508. if(town->visitingHero && !vstd::contains(town->visitingHero->artifWorn,ui16(17))) //visiting hero doesn't have spellboks
  509. {
  510. if(LOCPLINT->cb->getResourceAmount(6) < 500) //not enough gold to buy spellbook
  511. {
  512. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213],std::vector<SComponent*>(), soundBase::sound_todo);
  513. }
  514. else
  515. {
  516. CFunctionList<void()> fl = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,town->visitingHero,0);
  517. fl += boost::bind(&CCastleInterface::enterMageGuild,this);
  518. std::vector<SComponent*> vvv(1,new SComponent(SComponent::artifact,0,0));
  519. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214],vvv,fl,0,true);
  520. }
  521. }
  522. else
  523. {
  524. enterMageGuild();
  525. }
  526. break;
  527. }
  528. case 5: //tavern
  529. {
  530. enterTavern();
  531. break;
  532. }
  533. //TODO: case 6: //shipyard
  534. case 7: case 8: case 9: //fort/citadel/castle
  535. {
  536. CFortScreen *fs = new CFortScreen(this);
  537. LOCPLINT->pushInt(fs);
  538. break;
  539. }
  540. case 10: case 11: case 12: case 13: //hall
  541. enterHall();
  542. break;
  543. case 14: //marketplace
  544. {
  545. CMarketplaceWindow *cmw = new CMarketplaceWindow(0);
  546. LOCPLINT->pushInt(cmw);
  547. break;
  548. }
  549. case 15: //resource silo
  550. {
  551. LOCPLINT->showInfoDialog(CGI->buildh->buildings[town->subID][15]->Description(),std::vector<SComponent*>(), soundBase::sound_todo);
  552. break;
  553. }
  554. case 16: //blacksmith
  555. {
  556. const CGHeroInstance *hero = town->visitingHero;
  557. if(!hero)
  558. {
  559. std::string pom = CGI->generaltexth->allTexts[273];
  560. boost::algorithm::replace_first(pom,"%s",CGI->buildh->buildings[town->subID][16]->Name());
  561. LOCPLINT->showInfoDialog(pom,std::vector<SComponent*>(), soundBase::sound_todo);
  562. return;
  563. }
  564. int aid = town->town->warMachine;
  565. int price = CGI->arth->artifacts[aid].price;
  566. bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
  567. if(vstd::contains(hero->artifWorn,ui16(aid+9))) //hero already has machine
  568. possible = false;
  569. LOCPLINT->pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(aid,false),aid,hero->id));
  570. break;
  571. }
  572. //TODO: case 17: //special 1
  573. //TODO: case 18: //basic horde 1
  574. //TODO: case 19: //upg horde 1
  575. //TODO: case 20: //ship at shipyard
  576. //TODO: case 21: //special 2
  577. case 22: //special 3
  578. {
  579. switch(town->subID)
  580. {
  581. case 0: //brotherhood of sword
  582. enterTavern();
  583. break;
  584. default:
  585. tlog4<<"This building isn't handled...\n";
  586. break;
  587. }
  588. break;
  589. }
  590. //TODO: case 23: //special 4
  591. //TODO: case 24: //basic horde 2
  592. //TODO: case 25: //upg horde 2
  593. //TODO: case 26: //grail
  594. default:
  595. tlog4<<"This building isn't handled...\n";
  596. }
  597. }
  598. }
  599. void CCastleInterface::enterHall()
  600. {
  601. CHallInterface *h = new CHallInterface(this);
  602. LOCPLINT->pushInt(h);
  603. }
  604. void CCastleInterface::showAll( SDL_Surface * to/*=NULL*/)
  605. {
  606. blitAt(cityBg,pos,to);
  607. blitAt(townInt,pos.x,pos.y+374,to);
  608. LOCPLINT->adventureInt->resdatabar.draw(to);
  609. townlist->draw(to);
  610. statusbar->show(to);
  611. resdatabar->draw(to);
  612. garr->show(to);
  613. int pom;
  614. //draw fort icon
  615. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  616. pom = 2;
  617. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  618. pom = 1;
  619. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  620. pom = 0;
  621. else pom = 3;
  622. blitAt(fort->ourImages[pom].bitmap,pos.x+122,pos.y+413,to);
  623. //draw ((village/town/city) hall)/capitol icon
  624. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  625. pom = 3;
  626. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  627. pom = 2;
  628. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  629. pom = 1;
  630. else pom = 0;
  631. blitAt(hall->ourImages[pom].bitmap,pos.x+80,pos.y+413,to);
  632. //draw creatures icons and their growths
  633. for(int i=0;i<CREATURES_PER_TOWN;i++)
  634. {
  635. int cid = -1;
  636. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  637. {
  638. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  639. cid = town->town->upgradedCreatures[i];
  640. else
  641. cid = town->town->basicCreatures[i];
  642. }
  643. if (cid>=0)
  644. {
  645. int pomx, pomy;
  646. pomx = 22 + (55*((i>3)?(i-4):i));
  647. pomy = (i>3)?(507):(459);
  648. blitAt(graphics->smallImgs[cid],pos.x+pomx,pos.y+pomy,to);
  649. std::ostringstream oss;
  650. oss << '+' << town->creatureGrowth(i);
  651. CSDL_Ext::printAtMiddle(oss.str(),pos.x+pomx+16,pos.y+pomy+37,GEOR13,zwykly,to);
  652. }
  653. }
  654. //print name and income
  655. CSDL_Ext::printAt(town->name,pos.x+85,pos.y+389,GEOR13,zwykly,to);
  656. char temp[10];
  657. SDL_itoa(town->dailyIncome(),temp,10);
  658. CSDL_Ext::printAtMiddle(temp,pos.x+195,pos.y+442,GEOR13,zwykly,to);
  659. //blit town icon
  660. pom = town->subID*2;
  661. if (!town->hasFort())
  662. pom += F_NUMBER*2;
  663. if(town->builded >= MAX_BUILDING_PER_TURN)
  664. pom++;
  665. blitAt(graphics->bigTownPic->ourImages[pom].bitmap,pos.x+15,pos.y+387,to);
  666. hslotup.show(to);
  667. hslotdown.show(to);
  668. show(to);
  669. if(screen->w != 800 || screen->h !=600)
  670. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  671. exit->show(to);
  672. split->show(to);
  673. }
  674. void CCastleInterface::townChange()
  675. {
  676. const CGTownInstance * nt = townlist->items[townlist->selected];
  677. LOCPLINT->popIntTotally(this);
  678. LOCPLINT->pushInt(new CCastleInterface(nt));
  679. }
  680. void CCastleInterface::show(SDL_Surface * to)
  681. {
  682. count++;
  683. if(count==8)
  684. {
  685. count=0;
  686. animval++;
  687. }
  688. blitAt(cityBg,pos,to);
  689. //blit buildings
  690. for(size_t i=0;i<buildings.size();i++)
  691. {
  692. int frame = ((animval)%(buildings[i]->max - buildings[i]->offset)) + buildings[i]->offset;
  693. if(frame)
  694. {
  695. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  696. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  697. }
  698. else
  699. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  700. if(hBuild==buildings[i] && hBuild->border) //if this this higlighted structure and has border we'll blit it
  701. blitAt(hBuild->border,hBuild->pos,to);
  702. }
  703. }
  704. void CCastleInterface::activate()
  705. {
  706. showing = true;
  707. townlist->activate();
  708. garr->activate();
  709. LOCPLINT->statusbar = statusbar;
  710. exit->activate();
  711. split->activate();
  712. for(size_t i=0;i<buildings.size();i++) //XXX pls use iterators or at() but not []
  713. {
  714. buildings[i]->activate();
  715. }
  716. hslotdown.activate();
  717. hslotup.activate();
  718. }
  719. void CCastleInterface::deactivate()
  720. {
  721. showing = false;
  722. townlist->deactivate();
  723. garr->deactivate();
  724. exit->deactivate();
  725. split->deactivate();
  726. for(size_t i=0;i<buildings.size();i++) //XXX iterators
  727. {
  728. buildings[i]->deactivate();
  729. }
  730. hslotdown.deactivate();
  731. hslotup.deactivate();
  732. }
  733. void CCastleInterface::addBuilding(int bid)
  734. {
  735. //TODO: lepiej by bylo tylko dodawac co trzeba pamietajac o grupach
  736. deactivate();
  737. recreateBuildings();
  738. activate();
  739. }
  740. void CCastleInterface::removeBuilding(int bid)
  741. {
  742. //TODO: lepiej by bylo tylko usuwac co trzeba pamietajac o grupach
  743. recreateBuildings();
  744. }
  745. void CCastleInterface::recreateBuildings()
  746. {
  747. for(size_t i=0;i<buildings.size();i++)
  748. {
  749. if(showing)
  750. buildings[i]->deactivate();
  751. delete buildings[i];
  752. }
  753. buildings.clear();
  754. hBuild = NULL;
  755. std::set< std::pair<int,int> > s; //group - id
  756. for (std::set<si32>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  757. {
  758. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  759. {
  760. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  761. {
  762. Structure * st = CGI->townh->structures[town->subID][*i];
  763. if(st->group<0) //no group - just add it
  764. {
  765. buildings.push_back(new CBuildingRect(st));
  766. }
  767. else
  768. {
  769. std::set< std::pair<int,int> >::iterator obecny=s.end();
  770. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  771. {
  772. if(seti->first == st->group)
  773. {
  774. obecny = seti;
  775. break;
  776. }
  777. }
  778. if(obecny != s.end())
  779. {
  780. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  781. {
  782. for(size_t itpb = 0; itpb<buildings.size(); itpb++)
  783. {
  784. if(buildings[itpb]->str->ID == obecny->second)
  785. {
  786. delete buildings[itpb];
  787. buildings.erase(buildings.begin() + itpb);
  788. #ifndef __GNUC__
  789. obecny->second = st->ID; //XXX look closer!
  790. #else
  791. *(const_cast<int*>(&(obecny->second))) = st->ID;
  792. #endif
  793. buildings.push_back(new CBuildingRect(st));
  794. }
  795. }
  796. }
  797. }
  798. else
  799. {
  800. buildings.push_back(new CBuildingRect(st));
  801. s.insert(std::pair<int,int>(st->group,st->ID));
  802. }
  803. }
  804. }
  805. else continue;
  806. }
  807. else
  808. break;
  809. }
  810. std::sort(buildings.begin(),buildings.end(),srthlp);
  811. //code for Mana Vortex (there are two sets of animation frames - one without mage guild and one with
  812. if((town->subID == 5) && (town->builtBuildings.find(21)!=town->builtBuildings.end()))
  813. {
  814. CBuildingRect *vortex = NULL;
  815. for(size_t i=0;i<buildings.size();i++)
  816. {
  817. if(buildings[i]->str->ID==21)
  818. {
  819. vortex=buildings[i];
  820. break;
  821. }
  822. }
  823. if(town->builtBuildings.find(4)!=town->builtBuildings.end()) //there is mage Guild level 5
  824. {
  825. vortex->offset = 10;
  826. vortex->max = vortex->def->ourImages.size();
  827. }
  828. else
  829. {
  830. vortex->offset = 0;
  831. vortex->max = 10;
  832. }
  833. }
  834. //code for the shipyard in the Castle
  835. else if((town->subID == 0) && (town->builtBuildings.find(6)!=town->builtBuildings.end()))
  836. {
  837. CBuildingRect *shipyard = NULL;
  838. for(size_t i=0;i<buildings.size();i++)
  839. {
  840. if(buildings[i]->str->ID==6)
  841. {
  842. shipyard=buildings[i];
  843. break;
  844. }
  845. }
  846. if(town->builtBuildings.find(8)!=town->builtBuildings.end()) //there is citadel
  847. {
  848. shipyard->offset = 1;
  849. shipyard->max = shipyard->def->ourImages.size();
  850. }
  851. else
  852. {
  853. shipyard->offset = 0;
  854. shipyard->max = 1;
  855. }
  856. }
  857. }
  858. CRecruitmentWindow * CCastleInterface::showRecruitmentWindow( int building )
  859. {
  860. if(building>36)
  861. building-=7;
  862. std::vector<std::pair<int,int > > crs;
  863. int amount = (const_cast<CGTownInstance*>(town))->strInfo.creatures[building-30]; //trzeba odconstowac, bo inaczej operator [] by sypal :(
  864. if(town->builtBuildings.find(building+7) != town->builtBuildings.end()) //check if there is an upgraded building
  865. crs.push_back(std::make_pair(town->town->upgradedCreatures[building-30],amount));
  866. crs.push_back(std::make_pair(town->town->basicCreatures[building-30],amount));
  867. CRecruitmentWindow *rw = new CRecruitmentWindow(crs,boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  868. LOCPLINT->pushInt(rw);
  869. return rw;
  870. }
  871. void CCastleInterface::enterMageGuild()
  872. {
  873. LOCPLINT->pushInt(new CMageGuildScreen(this));
  874. }
  875. void CCastleInterface::enterTavern()
  876. {
  877. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(town);
  878. CTavernWindow *tv = new CTavernWindow(h[0],h[1],"GOSSIP TEST");
  879. LOCPLINT->pushInt(tv);
  880. }
  881. void CHallInterface::CBuildingBox::hover(bool on)
  882. {
  883. Hoverable::hover(on);
  884. if(on)
  885. {
  886. std::string toPrint;
  887. if(state==8)
  888. toPrint = CGI->generaltexth->hcommands[5];
  889. else
  890. toPrint = CGI->generaltexth->hcommands[state];
  891. std::vector<std::string> name;
  892. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->Name());
  893. LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
  894. }
  895. else
  896. LOCPLINT->statusbar->clear();
  897. }
  898. void CHallInterface::CBuildingBox::clickLeft (tribool down)
  899. {
  900. if(pressedL && (!down))
  901. {
  902. LOCPLINT->pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0));
  903. }
  904. ClickableL::clickLeft(down);
  905. }
  906. void CHallInterface::CBuildingBox::clickRight (tribool down)
  907. {
  908. if(down)
  909. {
  910. LOCPLINT->pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1));
  911. }
  912. ClickableR::clickRight(down);
  913. }
  914. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  915. {
  916. CCastleInterface *ci = LOCPLINT->castleInt;
  917. blitAt(ci->bicons->ourImages[BID].bitmap,pos.x,pos.y,to);
  918. int pom, pom2=-1;
  919. switch (state)
  920. {
  921. case 4:
  922. pom = 0;
  923. pom2 = 0;
  924. break;
  925. case 7:
  926. pom = 1;
  927. break;
  928. case 6:
  929. pom2 = 2;
  930. pom = 2;
  931. break;
  932. case 0: case 5: case 8:
  933. pom2 = 1;
  934. pom = 2;
  935. break;
  936. case 2: case 1: default:
  937. pom = 3;
  938. break;
  939. }
  940. blitAt(ci->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71,to);
  941. if(pom2>=0)
  942. blitAt(ci->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54,to);
  943. CSDL_Ext::printAtMiddle(CGI->buildh->buildings[ci->town->subID][BID]->Name(),pos.x-1+ci->bars->ourImages[0].bitmap->w/2,pos.y+71+ci->bars->ourImages[0].bitmap->h/2, GEOR13,zwykly,to);
  944. }
  945. void CHallInterface::CBuildingBox::activate()
  946. {
  947. Hoverable::activate();
  948. ClickableL::activate();
  949. ClickableR::activate();
  950. }
  951. void CHallInterface::CBuildingBox::deactivate()
  952. {
  953. Hoverable::deactivate();
  954. ClickableL::deactivate();
  955. ClickableR::deactivate();
  956. }
  957. CHallInterface::CBuildingBox::~CBuildingBox()
  958. {
  959. }
  960. CHallInterface::CBuildingBox::CBuildingBox(int id)
  961. :BID(id)
  962. {
  963. pos.w = 150;
  964. pos.h = 70;
  965. }
  966. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  967. :BID(id)
  968. {
  969. pos.x = x;
  970. pos.y = y;
  971. pos.w = 150;
  972. pos.h = 70;
  973. }
  974. CHallInterface::CHallInterface(CCastleInterface * owner)
  975. {
  976. resdatabar = new CMinorResDataBar;
  977. pos = owner->pos;
  978. resdatabar->pos.x += pos.x;
  979. resdatabar->pos.y += pos.y;
  980. bg = BitmapHandler::loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  981. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  982. exit = new AdventureMapButton
  983. (CGI->generaltexth->tcommands[8],"",boost::bind(&CHallInterface::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  984. exit->assignedKeys.insert(SDLK_ESCAPE);
  985. //preparing boxes with buildings//
  986. boxes.resize(5);
  987. for(size_t i=0;i<5;i++) //for each row
  988. {
  989. std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[owner->town->subID].second;
  990. for(size_t j=0; j<boxList[i].size();j++) //for each box
  991. {
  992. size_t k=0;
  993. for(;k<boxList[i][j].size();k++)//we are looking for the first not build structure
  994. {
  995. int bid = boxList[i][j][k];
  996. if(!vstd::contains(owner->town->builtBuildings,bid))
  997. {
  998. int x = 34 + 194*j,
  999. y = 37 + 104*i,
  1000. ID = bid;
  1001. if(boxList[i].size() == 2) //only two boxes in this row
  1002. x+=194;
  1003. else if(boxList[i].size() == 3) //only three boxes in this row
  1004. x+=97;
  1005. boxes[i].push_back(new CBuildingBox(bid,pos.x+x,pos.y+y));
  1006. //if this is horde dwelling for upgraded creatures and we already have one for basic creatures
  1007. if((bid == 25 && vstd::contains(owner->town->builtBuildings,24))
  1008. || (bid == 19 && vstd::contains(owner->town->builtBuildings,18))
  1009. )
  1010. {
  1011. boxes[i].back()->state = 4; //already built
  1012. }
  1013. else
  1014. {
  1015. boxes[i].back()->state = LOCPLINT->cb->canBuildStructure(owner->town,ID);
  1016. }
  1017. break;
  1018. }
  1019. }
  1020. if(k == boxList[i][j].size()) //all buildings built - let's take the last one
  1021. {
  1022. int x = 34 + 194*j,
  1023. y = 37 + 104*i;
  1024. if(boxList[i].size() == 2)
  1025. x+=194;
  1026. else if(boxList[i].size() == 3)
  1027. x+=97;
  1028. boxes[i].push_back(new CBuildingBox(boxList[i][j][k-1],pos.x+x,pos.y+y));
  1029. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  1030. }
  1031. }
  1032. }
  1033. }
  1034. CHallInterface::~CHallInterface()
  1035. {
  1036. SDL_FreeSurface(bg);
  1037. for(size_t i=0;i<boxes.size();i++)
  1038. for(size_t j=0;j<boxes[i].size();j++)
  1039. delete boxes[i][j]; //TODO whats wrong with smartpointers?
  1040. delete exit;
  1041. delete resdatabar;
  1042. }
  1043. void CHallInterface::close()
  1044. {
  1045. LOCPLINT->popIntTotally(this);
  1046. }
  1047. void CHallInterface::show(SDL_Surface * to)
  1048. {
  1049. blitAt(bg,pos,to);
  1050. LOCPLINT->castleInt->statusbar->show(to);
  1051. resdatabar->show(to);
  1052. exit->show(to);
  1053. for(int i=0; i<5; i++)
  1054. {
  1055. for(size_t j=0;j<boxes[i].size(); ++j)
  1056. boxes[i][j]->show(to);
  1057. }
  1058. }
  1059. void CHallInterface::activate()
  1060. {
  1061. for(int i=0;i<5;i++)
  1062. {
  1063. for(size_t j=0; j < boxes[i].size(); ++j)
  1064. {
  1065. boxes[i][j]->activate();
  1066. }
  1067. }
  1068. exit->activate();
  1069. }
  1070. void CHallInterface::deactivate()
  1071. {
  1072. for(int i=0;i<5;i++)
  1073. {
  1074. for(size_t j=0;j<boxes[i].size();++j)
  1075. {
  1076. boxes[i][j]->deactivate();
  1077. }
  1078. }
  1079. exit->deactivate();
  1080. }
  1081. void CHallInterface::CBuildWindow::activate()
  1082. {
  1083. ClickableR::activate();
  1084. if(mode)
  1085. return;
  1086. if(state==7)
  1087. buy->activate();
  1088. cancel->activate();
  1089. }
  1090. void CHallInterface::CBuildWindow::deactivate()
  1091. {
  1092. ClickableR::deactivate();
  1093. if(mode)
  1094. return;
  1095. if(state==7)
  1096. buy->deactivate();
  1097. cancel->deactivate();
  1098. }
  1099. void CHallInterface::CBuildWindow::Buy()
  1100. {
  1101. int building = bid;
  1102. LOCPLINT->popInts(2); //we - build window and hall screen
  1103. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,building);
  1104. }
  1105. void CHallInterface::CBuildWindow::close()
  1106. {
  1107. LOCPLINT->popIntTotally(this);
  1108. }
  1109. void CHallInterface::CBuildWindow::clickRight (tribool down)
  1110. {
  1111. if((!down || indeterminate(down)) && mode)
  1112. close();
  1113. }
  1114. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  1115. {
  1116. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  1117. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  1118. SDL_BlitSurface(bitmap,&poms,to,&pom);
  1119. if(!mode)
  1120. {
  1121. buy->show(to);
  1122. cancel->show(to);
  1123. }
  1124. }
  1125. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  1126. {
  1127. std::string ret;
  1128. if(state<7)
  1129. ret = CGI->generaltexth->hcommands[state];
  1130. switch (state)
  1131. {
  1132. case 4: case 5: case 6:
  1133. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->Name());
  1134. break;
  1135. case 7:
  1136. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1137. case 8:
  1138. {
  1139. ret = CGI->generaltexth->allTexts[52];
  1140. std::set<int> used;
  1141. used.insert(bid);
  1142. std::set<int> reqs;
  1143. for(std::set<int>::iterator i=CGI->townh->requirements[tid][bid].begin();
  1144. i!=CGI->townh->requirements[tid][bid].end();
  1145. i++
  1146. )
  1147. {
  1148. if (LOCPLINT->castleInt->town->builtBuildings.find(*i) == LOCPLINT->castleInt->town->builtBuildings.end())
  1149. reqs.insert(*i);
  1150. }
  1151. while(true)
  1152. {
  1153. size_t czystych=0;
  1154. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1155. {
  1156. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  1157. {
  1158. used.insert(*i);
  1159. for(
  1160. std::set<int>::iterator j=CGI->townh->requirements[tid][*i].begin();
  1161. j!=CGI->townh->requirements[tid][*i].end();
  1162. j++
  1163. )
  1164. {
  1165. if(LOCPLINT->castleInt->town->builtBuildings.find(*j) == //this building is not built
  1166. LOCPLINT->castleInt->town->builtBuildings.end())
  1167. reqs.insert(*j);
  1168. }
  1169. }
  1170. else
  1171. {
  1172. czystych++;
  1173. }
  1174. }
  1175. if(czystych==reqs.size())
  1176. break;
  1177. }
  1178. bool first=true;
  1179. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1180. {
  1181. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->Name());
  1182. first = false;
  1183. }
  1184. }
  1185. }
  1186. return ret;
  1187. }
  1188. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  1189. :tid(Tid), bid(Bid), state(State), mode(Mode)
  1190. {
  1191. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPUBUILD.bmp");
  1192. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1193. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  1194. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  1195. SDL_FreeSurface(hhlp);
  1196. pos.x = screen->w/2 - bitmap->w/2;
  1197. pos.y = screen->h/2 - bitmap->h/2;
  1198. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  1199. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->Name());
  1200. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->Description(),197,168,GEOR16,40,zwykly,bitmap);
  1201. CSDL_Ext::printAtMiddleWB(getTextForState(state),197,248,GEOR13,50,zwykly,bitmap);
  1202. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->generaltexth->hcommands[7],pom),197,30,GEOR16,tytulowy,bitmap);
  1203. int resamount=0;
  1204. for(int i=0;i<7;i++)
  1205. {
  1206. if(CGI->buildh->buildings[tid][bid]->resources[i])
  1207. {
  1208. resamount++;
  1209. }
  1210. }
  1211. int ah = (resamount>4) ? 304 : 341;
  1212. int cn=-1, it=0;
  1213. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  1214. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  1215. char buf[15];
  1216. while(++cn<7)
  1217. {
  1218. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  1219. continue;
  1220. SDL_itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  1221. if(it<4)
  1222. {
  1223. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+42,GEOR16,zwykly,bitmap);
  1224. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  1225. }
  1226. else
  1227. {
  1228. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+42,GEOR16,zwykly,bitmap);
  1229. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  1230. }
  1231. if(it==4)
  1232. ah+=75;
  1233. }
  1234. if(!mode)
  1235. {
  1236. buy = new AdventureMapButton
  1237. ("","",boost::bind(&CBuildWindow::Buy,this),pos.x+45,pos.y+446,"IBUY30.DEF",SDLK_RETURN);
  1238. cancel = new AdventureMapButton
  1239. ("","",boost::bind(&CBuildWindow::close,this),pos.x+290,pos.y+445,"ICANCEL.DEF",SDLK_ESCAPE);
  1240. if(state!=7)
  1241. buy->state=2;
  1242. }
  1243. }
  1244. CHallInterface::CBuildWindow::~CBuildWindow()
  1245. {
  1246. SDL_FreeSurface(bitmap);
  1247. if(!mode)
  1248. {
  1249. delete buy;
  1250. delete cancel;
  1251. }
  1252. }
  1253. CFortScreen::~CFortScreen()
  1254. {
  1255. for(size_t i=0;i<crePics.size();i++)
  1256. delete crePics[i];
  1257. for (size_t i=0;i<recAreas.size();i++)
  1258. delete recAreas[i];
  1259. SDL_FreeSurface(bg);
  1260. delete exit;
  1261. delete resdatabar;
  1262. }
  1263. void CFortScreen::show( SDL_Surface * to)
  1264. {
  1265. blitAt(bg,pos,to);
  1266. static unsigned char anim = 1;
  1267. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1268. {
  1269. crePics[i]->blitPic(to,pos.x+positions[i].x+159,pos.y+positions[i].y+4,!(anim%4));
  1270. }
  1271. anim++;
  1272. exit->show(to);
  1273. resdatabar->show(to);
  1274. LOCPLINT->statusbar->show(to);
  1275. }
  1276. void CFortScreen::activate()
  1277. {
  1278. LOCPLINT->statusbar = LOCPLINT->castleInt->statusbar;
  1279. exit->activate();
  1280. for (size_t i=0;i<recAreas.size(); ++i)
  1281. {
  1282. recAreas[i]->activate();
  1283. }
  1284. }
  1285. void CFortScreen::deactivate()
  1286. {
  1287. exit->deactivate();
  1288. for (size_t i=0;i<recAreas.size();i++)
  1289. {
  1290. recAreas[i]->deactivate();
  1291. }
  1292. }
  1293. void CFortScreen::close()
  1294. {
  1295. LOCPLINT->popIntTotally(this);
  1296. }
  1297. CFortScreen::CFortScreen( CCastleInterface * owner )
  1298. {
  1299. resdatabar = new CMinorResDataBar;
  1300. pos = owner->pos;
  1301. bg = NULL;
  1302. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CFortScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1303. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1304. genRect(126,386,10,155),genRect(126,386,404,155),
  1305. genRect(126,386,10,288),genRect(126,386,404,288),
  1306. genRect(126,386,206,421);
  1307. draw(owner,true);
  1308. resdatabar->pos += pos;
  1309. }
  1310. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1311. {
  1312. if(bg)
  1313. SDL_FreeSurface(bg);
  1314. char buf[20];
  1315. memset(buf,0,20);
  1316. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1317. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1318. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1319. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1320. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1321. SDL_FreeSurface(bg2);
  1322. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name(),400,13,GEORXX,zwykly,bg);
  1323. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1324. {
  1325. bool upgraded = owner->town->creatureDwelling(i,true);
  1326. bool present = owner->town->creatureDwelling(i,false);
  1327. CCreature *c = &CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1328. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,GEOR13,zwykly,bg); //cr. name
  1329. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1330. printAtMiddle(CGI->buildh->buildings[owner->town->subID][30+i+upgraded*7]->Name(),positions[i].x+79,positions[i].y+100,GEOR13,zwykly,bg); //dwelling name
  1331. if(present) //if creature is present print avail able quantity
  1332. {
  1333. SDL_itoa(owner->town->strInfo.creatures.find(i)->second,buf,10);
  1334. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,GEOR13,zwykly,bg);
  1335. }
  1336. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1337. //atttack
  1338. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,GEOR13,zwykly,bg);
  1339. SDL_itoa(c->attack,buf,10);
  1340. printToWR(buf,positions[i].x+381,positions[i].y+18,GEOR13,zwykly,bg);
  1341. //defense
  1342. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,GEOR13,zwykly,bg);
  1343. SDL_itoa(c->defence,buf,10);
  1344. printToWR(buf,positions[i].x+381,positions[i].y+38,GEOR13,zwykly,bg);
  1345. //damage
  1346. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,GEOR13,zwykly,bg);
  1347. SDL_itoa(c->damageMin,buf,10);
  1348. int hlp;
  1349. if(c->damageMin > 0)
  1350. hlp = log10f(c->damageMin)+2;
  1351. else
  1352. hlp = 2;
  1353. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1354. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1355. printToWR(buf,positions[i].x+381,positions[i].y+59,GEOR13,zwykly,bg);
  1356. //health
  1357. printAt(CGI->generaltexth->zelp[439].first,positions[i].x+288,positions[i].y+66,GEOR13,zwykly,bg);
  1358. SDL_itoa(c->hitPoints,buf,10);
  1359. printToWR(buf,positions[i].x+381,positions[i].y+79,GEOR13,zwykly,bg);
  1360. //speed
  1361. printAt(CGI->generaltexth->zelp[441].first,positions[i].x+288,positions[i].y+87,GEOR13,zwykly,bg);
  1362. SDL_itoa(c->speed,buf,10);
  1363. printToWR(buf,positions[i].x+381,positions[i].y+100,GEOR13,zwykly,bg);
  1364. if(present)//growth
  1365. {
  1366. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,GEOR13,zwykly,bg);
  1367. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1368. printToWR(buf,positions[i].x+381,positions[i].y+120,GEOR13,zwykly,bg);
  1369. }
  1370. if(first)
  1371. {
  1372. crePics.push_back(new CCreaturePic(c,false));
  1373. if(present)
  1374. {
  1375. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1376. recAreas.back()->pos = positions[i] + pos;
  1377. }
  1378. }
  1379. }
  1380. SDL_FreeSurface(icons);
  1381. }
  1382. void CFortScreen::RecArea::clickLeft (tribool down)
  1383. {
  1384. if(!down && pressedL)
  1385. {
  1386. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1387. }
  1388. ClickableL::clickLeft(down);
  1389. }
  1390. void CFortScreen::RecArea::activate()
  1391. {
  1392. ClickableL::activate();
  1393. ClickableR::activate();
  1394. }
  1395. void CFortScreen::RecArea::deactivate()
  1396. {
  1397. ClickableL::deactivate();
  1398. ClickableR::deactivate();
  1399. }
  1400. void CFortScreen::RecArea::clickRight( tribool down )
  1401. {
  1402. clickLeft(down);; //r-click does same as l-click - opens recr. window
  1403. }
  1404. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1405. {
  1406. resdatabar = new CMinorResDataBar;
  1407. pos = owner->pos;
  1408. resdatabar->pos.x += pos.x;
  1409. resdatabar->pos.y += pos.y;
  1410. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1411. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CMageGuildScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1412. exit->assignedKeys.insert(SDLK_ESCAPE);
  1413. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1414. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1415. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1416. positions.resize(5);
  1417. 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);
  1418. 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);
  1419. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1420. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1421. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1422. blitAt(view,332,76,bg);
  1423. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1424. {
  1425. size_t sp = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1426. for(size_t j=0; j<sp; j++)
  1427. {
  1428. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1429. {
  1430. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1431. spells[spells.size()-1].pos = positions[i][j];
  1432. blitAt(graphics->spellscr->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1433. }
  1434. else
  1435. {
  1436. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1437. }
  1438. }
  1439. }
  1440. SDL_FreeSurface(view);
  1441. for(size_t i=0;i<spells.size();i++)
  1442. {
  1443. spells[i].pos.x += pos.x;
  1444. spells[i].pos.y += pos.y;
  1445. }
  1446. delete scrolls2;
  1447. }
  1448. CMageGuildScreen::~CMageGuildScreen()
  1449. {
  1450. delete exit;
  1451. SDL_FreeSurface(bg);
  1452. delete resdatabar;
  1453. }
  1454. void CMageGuildScreen::close()
  1455. {
  1456. LOCPLINT->popIntTotally(this);
  1457. }
  1458. void CMageGuildScreen::show(SDL_Surface * to)
  1459. {
  1460. blitAt(bg,pos,to);
  1461. resdatabar->show(to);
  1462. LOCPLINT->statusbar->show(to);
  1463. exit->show(to);
  1464. }
  1465. void CMageGuildScreen::activate()
  1466. {
  1467. exit->activate();
  1468. for(size_t i=0;i<spells.size();i++)
  1469. {
  1470. spells[i].activate();
  1471. }
  1472. }
  1473. void CMageGuildScreen::deactivate()
  1474. {
  1475. exit->deactivate();
  1476. for(size_t i=0;i<spells.size();i++)
  1477. {
  1478. spells[i].deactivate();
  1479. }
  1480. }
  1481. void CMageGuildScreen::Scroll::clickLeft (tribool down)
  1482. {
  1483. if(down)
  1484. {
  1485. std::vector<SComponent*> comps(1,
  1486. new CCustomImgComponent(SComponent::spell,spell->id,0,graphics->spellscr->ourImages[spell->id].bitmap,false)
  1487. );
  1488. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1489. }
  1490. }
  1491. void CMageGuildScreen::Scroll::clickRight (tribool down)
  1492. {
  1493. if(down)
  1494. {
  1495. CInfoPopup *vinya = new CInfoPopup();
  1496. vinya->free = true;
  1497. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1498. (LOCPLINT->playerID,
  1499. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1500. spell->name,30,30);
  1501. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1502. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1503. LOCPLINT->pushInt(vinya);
  1504. }
  1505. }
  1506. void CMageGuildScreen::Scroll::hover(bool on)
  1507. {
  1508. Hoverable::hover(on);
  1509. if(on)
  1510. LOCPLINT->statusbar->print(spell->name);
  1511. else
  1512. LOCPLINT->statusbar->clear();
  1513. }
  1514. void CMageGuildScreen::Scroll::activate()
  1515. {
  1516. ClickableL::activate();
  1517. ClickableR::activate();
  1518. Hoverable::activate();
  1519. }
  1520. void CMageGuildScreen::Scroll::deactivate()
  1521. {
  1522. ClickableL::deactivate();
  1523. ClickableR::deactivate();
  1524. Hoverable::deactivate();
  1525. }
  1526. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1527. {
  1528. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1529. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1530. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1531. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1532. SDL_FreeSurface(bg2);
  1533. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1534. blitAt(bg2,64,50,bmp);
  1535. SDL_FreeSurface(bg2);
  1536. CCreatureAnimation cra(CGI->creh->creatures[creMachineID].animDefName);
  1537. cra.nextFrameMiddle(bmp,170,120,true,0,false);
  1538. char pom[75];
  1539. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID].nameSing.c_str()); //build a new ...
  1540. printAtMiddle(pom,165,28,GEORXX,tytulowy,bmp);
  1541. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,GEOR16,zwykly,bmp); //resource cost
  1542. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1543. printAtMiddle(pom,165,290,GEOR13,zwykly,bmp);
  1544. pos.w = bmp->w;
  1545. pos.h = bmp->h;
  1546. pos.x = screen->w/2 - pos.w/2;
  1547. pos.y = screen->h/2 - pos.h/2;
  1548. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF",SDLK_RETURN);
  1549. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1550. if(possible)
  1551. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1552. else
  1553. buy->bitmapOffset = 2;
  1554. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1555. }
  1556. void CBlacksmithDialog::show( SDL_Surface * to )
  1557. {
  1558. blitAt(bmp,pos,to);
  1559. buy->show(to);
  1560. cancel->show(to);
  1561. }
  1562. void CBlacksmithDialog::activate()
  1563. {
  1564. if(!buy->bitmapOffset)
  1565. buy->activate();
  1566. cancel->activate();
  1567. }
  1568. void CBlacksmithDialog::deactivate()
  1569. {
  1570. if(!buy->bitmapOffset)
  1571. buy->deactivate();
  1572. cancel->deactivate();
  1573. }
  1574. CBlacksmithDialog::~CBlacksmithDialog()
  1575. {
  1576. SDL_FreeSurface(bmp);
  1577. delete cancel;
  1578. delete buy;
  1579. }
  1580. void CBlacksmithDialog::close()
  1581. {
  1582. LOCPLINT->popIntTotally(this);
  1583. }