2
0

CCastleInterface.cpp 62 KB

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