CCastleInterface.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. LOCPLINT->adventureInt->activate();
  434. delete this;
  435. }
  436. void CCastleInterface::splitF()
  437. {
  438. }
  439. void CCastleInterface::buildingClicked(int building)
  440. {
  441. std::cout<<"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. CFortScreen *fs = new CFortScreen(this);
  487. deactivate();
  488. fs->activate();
  489. fs->show();
  490. break;
  491. }
  492. case 10: case 11: case 12: case 13:
  493. enterHall();
  494. break;
  495. case 16:
  496. {
  497. const CGHeroInstance *hero = town->visitingHero;
  498. if(!hero)
  499. {
  500. std::string pom = CGI->generaltexth->allTexts[273];
  501. boost::algorithm::replace_first(pom,"%s",CGI->buildh->buildings[town->subID][16]->name);
  502. LOCPLINT->showInfoDialog(pom,std::vector<SComponent*>());
  503. return;
  504. }
  505. int aid = town->town->warMachine;
  506. int price = CGI->arth->artifacts[aid].price;
  507. bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
  508. if(vstd::contains(hero->artifWorn,ui16(aid+9))) //hero already has machine
  509. possible = false;
  510. deactivate();
  511. (new CBlacksmithDialog(possible,142+aid,aid,hero->id))->activate();
  512. break;
  513. }
  514. default:
  515. std::cout<<"This building isn't handled...\n";
  516. }
  517. }
  518. }
  519. void CCastleInterface::enterHall()
  520. {
  521. deactivate();
  522. CHallInterface *h = new CHallInterface(this);
  523. subInt = h;
  524. h->activate();
  525. h->show();
  526. }
  527. void CCastleInterface::showAll( SDL_Surface * to/*=NULL*/, bool forceTotalRedraw /*= false*/)
  528. {
  529. if (!to)
  530. to=screen;
  531. blitAt(cityBg,0,0,to);
  532. blitAt(townInt,0,374,to);
  533. LOCPLINT->adventureInt->resdatabar.draw();
  534. townlist->draw();
  535. statusbar->show();
  536. garr->show();
  537. int pom;
  538. //draw fort icon
  539. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  540. pom = 2;
  541. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  542. pom = 1;
  543. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  544. pom = 0;
  545. else pom = 3;
  546. blitAt(fort->ourImages[pom].bitmap,122,413,to);
  547. //draw ((village/town/city) hall)/capitol icon
  548. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  549. pom = 3;
  550. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  551. pom = 2;
  552. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  553. pom = 1;
  554. else pom = 0;
  555. blitAt(hall->ourImages[pom].bitmap,80,413,to);
  556. //draw creatures icons and their growths
  557. for(int i=0;i<CREATURES_PER_TOWN;i++)
  558. {
  559. int cid = -1;
  560. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  561. {
  562. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  563. cid = town->town->upgradedCreatures[i];
  564. else
  565. cid = town->town->basicCreatures[i];
  566. }
  567. if (cid>=0)
  568. {
  569. int pomx, pomy;
  570. pomx = 22 + (55*((i>3)?(i-4):i));
  571. pomy = (i>3)?(507):(459);
  572. blitAt(graphics->smallImgs[cid],pomx,pomy,to);
  573. std::ostringstream oss;
  574. oss << '+' << town->creatureGrowth(i);
  575. CSDL_Ext::printAtMiddle(oss.str(),pomx+16,pomy+37,GEOR13,zwykly,to);
  576. }
  577. }
  578. //print name and income
  579. CSDL_Ext::printAt(town->name,85,389,GEOR13,zwykly,to);
  580. char temp[10];
  581. SDL_itoa(town->dailyIncome(),temp,10);
  582. CSDL_Ext::printAtMiddle(temp,195,442,GEOR13,zwykly,to);
  583. //blit town icon
  584. pom = town->subID*2;
  585. if (!town->hasFort())
  586. pom += F_NUMBER*2;
  587. if(town->builded >= MAX_BUILDING_PER_TURN)
  588. pom++;
  589. blitAt(graphics->bigTownPic->ourImages[pom].bitmap,15,387,to);
  590. hslotup.show();
  591. hslotdown.show();
  592. pom=false;
  593. if(forceTotalRedraw && !showing)
  594. pom = showing = true;
  595. show();
  596. if(pom)
  597. showing = false;
  598. }
  599. void CCastleInterface::townChange()
  600. {
  601. const CGTownInstance * nt = townlist->items[townlist->selected];
  602. deactivate();
  603. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  604. delete this;
  605. LOCPLINT->castleInt = new CCastleInterface(nt,true);
  606. }
  607. void CCastleInterface::show(SDL_Surface * to)
  608. {
  609. if(!showing)
  610. return;
  611. if (!to)
  612. to=screen;
  613. count++;
  614. if(count==8)
  615. {
  616. count=0;
  617. animval++;
  618. }
  619. blitAt(cityBg,0,0,to);
  620. //blit buildings
  621. for(int i=0;i<buildings.size();i++)
  622. {
  623. int frame = ((animval)%(buildings[i]->max - buildings[i]->offset)) + buildings[i]->offset;
  624. if(frame)
  625. {
  626. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  627. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  628. }
  629. else
  630. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  631. if(hBuild==buildings[i] && hBuild->border) //if this this higlighted structure and has border we'll blit it
  632. blitAt(hBuild->border,hBuild->pos,to);
  633. }
  634. }
  635. void CCastleInterface::activate()
  636. {
  637. if(subInt)
  638. {
  639. subInt->activate();
  640. return;
  641. }
  642. else
  643. subInt = NULL;
  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. for(int i=0;i<crePics.size();i++)
  1198. delete crePics[i];
  1199. for (int i=0;i<recAreas.size();i++)
  1200. delete recAreas[i];
  1201. SDL_FreeSurface(bg);
  1202. delete exit;
  1203. }
  1204. void CFortScreen::show( SDL_Surface * to)
  1205. {
  1206. blitAt(bg,0,0);
  1207. static unsigned char anim = 1;
  1208. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1209. {
  1210. crePics[i]->blitPic(screen,positions[i].x+159,positions[i].y+4,!(anim%4));
  1211. }
  1212. anim++;
  1213. exit->show();
  1214. resdatabar.show();
  1215. LOCPLINT->statusbar->show();
  1216. }
  1217. void CFortScreen::activate()
  1218. {
  1219. LOCPLINT->curint = this;
  1220. exit->activate();
  1221. for (int i=0;i<recAreas.size();i++)
  1222. {
  1223. recAreas[i]->activate();
  1224. }
  1225. LOCPLINT->objsToBlit -= LOCPLINT->castleInt;
  1226. LOCPLINT->objsToBlit += this;
  1227. }
  1228. void CFortScreen::deactivate()
  1229. {
  1230. exit->deactivate();
  1231. for (int i=0;i<recAreas.size();i++)
  1232. {
  1233. recAreas[i]->deactivate();
  1234. }
  1235. LOCPLINT->objsToBlit -= this;
  1236. LOCPLINT->objsToBlit += LOCPLINT->castleInt;
  1237. }
  1238. void CFortScreen::close()
  1239. {
  1240. deactivate();
  1241. delete this;
  1242. LOCPLINT->castleInt->activate();
  1243. }
  1244. CFortScreen::CFortScreen( CCastleInterface * owner )
  1245. {
  1246. bg = NULL;
  1247. exit = new AdventureMapButton(CGI->townh->tcommands[8],"",boost::bind(&CFortScreen::close,this),748,556,"TPMAGE1.DEF",false,NULL,false);
  1248. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1249. genRect(126,386,10,155),genRect(126,386,404,155),
  1250. genRect(126,386,10,288),genRect(126,386,404,288),
  1251. genRect(126,386,206,421);//genRect(126,386,10,421),genRect(126,386,404,421);
  1252. draw(owner,true);
  1253. }
  1254. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1255. {
  1256. if(bg)
  1257. SDL_FreeSurface(bg);
  1258. char buf[20];
  1259. memset(buf,0,20);
  1260. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1261. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1262. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1263. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1264. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1265. SDL_FreeSurface(bg2);
  1266. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->name,400,13,GEORXX,zwykly,bg);
  1267. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1268. {
  1269. bool upgraded = owner->town->creatureDwelling(i,true);
  1270. bool present = owner->town->creatureDwelling(i,false);
  1271. CCreature *c = &CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1272. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,GEOR13,zwykly,bg); //cr. name
  1273. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1274. 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
  1275. if(present) //if creature is present print avail able quantity
  1276. {
  1277. SDL_itoa(owner->town->strInfo.creatures.find(i)->second,buf,10);
  1278. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,GEOR13,zwykly,bg);
  1279. }
  1280. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1281. //atttack
  1282. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,GEOR13,zwykly,bg);
  1283. SDL_itoa(c->attack,buf,10);
  1284. printToWR(buf,positions[i].x+381,positions[i].y+18,GEOR13,zwykly,bg);
  1285. //defense
  1286. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,GEOR13,zwykly,bg);
  1287. SDL_itoa(c->defence,buf,10);
  1288. printToWR(buf,positions[i].x+381,positions[i].y+38,GEOR13,zwykly,bg);
  1289. //damage
  1290. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,GEOR13,zwykly,bg);
  1291. SDL_itoa(c->damageMin,buf,10);
  1292. int hlp=log10f(c->damageMin)+2;
  1293. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1294. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1295. printToWR(buf,positions[i].x+381,positions[i].y+59,GEOR13,zwykly,bg);
  1296. //health
  1297. printAt(CGI->preth->zelp[439].first,positions[i].x+288,positions[i].y+66,GEOR13,zwykly,bg);
  1298. SDL_itoa(c->hitPoints,buf,10);
  1299. printToWR(buf,positions[i].x+381,positions[i].y+79,GEOR13,zwykly,bg);
  1300. //speed
  1301. printAt(CGI->preth->zelp[441].first,positions[i].x+288,positions[i].y+87,GEOR13,zwykly,bg);
  1302. SDL_itoa(c->speed,buf,10);
  1303. printToWR(buf,positions[i].x+381,positions[i].y+100,GEOR13,zwykly,bg);
  1304. if(present)//growth
  1305. {
  1306. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,GEOR13,zwykly,bg);
  1307. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1308. printToWR(buf,positions[i].x+381,positions[i].y+120,GEOR13,zwykly,bg);
  1309. }
  1310. if(first)
  1311. {
  1312. crePics.push_back(new CCreaturePic(c,false));
  1313. if(present)
  1314. {
  1315. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1316. recAreas[recAreas.size()-1]->pos = positions[i];
  1317. }
  1318. }
  1319. }
  1320. SDL_FreeSurface(icons);
  1321. }
  1322. void CFortScreen::RecArea::clickLeft (tribool down)
  1323. {
  1324. if(!down && pressedL)
  1325. {
  1326. LOCPLINT->curint->deactivate();
  1327. CRecrutationWindow *rw = LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1328. rw->buy->callback += boost::bind(&CFortScreen::draw, static_cast<CFortScreen*>(LOCPLINT->curint), LOCPLINT->castleInt, false);
  1329. }
  1330. ClickableL::clickLeft(down);
  1331. }
  1332. void CFortScreen::RecArea::activate()
  1333. {
  1334. ClickableL::activate();
  1335. }
  1336. void CFortScreen::RecArea::deactivate()
  1337. {
  1338. ClickableL::deactivate();
  1339. }
  1340. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1341. {
  1342. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1343. exit = new AdventureMapButton(CGI->townh->tcommands[8],"",boost::bind(&CMageGuildScreen::close,this),748,556,"TPMAGE1.DEF",false,NULL,false);
  1344. scrolls = CDefHandler::giveDefEss("SPELLSCR.DEF");
  1345. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1346. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1347. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1348. positions.resize(5);
  1349. 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);
  1350. 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);
  1351. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1352. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1353. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1354. blitAt(view,332,76,bg);
  1355. for(int i=0; i<owner->town->town->mageLevel; i++)
  1356. {
  1357. int sp = 5 - i; //how many spells are available at this level
  1358. if(owner->town->subID==2 && vstd::contains(owner->town->builtBuildings,22)) sp++; //magic library in tower
  1359. for(int j=0; j<sp; j++)
  1360. {
  1361. if(i < owner->town->mageGuildLevel())
  1362. {
  1363. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1364. spells[spells.size()-1].pos = positions[i][j];
  1365. blitAt(scrolls->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1366. }
  1367. else
  1368. {
  1369. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1370. }
  1371. }
  1372. }
  1373. SDL_FreeSurface(view);
  1374. delete scrolls2;
  1375. }
  1376. CMageGuildScreen::~CMageGuildScreen()
  1377. {
  1378. delete exit;
  1379. delete scrolls;
  1380. SDL_FreeSurface(bg);
  1381. }
  1382. void CMageGuildScreen::close()
  1383. {
  1384. deactivate();
  1385. delete this;
  1386. LOCPLINT->castleInt->subInt = NULL;
  1387. LOCPLINT->castleInt->activate();
  1388. }
  1389. void CMageGuildScreen::show(SDL_Surface * to)
  1390. {
  1391. blitAt(bg,0,0);
  1392. resdatabar.show();
  1393. LOCPLINT->statusbar->show();
  1394. exit->show();
  1395. }
  1396. void CMageGuildScreen::activate()
  1397. {
  1398. LOCPLINT->objsToBlit += this;
  1399. LOCPLINT->castleInt->subInt = this;
  1400. exit->activate();
  1401. for(int i=0;i<spells.size();i++)
  1402. spells[i].activate();
  1403. }
  1404. void CMageGuildScreen::deactivate()
  1405. {
  1406. LOCPLINT->objsToBlit -= this;
  1407. exit->deactivate();
  1408. for(int i=0;i<spells.size();i++)
  1409. spells[i].deactivate();
  1410. }
  1411. void CMageGuildScreen::Scroll::clickLeft (tribool down)
  1412. {
  1413. if(down)
  1414. {
  1415. std::vector<SComponent*> comps(1,
  1416. new CCustomImgComponent(SComponent::spell,spell->id,0,
  1417. static_cast<CMageGuildScreen*>(LOCPLINT->castleInt->subInt)->scrolls->ourImages[spell->id].bitmap,false)
  1418. );
  1419. LOCPLINT->showInfoDialog(spell->descriptions[0],comps);
  1420. }
  1421. }
  1422. void CMageGuildScreen::Scroll::clickRight (tribool down)
  1423. {
  1424. if(down)
  1425. {
  1426. CInfoPopup *vinya = new CInfoPopup();
  1427. vinya->free = true;
  1428. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1429. (LOCPLINT->playerID,
  1430. spell->descriptions[0],
  1431. static_cast<CMageGuildScreen*>(LOCPLINT->castleInt->subInt)->scrolls->ourImages[spell->id].bitmap,
  1432. spell->name,30,30);
  1433. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1434. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1435. vinya->activate();
  1436. }
  1437. }
  1438. void CMageGuildScreen::Scroll::hover(bool on)
  1439. {
  1440. Hoverable::hover(on);
  1441. if(on)
  1442. LOCPLINT->statusbar->print(spell->name);
  1443. else
  1444. LOCPLINT->statusbar->clear();
  1445. }
  1446. void CMageGuildScreen::Scroll::activate()
  1447. {
  1448. ClickableL::activate();
  1449. ClickableR::activate();
  1450. Hoverable::activate();
  1451. }
  1452. void CMageGuildScreen::Scroll::deactivate()
  1453. {
  1454. ClickableL::deactivate();
  1455. ClickableR::deactivate();
  1456. Hoverable::deactivate();
  1457. }
  1458. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1459. {
  1460. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1461. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1462. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1463. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1464. SDL_FreeSurface(bg2);
  1465. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1466. blitAt(bg2,64,50,bmp);
  1467. SDL_FreeSurface(bg2);
  1468. CCreatureAnimation cra(CGI->creh->creatures[creMachineID].animDefName);
  1469. cra.nextFrameMiddle(bmp,165,130,true,false);
  1470. char pom[75];
  1471. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID].nameSing.c_str()); //build a new ...
  1472. printAtMiddle(pom,165,28,GEORXX,tytulowy,bmp);
  1473. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,GEOR16,zwykly,bmp); //resource cost
  1474. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1475. printAtMiddle(pom,165,290,GEOR13,zwykly,bmp);
  1476. pos.w = bmp->w;
  1477. pos.h = bmp->h;
  1478. pos.x = screen->w/2 - pos.w/2;
  1479. pos.y = screen->h/2 - pos.h/2;
  1480. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF");
  1481. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF");
  1482. if(possible)
  1483. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1484. else
  1485. buy->bitmapOffset = 2;
  1486. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1487. }
  1488. void CBlacksmithDialog::show( SDL_Surface * to/*=NULL*/ )
  1489. {
  1490. blitAt(bmp,pos);
  1491. buy->show();
  1492. cancel->show();
  1493. }
  1494. void CBlacksmithDialog::activate()
  1495. {
  1496. LOCPLINT->objsToBlit += this;
  1497. if(!buy->bitmapOffset)
  1498. buy->activate();
  1499. cancel->activate();
  1500. }
  1501. void CBlacksmithDialog::deactivate()
  1502. {
  1503. LOCPLINT->objsToBlit -= this;
  1504. if(!buy->bitmapOffset)
  1505. buy->deactivate();
  1506. cancel->deactivate();
  1507. }
  1508. CBlacksmithDialog::~CBlacksmithDialog()
  1509. {
  1510. SDL_FreeSurface(bmp);
  1511. delete cancel;
  1512. delete buy;
  1513. }
  1514. void CBlacksmithDialog::close()
  1515. {
  1516. deactivate();
  1517. delete this;
  1518. LOCPLINT->curint->activate();
  1519. }