CCastleInterface.cpp 45 KB

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