CCastleInterface.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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,Point(0,96),townInt,Point(62,374),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. const CGHeroInstance *h = NULL; //hero that "enters" mage guild
  509. if(!town->garrisonHero && !town->visitingHero) //no heroes in town
  510. h = NULL;
  511. else if(!town->garrisonHero) //only visiting hero
  512. h = town->visitingHero;
  513. else if(!town->visitingHero || hslotup.highlight) //only garrisoned hero OR both heroes present, garrisoned hero selected
  514. h = town->garrisonHero;
  515. else //both heroes present, use the visiting one
  516. h = town->visitingHero;
  517. if(h && !vstd::contains(h->artifWorn,ui16(17))) //hero doesn't have spellbok
  518. {
  519. if(LOCPLINT->cb->getResourceAmount(6) < 500) //not enough gold to buy spellbook
  520. {
  521. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213],std::vector<SComponent*>(), soundBase::sound_todo);
  522. }
  523. else
  524. {
  525. CFunctionList<void()> fl = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,h,0);
  526. fl += boost::bind(&CCastleInterface::enterMageGuild,this);
  527. std::vector<SComponent*> vvv(1,new SComponent(SComponent::artifact,0,0));
  528. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214],vvv,fl,0,true);
  529. }
  530. }
  531. else
  532. {
  533. enterMageGuild();
  534. }
  535. break;
  536. }
  537. case 5: //tavern
  538. {
  539. enterTavern();
  540. break;
  541. }
  542. //TODO: case 6: //shipyard
  543. case 7: case 8: case 9: //fort/citadel/castle
  544. {
  545. CFortScreen *fs = new CFortScreen(this);
  546. LOCPLINT->pushInt(fs);
  547. break;
  548. }
  549. case 10: case 11: case 12: case 13: //hall
  550. enterHall();
  551. break;
  552. case 14: //marketplace
  553. {
  554. CMarketplaceWindow *cmw = new CMarketplaceWindow(0);
  555. LOCPLINT->pushInt(cmw);
  556. break;
  557. }
  558. case 15: //resource silo
  559. {
  560. LOCPLINT->showInfoDialog(CGI->buildh->buildings[town->subID][15]->Description(),std::vector<SComponent*>(), soundBase::sound_todo);
  561. break;
  562. }
  563. case 16: //blacksmith
  564. {
  565. const CGHeroInstance *hero = town->visitingHero;
  566. if(!hero)
  567. {
  568. std::string pom = CGI->generaltexth->allTexts[273];
  569. boost::algorithm::replace_first(pom,"%s",CGI->buildh->buildings[town->subID][16]->Name());
  570. LOCPLINT->showInfoDialog(pom,std::vector<SComponent*>(), soundBase::sound_todo);
  571. return;
  572. }
  573. int aid = town->town->warMachine;
  574. int price = CGI->arth->artifacts[aid].price;
  575. bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
  576. if(vstd::contains(hero->artifWorn,ui16(aid+9))) //hero already has machine
  577. possible = false;
  578. LOCPLINT->pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(aid,false),aid,hero->id));
  579. break;
  580. }
  581. //TODO: case 17: //special 1
  582. //TODO: case 18: //basic horde 1
  583. //TODO: case 19: //upg horde 1
  584. //TODO: case 20: //ship at shipyard
  585. //TODO: case 21: //special 2
  586. case 22: //special 3
  587. {
  588. switch(town->subID)
  589. {
  590. case 0: //brotherhood of sword
  591. enterTavern();
  592. break;
  593. default:
  594. tlog4<<"This building isn't handled...\n";
  595. break;
  596. }
  597. break;
  598. }
  599. //TODO: case 23: //special 4
  600. //TODO: case 24: //basic horde 2
  601. //TODO: case 25: //upg horde 2
  602. //TODO: case 26: //grail
  603. default:
  604. tlog4<<"This building isn't handled...\n";
  605. }
  606. }
  607. }
  608. void CCastleInterface::enterHall()
  609. {
  610. CHallInterface *h = new CHallInterface(this);
  611. LOCPLINT->pushInt(h);
  612. }
  613. void CCastleInterface::showAll( SDL_Surface * to/*=NULL*/)
  614. {
  615. blitAt(cityBg,pos,to);
  616. blitAt(townInt,pos.x,pos.y+374,to);
  617. LOCPLINT->adventureInt->resdatabar.draw(to);
  618. townlist->draw(to);
  619. statusbar->show(to);
  620. resdatabar->draw(to);
  621. garr->show(to);
  622. int pom;
  623. //draw fort icon
  624. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  625. pom = 2;
  626. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  627. pom = 1;
  628. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  629. pom = 0;
  630. else pom = 3;
  631. blitAt(fort->ourImages[pom].bitmap,pos.x+122,pos.y+413,to);
  632. //draw ((village/town/city) hall)/capitol icon
  633. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  634. pom = 3;
  635. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  636. pom = 2;
  637. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  638. pom = 1;
  639. else pom = 0;
  640. blitAt(hall->ourImages[pom].bitmap,pos.x+80,pos.y+413,to);
  641. //draw creatures icons and their growths
  642. for(int i=0;i<CREATURES_PER_TOWN;i++)
  643. {
  644. int cid = -1;
  645. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  646. {
  647. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  648. cid = town->town->upgradedCreatures[i];
  649. else
  650. cid = town->town->basicCreatures[i];
  651. }
  652. if (cid>=0)
  653. {
  654. int pomx, pomy;
  655. pomx = 22 + (55*((i>3)?(i-4):i));
  656. pomy = (i>3)?(507):(459);
  657. blitAt(graphics->smallImgs[cid],pos.x+pomx,pos.y+pomy,to);
  658. std::ostringstream oss;
  659. oss << '+' << town->creatureGrowth(i);
  660. CSDL_Ext::printAtMiddle(oss.str(),pos.x+pomx+16,pos.y+pomy+37,GEOR13,zwykly,to);
  661. }
  662. }
  663. //print name and income
  664. CSDL_Ext::printAt(town->name,pos.x+85,pos.y+389,GEOR13,zwykly,to);
  665. char temp[10];
  666. SDL_itoa(town->dailyIncome(),temp,10);
  667. CSDL_Ext::printAtMiddle(temp,pos.x+195,pos.y+442,GEOR13,zwykly,to);
  668. //blit town icon
  669. pom = town->subID*2;
  670. if (!town->hasFort())
  671. pom += F_NUMBER*2;
  672. if(town->builded >= MAX_BUILDING_PER_TURN)
  673. pom++;
  674. blitAt(graphics->bigTownPic->ourImages[pom].bitmap,pos.x+15,pos.y+387,to);
  675. hslotup.show(to);
  676. hslotdown.show(to);
  677. show(to);
  678. if(screen->w != 800 || screen->h !=600)
  679. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  680. exit->show(to);
  681. split->show(to);
  682. }
  683. void CCastleInterface::townChange()
  684. {
  685. const CGTownInstance * nt = townlist->items[townlist->selected];
  686. LOCPLINT->popIntTotally(this);
  687. LOCPLINT->pushInt(new CCastleInterface(nt));
  688. }
  689. void CCastleInterface::show(SDL_Surface * to)
  690. {
  691. count++;
  692. if(count==8)
  693. {
  694. count=0;
  695. animval++;
  696. }
  697. blitAt(cityBg,pos,to);
  698. //blit buildings
  699. for(size_t i=0;i<buildings.size();i++)
  700. {
  701. int frame = ((animval)%(buildings[i]->max - buildings[i]->offset)) + buildings[i]->offset;
  702. if(frame)
  703. {
  704. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  705. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  706. }
  707. else
  708. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  709. if(hBuild==buildings[i] && hBuild->border) //if this this higlighted structure and has border we'll blit it
  710. blitAt(hBuild->border,hBuild->pos,to);
  711. }
  712. }
  713. void CCastleInterface::activate()
  714. {
  715. showing = true;
  716. townlist->activate();
  717. garr->activate();
  718. LOCPLINT->statusbar = statusbar;
  719. exit->activate();
  720. split->activate();
  721. for(size_t i=0;i<buildings.size();i++) //XXX pls use iterators or at() but not []
  722. {
  723. buildings[i]->activate();
  724. }
  725. hslotdown.activate();
  726. hslotup.activate();
  727. }
  728. void CCastleInterface::deactivate()
  729. {
  730. showing = false;
  731. townlist->deactivate();
  732. garr->deactivate();
  733. exit->deactivate();
  734. split->deactivate();
  735. for(size_t i=0;i<buildings.size();i++) //XXX iterators
  736. {
  737. buildings[i]->deactivate();
  738. }
  739. hslotdown.deactivate();
  740. hslotup.deactivate();
  741. }
  742. void CCastleInterface::addBuilding(int bid)
  743. {
  744. //TODO: lepiej by bylo tylko dodawac co trzeba pamietajac o grupach
  745. deactivate();
  746. recreateBuildings();
  747. activate();
  748. }
  749. void CCastleInterface::removeBuilding(int bid)
  750. {
  751. //TODO: lepiej by bylo tylko usuwac co trzeba pamietajac o grupach
  752. recreateBuildings();
  753. }
  754. void CCastleInterface::recreateBuildings()
  755. {
  756. for(size_t i=0;i<buildings.size();i++)
  757. {
  758. if(showing)
  759. buildings[i]->deactivate();
  760. delete buildings[i];
  761. }
  762. buildings.clear();
  763. hBuild = NULL;
  764. std::set< std::pair<int,int> > s; //group - id
  765. for (std::set<si32>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  766. {
  767. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  768. {
  769. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  770. {
  771. Structure * st = CGI->townh->structures[town->subID][*i];
  772. if(st->group<0) //no group - just add it
  773. {
  774. buildings.push_back(new CBuildingRect(st));
  775. }
  776. else
  777. {
  778. std::set< std::pair<int,int> >::iterator obecny=s.end();
  779. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  780. {
  781. if(seti->first == st->group)
  782. {
  783. obecny = seti;
  784. break;
  785. }
  786. }
  787. if(obecny != s.end())
  788. {
  789. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  790. {
  791. for(size_t itpb = 0; itpb<buildings.size(); itpb++)
  792. {
  793. if(buildings[itpb]->str->ID == obecny->second)
  794. {
  795. delete buildings[itpb];
  796. buildings.erase(buildings.begin() + itpb);
  797. #ifndef __GNUC__
  798. obecny->second = st->ID; //XXX look closer!
  799. #else
  800. *(const_cast<int*>(&(obecny->second))) = st->ID;
  801. #endif
  802. buildings.push_back(new CBuildingRect(st));
  803. }
  804. }
  805. }
  806. }
  807. else
  808. {
  809. buildings.push_back(new CBuildingRect(st));
  810. s.insert(std::pair<int,int>(st->group,st->ID));
  811. }
  812. }
  813. }
  814. else continue;
  815. }
  816. else
  817. break;
  818. }
  819. std::sort(buildings.begin(),buildings.end(),srthlp);
  820. //code for Mana Vortex (there are two sets of animation frames - one without mage guild and one with
  821. if((town->subID == 5) && (town->builtBuildings.find(21)!=town->builtBuildings.end()))
  822. {
  823. CBuildingRect *vortex = NULL;
  824. for(size_t i=0;i<buildings.size();i++)
  825. {
  826. if(buildings[i]->str->ID==21)
  827. {
  828. vortex=buildings[i];
  829. break;
  830. }
  831. }
  832. if(town->builtBuildings.find(4)!=town->builtBuildings.end()) //there is mage Guild level 5
  833. {
  834. vortex->offset = 10;
  835. vortex->max = vortex->def->ourImages.size();
  836. }
  837. else
  838. {
  839. vortex->offset = 0;
  840. vortex->max = 10;
  841. }
  842. }
  843. //code for the shipyard in the Castle
  844. else if((town->subID == 0) && (town->builtBuildings.find(6)!=town->builtBuildings.end()))
  845. {
  846. CBuildingRect *shipyard = NULL;
  847. for(size_t i=0;i<buildings.size();i++)
  848. {
  849. if(buildings[i]->str->ID==6)
  850. {
  851. shipyard=buildings[i];
  852. break;
  853. }
  854. }
  855. if(town->builtBuildings.find(8)!=town->builtBuildings.end()) //there is citadel
  856. {
  857. shipyard->offset = 1;
  858. shipyard->max = shipyard->def->ourImages.size();
  859. }
  860. else
  861. {
  862. shipyard->offset = 0;
  863. shipyard->max = 1;
  864. }
  865. }
  866. }
  867. CRecruitmentWindow * CCastleInterface::showRecruitmentWindow( int building )
  868. {
  869. if(building>36) //upg dwelling
  870. building-=7;
  871. int level = building-30;
  872. assert(level >= 0 && level <= 6);
  873. std::vector<std::pair<int,int > > crs;
  874. int amount = town->creatures[level].first;
  875. const std::vector<ui32> &cres = town->creatures[level].second;
  876. for(size_t i = 0; i < cres.size(); i++)
  877. crs.push_back(std::make_pair((int)cres[i],amount));
  878. CRecruitmentWindow *rw = new CRecruitmentWindow(crs,boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  879. LOCPLINT->pushInt(rw);
  880. return rw;
  881. }
  882. void CCastleInterface::enterMageGuild()
  883. {
  884. LOCPLINT->pushInt(new CMageGuildScreen(this));
  885. }
  886. void CCastleInterface::enterTavern()
  887. {
  888. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(town);
  889. CTavernWindow *tv = new CTavernWindow(h[0],h[1],"GOSSIP TEST");
  890. LOCPLINT->pushInt(tv);
  891. }
  892. void CHallInterface::CBuildingBox::hover(bool on)
  893. {
  894. Hoverable::hover(on);
  895. if(on)
  896. {
  897. std::string toPrint;
  898. if(state==8)
  899. toPrint = CGI->generaltexth->hcommands[5];
  900. else
  901. toPrint = CGI->generaltexth->hcommands[state];
  902. std::vector<std::string> name;
  903. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->Name());
  904. LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
  905. }
  906. else
  907. LOCPLINT->statusbar->clear();
  908. }
  909. void CHallInterface::CBuildingBox::clickLeft (tribool down)
  910. {
  911. if(pressedL && (!down))
  912. {
  913. LOCPLINT->pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0));
  914. }
  915. ClickableL::clickLeft(down);
  916. }
  917. void CHallInterface::CBuildingBox::clickRight (tribool down)
  918. {
  919. if(down)
  920. {
  921. LOCPLINT->pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1));
  922. }
  923. ClickableR::clickRight(down);
  924. }
  925. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  926. {
  927. CCastleInterface *ci = LOCPLINT->castleInt;
  928. blitAt(ci->bicons->ourImages[BID].bitmap,pos.x,pos.y,to);
  929. int pom, pom2=-1;
  930. switch (state)
  931. {
  932. case 4:
  933. pom = 0;
  934. pom2 = 0;
  935. break;
  936. case 7:
  937. pom = 1;
  938. break;
  939. case 6:
  940. pom2 = 2;
  941. pom = 2;
  942. break;
  943. case 0: case 5: case 8:
  944. pom2 = 1;
  945. pom = 2;
  946. break;
  947. case 2: case 1: default:
  948. pom = 3;
  949. break;
  950. }
  951. blitAt(ci->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71,to);
  952. if(pom2>=0)
  953. blitAt(ci->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54,to);
  954. 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);
  955. }
  956. void CHallInterface::CBuildingBox::activate()
  957. {
  958. Hoverable::activate();
  959. ClickableL::activate();
  960. ClickableR::activate();
  961. }
  962. void CHallInterface::CBuildingBox::deactivate()
  963. {
  964. Hoverable::deactivate();
  965. ClickableL::deactivate();
  966. ClickableR::deactivate();
  967. }
  968. CHallInterface::CBuildingBox::~CBuildingBox()
  969. {
  970. }
  971. CHallInterface::CBuildingBox::CBuildingBox(int id)
  972. :BID(id)
  973. {
  974. pos.w = 150;
  975. pos.h = 70;
  976. }
  977. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  978. :BID(id)
  979. {
  980. pos.x = x;
  981. pos.y = y;
  982. pos.w = 150;
  983. pos.h = 70;
  984. }
  985. CHallInterface::CHallInterface(CCastleInterface * owner)
  986. {
  987. resdatabar = new CMinorResDataBar;
  988. pos = owner->pos;
  989. resdatabar->pos.x += pos.x;
  990. resdatabar->pos.y += pos.y;
  991. bg = BitmapHandler::loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  992. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  993. exit = new AdventureMapButton
  994. (CGI->generaltexth->tcommands[8],"",boost::bind(&CHallInterface::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  995. exit->assignedKeys.insert(SDLK_ESCAPE);
  996. //preparing boxes with buildings//
  997. boxes.resize(5);
  998. for(size_t i=0;i<5;i++) //for each row
  999. {
  1000. std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[owner->town->subID].second;
  1001. for(size_t j=0; j<boxList[i].size();j++) //for each box
  1002. {
  1003. size_t k=0;
  1004. for(;k<boxList[i][j].size();k++)//we are looking for the first not build structure
  1005. {
  1006. int bid = boxList[i][j][k];
  1007. if(!vstd::contains(owner->town->builtBuildings,bid))
  1008. {
  1009. int x = 34 + 194*j,
  1010. y = 37 + 104*i,
  1011. ID = bid;
  1012. if(boxList[i].size() == 2) //only two boxes in this row
  1013. x+=194;
  1014. else if(boxList[i].size() == 3) //only three boxes in this row
  1015. x+=97;
  1016. boxes[i].push_back(new CBuildingBox(bid,pos.x+x,pos.y+y));
  1017. //if this is horde dwelling for upgraded creatures and we already have one for basic creatures
  1018. if((bid == 25 && vstd::contains(owner->town->builtBuildings,24))
  1019. || (bid == 19 && vstd::contains(owner->town->builtBuildings,18))
  1020. )
  1021. {
  1022. boxes[i].back()->state = 4; //already built
  1023. }
  1024. else
  1025. {
  1026. boxes[i].back()->state = LOCPLINT->cb->canBuildStructure(owner->town,ID);
  1027. }
  1028. break;
  1029. }
  1030. }
  1031. if(k == boxList[i][j].size()) //all buildings built - let's take the last one
  1032. {
  1033. int x = 34 + 194*j,
  1034. y = 37 + 104*i;
  1035. if(boxList[i].size() == 2)
  1036. x+=194;
  1037. else if(boxList[i].size() == 3)
  1038. x+=97;
  1039. boxes[i].push_back(new CBuildingBox(boxList[i][j][k-1],pos.x+x,pos.y+y));
  1040. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  1041. }
  1042. }
  1043. }
  1044. }
  1045. CHallInterface::~CHallInterface()
  1046. {
  1047. SDL_FreeSurface(bg);
  1048. for(size_t i=0;i<boxes.size();i++)
  1049. for(size_t j=0;j<boxes[i].size();j++)
  1050. delete boxes[i][j]; //TODO whats wrong with smartpointers?
  1051. delete exit;
  1052. delete resdatabar;
  1053. }
  1054. void CHallInterface::close()
  1055. {
  1056. LOCPLINT->popIntTotally(this);
  1057. }
  1058. void CHallInterface::show(SDL_Surface * to)
  1059. {
  1060. blitAt(bg,pos,to);
  1061. LOCPLINT->castleInt->statusbar->show(to);
  1062. resdatabar->show(to);
  1063. exit->show(to);
  1064. for(int i=0; i<5; i++)
  1065. {
  1066. for(size_t j=0;j<boxes[i].size(); ++j)
  1067. boxes[i][j]->show(to);
  1068. }
  1069. }
  1070. void CHallInterface::activate()
  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]->activate();
  1077. }
  1078. }
  1079. exit->activate();
  1080. }
  1081. void CHallInterface::deactivate()
  1082. {
  1083. for(int i=0;i<5;i++)
  1084. {
  1085. for(size_t j=0;j<boxes[i].size();++j)
  1086. {
  1087. boxes[i][j]->deactivate();
  1088. }
  1089. }
  1090. exit->deactivate();
  1091. }
  1092. void CHallInterface::CBuildWindow::activate()
  1093. {
  1094. ClickableR::activate();
  1095. if(mode)
  1096. return;
  1097. if(state==7)
  1098. buy->activate();
  1099. cancel->activate();
  1100. }
  1101. void CHallInterface::CBuildWindow::deactivate()
  1102. {
  1103. ClickableR::deactivate();
  1104. if(mode)
  1105. return;
  1106. if(state==7)
  1107. buy->deactivate();
  1108. cancel->deactivate();
  1109. }
  1110. void CHallInterface::CBuildWindow::Buy()
  1111. {
  1112. int building = bid;
  1113. LOCPLINT->popInts(2); //we - build window and hall screen
  1114. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,building);
  1115. }
  1116. void CHallInterface::CBuildWindow::close()
  1117. {
  1118. LOCPLINT->popIntTotally(this);
  1119. }
  1120. void CHallInterface::CBuildWindow::clickRight (tribool down)
  1121. {
  1122. if((!down || indeterminate(down)) && mode)
  1123. close();
  1124. }
  1125. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  1126. {
  1127. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  1128. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  1129. SDL_BlitSurface(bitmap,&poms,to,&pom);
  1130. if(!mode)
  1131. {
  1132. buy->show(to);
  1133. cancel->show(to);
  1134. }
  1135. }
  1136. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  1137. {
  1138. std::string ret;
  1139. if(state<7)
  1140. ret = CGI->generaltexth->hcommands[state];
  1141. switch (state)
  1142. {
  1143. case 4: case 5: case 6:
  1144. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->Name());
  1145. break;
  1146. case 7:
  1147. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1148. case 8:
  1149. {
  1150. ret = CGI->generaltexth->allTexts[52];
  1151. std::set<int> used;
  1152. used.insert(bid);
  1153. std::set<int> reqs;
  1154. for(std::set<int>::iterator i=CGI->townh->requirements[tid][bid].begin();
  1155. i!=CGI->townh->requirements[tid][bid].end();
  1156. i++
  1157. )
  1158. {
  1159. if (LOCPLINT->castleInt->town->builtBuildings.find(*i) == LOCPLINT->castleInt->town->builtBuildings.end())
  1160. reqs.insert(*i);
  1161. }
  1162. while(true)
  1163. {
  1164. size_t czystych=0;
  1165. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1166. {
  1167. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  1168. {
  1169. used.insert(*i);
  1170. for(
  1171. std::set<int>::iterator j=CGI->townh->requirements[tid][*i].begin();
  1172. j!=CGI->townh->requirements[tid][*i].end();
  1173. j++
  1174. )
  1175. {
  1176. if(LOCPLINT->castleInt->town->builtBuildings.find(*j) == //this building is not built
  1177. LOCPLINT->castleInt->town->builtBuildings.end())
  1178. reqs.insert(*j);
  1179. }
  1180. }
  1181. else
  1182. {
  1183. czystych++;
  1184. }
  1185. }
  1186. if(czystych==reqs.size())
  1187. break;
  1188. }
  1189. bool first=true;
  1190. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1191. {
  1192. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->Name());
  1193. first = false;
  1194. }
  1195. }
  1196. }
  1197. return ret;
  1198. }
  1199. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  1200. :tid(Tid), bid(Bid), state(State), mode(Mode)
  1201. {
  1202. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPUBUILD.bmp");
  1203. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1204. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  1205. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  1206. SDL_FreeSurface(hhlp);
  1207. pos.x = screen->w/2 - bitmap->w/2;
  1208. pos.y = screen->h/2 - bitmap->h/2;
  1209. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  1210. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->Name());
  1211. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->Description(),197,168,GEOR16,40,zwykly,bitmap);
  1212. CSDL_Ext::printAtMiddleWB(getTextForState(state),197,248,GEOR13,50,zwykly,bitmap);
  1213. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->generaltexth->hcommands[7],pom),197,30,GEOR16,tytulowy,bitmap);
  1214. int resamount=0;
  1215. for(int i=0;i<7;i++)
  1216. {
  1217. if(CGI->buildh->buildings[tid][bid]->resources[i])
  1218. {
  1219. resamount++;
  1220. }
  1221. }
  1222. int ah = (resamount>4) ? 304 : 341;
  1223. int cn=-1, it=0;
  1224. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  1225. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  1226. char buf[15];
  1227. while(++cn<7)
  1228. {
  1229. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  1230. continue;
  1231. SDL_itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  1232. if(it<4)
  1233. {
  1234. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+42,GEOR16,zwykly,bitmap);
  1235. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  1236. }
  1237. else
  1238. {
  1239. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+42,GEOR16,zwykly,bitmap);
  1240. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  1241. }
  1242. if(it==4)
  1243. ah+=75;
  1244. }
  1245. if(!mode)
  1246. {
  1247. buy = new AdventureMapButton
  1248. ("","",boost::bind(&CBuildWindow::Buy,this),pos.x+45,pos.y+446,"IBUY30.DEF",SDLK_RETURN);
  1249. cancel = new AdventureMapButton
  1250. ("","",boost::bind(&CBuildWindow::close,this),pos.x+290,pos.y+445,"ICANCEL.DEF",SDLK_ESCAPE);
  1251. if(state!=7)
  1252. buy->state=2;
  1253. }
  1254. }
  1255. CHallInterface::CBuildWindow::~CBuildWindow()
  1256. {
  1257. SDL_FreeSurface(bitmap);
  1258. if(!mode)
  1259. {
  1260. delete buy;
  1261. delete cancel;
  1262. }
  1263. }
  1264. CFortScreen::~CFortScreen()
  1265. {
  1266. for(size_t i=0;i<crePics.size();i++)
  1267. delete crePics[i];
  1268. for (size_t i=0;i<recAreas.size();i++)
  1269. delete recAreas[i];
  1270. SDL_FreeSurface(bg);
  1271. delete exit;
  1272. delete resdatabar;
  1273. }
  1274. void CFortScreen::show( SDL_Surface * to)
  1275. {
  1276. blitAt(bg,pos,to);
  1277. static unsigned char anim = 1;
  1278. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1279. {
  1280. crePics[i]->blitPic(to,pos.x+positions[i].x+159,pos.y+positions[i].y+4,!(anim%4));
  1281. }
  1282. anim++;
  1283. exit->show(to);
  1284. resdatabar->show(to);
  1285. LOCPLINT->statusbar->show(to);
  1286. }
  1287. void CFortScreen::activate()
  1288. {
  1289. LOCPLINT->statusbar = LOCPLINT->castleInt->statusbar;
  1290. exit->activate();
  1291. for (size_t i=0;i<recAreas.size(); ++i)
  1292. {
  1293. recAreas[i]->activate();
  1294. }
  1295. }
  1296. void CFortScreen::deactivate()
  1297. {
  1298. exit->deactivate();
  1299. for (size_t i=0;i<recAreas.size();i++)
  1300. {
  1301. recAreas[i]->deactivate();
  1302. }
  1303. }
  1304. void CFortScreen::close()
  1305. {
  1306. LOCPLINT->popIntTotally(this);
  1307. }
  1308. CFortScreen::CFortScreen( CCastleInterface * owner )
  1309. {
  1310. resdatabar = new CMinorResDataBar;
  1311. pos = owner->pos;
  1312. bg = NULL;
  1313. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CFortScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1314. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1315. genRect(126,386,10,155),genRect(126,386,404,155),
  1316. genRect(126,386,10,288),genRect(126,386,404,288),
  1317. genRect(126,386,206,421);
  1318. draw(owner,true);
  1319. resdatabar->pos += pos;
  1320. }
  1321. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1322. {
  1323. if(bg)
  1324. SDL_FreeSurface(bg);
  1325. char buf[20];
  1326. memset(buf,0,20);
  1327. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1328. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1329. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1330. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1331. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1332. SDL_FreeSurface(bg2);
  1333. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name(),400,13,GEORXX,zwykly,bg);
  1334. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1335. {
  1336. bool upgraded = owner->town->creatureDwelling(i,true);
  1337. bool present = owner->town->creatureDwelling(i,false);
  1338. CCreature *c = &CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1339. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,GEOR13,zwykly,bg); //cr. name
  1340. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1341. 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
  1342. if(present) //if creature is present print avail able quantity
  1343. {
  1344. SDL_itoa(owner->town->creatures[i].first,buf,10);
  1345. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,GEOR13,zwykly,bg);
  1346. }
  1347. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1348. //atttack
  1349. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,GEOR13,zwykly,bg);
  1350. SDL_itoa(c->attack,buf,10);
  1351. printToWR(buf,positions[i].x+381,positions[i].y+18,GEOR13,zwykly,bg);
  1352. //defense
  1353. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,GEOR13,zwykly,bg);
  1354. SDL_itoa(c->defence,buf,10);
  1355. printToWR(buf,positions[i].x+381,positions[i].y+38,GEOR13,zwykly,bg);
  1356. //damage
  1357. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,GEOR13,zwykly,bg);
  1358. SDL_itoa(c->damageMin,buf,10);
  1359. int hlp;
  1360. if(c->damageMin > 0)
  1361. hlp = log10f(c->damageMin)+2;
  1362. else
  1363. hlp = 2;
  1364. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1365. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1366. printToWR(buf,positions[i].x+381,positions[i].y+59,GEOR13,zwykly,bg);
  1367. //health
  1368. printAt(CGI->generaltexth->zelp[439].first,positions[i].x+288,positions[i].y+66,GEOR13,zwykly,bg);
  1369. SDL_itoa(c->hitPoints,buf,10);
  1370. printToWR(buf,positions[i].x+381,positions[i].y+79,GEOR13,zwykly,bg);
  1371. //speed
  1372. printAt(CGI->generaltexth->zelp[441].first,positions[i].x+288,positions[i].y+87,GEOR13,zwykly,bg);
  1373. SDL_itoa(c->speed,buf,10);
  1374. printToWR(buf,positions[i].x+381,positions[i].y+100,GEOR13,zwykly,bg);
  1375. if(present)//growth
  1376. {
  1377. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,GEOR13,zwykly,bg);
  1378. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1379. printToWR(buf,positions[i].x+381,positions[i].y+120,GEOR13,zwykly,bg);
  1380. }
  1381. if(first)
  1382. {
  1383. crePics.push_back(new CCreaturePic(c,false));
  1384. if(present)
  1385. {
  1386. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1387. recAreas.back()->pos = positions[i] + pos;
  1388. }
  1389. }
  1390. }
  1391. SDL_FreeSurface(icons);
  1392. }
  1393. void CFortScreen::RecArea::clickLeft (tribool down)
  1394. {
  1395. if(!down && pressedL)
  1396. {
  1397. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1398. }
  1399. ClickableL::clickLeft(down);
  1400. }
  1401. void CFortScreen::RecArea::activate()
  1402. {
  1403. ClickableL::activate();
  1404. ClickableR::activate();
  1405. }
  1406. void CFortScreen::RecArea::deactivate()
  1407. {
  1408. ClickableL::deactivate();
  1409. ClickableR::deactivate();
  1410. }
  1411. void CFortScreen::RecArea::clickRight( tribool down )
  1412. {
  1413. clickLeft(down);; //r-click does same as l-click - opens recr. window
  1414. }
  1415. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1416. {
  1417. resdatabar = new CMinorResDataBar;
  1418. pos = owner->pos;
  1419. resdatabar->pos.x += pos.x;
  1420. resdatabar->pos.y += pos.y;
  1421. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1422. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CMageGuildScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1423. exit->assignedKeys.insert(SDLK_ESCAPE);
  1424. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1425. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1426. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1427. positions.resize(5);
  1428. 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);
  1429. 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);
  1430. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1431. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1432. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1433. blitAt(view,332,76,bg);
  1434. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1435. {
  1436. size_t sp = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1437. for(size_t j=0; j<sp; j++)
  1438. {
  1439. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1440. {
  1441. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1442. spells[spells.size()-1].pos = positions[i][j];
  1443. blitAt(graphics->spellscr->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1444. }
  1445. else
  1446. {
  1447. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1448. }
  1449. }
  1450. }
  1451. SDL_FreeSurface(view);
  1452. for(size_t i=0;i<spells.size();i++)
  1453. {
  1454. spells[i].pos.x += pos.x;
  1455. spells[i].pos.y += pos.y;
  1456. }
  1457. delete scrolls2;
  1458. }
  1459. CMageGuildScreen::~CMageGuildScreen()
  1460. {
  1461. delete exit;
  1462. SDL_FreeSurface(bg);
  1463. delete resdatabar;
  1464. }
  1465. void CMageGuildScreen::close()
  1466. {
  1467. LOCPLINT->popIntTotally(this);
  1468. }
  1469. void CMageGuildScreen::show(SDL_Surface * to)
  1470. {
  1471. blitAt(bg,pos,to);
  1472. resdatabar->show(to);
  1473. LOCPLINT->statusbar->show(to);
  1474. exit->show(to);
  1475. }
  1476. void CMageGuildScreen::activate()
  1477. {
  1478. exit->activate();
  1479. for(size_t i=0;i<spells.size();i++)
  1480. {
  1481. spells[i].activate();
  1482. }
  1483. }
  1484. void CMageGuildScreen::deactivate()
  1485. {
  1486. exit->deactivate();
  1487. for(size_t i=0;i<spells.size();i++)
  1488. {
  1489. spells[i].deactivate();
  1490. }
  1491. }
  1492. void CMageGuildScreen::Scroll::clickLeft (tribool down)
  1493. {
  1494. if(down)
  1495. {
  1496. std::vector<SComponent*> comps(1,
  1497. new CCustomImgComponent(SComponent::spell,spell->id,0,graphics->spellscr->ourImages[spell->id].bitmap,false)
  1498. );
  1499. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1500. }
  1501. }
  1502. void CMageGuildScreen::Scroll::clickRight (tribool down)
  1503. {
  1504. if(down)
  1505. {
  1506. CInfoPopup *vinya = new CInfoPopup();
  1507. vinya->free = true;
  1508. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1509. (LOCPLINT->playerID,
  1510. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1511. spell->name,30,30);
  1512. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1513. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1514. LOCPLINT->pushInt(vinya);
  1515. }
  1516. }
  1517. void CMageGuildScreen::Scroll::hover(bool on)
  1518. {
  1519. Hoverable::hover(on);
  1520. if(on)
  1521. LOCPLINT->statusbar->print(spell->name);
  1522. else
  1523. LOCPLINT->statusbar->clear();
  1524. }
  1525. void CMageGuildScreen::Scroll::activate()
  1526. {
  1527. ClickableL::activate();
  1528. ClickableR::activate();
  1529. Hoverable::activate();
  1530. }
  1531. void CMageGuildScreen::Scroll::deactivate()
  1532. {
  1533. ClickableL::deactivate();
  1534. ClickableR::deactivate();
  1535. Hoverable::deactivate();
  1536. }
  1537. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1538. {
  1539. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1540. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1541. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1542. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1543. SDL_FreeSurface(bg2);
  1544. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1545. blitAt(bg2,64,50,bmp);
  1546. SDL_FreeSurface(bg2);
  1547. CCreatureAnimation cra(CGI->creh->creatures[creMachineID].animDefName);
  1548. cra.nextFrameMiddle(bmp,170,120,true,0,false);
  1549. char pom[75];
  1550. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID].nameSing.c_str()); //build a new ...
  1551. printAtMiddle(pom,165,28,GEORXX,tytulowy,bmp);
  1552. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,GEOR16,zwykly,bmp); //resource cost
  1553. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1554. printAtMiddle(pom,165,290,GEOR13,zwykly,bmp);
  1555. pos.w = bmp->w;
  1556. pos.h = bmp->h;
  1557. pos.x = screen->w/2 - pos.w/2;
  1558. pos.y = screen->h/2 - pos.h/2;
  1559. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF",SDLK_RETURN);
  1560. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1561. if(possible)
  1562. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1563. else
  1564. buy->bitmapOffset = 2;
  1565. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1566. }
  1567. void CBlacksmithDialog::show( SDL_Surface * to )
  1568. {
  1569. blitAt(bmp,pos,to);
  1570. buy->show(to);
  1571. cancel->show(to);
  1572. }
  1573. void CBlacksmithDialog::activate()
  1574. {
  1575. if(!buy->bitmapOffset)
  1576. buy->activate();
  1577. cancel->activate();
  1578. }
  1579. void CBlacksmithDialog::deactivate()
  1580. {
  1581. if(!buy->bitmapOffset)
  1582. buy->deactivate();
  1583. cancel->deactivate();
  1584. }
  1585. CBlacksmithDialog::~CBlacksmithDialog()
  1586. {
  1587. SDL_FreeSurface(bmp);
  1588. delete cancel;
  1589. delete buy;
  1590. }
  1591. void CBlacksmithDialog::close()
  1592. {
  1593. LOCPLINT->popIntTotally(this);
  1594. }