CCastleInterface.cpp 46 KB

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