CCastleInterface.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  1. #include "../stdafx.h"
  2. #include "CCastleInterface.h"
  3. #include "AdventureMapButton.h"
  4. #include "CAdvmapInterface.h"
  5. #include "../CCallback.h"
  6. #include "CGameInfo.h"
  7. #include "CHeroWindow.h"
  8. #include "CMessage.h"
  9. #include "SDL_Extensions.h"
  10. #include "CCreatureAnimation.h"
  11. #include "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 <boost/lexical_cast.hpp>
  24. #include <cmath>
  25. #include <sstream>
  26. #include <boost/format.hpp>
  27. using namespace boost::assign;
  28. using namespace CSDL_Ext;
  29. /*
  30. * CCastleInterface.cpp, part of VCMI engine
  31. *
  32. * Authors: listed in file AUTHORS in main folder
  33. *
  34. * License: GNU General Public License v2.0 or later
  35. * Full text of license available in license.txt file, in main folder
  36. *
  37. */
  38. CBuildingRect::CBuildingRect(Structure *Str)
  39. :moi(false), offset(0), str(Str)
  40. {
  41. def = CDefHandler::giveDef(Str->defName);
  42. max = def->ourImages.size();
  43. if(str->ID == 33 && str->townID == 4) //little 'hack' for estate in necropolis - background color is not always the first color in the palette
  44. {
  45. for(std::vector<Cimage>::iterator i=def->ourImages.begin();i!=def->ourImages.end();i++)
  46. {
  47. SDL_SetColorKey(i->bitmap,SDL_SRCCOLORKEY,*((char*)i->bitmap->pixels));
  48. }
  49. }
  50. pos.x = str->pos.x + LOCPLINT->castleInt->pos.x;
  51. pos.y = str->pos.y + LOCPLINT->castleInt->pos.y;
  52. pos.w = def->ourImages[0].bitmap->w;
  53. pos.h = def->ourImages[0].bitmap->h;
  54. if(Str->ID<0 || (Str->ID>=27 && Str->ID<=29))
  55. {
  56. area = border = NULL;
  57. return;
  58. }
  59. border = BitmapHandler::loadBitmap(str->borderName);
  60. if (border)
  61. {
  62. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  63. }
  64. else
  65. {
  66. tlog2 << "Warning: no border for "<<Str->ID<<std::endl;
  67. }
  68. area = BitmapHandler::loadBitmap(str->areaName); //FIXME look up
  69. if (area)
  70. {
  71. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  72. }
  73. else
  74. {
  75. tlog2 << "Warning: no area for "<<Str->ID<<std::endl;
  76. }
  77. }
  78. CBuildingRect::~CBuildingRect()
  79. {
  80. delete def;
  81. if(border)
  82. SDL_FreeSurface(border);
  83. if(area)
  84. SDL_FreeSurface(area);
  85. }
  86. void CBuildingRect::activate()
  87. {
  88. activateHover();
  89. activateLClick();
  90. activateRClick();
  91. }
  92. void CBuildingRect::deactivate()
  93. {
  94. deactivateHover();
  95. deactivateLClick();
  96. deactivateRClick();
  97. if(moi)
  98. deactivateMouseMove();
  99. moi=false;
  100. }
  101. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  102. {
  103. if(str->pos.z != p2.str->pos.z)
  104. return (str->pos.z) < (p2.str->pos.z);
  105. else
  106. return (str->ID) < (p2.str->ID);
  107. }
  108. void CBuildingRect::hover(bool on)
  109. {
  110. //Hoverable::hover(on);
  111. if(on)
  112. {
  113. if(!moi)
  114. activateMouseMove();
  115. moi = true;
  116. }
  117. else
  118. {
  119. if(moi)
  120. deactivateMouseMove();
  121. moi = false;
  122. if(LOCPLINT->castleInt->hBuild == this)
  123. {
  124. LOCPLINT->castleInt->hBuild = NULL;
  125. GH.statusbar->clear();
  126. //call mouseMoved in other buildings, cursor might have been moved while they were inactive (eg. because of r-click popup)
  127. for(size_t i = 0; i < LOCPLINT->castleInt->buildings.size(); i++)
  128. LOCPLINT->castleInt->buildings[i]->mouseMoved(GH.current->motion);
  129. }
  130. }
  131. }
  132. void CBuildingRect::clickLeft(tribool down, bool previousState)
  133. {
  134. if( area && (LOCPLINT->castleInt->hBuild==this) && !(indeterminate(down)) &&
  135. !CSDL_Ext::isTransparent(area, GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) ) //inside building image
  136. {
  137. if(previousState && !down)
  138. LOCPLINT->castleInt->buildingClicked(str->ID);
  139. //ClickableL::clickLeft(down);
  140. }
  141. }
  142. void CBuildingRect::clickRight(tribool down, bool previousState)
  143. {
  144. if((!area) || (!((bool)down)) || (this!=LOCPLINT->castleInt->hBuild))
  145. return;
  146. if( !CSDL_Ext::isTransparent(area, GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) ) //inside building image
  147. {
  148. CBuilding *bld = CGI->buildh->buildings[str->townID][str->ID];
  149. assert(bld);
  150. CInfoPopup *vinya = new CInfoPopup();
  151. vinya->free = true;
  152. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  153. (LOCPLINT->playerID,
  154. bld->Description(),
  155. LOCPLINT->castleInt->bicons->ourImages[str->ID].bitmap,
  156. bld->Name());
  157. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  158. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  159. GH.pushInt(vinya);
  160. }
  161. }
  162. void CBuildingRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  163. {
  164. if(area && isItIn(&pos,sEvent.x, sEvent.y))
  165. {
  166. if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
  167. {
  168. if(LOCPLINT->castleInt->hBuild == this)
  169. {
  170. LOCPLINT->castleInt->hBuild = NULL;
  171. GH.statusbar->clear();
  172. }
  173. }
  174. else //inside the area of this building
  175. {
  176. if(LOCPLINT->castleInt->hBuild) //a building is hovered
  177. {
  178. if((*LOCPLINT->castleInt->hBuild)<(*this)) //set if we are on top
  179. {
  180. LOCPLINT->castleInt->hBuild = this;
  181. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->Name().length())
  182. GH.statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  183. else
  184. GH.statusbar->print(str->name);
  185. }
  186. }
  187. else //no building hovered
  188. {
  189. LOCPLINT->castleInt->hBuild = this;
  190. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->Name().length())
  191. GH.statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  192. else
  193. GH.statusbar->print(str->name);
  194. }
  195. }
  196. }
  197. //if(border)
  198. // blitAt(border,pos.x,pos.y);
  199. }
  200. void CHeroGSlot::hover (bool on)
  201. {
  202. if(!on)
  203. {
  204. GH.statusbar->clear();
  205. return;
  206. }
  207. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  208. std::string temp;
  209. if(hero)
  210. {
  211. if(highlight)//view NNN
  212. {
  213. temp = CGI->generaltexth->tcommands[4];
  214. boost::algorithm::replace_first(temp,"%s",hero->name);
  215. }
  216. else if(other->hero && other->highlight)//exchange
  217. {
  218. temp = CGI->generaltexth->tcommands[7];
  219. boost::algorithm::replace_first(temp,"%s",hero->name);
  220. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  221. }
  222. else// select NNN (in ZZZ)
  223. {
  224. if(upg)//down - visiting
  225. {
  226. temp = CGI->generaltexth->tcommands[32];
  227. boost::algorithm::replace_first(temp,"%s",hero->name);
  228. }
  229. else //up - garrison
  230. {
  231. temp = CGI->generaltexth->tcommands[12];
  232. boost::algorithm::replace_first(temp,"%s",hero->name);
  233. }
  234. }
  235. }
  236. else //we are empty slot
  237. {
  238. if(other->highlight && other->hero) //move NNNN
  239. {
  240. temp = CGI->generaltexth->tcommands[6];
  241. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  242. }
  243. else //empty
  244. {
  245. temp = CGI->generaltexth->allTexts[507];
  246. }
  247. }
  248. if(temp.size())
  249. GH.statusbar->print(temp);
  250. }
  251. void CHeroGSlot::clickRight(tribool down, bool previousState)
  252. {
  253. }
  254. void CHeroGSlot::clickLeft(tribool down, bool previousState)
  255. {
  256. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  257. if(!down)
  258. {
  259. owner->garr->splitting = false;
  260. owner->garr->highlighted = NULL;
  261. if(hero && highlight)
  262. {
  263. setHighlight(false);
  264. LOCPLINT->openHeroWindow(hero);
  265. }
  266. else if(other->hero && other->highlight)
  267. {
  268. bool allow = true;
  269. if(upg) //moving hero out of town - check if it is allowed
  270. {
  271. if(!hero && LOCPLINT->cb->howManyHeroes(false) >= 8)
  272. {
  273. std::string tmp = CGI->generaltexth->allTexts[18]; //You already have %d adventuring heroes under your command.
  274. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  275. LOCPLINT->showInfoDialog(tmp,std::vector<SComponent*>(), soundBase::sound_todo);
  276. allow = false;
  277. }
  278. else if(!other->hero->stacksCount()) //hero has no creatures - strange, but if we have appropriate error message...
  279. {
  280. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[19],std::vector<SComponent*>(), soundBase::sound_todo); //This hero has no creatures. A hero must have creatures before he can brave the dangers of the countryside.
  281. allow = false;
  282. }
  283. }
  284. setHighlight(false);
  285. other->setHighlight(false);
  286. if(allow)
  287. LOCPLINT->cb->swapGarrisonHero(owner->town);
  288. }
  289. else if(hero)
  290. {
  291. setHighlight(true);
  292. owner->garr->highlighted = NULL;
  293. show(screen2);
  294. }
  295. hover(false);hover(true); //refresh statusbar
  296. }
  297. //if(indeterminate(down) && !isItIn(&other->pos,GH.current->motion.x,GH.current->motion.y))
  298. //{
  299. // other->highlight = highlight = false;
  300. // show(screen2);
  301. //}
  302. }
  303. void CHeroGSlot::activate()
  304. {
  305. activateLClick();
  306. activateRClick();
  307. activateHover();
  308. }
  309. void CHeroGSlot::deactivate()
  310. {
  311. highlight = false;
  312. deactivateLClick();
  313. deactivateRClick();
  314. deactivateHover();
  315. }
  316. void CHeroGSlot::show(SDL_Surface * to)
  317. {
  318. if(hero) //there is hero
  319. blitAt(graphics->portraitLarge[hero->portrait],pos,to);
  320. else if(!upg) //up garrison
  321. blitAt(graphics->flags->ourImages[LOCPLINT->castleInt->town->getOwner()].bitmap,pos,to);
  322. if(highlight)
  323. blitAt(graphics->bigImgs[-1],pos,to);
  324. }
  325. CHeroGSlot::CHeroGSlot(int x, int y, int updown, const CGHeroInstance *h, CCastleInterface * Owner)
  326. {
  327. owner = Owner;
  328. pos.x = x;
  329. pos.y = y;
  330. pos.w = 58;
  331. pos.h = 64;
  332. hero = h;
  333. upg = updown;
  334. highlight = false;
  335. }
  336. CHeroGSlot::~CHeroGSlot()
  337. {
  338. }
  339. void CHeroGSlot::setHighlight( bool on )
  340. {
  341. highlight = on;
  342. if(owner->hslotup.hero && owner->hslotdown.hero) //two heroes in town
  343. {
  344. for(size_t i = 0; i<owner->garr->splitButtons.size(); i++) //splitting enabled when slot higlighted
  345. owner->garr->splitButtons[i]->block(!on);
  346. }
  347. }
  348. static std::string getBgName(int type) //TODO - co z tym zrobi�?
  349. {
  350. switch (type)
  351. {
  352. case 0:
  353. return "TBCSBACK.bmp";
  354. case 1:
  355. return "TBRMBACK.bmp";
  356. case 2:
  357. return "TBTWBACK.bmp";
  358. case 3:
  359. return "TBINBACK.bmp";
  360. case 4:
  361. return "TBNCBACK.bmp";
  362. case 5:
  363. return "TBDNBACK.bmp";
  364. case 6:
  365. return "TBSTBACK.bmp";
  366. case 7:
  367. return "TBFRBACK.bmp";
  368. case 8:
  369. return "TBELBACK.bmp";
  370. default:
  371. #ifndef __GNUC__
  372. throw new std::exception("std::string getBgName(int type): invalid type");
  373. #else
  374. throw new std::exception();
  375. #endif
  376. }
  377. }
  378. class SORTHELP
  379. {
  380. public:
  381. bool operator ()
  382. (const CBuildingRect *a ,
  383. const CBuildingRect *b)
  384. {
  385. return (*a)<(*b);
  386. }
  387. } srthlp ;
  388. CCastleInterface::CCastleInterface(const CGTownInstance * Town, int listPos)
  389. :hslotup(241,387,0,Town->garrisonHero,this),hslotdown(241,483,1,Town->visitingHero,this)
  390. {
  391. showing = false;
  392. bars = CDefHandler::giveDefEss("TPTHBAR.DEF");
  393. status = CDefHandler::giveDefEss("TPTHCHK.DEF");
  394. LOCPLINT->castleInt = this;
  395. hall = NULL;
  396. fort = NULL;
  397. market = NULL;
  398. townInt = BitmapHandler::loadBitmap("TOWNSCRN.bmp");
  399. cityBg = BitmapHandler::loadBitmap(getBgName(Town->subID));
  400. pos.x = screen->w/2 - 400;
  401. pos.y = screen->h/2 - 300;
  402. hslotup.pos.x += pos.x;
  403. hslotup.pos.y += pos.y;
  404. hslotdown.pos.x += pos.x;
  405. hslotdown.pos.y += pos.y;
  406. hBuild = NULL;
  407. count=0;
  408. town = Town;
  409. animval = 0;
  410. winMode = 1;
  411. //garrison
  412. garr = new CGarrisonInt(pos.x+305,pos.y+387,4,Point(0,96),townInt,Point(62,374),town,town->visitingHero);
  413. townlist = new CTownList(3,pos.x+744,pos.y+414,"IAM014.DEF","IAM015.DEF");//744,526);
  414. exit = new AdventureMapButton
  415. (CGI->generaltexth->tcommands[8],"",boost::bind(&CCastleInterface::close,this),pos.x+744,pos.y+544,"TSBTNS.DEF",SDLK_RETURN);
  416. exit->assignedKeys.insert(SDLK_ESCAPE);
  417. split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+744,pos.y+382,"TSBTNS.DEF");
  418. split->callback += boost::bind(&CCastleInterface::splitClicked,this);
  419. garr->splitButtons.push_back(split);
  420. statusbar = new CStatusBar(pos.x+7,pos.y+555,"TSTATBAR.bmp",732);
  421. resdatabar = new CResDataBar("ZRESBAR.bmp",pos.x+3,pos.y+575,32,2,85,85);
  422. townlist->fun = boost::bind(&CCastleInterface::townChange,this);
  423. //townlist->genList();
  424. townlist->selected = vstd::findPos(LOCPLINT->towns,Town);
  425. townlist->from = townlist->selected - listPos;
  426. amax(townlist->from, 0);
  427. amin(townlist->from, LOCPLINT->towns.size() - townlist->SIZE);
  428. graphics->blueToPlayersAdv(townInt,LOCPLINT->playerID);
  429. exit->bitmapOffset = 4;
  430. //growth icons and buildings
  431. recreateBuildings();
  432. recreateIcons();
  433. std::string defname;
  434. switch (town->subID)
  435. {
  436. case 0:
  437. defname = "HALLCSTL.DEF";
  438. musicID = musicBase::castleTown;
  439. break;
  440. case 1:
  441. defname = "HALLRAMP.DEF";
  442. musicID = musicBase::rampartTown;
  443. break;
  444. case 2:
  445. defname = "HALLTOWR.DEF";
  446. musicID = musicBase::towerTown;
  447. break;
  448. case 3:
  449. defname = "HALLINFR.DEF";
  450. musicID = musicBase::infernoTown;
  451. break;
  452. case 4:
  453. defname = "HALLNECR.DEF";
  454. musicID = musicBase::necroTown;
  455. break;
  456. case 5:
  457. defname = "HALLDUNG.DEF";
  458. musicID = musicBase::dungeonTown;
  459. break;
  460. case 6:
  461. defname = "HALLSTRN.DEF";
  462. musicID = musicBase::strongHoldTown;
  463. break;
  464. case 7:
  465. defname = "HALLFORT.DEF";
  466. musicID = musicBase::fortressTown;
  467. break;
  468. case 8:
  469. defname = "HALLELEM.DEF";
  470. musicID = musicBase::elemTown;
  471. break;
  472. default:
  473. throw new std::string("Wrong town subID");
  474. }
  475. bicons = CDefHandler::giveDefEss(defname);
  476. CGI->musich->playMusic(musicID, -1);
  477. }
  478. CCastleInterface::~CCastleInterface()
  479. {
  480. delete bars;
  481. delete status;
  482. SDL_FreeSurface(townInt);
  483. SDL_FreeSurface(cityBg);
  484. delete exit;
  485. //delete split;
  486. delete hall;
  487. delete fort;
  488. delete market;
  489. delete garr;
  490. delete townlist;
  491. delete statusbar;
  492. delete resdatabar;
  493. for(size_t i=0;i<buildings.size();i++)
  494. {
  495. delete buildings[i];
  496. }
  497. delete bicons;
  498. for(size_t i=0;i<creainfo.size();i++)
  499. {
  500. delete creainfo[i];
  501. }
  502. }
  503. void CCastleInterface::close()
  504. {
  505. if(town->visitingHero)
  506. adventureInt->select(town->visitingHero);
  507. else
  508. adventureInt->select(town);
  509. LOCPLINT->castleInt = NULL;
  510. GH.popIntTotally(this);
  511. CGI->musich->stopMusic(5000);
  512. }
  513. void CCastleInterface::splitF()
  514. {
  515. }
  516. void CCastleInterface::buildingClicked(int building)
  517. {
  518. tlog5<<"You've clicked on "<<building<<std::endl;
  519. if(building==19 || building==18)
  520. {
  521. building = town->town->hordeLvl[0] + 30;
  522. }
  523. else if(building==24 || building==25)
  524. {
  525. building = town->town->hordeLvl[1] + 30;
  526. }
  527. const CBuilding *b = CGI->buildh->buildings[town->subID][building];
  528. if(building >= 30)
  529. {
  530. showRecruitmentWindow(building);
  531. }
  532. else
  533. {
  534. switch(building)
  535. {
  536. case 0: case 1: case 2: case 3: case 4: //mage guild
  537. {
  538. const CGHeroInstance *h = NULL; //hero that "enters" mage guild
  539. if(!town->garrisonHero && !town->visitingHero) //no heroes in town
  540. h = NULL;
  541. else if(!town->garrisonHero) //only visiting hero
  542. h = town->visitingHero;
  543. else if(!town->visitingHero || hslotup.highlight) //only garrisoned hero OR both heroes present, garrisoned hero selected
  544. h = town->garrisonHero;
  545. else //both heroes present, use the visiting one
  546. h = town->visitingHero;
  547. if(h && !vstd::contains(h->artifWorn,ui16(17))) //hero doesn't have spellbok
  548. {
  549. if(LOCPLINT->cb->getResourceAmount(6) < 500) //not enough gold to buy spellbook
  550. {
  551. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213]);
  552. }
  553. else
  554. {
  555. CFunctionList<void()> fl = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,h,0);
  556. fl += boost::bind(&CCastleInterface::enterMageGuild,this);
  557. std::vector<SComponent*> vvv(1,new SComponent(SComponent::artifact,0,0));
  558. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214],vvv,fl,0,true);
  559. }
  560. }
  561. else
  562. {
  563. enterMageGuild();
  564. }
  565. break;
  566. }
  567. case 5: //tavern
  568. {
  569. enterTavern();
  570. break;
  571. }
  572. case 6: //shipyard
  573. {
  574. LOCPLINT->showShipyardDialog(town);
  575. break;
  576. }
  577. case 7: case 8: case 9: //fort/citadel/castle
  578. {
  579. CFortScreen *fs = new CFortScreen(this);
  580. GH.pushInt(fs);
  581. break;
  582. }
  583. case 10: case 11: case 12: case 13: //hall
  584. if(town->visitingHero && town->visitingHero->hasArt(2)) //hero has grail
  585. {
  586. if(!vstd::contains(town->forbiddenBuildings, 26))
  587. {
  588. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[597], //Do you wish this to be the permanent home of the Grail?
  589. std::vector<SComponent*>(),
  590. boost::bind(&CCallback::buildBuilding, LOCPLINT->cb, town, 26),
  591. boost::bind(&CCastleInterface::enterHall, this), true);
  592. }
  593. else
  594. {
  595. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[673]);
  596. (dynamic_cast<CInfoWindow*>(GH.topInt()))->buttons[0]->callback += boost::bind(&CCastleInterface::enterHall, this);
  597. }
  598. }
  599. else
  600. enterHall();
  601. break;
  602. case 14: //marketplace
  603. {
  604. CMarketplaceWindow *cmw = new CMarketplaceWindow(town, town->visitingHero);
  605. GH.pushInt(cmw);
  606. break;
  607. }
  608. //case 15: //resource silo - default handling only
  609. case 16: //blacksmith
  610. enterBlacksmith(town->town->warMachine);
  611. break;
  612. case 17:
  613. {
  614. switch(town->subID)
  615. {
  616. /*Rampart*/ case 1://Mystic Pond
  617. enterFountain(building);
  618. break;
  619. /*Tower*/ case 2://Artifact Merchant
  620. /*Dungeon*/ case 5://Artifact Merchant
  621. /*Conflux*/ case 8://Artifact Merchant
  622. GH.pushInt(new CMarketplaceWindow(town, town->visitingHero, RESOURCE_ARTIFACT));
  623. break;
  624. default:
  625. defaultBuildingClicked(building);
  626. break;
  627. }
  628. break;
  629. }
  630. //case 18: //basic horde 1 - can't be selected
  631. //case 19: //upg horde 1 - can't be selected
  632. case 20: //ship at shipyard
  633. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[51]); //Cannot build another boat
  634. break;
  635. case 21: //special 2
  636. {
  637. switch(town->subID)
  638. {
  639. /*Rampart*/ case 1: //Fountain of Fortune
  640. enterFountain(building);
  641. break;
  642. /*Stronghold*/case 6: //Freelancer's Guild
  643. if(town->visitingHero)
  644. GH.pushInt(new CMarketplaceWindow(town, town->visitingHero, CREATURE_RESOURCE));
  645. else
  646. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[273]) % b->Name())); //Only visiting heroes may use the %s.
  647. break;
  648. /*Conflux*/ case 8: //Magic University
  649. tlog4<<"Magic University not handled\n";
  650. break;
  651. default:
  652. defaultBuildingClicked(building);
  653. break;
  654. }
  655. break;
  656. }
  657. case 22: //special 3
  658. {
  659. switch(town->subID)
  660. {
  661. /*Castle*/ case 0: //brotherhood of sword
  662. enterTavern();
  663. break;
  664. /*Inferno*/ case 3: //Castle Gate
  665. tlog4<<"Castle Gate not handled\n";
  666. break;
  667. /*Necropolis*/ case 4: //Skeleton Transformer
  668. tlog4<<"Skeleton Transformer not handled\n";
  669. break;
  670. /*Dungeon*/ case 5: //Portal of Summoning
  671. tlog4<<"Portal of Summoning not handled\n";
  672. break;
  673. /*Stronghold*/ case 6: //Ballista Yard
  674. enterBlacksmith(4);
  675. break;
  676. default:
  677. defaultBuildingClicked(building);
  678. break;
  679. }
  680. break;
  681. }
  682. //case 23: //special 4 - default handling only
  683. //case 24: //basic horde 2 - can't be selected
  684. //case 25: //upg horde 2 - can't be selected
  685. //case 26: //grail - default handling only
  686. default:
  687. defaultBuildingClicked(building);
  688. break;
  689. }
  690. }
  691. }
  692. void CCastleInterface::defaultBuildingClicked(int building)
  693. {
  694. std::vector<SComponent*> comps(1,
  695. new CCustomImgComponent(SComponent::building,town->subID,building,bicons->ourImages[building].bitmap,false));
  696. LOCPLINT->showInfoDialog(
  697. CGI->buildh->buildings[town->subID][building]->Description(),
  698. comps, soundBase::sound_todo);
  699. }
  700. void CCastleInterface::enterFountain(int building)
  701. {
  702. std::vector<SComponent*> comps(1,
  703. new CCustomImgComponent(SComponent::building,town->subID,building,bicons->ourImages[building].bitmap,false));
  704. std::string descr = CGI->buildh->buildings[town->subID][building]->Description();
  705. if ( building == 21)//we need description for mystic pond as well
  706. descr += "\n\n"+CGI->buildh->buildings[town->subID][17]->Description();
  707. if (town->bonusValue.first == 0)//fountain was builded this week
  708. descr += "\n\n"+ CGI->generaltexth->allTexts[677];
  709. else//fountain produced something;
  710. {
  711. descr+= "\n\n"+ CGI->generaltexth->allTexts[678];
  712. boost::algorithm::replace_first(descr,"%s",CGI->generaltexth->restypes[town->bonusValue.first]);
  713. char buf[10];
  714. SDL_itoa(town->bonusValue.second,buf,10);
  715. boost::algorithm::replace_first(descr,"%d",buf);
  716. }
  717. LOCPLINT->showInfoDialog(descr, comps, soundBase::sound_todo);
  718. }
  719. void CCastleInterface::enterBlacksmith(int ArtifactID)
  720. {
  721. const CGHeroInstance *hero = town->visitingHero;
  722. if(!hero)
  723. {
  724. std::string pom = CGI->generaltexth->allTexts[273];
  725. boost::algorithm::replace_first(pom,"%s",CGI->buildh->buildings[town->subID][16]->Name());
  726. LOCPLINT->showInfoDialog(pom,std::vector<SComponent*>(), soundBase::sound_todo);
  727. return;
  728. }
  729. int price = CGI->arth->artifacts[ArtifactID].price;
  730. bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
  731. if(vstd::contains(hero->artifWorn,ui16(ArtifactID+9))) //hero already has machine
  732. possible = false;
  733. GH.pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(ArtifactID,false),ArtifactID,hero->id));
  734. }
  735. void CCastleInterface::enterHall()
  736. {
  737. CHallInterface *h = new CHallInterface(this);
  738. GH.pushInt(h);
  739. }
  740. void CCastleInterface::showAll( SDL_Surface * to/*=NULL*/)
  741. {
  742. blitAt(cityBg,pos,to);
  743. blitAt(townInt,pos.x,pos.y+374,to);
  744. adventureInt->resdatabar.draw(to);
  745. townlist->draw(to);
  746. statusbar->show(to);
  747. resdatabar->draw(to);
  748. garr->show(to);
  749. //draw creatures icons and their growths
  750. for(size_t i=0;i<creainfo.size();i++)
  751. creainfo[i]->show(to);
  752. //print name
  753. CSDL_Ext::printAt(town->name,pos.x+85,pos.y+387,FONT_MEDIUM,zwykly,to);
  754. //blit town icon
  755. int pom = town->subID*2;
  756. if (!town->hasFort())
  757. pom += F_NUMBER*2;
  758. if(town->builded >= MAX_BUILDING_PER_TURN)
  759. pom++;
  760. blitAt(graphics->bigTownPic->ourImages[pom].bitmap,pos.x+15,pos.y+387,to);
  761. hslotup.show(to);
  762. hslotdown.show(to);
  763. market->show(to);
  764. fort->show(to);
  765. hall->show(to);
  766. show(to);
  767. if(screen->w != 800 || screen->h !=600)
  768. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  769. exit->show(to);
  770. //split->show(to);
  771. }
  772. void CCastleInterface::townChange()
  773. {
  774. const CGTownInstance * nt = LOCPLINT->towns[townlist->selected];
  775. int tpos = townlist->selected - townlist->from;
  776. GH.popIntTotally(this);
  777. GH.pushInt(new CCastleInterface(nt, tpos));
  778. }
  779. void CCastleInterface::show(SDL_Surface * to)
  780. {
  781. count++;
  782. if(count==8)
  783. {
  784. count=0;
  785. animval++;
  786. }
  787. blitAt(cityBg,pos,to);
  788. //blit buildings
  789. for(size_t i=0;i<buildings.size();i++)
  790. {
  791. int frame = ((animval)%(buildings[i]->max - buildings[i]->offset)) + buildings[i]->offset;
  792. if(frame)
  793. {
  794. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  795. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  796. }
  797. else
  798. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  799. if(hBuild==buildings[i] && hBuild->border) //if this this higlighted structure and has border we'll blit it
  800. blitAt(hBuild->border,hBuild->pos,to);
  801. }
  802. statusbar->show(to);//refreshing statusbar
  803. }
  804. void CCastleInterface::activate()
  805. {
  806. showing = true;
  807. townlist->activate();
  808. garr->activate();
  809. GH.statusbar = statusbar;
  810. exit->activate();
  811. fort->activate();
  812. hall->activate();
  813. market->activate();
  814. //split->activate();
  815. for(size_t i=0;i<buildings.size();i++) //XXX pls use iterators or at() but not []
  816. {
  817. buildings[i]->activate();
  818. }
  819. for(size_t i=0;i<creainfo.size();i++)
  820. creainfo[i]->activate();
  821. hslotdown.activate();
  822. hslotup.activate();
  823. activateKeys();
  824. }
  825. void CCastleInterface::deactivate()
  826. {
  827. showing = false;
  828. townlist->deactivate();
  829. garr->deactivate();
  830. exit->deactivate();
  831. fort->deactivate();
  832. hall->deactivate();
  833. market->deactivate();
  834. //split->deactivate();
  835. for(size_t i=0;i<buildings.size();i++) //XXX iterators
  836. {
  837. buildings[i]->deactivate();
  838. }
  839. for(size_t i=0;i<creainfo.size();i++)
  840. creainfo[i]->deactivate();
  841. hslotdown.deactivate();
  842. hslotup.deactivate();
  843. deactivateKeys();
  844. }
  845. void CCastleInterface::addBuilding(int bid)
  846. {
  847. //TODO: lepiej by bylo tylko dodawac co trzeba pamietajac o grupach
  848. if ( winMode == 2 )//we will only build buildings, no need to update interface - it will be closed in a moment
  849. return;
  850. deactivate();
  851. recreateBuildings();
  852. recreateIcons();
  853. activate();
  854. }
  855. void CCastleInterface::removeBuilding(int bid)
  856. {
  857. //TODO: lepiej by bylo tylko usuwac co trzeba pamietajac o grupach
  858. recreateBuildings();
  859. recreateIcons();
  860. }
  861. void CCastleInterface::recreateBuildings()
  862. {
  863. for(size_t i=0;i<buildings.size();i++)
  864. {
  865. if(showing)
  866. buildings[i]->deactivate();
  867. delete buildings[i];
  868. }
  869. buildings.clear();
  870. hBuild = NULL;
  871. std::set< std::pair<int,int> > s; //group - id
  872. for (std::set<si32>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  873. {
  874. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  875. {
  876. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  877. {
  878. Structure * st = CGI->townh->structures[town->subID][*i];
  879. if(st->group<0) //no group - just add it
  880. {
  881. buildings.push_back(new CBuildingRect(st));
  882. }
  883. else
  884. {
  885. std::set< std::pair<int,int> >::iterator obecny=s.end();
  886. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  887. {
  888. if(seti->first == st->group)
  889. {
  890. obecny = seti;
  891. break;
  892. }
  893. }
  894. if(obecny != s.end())
  895. {
  896. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  897. {
  898. for(size_t itpb = 0; itpb<buildings.size(); itpb++)
  899. {
  900. if(buildings[itpb]->str->ID == obecny->second)
  901. {
  902. delete buildings[itpb];
  903. buildings.erase(buildings.begin() + itpb);
  904. *(const_cast<int*>(&(obecny->second))) = st->ID;
  905. buildings.push_back(new CBuildingRect(st));
  906. }
  907. }
  908. }
  909. }
  910. else
  911. {
  912. buildings.push_back(new CBuildingRect(st));
  913. s.insert(std::pair<int,int>(st->group,st->ID));
  914. }
  915. }
  916. }
  917. else continue;
  918. }
  919. else
  920. break;
  921. }
  922. //ship in shipyard
  923. bool isThereShip = false;
  924. if(vstd::contains(town->builtBuildings,6))
  925. {
  926. std::vector <const CGObjectInstance *> vobjs = LOCPLINT->cb->getVisitableObjs(town->bestLocation());
  927. if(vobjs.size() && (vobjs.front()->ID == 8 || vobjs.front()->ID == HEROI_TYPE)) //there is visitable obj at shipyard output tile and it's a boat or hero (on boat)
  928. {
  929. Structure * st = CGI->townh->structures[town->subID][20];
  930. buildings.push_back(new CBuildingRect(st));
  931. s.insert(std::pair<int,int>(st->group,st->ID));
  932. isThereShip = true;
  933. }
  934. }
  935. std::sort(buildings.begin(),buildings.end(),srthlp);
  936. //code for Mana Vortex (there are two sets of animation frames - one without mage guild and one with
  937. if((town->subID == 5) && (town->builtBuildings.find(21)!=town->builtBuildings.end()))
  938. {
  939. CBuildingRect *vortex = NULL;
  940. for(size_t i=0;i<buildings.size();i++)
  941. {
  942. if(buildings[i]->str->ID==21)
  943. {
  944. vortex=buildings[i];
  945. break;
  946. }
  947. }
  948. if(town->builtBuildings.find(4)!=town->builtBuildings.end()) //there is mage Guild level 5
  949. {
  950. vortex->offset = 10;
  951. vortex->max = vortex->def->ourImages.size();
  952. }
  953. else
  954. {
  955. vortex->offset = 0;
  956. vortex->max = 10;
  957. }
  958. }
  959. //code for the shipyard in the Castle
  960. else if(town->subID == 0)
  961. {
  962. int shipID = 0;
  963. if(isThereShip)
  964. shipID = 20;
  965. else if(vstd::contains(town->builtBuildings, 6))
  966. shipID = 6;
  967. if(shipID)
  968. {
  969. CBuildingRect *shipyard = NULL;
  970. for(size_t i=0;i<buildings.size();i++)
  971. {
  972. if(buildings[i]->str->ID==shipID)
  973. {
  974. shipyard=buildings[i];
  975. break;
  976. }
  977. }
  978. if(town->builtBuildings.find(8)!=town->builtBuildings.end()) //there is citadel
  979. {
  980. shipyard->offset = 1;
  981. shipyard->max = shipyard->def->ourImages.size();
  982. }
  983. else
  984. {
  985. shipyard->offset = 0;
  986. shipyard->max = 1;
  987. }
  988. }
  989. }
  990. if(showing)
  991. for(size_t i=0;i<buildings.size();i++)
  992. buildings[i]->activate();
  993. }
  994. void CCastleInterface::recreateIcons()
  995. {
  996. delete fort;
  997. delete hall;
  998. delete market;
  999. hall = new CTownInfo(0);
  1000. fort = new CTownInfo(1);
  1001. market = new CTownInfo(2);
  1002. for(size_t i=0;i<creainfo.size();i++)
  1003. {
  1004. if(showing)
  1005. creainfo[i]->deactivate();
  1006. delete creainfo[i];
  1007. }
  1008. creainfo.clear();
  1009. for(size_t i=0;i<CREATURES_PER_TOWN;i++)
  1010. {
  1011. int crid = -1;
  1012. int bid = 30+i;
  1013. if (town->builtBuildings.find(bid)!=town->builtBuildings.end())
  1014. {
  1015. if (town->builtBuildings.find(bid+CREATURES_PER_TOWN)!=town->builtBuildings.end())
  1016. {
  1017. crid = town->town->upgradedCreatures[i];
  1018. bid += CREATURES_PER_TOWN;
  1019. }
  1020. else
  1021. crid = town->town->basicCreatures[i];
  1022. }
  1023. if (crid>=0)
  1024. creainfo.push_back(new CCreaInfo(crid,bid));
  1025. }
  1026. }
  1027. CCastleInterface::CCreaInfo::~CCreaInfo()
  1028. {
  1029. }
  1030. CCastleInterface::CCreaInfo::CCreaInfo(int CRID, int BID)
  1031. {
  1032. used = LCLICK | RCLICK | HOVER;
  1033. CCastleInterface * ci=LOCPLINT->castleInt;
  1034. bid = BID;
  1035. crid = CRID;
  1036. int i = (bid-30)%CREATURES_PER_TOWN;
  1037. pos.x = ci->pos.x+14+(55*(i%4));
  1038. pos.y = (i>3)?(507+ci->pos.y):(459+ci->pos.y);
  1039. pos.w = 48;
  1040. pos.h = 48;
  1041. }
  1042. void CCastleInterface::CCreaInfo::hover(bool on)
  1043. {
  1044. if(on)
  1045. {
  1046. std::string descr=CGI->generaltexth->allTexts[588];
  1047. boost::algorithm::replace_first(descr,"%s",CGI->creh->creatures[crid]->namePl);
  1048. GH.statusbar->print(descr);
  1049. }
  1050. else
  1051. GH.statusbar->clear();
  1052. }
  1053. void CCastleInterface::CCreaInfo::clickLeft(tribool down, bool previousState)
  1054. {
  1055. if(previousState && (!down))
  1056. {
  1057. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1058. }
  1059. };
  1060. int CCastleInterface::CCreaInfo::AddToString(std::string from, std::string & to, int numb)
  1061. {
  1062. if (!numb)
  1063. return 0;//do not add string if 0
  1064. boost::algorithm::replace_first(from,"%+d", "+"+boost::lexical_cast<std::string>(numb));
  1065. to+="\n"+from;
  1066. return numb;
  1067. };
  1068. void CCastleInterface::CCreaInfo::clickRight(tribool down, bool previousState)
  1069. {
  1070. if(down)
  1071. {
  1072. CCastleInterface * ci=LOCPLINT->castleInt;
  1073. std::set<si32> bld =ci->town->builtBuildings;
  1074. int summ=0, cnt=0;
  1075. int level=(bid-30)%CREATURES_PER_TOWN;
  1076. std::string descr=CGI->generaltexth->allTexts[589];//Growth of creature is number
  1077. boost::algorithm::replace_first(descr,"%s",CGI->creh->creatures[crid]->nameSing);
  1078. boost::algorithm::replace_first(descr,"%d", boost::lexical_cast<std::string>(
  1079. ci->town->creatureGrowth(level)));
  1080. descr +="\n"+CGI->generaltexth->allTexts[590];
  1081. summ = CGI->creh->creatures[crid]->growth;
  1082. boost::algorithm::replace_first(descr,"%d", boost::lexical_cast<std::string>(summ));
  1083. if ( bld.find(9)!=bld.end())//castle +100% to basic
  1084. summ+=AddToString(CGI->buildh->buildings[ci->town->subID][9]->Name()+" %+d",descr,summ);
  1085. else if ( bld.find(8)!=bld.end())//else if citadel+50% to basic
  1086. summ+=AddToString(CGI->buildh->buildings[ci->town->subID][8]->Name()+" %+d",descr,summ/2);
  1087. if(ci->town->town->hordeLvl[0]==level)//horde, x to summ
  1088. if((bld.find(18)!=bld.end()) || (bld.find(19)!=bld.end()))
  1089. summ+=AddToString(CGI->buildh->buildings[ci->town->subID][18]->Name()+" %+d",descr,
  1090. CGI->creh->creatures[crid]->hordeGrowth);
  1091. if(ci->town->town->hordeLvl[1]==level)//horde, x to summ
  1092. if((bld.find(24)!=bld.end()) || (bld.find(25)!=bld.end()))
  1093. summ+=AddToString(CGI->buildh->buildings[ci->town->subID][24]->Name()+" %+d",descr,
  1094. CGI->creh->creatures[crid]->hordeGrowth);
  1095. cnt = 0;
  1096. int creaLevel = (bid-30)%CREATURES_PER_TOWN;//dwellings have unupgraded units
  1097. for (std::vector<CGDwelling*>::const_iterator it = CGI->state->players[ci->town->tempOwner].dwellings.begin();
  1098. it !=CGI->state->players[ci->town->tempOwner].dwellings.end(); ++it)
  1099. if (CGI->creh->creatures[ci->town->town->basicCreatures[level]]->idNumber == (*it)->creatures[0].second[0])
  1100. cnt++;//external dwellings count to summ
  1101. summ+=AddToString(CGI->generaltexth->allTexts[591],descr,cnt);
  1102. const CGHeroInstance * ch = ci->town->garrisonHero;
  1103. for (cnt = 0; cnt<2; cnt++) // "loop" to avoid copy-pasting code
  1104. {
  1105. if(ch)
  1106. {
  1107. for(std::list<Bonus>::const_iterator i=ch->bonuses.begin(); i != ch->bonuses.end(); i++)
  1108. if(i->type == Bonus::CREATURE_GROWTH && i->subtype == level)
  1109. if (i->source == Bonus::ARTIFACT)
  1110. summ+=AddToString(CGI->arth->artifacts[i->id].Name()+" %+d",descr,i->val);
  1111. };
  1112. ch = ci->town->visitingHero;
  1113. };
  1114. //TODO player bonuses
  1115. if(bld.find(26)!=bld.end()) //grail - +50% to ALL growth
  1116. summ+=AddToString(CGI->buildh->buildings[ci->town->subID][26]->Name()+" %+d",descr,summ/2);
  1117. CInfoPopup *mess = new CInfoPopup();//creating popup
  1118. mess->free = true;
  1119. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1120. (LOCPLINT->playerID, descr,graphics->bigImgs[crid],"");
  1121. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1122. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1123. GH.pushInt(mess);
  1124. }
  1125. }
  1126. void CCastleInterface::CCreaInfo::show(SDL_Surface * to)
  1127. {
  1128. blitAt(graphics->smallImgs[crid],pos.x+8,pos.y,to);
  1129. std::ostringstream oss;
  1130. oss << '+' << LOCPLINT->castleInt->town->creatureGrowth((bid-30)%CREATURES_PER_TOWN);
  1131. CSDL_Ext::printAtMiddle(oss.str(),pos.x+24,pos.y+37,FONT_TINY,zwykly,to);
  1132. }
  1133. CCastleInterface::CTownInfo::~CTownInfo()
  1134. {
  1135. if (pic)
  1136. delete pic;
  1137. }
  1138. CCastleInterface::CTownInfo::CTownInfo(int BID)
  1139. {
  1140. used = LCLICK | RCLICK | HOVER;
  1141. int pom=0;
  1142. CCastleInterface * ci=LOCPLINT->castleInt;
  1143. switch (BID)
  1144. {
  1145. case 0: //hall
  1146. bid = 10 + ci->town->hallLevel();
  1147. pos.x = ci->pos.x+80; pos.y = ci->pos.y+413; pos.w=40; pos.h=40;
  1148. pic = CDefHandler::giveDef("ITMTL.DEF");
  1149. break;
  1150. case 1: //fort
  1151. bid = 6 + ci->town->fortLevel();
  1152. pos.x = ci->pos.x+122; pos.y = ci->pos.y+413; pos.w=40; pos.h=40;
  1153. pic = CDefHandler::giveDef("ITMCL.DEF");
  1154. break;
  1155. case 2: pos.x = ci->pos.x+164;pos.y = ci->pos.y+409; pos.w=64; pos.h=44;
  1156. pic = NULL;
  1157. bid = 14;
  1158. break;
  1159. }
  1160. }
  1161. void CCastleInterface::CTownInfo::hover(bool on)
  1162. {
  1163. if(on)
  1164. {
  1165. std::string descr;
  1166. if ( bid == 6 ) {} //empty "no fort" icon. no hover message
  1167. else
  1168. if ( bid == 14 ) //marketplace/income icon
  1169. descr = CGI->generaltexth->allTexts[255];
  1170. else
  1171. descr = CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][bid]->Name();
  1172. GH.statusbar->print(descr);
  1173. }
  1174. else
  1175. GH.statusbar->clear();
  1176. }
  1177. void CCastleInterface::CTownInfo::clickLeft(tribool down, bool previousState)
  1178. {
  1179. if(previousState && (!down))
  1180. if (LOCPLINT->castleInt->town->builtBuildings.find(bid)!=LOCPLINT->castleInt->town->builtBuildings.end())
  1181. LOCPLINT->castleInt->buildingClicked(bid);//activate building
  1182. }
  1183. void CCastleInterface::CTownInfo::clickRight(tribool down, bool previousState)
  1184. {
  1185. if(down)
  1186. {
  1187. if (( bid == 6 ) || ( bid == 14) )
  1188. return;
  1189. CInfoPopup *mess = new CInfoPopup();
  1190. mess->free = true;
  1191. CCastleInterface * ci=LOCPLINT->castleInt;
  1192. CBuilding *bld = CGI->buildh->buildings[ci->town->subID][bid];
  1193. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1194. (LOCPLINT->playerID,bld->Description(),
  1195. LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,
  1196. bld->Name());
  1197. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1198. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1199. GH.pushInt(mess);
  1200. }
  1201. }
  1202. void CCastleInterface::CTownInfo::show(SDL_Surface * to)
  1203. {
  1204. if ( bid == 14 )//marketplace/income
  1205. {
  1206. std::ostringstream oss;
  1207. oss << LOCPLINT->castleInt->town->dailyIncome();
  1208. CSDL_Ext::printAtMiddle(oss.str(),pos.x+32,pos.y+32,FONT_SMALL,zwykly,to);
  1209. }
  1210. else if ( bid == 6 )//no fort
  1211. blitAt(pic->ourImages[3].bitmap,pos.x,pos.y,to);
  1212. else if (bid < 10)//fort-castle
  1213. blitAt(pic->ourImages[bid-7].bitmap,pos.x,pos.y,to);
  1214. else//town halls
  1215. blitAt(pic->ourImages[bid-10].bitmap,pos.x,pos.y,to);
  1216. }
  1217. CRecruitmentWindow * CCastleInterface::showRecruitmentWindow( int building )
  1218. {
  1219. if(building>36) //upg dwelling
  1220. building-=7;
  1221. int level = building-30;
  1222. assert(level >= 0 && level <= 6);
  1223. //std::vector<std::pair<int,int > > crs;
  1224. //int amount = town->creatures[level].first;
  1225. //const std::vector<ui32> &cres = town->creatures[level].second;
  1226. //for(size_t i = 0; i < cres.size(); i++)
  1227. // crs.push_back(std::make_pair((int)cres[i],amount));
  1228. //CRecruitmentWindow *rw = new CRecruitmentWindow(crs,boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  1229. CRecruitmentWindow *rw = new CRecruitmentWindow(town, level, town, boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  1230. GH.pushInt(rw);
  1231. return rw;
  1232. }
  1233. void CCastleInterface::enterMageGuild()
  1234. {
  1235. GH.pushInt(new CMageGuildScreen(this));
  1236. }
  1237. void CCastleInterface::enterTavern()
  1238. {
  1239. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(town);
  1240. CTavernWindow *tv = new CTavernWindow(h[0],h[1],"GOSSIP TEST");
  1241. GH.pushInt(tv);
  1242. }
  1243. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  1244. {
  1245. if(key.state != SDL_PRESSED) return;
  1246. switch(key.keysym.sym)
  1247. {
  1248. case SDLK_UP:
  1249. if(townlist->selected)
  1250. {
  1251. townlist->selected--;
  1252. townlist->from--;
  1253. townChange();
  1254. }
  1255. break;
  1256. case SDLK_DOWN:
  1257. if(townlist->selected < LOCPLINT->towns.size() - 1)
  1258. {
  1259. townlist->selected++;
  1260. townlist->from++;
  1261. townChange();
  1262. }
  1263. break;
  1264. case SDLK_SPACE:
  1265. if(town->visitingHero && town->garrisonHero)
  1266. {
  1267. LOCPLINT->cb->swapGarrisonHero(town);
  1268. }
  1269. break;
  1270. default:
  1271. break;
  1272. }
  1273. }
  1274. void CCastleInterface::splitClicked()
  1275. {
  1276. if(town->visitingHero && town->garrisonHero && (hslotdown.highlight || hslotup.highlight))
  1277. {
  1278. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  1279. }
  1280. }
  1281. void CHallInterface::CBuildingBox::hover(bool on)
  1282. {
  1283. //Hoverable::hover(on);
  1284. if(on)
  1285. {
  1286. std::string toPrint;
  1287. if(state==8)
  1288. toPrint = CGI->generaltexth->hcommands[5];
  1289. else if(state==5)//"already builded today" message
  1290. toPrint = CGI->generaltexth->allTexts[223];
  1291. else
  1292. toPrint = CGI->generaltexth->hcommands[state];
  1293. std::vector<std::string> name;
  1294. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->Name());
  1295. GH.statusbar->print(CSDL_Ext::processStr(toPrint,name));
  1296. }
  1297. else
  1298. GH.statusbar->clear();
  1299. }
  1300. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1301. {
  1302. if(previousState && (!down))
  1303. {
  1304. GH.pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0));
  1305. }
  1306. //ClickableL::clickLeft(down);
  1307. }
  1308. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1309. {
  1310. if(down)
  1311. {
  1312. GH.pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1));
  1313. }
  1314. //ClickableR::clickRight(down);
  1315. }
  1316. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  1317. {
  1318. CCastleInterface *ci = LOCPLINT->castleInt;
  1319. if (( (BID == 18) && (vstd::contains(ci->town->builtBuildings,(ci->town->town->hordeLvl[0]+37))))
  1320. || ( (BID == 24) && (vstd::contains(ci->town->builtBuildings,(ci->town->town->hordeLvl[1]+37)))) )
  1321. blitAt(ci->bicons->ourImages[BID+1].bitmap,pos.x,pos.y,to);
  1322. else
  1323. blitAt(ci->bicons->ourImages[BID].bitmap,pos.x,pos.y,to);
  1324. int pom, pom2=-1;
  1325. switch (state)
  1326. {
  1327. case 4:
  1328. pom = 0;
  1329. pom2 = 0;
  1330. break;
  1331. case 7:
  1332. pom = 1;
  1333. break;
  1334. case 6:
  1335. pom2 = 2;
  1336. pom = 2;
  1337. break;
  1338. case 5: case 8:
  1339. pom2 = 1;
  1340. pom = 2;
  1341. break;
  1342. case 0: case 2: case 1: default:
  1343. pom = 3;
  1344. break;
  1345. }
  1346. blitAt(ci->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71,to);
  1347. if(pom2>=0)
  1348. blitAt(ci->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54,to);
  1349. CSDL_Ext::printAtMiddle(CGI->buildh->buildings[ci->town->subID][BID]->Name(),pos.x-1+ci->bars->ourImages[0].bitmap->w/2,pos.y+71+ci->bars->ourImages[0].bitmap->h/2, FONT_SMALL,zwykly,to);
  1350. }
  1351. CHallInterface::CBuildingBox::~CBuildingBox()
  1352. {
  1353. }
  1354. CHallInterface::CBuildingBox::CBuildingBox(int id)
  1355. :BID(id)
  1356. {
  1357. used = LCLICK | RCLICK | HOVER;
  1358. pos.w = 150;
  1359. pos.h = 88;
  1360. }
  1361. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  1362. :BID(id)
  1363. {
  1364. used = LCLICK | RCLICK | HOVER;
  1365. pos.x = x;
  1366. pos.y = y;
  1367. pos.w = 150;
  1368. pos.h = 88;
  1369. }
  1370. CHallInterface::CHallInterface(CCastleInterface * owner)
  1371. {
  1372. resdatabar = new CMinorResDataBar;
  1373. pos = owner->pos;
  1374. resdatabar->pos.x += pos.x;
  1375. resdatabar->pos.y += pos.y;
  1376. LOCPLINT->castleInt->statusbar->clear();
  1377. bg = BitmapHandler::loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  1378. bid = owner->town->hallLevel()+10;
  1379. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1380. exit = new AdventureMapButton
  1381. (CGI->generaltexth->hcommands[8],"",boost::bind(&CHallInterface::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1382. exit->assignedKeys.insert(SDLK_ESCAPE);
  1383. //preparing boxes with buildings//
  1384. boxes.resize(5);
  1385. for(size_t i=0;i<5;i++) //for each row
  1386. {
  1387. std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[owner->town->subID].second;
  1388. for(size_t j=0; j<boxList[i].size();j++) //for each box
  1389. {
  1390. size_t k=0;
  1391. for(;k<boxList[i][j].size();k++)//we are looking for the first not build structure
  1392. {
  1393. int bid = boxList[i][j][k];
  1394. if(!vstd::contains(owner->town->builtBuildings,bid))
  1395. {
  1396. int x = 34 + 194*j,
  1397. y = 37 + 104*i,
  1398. ID = bid;
  1399. if(boxList[i].size() == 2) //only two boxes in this row
  1400. x+=194;
  1401. else if(boxList[i].size() == 3) //only three boxes in this row
  1402. x+=97;
  1403. boxes[i].push_back(new CBuildingBox(bid,pos.x+x,pos.y+y));
  1404. //if this is horde dwelling for upgraded creatures and we already have one for basic creatures
  1405. if((bid == 25 && vstd::contains(owner->town->builtBuildings,24))
  1406. || (bid == 19 && vstd::contains(owner->town->builtBuildings,18))
  1407. )
  1408. {
  1409. boxes[i].back()->state = 4; //already built
  1410. }
  1411. else
  1412. {
  1413. boxes[i].back()->state = LOCPLINT->cb->canBuildStructure(owner->town,ID);
  1414. }
  1415. break;
  1416. }
  1417. }
  1418. if(k == boxList[i][j].size()) //all buildings built - let's take the last one
  1419. {
  1420. int x = 34 + 194*j,
  1421. y = 37 + 104*i;
  1422. if(boxList[i].size() == 2)
  1423. x+=194;
  1424. else if(boxList[i].size() == 3)
  1425. x+=97;
  1426. boxes[i].push_back(new CBuildingBox(boxList[i][j][k-1],pos.x+x,pos.y+y));
  1427. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  1428. }
  1429. }
  1430. }
  1431. }
  1432. CHallInterface::~CHallInterface()
  1433. {
  1434. SDL_FreeSurface(bg);
  1435. for(size_t i=0;i<boxes.size();i++)
  1436. for(size_t j=0;j<boxes[i].size();j++)
  1437. delete boxes[i][j]; //TODO whats wrong with smartpointers?
  1438. delete exit;
  1439. delete resdatabar;
  1440. }
  1441. void CHallInterface::close()
  1442. {
  1443. GH.popInts(LOCPLINT->castleInt->winMode == 2? 2 : 1 );
  1444. }
  1445. void CHallInterface::show(SDL_Surface * to)
  1446. {
  1447. blitAt(bg,pos,to);
  1448. LOCPLINT->castleInt->statusbar->show(to);
  1449. printAtMiddle(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][bid]->Name(),399+pos.x,12+pos.y,FONT_MEDIUM,zwykly,to);
  1450. resdatabar->show(to);
  1451. exit->show(to);
  1452. for(int i=0; i<5; i++)
  1453. {
  1454. for(size_t j=0;j<boxes[i].size(); ++j)
  1455. boxes[i][j]->show(to);
  1456. }
  1457. }
  1458. void CHallInterface::activate()
  1459. {
  1460. for(int i=0;i<5;i++)
  1461. {
  1462. for(size_t j=0; j < boxes[i].size(); ++j)
  1463. {
  1464. boxes[i][j]->activate();
  1465. }
  1466. }
  1467. exit->activate();
  1468. }
  1469. void CHallInterface::deactivate()
  1470. {
  1471. for(int i=0;i<5;i++)
  1472. {
  1473. for(size_t j=0;j<boxes[i].size();++j)
  1474. {
  1475. boxes[i][j]->deactivate();
  1476. }
  1477. }
  1478. exit->deactivate();
  1479. }
  1480. void CHallInterface::CBuildWindow::activate()
  1481. {
  1482. activateRClick();
  1483. if(mode)
  1484. return;
  1485. if(state==7)
  1486. buy->activate();
  1487. cancel->activate();
  1488. }
  1489. void CHallInterface::CBuildWindow::deactivate()
  1490. {
  1491. deactivateRClick();
  1492. if(mode)
  1493. return;
  1494. if(state==7)
  1495. buy->deactivate();
  1496. cancel->deactivate();
  1497. }
  1498. void CHallInterface::CBuildWindow::Buy()
  1499. {
  1500. int building = bid;
  1501. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,building);
  1502. GH.popInts(LOCPLINT->castleInt->winMode == 2? 3 : 2 ); //we - build window and hall screen
  1503. }
  1504. void CHallInterface::CBuildWindow::close()
  1505. {
  1506. GH.popIntTotally(this);
  1507. }
  1508. void CHallInterface::CBuildWindow::clickRight(tribool down, bool previousState)
  1509. {
  1510. if((!down || indeterminate(down)) && mode)
  1511. close();
  1512. }
  1513. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  1514. {
  1515. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  1516. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  1517. SDL_BlitSurface(bitmap,&poms,to,&pom);
  1518. if(!mode)
  1519. {
  1520. buy->show(to);
  1521. cancel->show(to);
  1522. }
  1523. }
  1524. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  1525. {
  1526. std::string ret;
  1527. if(state<7)
  1528. ret = CGI->generaltexth->hcommands[state];
  1529. switch (state)
  1530. {
  1531. case 4: case 5: case 6:
  1532. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->Name());
  1533. break;
  1534. case 7:
  1535. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1536. case 8:
  1537. {
  1538. ret = CGI->generaltexth->allTexts[52];
  1539. std::set<int> reqs= LOCPLINT->cb->getBuildingRequiments(LOCPLINT->castleInt->town, bid);
  1540. bool first=true;
  1541. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1542. {
  1543. if (vstd::contains(LOCPLINT->castleInt->town->builtBuildings, *i))
  1544. continue;//skipping constructed buildings
  1545. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->Name());
  1546. first = false;//TODO - currently can return "Mage guild lvl 1, MG lvl 2..." - extra check needed
  1547. }
  1548. }
  1549. }
  1550. return ret;
  1551. }
  1552. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  1553. :tid(Tid), bid(Bid), state(State), mode(Mode)
  1554. {
  1555. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPUBUILD.bmp");
  1556. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1557. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  1558. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  1559. SDL_FreeSurface(hhlp);
  1560. pos.x = screen->w/2 - bitmap->w/2;
  1561. pos.y = screen->h/2 - bitmap->h/2;
  1562. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  1563. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->Name());
  1564. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->Description(),197,168,FONT_MEDIUM,43,zwykly,bitmap);
  1565. CSDL_Ext::printAtMiddleWB(getTextForState(state),199,248,FONT_SMALL,50,zwykly,bitmap);
  1566. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->generaltexth->hcommands[7],pom),197,30,FONT_BIG,tytulowy,bitmap);
  1567. int resamount=0;
  1568. for(int i=0;i<7;i++)
  1569. {
  1570. if(CGI->buildh->buildings[tid][bid]->resources[i])
  1571. {
  1572. resamount++;
  1573. }
  1574. }
  1575. int ah = (resamount>4) ? 304 : 340;
  1576. int cn=-1, it=0;
  1577. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  1578. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  1579. char buf[15];
  1580. while(++cn<7)
  1581. {
  1582. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  1583. continue;
  1584. SDL_itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  1585. if(it<4)
  1586. {
  1587. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+47,FONT_SMALL,zwykly,bitmap);
  1588. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  1589. }
  1590. else
  1591. {
  1592. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+47,FONT_SMALL,zwykly,bitmap);
  1593. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  1594. }
  1595. if(it==4)
  1596. ah+=75;
  1597. }
  1598. if(!mode)
  1599. {
  1600. buy = new AdventureMapButton
  1601. ("","",boost::bind(&CBuildWindow::Buy,this),pos.x+45,pos.y+446,"IBUY30.DEF",SDLK_RETURN);
  1602. cancel = new AdventureMapButton
  1603. ("","",boost::bind(&CBuildWindow::close,this),pos.x+290,pos.y+445,"ICANCEL.DEF",SDLK_ESCAPE);
  1604. if(state!=7)
  1605. buy->state=2;
  1606. }
  1607. }
  1608. CHallInterface::CBuildWindow::~CBuildWindow()
  1609. {
  1610. SDL_FreeSurface(bitmap);
  1611. if(!mode)
  1612. {
  1613. delete buy;
  1614. delete cancel;
  1615. }
  1616. }
  1617. CFortScreen::~CFortScreen()
  1618. {
  1619. for(size_t i=0;i<crePics.size();i++)
  1620. delete crePics[i];
  1621. for (size_t i=0;i<recAreas.size();i++)
  1622. delete recAreas[i];
  1623. SDL_FreeSurface(bg);
  1624. delete exit;
  1625. delete resdatabar;
  1626. }
  1627. void CFortScreen::show( SDL_Surface * to)
  1628. {
  1629. blitAt(bg,pos,to);
  1630. static unsigned char anim = 1;
  1631. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1632. {
  1633. crePics[i]->blitPic(to,pos.x+positions[i].x+159,pos.y+positions[i].y+4,!(anim%4));
  1634. }
  1635. anim++;
  1636. exit->show(to);
  1637. resdatabar->show(to);
  1638. GH.statusbar->show(to);
  1639. }
  1640. void CFortScreen::activate()
  1641. {
  1642. GH.statusbar = LOCPLINT->castleInt->statusbar;
  1643. exit->activate();
  1644. for (size_t i=0;i<recAreas.size(); ++i)
  1645. {
  1646. recAreas[i]->activate();
  1647. }
  1648. }
  1649. void CFortScreen::deactivate()
  1650. {
  1651. exit->deactivate();
  1652. for (size_t i=0;i<recAreas.size();i++)
  1653. {
  1654. recAreas[i]->deactivate();
  1655. }
  1656. }
  1657. void CFortScreen::close()
  1658. {
  1659. GH.popInts(LOCPLINT->castleInt->winMode == 3? 2 : 1 );
  1660. }
  1661. CFortScreen::CFortScreen( CCastleInterface * owner )
  1662. {
  1663. resdatabar = new CMinorResDataBar;
  1664. pos = owner->pos;
  1665. bg = NULL;
  1666. LOCPLINT->castleInt->statusbar->clear();
  1667. std::string temp = CGI->generaltexth->fcommands[6];
  1668. boost::algorithm::replace_first(temp,"%s",CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name());
  1669. exit = new AdventureMapButton(temp,"",boost::bind(&CFortScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1670. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1671. genRect(126,386,10,155),genRect(126,386,404,155),
  1672. genRect(126,386,10,288),genRect(126,386,404,288),
  1673. genRect(126,386,206,421);
  1674. draw(owner,true);
  1675. resdatabar->pos += pos;
  1676. }
  1677. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1678. {
  1679. if(bg)
  1680. SDL_FreeSurface(bg);
  1681. char buf[20];
  1682. memset(buf,0,20);
  1683. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1684. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1685. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1686. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1687. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1688. SDL_FreeSurface(bg2);
  1689. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name(),400,13,FONT_MEDIUM,zwykly,bg);
  1690. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1691. {
  1692. bool upgraded = owner->town->creatureDwelling(i,true);
  1693. bool present = owner->town->creatureDwelling(i,false);
  1694. CCreature *c = CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1695. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,FONT_SMALL,zwykly,bg); //cr. name
  1696. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1697. printAtMiddle(CGI->buildh->buildings[owner->town->subID][30+i+upgraded*7]->Name(),positions[i].x+79,positions[i].y+100,FONT_SMALL,zwykly,bg); //dwelling name
  1698. if(present) //if creature is present print avail able quantity
  1699. {
  1700. SDL_itoa(owner->town->creatures[i].first,buf,10);
  1701. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,FONT_SMALL,zwykly,bg);
  1702. }
  1703. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1704. //attack
  1705. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,FONT_SMALL,zwykly,bg);
  1706. SDL_itoa(c->Attack(),buf,10);
  1707. printTo(buf,positions[i].x+381,positions[i].y+21,FONT_SMALL,zwykly,bg);
  1708. //defense
  1709. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,FONT_SMALL,zwykly,bg);
  1710. SDL_itoa(c->Defense(),buf,10);
  1711. printTo(buf,positions[i].x+381,positions[i].y+41,FONT_SMALL,zwykly,bg);
  1712. //damage
  1713. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,FONT_SMALL,zwykly,bg);
  1714. SDL_itoa(c->damageMin,buf,10);
  1715. int hlp;
  1716. if(c->damageMin > 0)
  1717. hlp = log10f(c->damageMin)+2;
  1718. else
  1719. hlp = 2;
  1720. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1721. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1722. printTo(buf,positions[i].x+381,positions[i].y+62,FONT_SMALL,zwykly,bg);
  1723. //health
  1724. printAt(CGI->generaltexth->zelp[439].first,positions[i].x+288,positions[i].y+66,FONT_SMALL,zwykly,bg);
  1725. SDL_itoa(c->MaxHealth(),buf,10);
  1726. printTo(buf,positions[i].x+381,positions[i].y+82,FONT_SMALL,zwykly,bg);
  1727. //speed
  1728. printAt(CGI->generaltexth->zelp[441].first,positions[i].x+288,positions[i].y+87,FONT_SMALL,zwykly,bg);
  1729. SDL_itoa(c->valOfBonuses(Bonus::STACKS_SPEED), buf,10);
  1730. printTo(buf,positions[i].x+381,positions[i].y+103,FONT_SMALL,zwykly,bg);
  1731. if(present)//growth
  1732. {
  1733. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,FONT_SMALL,zwykly,bg);
  1734. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1735. printTo(buf,positions[i].x+381,positions[i].y+123,FONT_SMALL,zwykly,bg);
  1736. }
  1737. if(first)
  1738. {
  1739. crePics.push_back(new CCreaturePic(c,false));
  1740. if(present)
  1741. {
  1742. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1743. recAreas.back()->pos = positions[i] + pos;
  1744. }
  1745. }
  1746. }
  1747. SDL_FreeSurface(icons);
  1748. }
  1749. void CFortScreen::RecArea::clickLeft(tribool down, bool previousState)
  1750. {
  1751. if(!down && previousState)
  1752. {
  1753. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1754. }
  1755. //ClickableL::clickLeft(down);
  1756. }
  1757. void CFortScreen::RecArea::clickRight(tribool down, bool previousState)
  1758. {
  1759. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1760. }
  1761. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1762. {
  1763. resdatabar = new CMinorResDataBar;
  1764. pos = owner->pos;
  1765. resdatabar->pos.x += pos.x;
  1766. resdatabar->pos.y += pos.y;
  1767. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1768. LOCPLINT->castleInt->statusbar->clear();
  1769. exit = new AdventureMapButton(CGI->generaltexth->allTexts[593],"",boost::bind(&CMageGuildScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1770. exit->assignedKeys.insert(SDLK_ESCAPE);
  1771. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1772. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1773. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1774. positions.resize(5);
  1775. 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);
  1776. 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);
  1777. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1778. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1779. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1780. blitAt(view,332,76,bg);
  1781. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1782. {
  1783. size_t sp = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1784. for(size_t j=0; j<sp; j++)
  1785. {
  1786. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1787. {
  1788. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1789. spells[spells.size()-1].pos = positions[i][j];
  1790. blitAt(graphics->spellscr->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1791. }
  1792. else
  1793. {
  1794. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1795. }
  1796. }
  1797. }
  1798. SDL_FreeSurface(view);
  1799. for(size_t i=0;i<spells.size();i++)
  1800. {
  1801. spells[i].pos.x += pos.x;
  1802. spells[i].pos.y += pos.y;
  1803. }
  1804. delete scrolls2;
  1805. }
  1806. CMageGuildScreen::~CMageGuildScreen()
  1807. {
  1808. delete exit;
  1809. SDL_FreeSurface(bg);
  1810. delete resdatabar;
  1811. }
  1812. void CMageGuildScreen::close()
  1813. {
  1814. GH.popIntTotally(this);
  1815. }
  1816. void CMageGuildScreen::show(SDL_Surface * to)
  1817. {
  1818. blitAt(bg,pos,to);
  1819. resdatabar->show(to);
  1820. GH.statusbar->show(to);
  1821. exit->show(to);
  1822. }
  1823. void CMageGuildScreen::activate()
  1824. {
  1825. exit->activate();
  1826. for(size_t i=0;i<spells.size();i++)
  1827. {
  1828. spells[i].activate();
  1829. }
  1830. }
  1831. void CMageGuildScreen::deactivate()
  1832. {
  1833. exit->deactivate();
  1834. for(size_t i=0;i<spells.size();i++)
  1835. {
  1836. spells[i].deactivate();
  1837. }
  1838. }
  1839. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1840. {
  1841. if(down)
  1842. {
  1843. std::vector<SComponent*> comps(1,
  1844. new CCustomImgComponent(SComponent::spell,spell->id,0,graphics->spellscr->ourImages[spell->id].bitmap,false)
  1845. );
  1846. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1847. }
  1848. }
  1849. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1850. {
  1851. if(down)
  1852. {
  1853. CInfoPopup *vinya = new CInfoPopup();
  1854. vinya->free = true;
  1855. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1856. (LOCPLINT->playerID,
  1857. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1858. spell->name,30,30);
  1859. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1860. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1861. GH.pushInt(vinya);
  1862. }
  1863. }
  1864. void CMageGuildScreen::Scroll::hover(bool on)
  1865. {
  1866. //Hoverable::hover(on);
  1867. if(on)
  1868. GH.statusbar->print(spell->name);
  1869. else
  1870. GH.statusbar->clear();
  1871. }
  1872. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1873. {
  1874. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1875. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1876. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1877. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1878. SDL_FreeSurface(bg2);
  1879. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1880. blitAt(bg2,64,50,bmp);
  1881. SDL_FreeSurface(bg2);
  1882. CCreatureAnimation cra(CGI->creh->creatures[creMachineID]->animDefName);
  1883. cra.nextFrameMiddle(bmp,170,120,true,0,false);
  1884. char pom[75];
  1885. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID]->nameSing.c_str()); //build a new ...
  1886. printAtMiddle(pom,165,28,FONT_MEDIUM,tytulowy,bmp);
  1887. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,FONT_MEDIUM,zwykly,bmp); //resource cost
  1888. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1889. printAtMiddle(pom,165,290,FONT_MEDIUM,zwykly,bmp);
  1890. pos.w = bmp->w;
  1891. pos.h = bmp->h;
  1892. pos.x = screen->w/2 - pos.w/2;
  1893. pos.y = screen->h/2 - pos.h/2;
  1894. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF",SDLK_RETURN);
  1895. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1896. if(possible)
  1897. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1898. else
  1899. buy->bitmapOffset = 2;
  1900. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1901. }
  1902. void CBlacksmithDialog::show( SDL_Surface * to )
  1903. {
  1904. blitAt(bmp,pos,to);
  1905. buy->show(to);
  1906. cancel->show(to);
  1907. }
  1908. void CBlacksmithDialog::activate()
  1909. {
  1910. if(!buy->bitmapOffset)
  1911. buy->activate();
  1912. cancel->activate();
  1913. }
  1914. void CBlacksmithDialog::deactivate()
  1915. {
  1916. if(!buy->bitmapOffset)
  1917. buy->deactivate();
  1918. cancel->deactivate();
  1919. }
  1920. CBlacksmithDialog::~CBlacksmithDialog()
  1921. {
  1922. SDL_FreeSurface(bmp);
  1923. delete cancel;
  1924. delete buy;
  1925. }
  1926. void CBlacksmithDialog::close()
  1927. {
  1928. GH.popIntTotally(this);
  1929. }