CCastleInterface.cpp 45 KB

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