CCastleInterface.cpp 59 KB

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