CCastleInterface.cpp 58 KB

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