CCastleInterface.cpp 58 KB

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