CCastleInterface.cpp 46 KB

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