CCastleInterface.cpp 47 KB

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