CCastleInterface.cpp 59 KB

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