CCastleInterface.cpp 61 KB

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