CCastleInterface.cpp 58 KB

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