CCastleInterface.cpp 45 KB

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