CCastleInterface.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  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. //TODO player bonuses
  1093. if(bld.find(26)!=bld.end()) //grail - +50% to ALL growth
  1094. summ+=AddToString(CGI->buildh->buildings[ci->town->subID][26]->Name()+" %+d",descr,summ/2);
  1095. CInfoPopup *mess = new CInfoPopup();//creating popup
  1096. mess->free = true;
  1097. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1098. (LOCPLINT->playerID, descr,graphics->bigImgs[crid],"");
  1099. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1100. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1101. GH.pushInt(mess);
  1102. }
  1103. }
  1104. void CCastleInterface::CCreaInfo::show(SDL_Surface * to)
  1105. {
  1106. blitAt(graphics->smallImgs[crid],pos.x+8,pos.y,to);
  1107. std::ostringstream oss;
  1108. oss << '+' << LOCPLINT->castleInt->town->creatureGrowth((bid-30)%CREATURES_PER_TOWN);
  1109. CSDL_Ext::printAtMiddle(oss.str(),pos.x+24,pos.y+37,FONT_TINY,zwykly,to);
  1110. }
  1111. CCastleInterface::CTownInfo::~CTownInfo()
  1112. {
  1113. if (pic)
  1114. delete pic;
  1115. }
  1116. CCastleInterface::CTownInfo::CTownInfo(int BID)
  1117. {
  1118. used = LCLICK | RCLICK | HOVER;
  1119. int pom=0;
  1120. CCastleInterface * ci=LOCPLINT->castleInt;
  1121. switch (BID)
  1122. {
  1123. case 0: //hall
  1124. bid = 10 + ci->town->hallLevel();
  1125. pos.x = ci->pos.x+80; pos.y = ci->pos.y+413; pos.w=40; pos.h=40;
  1126. pic = CDefHandler::giveDef("ITMTL.DEF");
  1127. break;
  1128. case 1: //fort
  1129. bid = 6 + ci->town->fortLevel();
  1130. pos.x = ci->pos.x+122; pos.y = ci->pos.y+413; pos.w=40; pos.h=40;
  1131. pic = CDefHandler::giveDef("ITMCL.DEF");
  1132. break;
  1133. case 2: pos.x = ci->pos.x+164;pos.y = ci->pos.y+409; pos.w=64; pos.h=44;
  1134. pic = NULL;
  1135. bid = 14;
  1136. break;
  1137. }
  1138. }
  1139. void CCastleInterface::CTownInfo::hover(bool on)
  1140. {
  1141. if(on)
  1142. {
  1143. std::string descr;
  1144. if ( bid == 6 ) {} //empty "no fort" icon. no hover message
  1145. else
  1146. if ( bid == 14 ) //marketplace/income icon
  1147. descr = CGI->generaltexth->allTexts[255];
  1148. else
  1149. descr = CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][bid]->Name();
  1150. LOCPLINT->statusbar->print(descr);
  1151. }
  1152. else
  1153. LOCPLINT->statusbar->clear();
  1154. }
  1155. void CCastleInterface::CTownInfo::clickLeft(tribool down, bool previousState)
  1156. {
  1157. if(previousState && (!down))
  1158. if (LOCPLINT->castleInt->town->builtBuildings.find(bid)!=LOCPLINT->castleInt->town->builtBuildings.end())
  1159. LOCPLINT->castleInt->buildingClicked(bid);//activate building
  1160. }
  1161. void CCastleInterface::CTownInfo::clickRight(tribool down, bool previousState)
  1162. {
  1163. if(down)
  1164. {
  1165. if (( bid == 6 ) || ( bid == 14) )
  1166. return;
  1167. CInfoPopup *mess = new CInfoPopup();
  1168. mess->free = true;
  1169. CCastleInterface * ci=LOCPLINT->castleInt;
  1170. CBuilding *bld = CGI->buildh->buildings[ci->town->subID][bid];
  1171. mess->bitmap = CMessage::drawBoxTextBitmapSub
  1172. (LOCPLINT->playerID,bld->Description(),
  1173. LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,
  1174. bld->Name());
  1175. mess->pos.x = screen->w/2 - mess->bitmap->w/2;
  1176. mess->pos.y = screen->h/2 - mess->bitmap->h/2;
  1177. GH.pushInt(mess);
  1178. }
  1179. }
  1180. void CCastleInterface::CTownInfo::show(SDL_Surface * to)
  1181. {
  1182. if ( bid == 14 )//marketplace/income
  1183. {
  1184. std::ostringstream oss;
  1185. oss << LOCPLINT->castleInt->town->dailyIncome();
  1186. CSDL_Ext::printAtMiddle(oss.str(),pos.x+32,pos.y+32,FONT_SMALL,zwykly,to);
  1187. }
  1188. else if ( bid == 6 )//no fort
  1189. blitAt(pic->ourImages[3].bitmap,pos.x,pos.y,to);
  1190. else if (bid < 10)//fort-castle
  1191. blitAt(pic->ourImages[bid-7].bitmap,pos.x,pos.y,to);
  1192. else//town halls
  1193. blitAt(pic->ourImages[bid-10].bitmap,pos.x,pos.y,to);
  1194. }
  1195. CRecruitmentWindow * CCastleInterface::showRecruitmentWindow( int building )
  1196. {
  1197. if(building>36) //upg dwelling
  1198. building-=7;
  1199. int level = building-30;
  1200. assert(level >= 0 && level <= 6);
  1201. //std::vector<std::pair<int,int > > crs;
  1202. //int amount = town->creatures[level].first;
  1203. //const std::vector<ui32> &cres = town->creatures[level].second;
  1204. //for(size_t i = 0; i < cres.size(); i++)
  1205. // crs.push_back(std::make_pair((int)cres[i],amount));
  1206. //CRecruitmentWindow *rw = new CRecruitmentWindow(crs,boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  1207. CRecruitmentWindow *rw = new CRecruitmentWindow(town, level, town, boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  1208. GH.pushInt(rw);
  1209. return rw;
  1210. }
  1211. void CCastleInterface::enterMageGuild()
  1212. {
  1213. GH.pushInt(new CMageGuildScreen(this));
  1214. }
  1215. void CCastleInterface::enterTavern()
  1216. {
  1217. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(town);
  1218. CTavernWindow *tv = new CTavernWindow(h[0],h[1],"GOSSIP TEST");
  1219. GH.pushInt(tv);
  1220. }
  1221. void CCastleInterface::keyPressed( const SDL_KeyboardEvent & key )
  1222. {
  1223. if(key.state != SDL_PRESSED) return;
  1224. switch(key.keysym.sym)
  1225. {
  1226. case SDLK_UP:
  1227. if(townlist->selected)
  1228. {
  1229. townlist->selected--;
  1230. townlist->from--;
  1231. townChange();
  1232. }
  1233. break;
  1234. case SDLK_DOWN:
  1235. if(townlist->selected < townlist->items.size() - 1)
  1236. {
  1237. townlist->selected++;
  1238. townlist->from++;
  1239. townChange();
  1240. }
  1241. break;
  1242. case SDLK_SPACE:
  1243. if(town->visitingHero && town->garrisonHero)
  1244. {
  1245. LOCPLINT->cb->swapGarrisonHero(town);
  1246. }
  1247. break;
  1248. default:
  1249. break;
  1250. }
  1251. }
  1252. void CCastleInterface::splitClicked()
  1253. {
  1254. if(town->visitingHero && town->garrisonHero && (hslotdown.highlight || hslotup.highlight))
  1255. {
  1256. LOCPLINT->heroExchangeStarted(town->visitingHero->id, town->garrisonHero->id);
  1257. }
  1258. }
  1259. void CHallInterface::CBuildingBox::hover(bool on)
  1260. {
  1261. //Hoverable::hover(on);
  1262. if(on)
  1263. {
  1264. std::string toPrint;
  1265. if(state==8)
  1266. toPrint = CGI->generaltexth->hcommands[5];
  1267. else if(state==5)//"already builded today" message
  1268. toPrint = CGI->generaltexth->allTexts[223];
  1269. else
  1270. toPrint = CGI->generaltexth->hcommands[state];
  1271. std::vector<std::string> name;
  1272. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->Name());
  1273. LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
  1274. }
  1275. else
  1276. LOCPLINT->statusbar->clear();
  1277. }
  1278. void CHallInterface::CBuildingBox::clickLeft(tribool down, bool previousState)
  1279. {
  1280. if(previousState && (!down))
  1281. {
  1282. GH.pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0));
  1283. }
  1284. //ClickableL::clickLeft(down);
  1285. }
  1286. void CHallInterface::CBuildingBox::clickRight(tribool down, bool previousState)
  1287. {
  1288. if(down)
  1289. {
  1290. GH.pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1));
  1291. }
  1292. //ClickableR::clickRight(down);
  1293. }
  1294. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  1295. {
  1296. CCastleInterface *ci = LOCPLINT->castleInt;
  1297. if (( (BID == 18) && (vstd::contains(ci->town->builtBuildings,(ci->town->town->hordeLvl[0]+37))))
  1298. || ( (BID == 24) && (vstd::contains(ci->town->builtBuildings,(ci->town->town->hordeLvl[1]+37)))) )
  1299. blitAt(ci->bicons->ourImages[BID+1].bitmap,pos.x,pos.y,to);
  1300. else
  1301. blitAt(ci->bicons->ourImages[BID].bitmap,pos.x,pos.y,to);
  1302. int pom, pom2=-1;
  1303. switch (state)
  1304. {
  1305. case 4:
  1306. pom = 0;
  1307. pom2 = 0;
  1308. break;
  1309. case 7:
  1310. pom = 1;
  1311. break;
  1312. case 6:
  1313. pom2 = 2;
  1314. pom = 2;
  1315. break;
  1316. case 5: case 8:
  1317. pom2 = 1;
  1318. pom = 2;
  1319. break;
  1320. case 0: case 2: case 1: default:
  1321. pom = 3;
  1322. break;
  1323. }
  1324. blitAt(ci->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71,to);
  1325. if(pom2>=0)
  1326. blitAt(ci->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54,to);
  1327. 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);
  1328. }
  1329. CHallInterface::CBuildingBox::~CBuildingBox()
  1330. {
  1331. }
  1332. CHallInterface::CBuildingBox::CBuildingBox(int id)
  1333. :BID(id)
  1334. {
  1335. used = LCLICK | RCLICK | HOVER;
  1336. pos.w = 150;
  1337. pos.h = 88;
  1338. }
  1339. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  1340. :BID(id)
  1341. {
  1342. used = LCLICK | RCLICK | HOVER;
  1343. pos.x = x;
  1344. pos.y = y;
  1345. pos.w = 150;
  1346. pos.h = 88;
  1347. }
  1348. CHallInterface::CHallInterface(CCastleInterface * owner)
  1349. {
  1350. resdatabar = new CMinorResDataBar;
  1351. pos = owner->pos;
  1352. resdatabar->pos.x += pos.x;
  1353. resdatabar->pos.y += pos.y;
  1354. LOCPLINT->castleInt->statusbar->clear();
  1355. bg = BitmapHandler::loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  1356. bid = owner->town->hallLevel()+10;
  1357. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1358. exit = new AdventureMapButton
  1359. (CGI->generaltexth->hcommands[8],"",boost::bind(&CHallInterface::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1360. exit->assignedKeys.insert(SDLK_ESCAPE);
  1361. //preparing boxes with buildings//
  1362. boxes.resize(5);
  1363. for(size_t i=0;i<5;i++) //for each row
  1364. {
  1365. std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[owner->town->subID].second;
  1366. for(size_t j=0; j<boxList[i].size();j++) //for each box
  1367. {
  1368. size_t k=0;
  1369. for(;k<boxList[i][j].size();k++)//we are looking for the first not build structure
  1370. {
  1371. int bid = boxList[i][j][k];
  1372. if(!vstd::contains(owner->town->builtBuildings,bid))
  1373. {
  1374. int x = 34 + 194*j,
  1375. y = 37 + 104*i,
  1376. ID = bid;
  1377. if(boxList[i].size() == 2) //only two boxes in this row
  1378. x+=194;
  1379. else if(boxList[i].size() == 3) //only three boxes in this row
  1380. x+=97;
  1381. boxes[i].push_back(new CBuildingBox(bid,pos.x+x,pos.y+y));
  1382. //if this is horde dwelling for upgraded creatures and we already have one for basic creatures
  1383. if((bid == 25 && vstd::contains(owner->town->builtBuildings,24))
  1384. || (bid == 19 && vstd::contains(owner->town->builtBuildings,18))
  1385. )
  1386. {
  1387. boxes[i].back()->state = 4; //already built
  1388. }
  1389. else
  1390. {
  1391. boxes[i].back()->state = LOCPLINT->cb->canBuildStructure(owner->town,ID);
  1392. }
  1393. break;
  1394. }
  1395. }
  1396. if(k == boxList[i][j].size()) //all buildings built - let's take the last one
  1397. {
  1398. int x = 34 + 194*j,
  1399. y = 37 + 104*i;
  1400. if(boxList[i].size() == 2)
  1401. x+=194;
  1402. else if(boxList[i].size() == 3)
  1403. x+=97;
  1404. boxes[i].push_back(new CBuildingBox(boxList[i][j][k-1],pos.x+x,pos.y+y));
  1405. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  1406. }
  1407. }
  1408. }
  1409. }
  1410. CHallInterface::~CHallInterface()
  1411. {
  1412. SDL_FreeSurface(bg);
  1413. for(size_t i=0;i<boxes.size();i++)
  1414. for(size_t j=0;j<boxes[i].size();j++)
  1415. delete boxes[i][j]; //TODO whats wrong with smartpointers?
  1416. delete exit;
  1417. delete resdatabar;
  1418. }
  1419. void CHallInterface::close()
  1420. {
  1421. GH.popIntTotally(this);
  1422. }
  1423. void CHallInterface::show(SDL_Surface * to)
  1424. {
  1425. blitAt(bg,pos,to);
  1426. LOCPLINT->castleInt->statusbar->show(to);
  1427. printAtMiddle(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][bid]->Name(),399+pos.x,12+pos.y,FONT_MEDIUM,zwykly,to);
  1428. resdatabar->show(to);
  1429. exit->show(to);
  1430. for(int i=0; i<5; i++)
  1431. {
  1432. for(size_t j=0;j<boxes[i].size(); ++j)
  1433. boxes[i][j]->show(to);
  1434. }
  1435. }
  1436. void CHallInterface::activate()
  1437. {
  1438. for(int i=0;i<5;i++)
  1439. {
  1440. for(size_t j=0; j < boxes[i].size(); ++j)
  1441. {
  1442. boxes[i][j]->activate();
  1443. }
  1444. }
  1445. exit->activate();
  1446. }
  1447. void CHallInterface::deactivate()
  1448. {
  1449. for(int i=0;i<5;i++)
  1450. {
  1451. for(size_t j=0;j<boxes[i].size();++j)
  1452. {
  1453. boxes[i][j]->deactivate();
  1454. }
  1455. }
  1456. exit->deactivate();
  1457. }
  1458. void CHallInterface::CBuildWindow::activate()
  1459. {
  1460. activateRClick();
  1461. if(mode)
  1462. return;
  1463. if(state==7)
  1464. buy->activate();
  1465. cancel->activate();
  1466. }
  1467. void CHallInterface::CBuildWindow::deactivate()
  1468. {
  1469. deactivateRClick();
  1470. if(mode)
  1471. return;
  1472. if(state==7)
  1473. buy->deactivate();
  1474. cancel->deactivate();
  1475. }
  1476. void CHallInterface::CBuildWindow::Buy()
  1477. {
  1478. int building = bid;
  1479. GH.popInts(2); //we - build window and hall screen
  1480. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,building);
  1481. }
  1482. void CHallInterface::CBuildWindow::close()
  1483. {
  1484. GH.popIntTotally(this);
  1485. }
  1486. void CHallInterface::CBuildWindow::clickRight(tribool down, bool previousState)
  1487. {
  1488. if((!down || indeterminate(down)) && mode)
  1489. close();
  1490. }
  1491. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  1492. {
  1493. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  1494. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  1495. SDL_BlitSurface(bitmap,&poms,to,&pom);
  1496. if(!mode)
  1497. {
  1498. buy->show(to);
  1499. cancel->show(to);
  1500. }
  1501. }
  1502. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  1503. {
  1504. std::string ret;
  1505. if(state<7)
  1506. ret = CGI->generaltexth->hcommands[state];
  1507. switch (state)
  1508. {
  1509. case 4: case 5: case 6:
  1510. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->Name());
  1511. break;
  1512. case 7:
  1513. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1514. case 8:
  1515. {
  1516. ret = CGI->generaltexth->allTexts[52];
  1517. std::set<int> reqs= LOCPLINT->cb->getBuildingRequiments(LOCPLINT->castleInt->town, bid);
  1518. bool first=true;
  1519. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1520. {
  1521. if (vstd::contains(LOCPLINT->castleInt->town->builtBuildings, *i))
  1522. continue;//skipping constructed buildings
  1523. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->Name());
  1524. first = false;//TODO - currently can return "Mage guild lvl 1, MG lvl 2..." - extra check needed
  1525. }
  1526. }
  1527. }
  1528. return ret;
  1529. }
  1530. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  1531. :tid(Tid), bid(Bid), state(State), mode(Mode)
  1532. {
  1533. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPUBUILD.bmp");
  1534. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1535. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  1536. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  1537. SDL_FreeSurface(hhlp);
  1538. pos.x = screen->w/2 - bitmap->w/2;
  1539. pos.y = screen->h/2 - bitmap->h/2;
  1540. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  1541. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->Name());
  1542. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->Description(),197,168,FONT_MEDIUM,43,zwykly,bitmap);
  1543. CSDL_Ext::printAtMiddleWB(getTextForState(state),199,248,FONT_SMALL,50,zwykly,bitmap);
  1544. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->generaltexth->hcommands[7],pom),197,30,FONT_BIG,tytulowy,bitmap);
  1545. int resamount=0;
  1546. for(int i=0;i<7;i++)
  1547. {
  1548. if(CGI->buildh->buildings[tid][bid]->resources[i])
  1549. {
  1550. resamount++;
  1551. }
  1552. }
  1553. int ah = (resamount>4) ? 304 : 340;
  1554. int cn=-1, it=0;
  1555. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  1556. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  1557. char buf[15];
  1558. while(++cn<7)
  1559. {
  1560. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  1561. continue;
  1562. SDL_itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  1563. if(it<4)
  1564. {
  1565. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+47,FONT_SMALL,zwykly,bitmap);
  1566. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  1567. }
  1568. else
  1569. {
  1570. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+47,FONT_SMALL,zwykly,bitmap);
  1571. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  1572. }
  1573. if(it==4)
  1574. ah+=75;
  1575. }
  1576. if(!mode)
  1577. {
  1578. buy = new AdventureMapButton
  1579. ("","",boost::bind(&CBuildWindow::Buy,this),pos.x+45,pos.y+446,"IBUY30.DEF",SDLK_RETURN);
  1580. cancel = new AdventureMapButton
  1581. ("","",boost::bind(&CBuildWindow::close,this),pos.x+290,pos.y+445,"ICANCEL.DEF",SDLK_ESCAPE);
  1582. if(state!=7)
  1583. buy->state=2;
  1584. }
  1585. }
  1586. CHallInterface::CBuildWindow::~CBuildWindow()
  1587. {
  1588. SDL_FreeSurface(bitmap);
  1589. if(!mode)
  1590. {
  1591. delete buy;
  1592. delete cancel;
  1593. }
  1594. }
  1595. CFortScreen::~CFortScreen()
  1596. {
  1597. for(size_t i=0;i<crePics.size();i++)
  1598. delete crePics[i];
  1599. for (size_t i=0;i<recAreas.size();i++)
  1600. delete recAreas[i];
  1601. SDL_FreeSurface(bg);
  1602. delete exit;
  1603. delete resdatabar;
  1604. }
  1605. void CFortScreen::show( SDL_Surface * to)
  1606. {
  1607. blitAt(bg,pos,to);
  1608. static unsigned char anim = 1;
  1609. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1610. {
  1611. crePics[i]->blitPic(to,pos.x+positions[i].x+159,pos.y+positions[i].y+4,!(anim%4));
  1612. }
  1613. anim++;
  1614. exit->show(to);
  1615. resdatabar->show(to);
  1616. LOCPLINT->statusbar->show(to);
  1617. }
  1618. void CFortScreen::activate()
  1619. {
  1620. LOCPLINT->statusbar = LOCPLINT->castleInt->statusbar;
  1621. exit->activate();
  1622. for (size_t i=0;i<recAreas.size(); ++i)
  1623. {
  1624. recAreas[i]->activate();
  1625. }
  1626. }
  1627. void CFortScreen::deactivate()
  1628. {
  1629. exit->deactivate();
  1630. for (size_t i=0;i<recAreas.size();i++)
  1631. {
  1632. recAreas[i]->deactivate();
  1633. }
  1634. }
  1635. void CFortScreen::close()
  1636. {
  1637. GH.popIntTotally(this);
  1638. }
  1639. CFortScreen::CFortScreen( CCastleInterface * owner )
  1640. {
  1641. resdatabar = new CMinorResDataBar;
  1642. pos = owner->pos;
  1643. bg = NULL;
  1644. LOCPLINT->castleInt->statusbar->clear();
  1645. std::string temp = CGI->generaltexth->fcommands[6];
  1646. boost::algorithm::replace_first(temp,"%s",CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name());
  1647. exit = new AdventureMapButton(temp,"",boost::bind(&CFortScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1648. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1649. genRect(126,386,10,155),genRect(126,386,404,155),
  1650. genRect(126,386,10,288),genRect(126,386,404,288),
  1651. genRect(126,386,206,421);
  1652. draw(owner,true);
  1653. resdatabar->pos += pos;
  1654. }
  1655. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1656. {
  1657. if(bg)
  1658. SDL_FreeSurface(bg);
  1659. char buf[20];
  1660. memset(buf,0,20);
  1661. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1662. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1663. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1664. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1665. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1666. SDL_FreeSurface(bg2);
  1667. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name(),400,13,FONT_MEDIUM,zwykly,bg);
  1668. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1669. {
  1670. bool upgraded = owner->town->creatureDwelling(i,true);
  1671. bool present = owner->town->creatureDwelling(i,false);
  1672. CCreature *c = &CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1673. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,FONT_SMALL,zwykly,bg); //cr. name
  1674. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1675. 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
  1676. if(present) //if creature is present print avail able quantity
  1677. {
  1678. SDL_itoa(owner->town->creatures[i].first,buf,10);
  1679. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,FONT_SMALL,zwykly,bg);
  1680. }
  1681. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1682. //attack
  1683. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,FONT_SMALL,zwykly,bg);
  1684. SDL_itoa(c->attack,buf,10);
  1685. printTo(buf,positions[i].x+381,positions[i].y+21,FONT_SMALL,zwykly,bg);
  1686. //defense
  1687. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,FONT_SMALL,zwykly,bg);
  1688. SDL_itoa(c->defence,buf,10);
  1689. printTo(buf,positions[i].x+381,positions[i].y+41,FONT_SMALL,zwykly,bg);
  1690. //damage
  1691. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,FONT_SMALL,zwykly,bg);
  1692. SDL_itoa(c->damageMin,buf,10);
  1693. int hlp;
  1694. if(c->damageMin > 0)
  1695. hlp = log10f(c->damageMin)+2;
  1696. else
  1697. hlp = 2;
  1698. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1699. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1700. printTo(buf,positions[i].x+381,positions[i].y+62,FONT_SMALL,zwykly,bg);
  1701. //health
  1702. printAt(CGI->generaltexth->zelp[439].first,positions[i].x+288,positions[i].y+66,FONT_SMALL,zwykly,bg);
  1703. SDL_itoa(c->hitPoints,buf,10);
  1704. printTo(buf,positions[i].x+381,positions[i].y+82,FONT_SMALL,zwykly,bg);
  1705. //speed
  1706. printAt(CGI->generaltexth->zelp[441].first,positions[i].x+288,positions[i].y+87,FONT_SMALL,zwykly,bg);
  1707. SDL_itoa(c->speed,buf,10);
  1708. printTo(buf,positions[i].x+381,positions[i].y+103,FONT_SMALL,zwykly,bg);
  1709. if(present)//growth
  1710. {
  1711. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,FONT_SMALL,zwykly,bg);
  1712. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1713. printTo(buf,positions[i].x+381,positions[i].y+123,FONT_SMALL,zwykly,bg);
  1714. }
  1715. if(first)
  1716. {
  1717. crePics.push_back(new CCreaturePic(c,false));
  1718. if(present)
  1719. {
  1720. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1721. recAreas.back()->pos = positions[i] + pos;
  1722. }
  1723. }
  1724. }
  1725. SDL_FreeSurface(icons);
  1726. }
  1727. void CFortScreen::RecArea::clickLeft(tribool down, bool previousState)
  1728. {
  1729. if(!down && previousState)
  1730. {
  1731. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1732. }
  1733. //ClickableL::clickLeft(down);
  1734. }
  1735. void CFortScreen::RecArea::clickRight(tribool down, bool previousState)
  1736. {
  1737. clickLeft(down, false); //r-click does same as l-click - opens recr. window
  1738. }
  1739. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1740. {
  1741. resdatabar = new CMinorResDataBar;
  1742. pos = owner->pos;
  1743. resdatabar->pos.x += pos.x;
  1744. resdatabar->pos.y += pos.y;
  1745. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1746. LOCPLINT->castleInt->statusbar->clear();
  1747. exit = new AdventureMapButton(CGI->generaltexth->allTexts[593],"",boost::bind(&CMageGuildScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1748. exit->assignedKeys.insert(SDLK_ESCAPE);
  1749. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1750. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1751. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1752. positions.resize(5);
  1753. 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);
  1754. 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);
  1755. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1756. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1757. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1758. blitAt(view,332,76,bg);
  1759. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1760. {
  1761. size_t sp = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1762. for(size_t j=0; j<sp; j++)
  1763. {
  1764. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1765. {
  1766. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1767. spells[spells.size()-1].pos = positions[i][j];
  1768. blitAt(graphics->spellscr->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1769. }
  1770. else
  1771. {
  1772. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1773. }
  1774. }
  1775. }
  1776. SDL_FreeSurface(view);
  1777. for(size_t i=0;i<spells.size();i++)
  1778. {
  1779. spells[i].pos.x += pos.x;
  1780. spells[i].pos.y += pos.y;
  1781. }
  1782. delete scrolls2;
  1783. }
  1784. CMageGuildScreen::~CMageGuildScreen()
  1785. {
  1786. delete exit;
  1787. SDL_FreeSurface(bg);
  1788. delete resdatabar;
  1789. }
  1790. void CMageGuildScreen::close()
  1791. {
  1792. GH.popIntTotally(this);
  1793. }
  1794. void CMageGuildScreen::show(SDL_Surface * to)
  1795. {
  1796. blitAt(bg,pos,to);
  1797. resdatabar->show(to);
  1798. LOCPLINT->statusbar->show(to);
  1799. exit->show(to);
  1800. }
  1801. void CMageGuildScreen::activate()
  1802. {
  1803. exit->activate();
  1804. for(size_t i=0;i<spells.size();i++)
  1805. {
  1806. spells[i].activate();
  1807. }
  1808. }
  1809. void CMageGuildScreen::deactivate()
  1810. {
  1811. exit->deactivate();
  1812. for(size_t i=0;i<spells.size();i++)
  1813. {
  1814. spells[i].deactivate();
  1815. }
  1816. }
  1817. void CMageGuildScreen::Scroll::clickLeft(tribool down, bool previousState)
  1818. {
  1819. if(down)
  1820. {
  1821. std::vector<SComponent*> comps(1,
  1822. new CCustomImgComponent(SComponent::spell,spell->id,0,graphics->spellscr->ourImages[spell->id].bitmap,false)
  1823. );
  1824. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1825. }
  1826. }
  1827. void CMageGuildScreen::Scroll::clickRight(tribool down, bool previousState)
  1828. {
  1829. if(down)
  1830. {
  1831. CInfoPopup *vinya = new CInfoPopup();
  1832. vinya->free = true;
  1833. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1834. (LOCPLINT->playerID,
  1835. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1836. spell->name,30,30);
  1837. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1838. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1839. GH.pushInt(vinya);
  1840. }
  1841. }
  1842. void CMageGuildScreen::Scroll::hover(bool on)
  1843. {
  1844. //Hoverable::hover(on);
  1845. if(on)
  1846. LOCPLINT->statusbar->print(spell->name);
  1847. else
  1848. LOCPLINT->statusbar->clear();
  1849. }
  1850. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1851. {
  1852. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1853. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1854. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1855. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1856. SDL_FreeSurface(bg2);
  1857. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1858. blitAt(bg2,64,50,bmp);
  1859. SDL_FreeSurface(bg2);
  1860. CCreatureAnimation cra(CGI->creh->creatures[creMachineID].animDefName);
  1861. cra.nextFrameMiddle(bmp,170,120,true,0,false);
  1862. char pom[75];
  1863. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID].nameSing.c_str()); //build a new ...
  1864. printAtMiddle(pom,165,28,FONT_MEDIUM,tytulowy,bmp);
  1865. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,FONT_MEDIUM,zwykly,bmp); //resource cost
  1866. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1867. printAtMiddle(pom,165,290,FONT_MEDIUM,zwykly,bmp);
  1868. pos.w = bmp->w;
  1869. pos.h = bmp->h;
  1870. pos.x = screen->w/2 - pos.w/2;
  1871. pos.y = screen->h/2 - pos.h/2;
  1872. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF",SDLK_RETURN);
  1873. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1874. if(possible)
  1875. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1876. else
  1877. buy->bitmapOffset = 2;
  1878. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1879. }
  1880. void CBlacksmithDialog::show( SDL_Surface * to )
  1881. {
  1882. blitAt(bmp,pos,to);
  1883. buy->show(to);
  1884. cancel->show(to);
  1885. }
  1886. void CBlacksmithDialog::activate()
  1887. {
  1888. if(!buy->bitmapOffset)
  1889. buy->activate();
  1890. cancel->activate();
  1891. }
  1892. void CBlacksmithDialog::deactivate()
  1893. {
  1894. if(!buy->bitmapOffset)
  1895. buy->deactivate();
  1896. cancel->deactivate();
  1897. }
  1898. CBlacksmithDialog::~CBlacksmithDialog()
  1899. {
  1900. SDL_FreeSurface(bmp);
  1901. delete cancel;
  1902. delete buy;
  1903. }
  1904. void CBlacksmithDialog::close()
  1905. {
  1906. GH.popIntTotally(this);
  1907. }