CCastleInterface.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. extern TTF_Font * GEOR16;
  38. CBuildingRect::CBuildingRect(Structure *Str)
  39. :moi(false), offset(0), str(Str)
  40. {
  41. def = CDefHandler::giveDef(Str->defName);
  42. max = def->ourImages.size();
  43. if(str->ID == 33 && str->townID == 4) //little 'hack' for estate in necropolis - background color is not always the first color in the palette
  44. {
  45. for(std::vector<Cimage>::iterator i=def->ourImages.begin();i!=def->ourImages.end();i++)
  46. {
  47. SDL_SetColorKey(i->bitmap,SDL_SRCCOLORKEY,*((char*)i->bitmap->pixels));
  48. }
  49. }
  50. pos.x = str->pos.x + LOCPLINT->castleInt->pos.x;
  51. pos.y = str->pos.y + LOCPLINT->castleInt->pos.y;
  52. pos.w = def->ourImages[0].bitmap->w;
  53. pos.h = def->ourImages[0].bitmap->h;
  54. if(Str->ID<0 || (Str->ID>=27 && Str->ID<=29))
  55. {
  56. area = border = NULL;
  57. return;
  58. }
  59. if (border = BitmapHandler::loadBitmap(str->borderName)) //FIXME hmmm if '=' use () else '==' fatal mistake
  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. if (area = BitmapHandler::loadBitmap(str->areaName))//FIXME look up
  68. {
  69. ;//SDL_SetColorKey(area,SDL_SRCCOLORKEY,SDL_MapRGB(area->format,0,255,255));
  70. }
  71. else
  72. {
  73. tlog2 << "Warning: no area for "<<Str->ID<<std::endl;
  74. }
  75. }
  76. CBuildingRect::~CBuildingRect()
  77. {
  78. delete def;
  79. if(border)
  80. SDL_FreeSurface(border);
  81. if(area)
  82. SDL_FreeSurface(area);
  83. }
  84. void CBuildingRect::activate()
  85. {
  86. Hoverable::activate();
  87. ClickableL::activate();
  88. ClickableR::activate();
  89. }
  90. void CBuildingRect::deactivate()
  91. {
  92. Hoverable::deactivate();
  93. ClickableL::deactivate();
  94. ClickableR::deactivate();
  95. if(moi)
  96. MotionInterested::deactivate();
  97. moi=false;
  98. }
  99. bool CBuildingRect::operator<(const CBuildingRect & p2) const
  100. {
  101. if(str->pos.z != p2.str->pos.z)
  102. return (str->pos.z) < (p2.str->pos.z);
  103. else
  104. return (str->ID) < (p2.str->ID);
  105. }
  106. void CBuildingRect::hover(bool on)
  107. {
  108. Hoverable::hover(on);
  109. if(on)
  110. {
  111. if(!moi)
  112. MotionInterested::activate();
  113. moi = true;
  114. }
  115. else
  116. {
  117. if(moi)
  118. MotionInterested::deactivate();
  119. moi = false;
  120. if(LOCPLINT->castleInt->hBuild == this)
  121. {
  122. LOCPLINT->castleInt->hBuild = NULL;
  123. LOCPLINT->statusbar->clear();
  124. }
  125. }
  126. }
  127. void CBuildingRect::clickLeft (tribool down)
  128. {
  129. if(area && (LOCPLINT->castleInt->hBuild==this) && !(indeterminate(down)) && (CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->current->motion.y-pos.y) != 0)) //na polu
  130. {
  131. if(pressedL && !down)
  132. LOCPLINT->castleInt->buildingClicked(str->ID);
  133. ClickableL::clickLeft(down);
  134. }
  135. //todo - handle
  136. }
  137. void CBuildingRect::clickRight (tribool down)
  138. {
  139. if((!area) || (!((bool)down)) || (this!=LOCPLINT->castleInt->hBuild))
  140. return;
  141. if((CSDL_Ext::SDL_GetPixel(area,LOCPLINT->current->motion.x-pos.x,LOCPLINT->current->motion.y-pos.y) != 0)) //na polu
  142. {
  143. CBuilding *bld = CGI->buildh->buildings[str->townID][str->ID];
  144. assert(bld);
  145. CInfoPopup *vinya = new CInfoPopup();
  146. vinya->free = true;
  147. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  148. (LOCPLINT->playerID,
  149. bld->Description(),
  150. LOCPLINT->castleInt->bicons->ourImages[str->ID].bitmap,
  151. bld->Name());
  152. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  153. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  154. LOCPLINT->pushInt(vinya);
  155. }
  156. }
  157. void CBuildingRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  158. {
  159. if(area)
  160. {
  161. if(CSDL_Ext::SDL_GetPixel(area,sEvent.x-pos.x,sEvent.y-pos.y) == 0) //hovered pixel is inside this building
  162. {
  163. if(LOCPLINT->castleInt->hBuild == this)
  164. {
  165. LOCPLINT->castleInt->hBuild = NULL;
  166. LOCPLINT->statusbar->clear();
  167. }
  168. }
  169. else //inside the area of this building
  170. {
  171. if(LOCPLINT->castleInt->hBuild) //a building is hovered
  172. {
  173. if((*LOCPLINT->castleInt->hBuild)<(*this)) //set if we are on top
  174. {
  175. LOCPLINT->castleInt->hBuild = this;
  176. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->Name().length())
  177. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  178. else
  179. LOCPLINT->statusbar->print(str->name);
  180. }
  181. }
  182. else //no building hovered
  183. {
  184. LOCPLINT->castleInt->hBuild = this;
  185. if(CGI->buildh->buildings[str->townID][str->ID] && CGI->buildh->buildings[str->townID][str->ID]->Name().length())
  186. LOCPLINT->statusbar->print(CGI->buildh->buildings[str->townID][str->ID]->Name());
  187. else
  188. LOCPLINT->statusbar->print(str->name);
  189. }
  190. }
  191. }
  192. //if(border)
  193. // blitAt(border,pos.x,pos.y);
  194. }
  195. void CHeroGSlot::hover (bool on)
  196. {
  197. if(!on) return;
  198. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  199. std::string temp;
  200. if(hero)
  201. {
  202. if(highlight)//view NNN
  203. {
  204. temp = CGI->generaltexth->tcommands[4];
  205. boost::algorithm::replace_first(temp,"%s",hero->name);
  206. }
  207. else if(other->hero && other->highlight)//exchange
  208. {
  209. temp = CGI->generaltexth->tcommands[7];
  210. boost::algorithm::replace_first(temp,"%s",hero->name);
  211. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  212. }
  213. else// select NNN (in ZZZ)
  214. {
  215. if(upg)//down - visiting
  216. {
  217. temp = CGI->generaltexth->tcommands[32];
  218. boost::algorithm::replace_first(temp,"%s",hero->name);
  219. }
  220. else //up - garrison
  221. {
  222. temp = CGI->generaltexth->tcommands[12];
  223. boost::algorithm::replace_first(temp,"%s",hero->name);
  224. }
  225. }
  226. }
  227. else //we are empty slot
  228. {
  229. if(other->highlight && other->hero) //move NNNN
  230. {
  231. temp = CGI->generaltexth->tcommands[6];
  232. boost::algorithm::replace_first(temp,"%s",other->hero->name);
  233. }
  234. else //empty
  235. {
  236. temp = CGI->generaltexth->allTexts[507];
  237. }
  238. }
  239. if(temp.size())
  240. LOCPLINT->statusbar->print(temp);
  241. }
  242. void CHeroGSlot::clickRight (boost::logic::tribool down)
  243. {
  244. }
  245. void CHeroGSlot::clickLeft(boost::logic::tribool down)
  246. {
  247. CHeroGSlot *other = upg ? &owner->hslotup : &owner->hslotdown;
  248. if(!down)
  249. {
  250. if(hero && highlight)
  251. {
  252. highlight = false;
  253. LOCPLINT->openHeroWindow(hero);
  254. }
  255. else if(other->hero && other->highlight)
  256. {
  257. bool allow = true;
  258. if(upg) //moving hero out of town - check if it is allowed
  259. {
  260. if(!hero && LOCPLINT->cb->howManyHeroes(false) >= 8)
  261. {
  262. std::string tmp = CGI->generaltexth->allTexts[18]; //You already have %d adventuring heroes under your command.
  263. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes(false)));
  264. LOCPLINT->showInfoDialog(tmp,std::vector<SComponent*>(), soundBase::sound_todo);
  265. allow = false;
  266. }
  267. else if(!other->hero->army.slots.size()) //hero has no creatures - strange, but if we have appropriate error message...
  268. {
  269. 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.
  270. allow = false;
  271. }
  272. }
  273. other->highlight = highlight = false;
  274. if(allow)
  275. LOCPLINT->cb->swapGarrisonHero(owner->town);
  276. }
  277. else if(hero)
  278. {
  279. highlight = true;
  280. owner->garr->highlighted = NULL;
  281. show(screen2);
  282. }
  283. hover(false);hover(true); //refresh statusbar
  284. }
  285. if(indeterminate(down) && !isItIn(&other->pos,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  286. {
  287. other->highlight = highlight = false;
  288. show(screen2);
  289. }
  290. }
  291. void CHeroGSlot::activate()
  292. {
  293. ClickableL::activate();
  294. ClickableR::activate();
  295. Hoverable::activate();
  296. }
  297. void CHeroGSlot::deactivate()
  298. {
  299. highlight = false;
  300. ClickableL::deactivate();
  301. ClickableR::deactivate();
  302. Hoverable::deactivate();
  303. }
  304. void CHeroGSlot::show(SDL_Surface * to)
  305. {
  306. if(hero) //there is hero
  307. blitAt(graphics->portraitLarge[hero->portrait],pos,to);
  308. else if(!upg) //up garrison
  309. blitAt(graphics->flags->ourImages[LOCPLINT->castleInt->town->getOwner()].bitmap,pos,to);
  310. if(highlight)
  311. blitAt(graphics->bigImgs[-1],pos,to);
  312. }
  313. CHeroGSlot::CHeroGSlot(int x, int y, int updown, const CGHeroInstance *h, CCastleInterface * Owner)
  314. {
  315. owner = Owner;
  316. pos.x = x;
  317. pos.y = y;
  318. pos.w = 58;
  319. pos.h = 64;
  320. hero = h;
  321. upg = updown;
  322. highlight = false;
  323. }
  324. CHeroGSlot::~CHeroGSlot()
  325. {
  326. }
  327. std::string getBgName(int type) //TODO - co z tym zrobi�?
  328. {
  329. switch (type)
  330. {
  331. case 0:
  332. return "TBCSBACK.bmp";
  333. case 1:
  334. return "TBRMBACK.bmp";
  335. case 2:
  336. return "TBTWBACK.bmp";
  337. case 3:
  338. return "TBINBACK.bmp";
  339. case 4:
  340. return "TBNCBACK.bmp";
  341. case 5:
  342. return "TBDNBACK.bmp";
  343. case 6:
  344. return "TBSTBACK.bmp";
  345. case 7:
  346. return "TBFRBACK.bmp";
  347. case 8:
  348. return "TBELBACK.bmp";
  349. default:
  350. #ifndef __GNUC__
  351. throw new std::exception("std::string getBgName(int type): invalid type");
  352. #else
  353. throw new std::exception();
  354. #endif
  355. }
  356. }
  357. class SORTHELP
  358. {
  359. public:
  360. bool operator ()
  361. (const CBuildingRect *a ,
  362. const CBuildingRect *b)
  363. {
  364. return (*a)<(*b);
  365. }
  366. } srthlp ;
  367. CCastleInterface::CCastleInterface(const CGTownInstance * Town)
  368. :hslotup(241,387,0,Town->garrisonHero,this),hslotdown(241,483,1,Town->visitingHero,this)
  369. {
  370. bars = CDefHandler::giveDefEss("TPTHBAR.DEF");
  371. status = CDefHandler::giveDefEss("TPTHCHK.DEF");
  372. LOCPLINT->castleInt = this;
  373. hall = NULL;
  374. townInt = BitmapHandler::loadBitmap("TOWNSCRN.bmp");
  375. cityBg = BitmapHandler::loadBitmap(getBgName(Town->subID));
  376. pos.x = screen->w/2 - 400;
  377. pos.y = screen->h/2 - 300;
  378. hslotup.pos.x += pos.x;
  379. hslotup.pos.y += pos.y;
  380. hslotdown.pos.x += pos.x;
  381. hslotdown.pos.y += pos.y;
  382. hall = CDefHandler::giveDef("ITMTL.DEF");
  383. fort = CDefHandler::giveDef("ITMCL.DEF");
  384. hBuild = NULL;
  385. count=0;
  386. town = Town;
  387. //garrison
  388. garr = new CGarrisonInt(pos.x+305,pos.y+387,4,32,townInt,243,13,town,town->visitingHero);
  389. townlist = new CTownList(3,pos.x+744,pos.y+414,"IAM014.DEF","IAM015.DEF");//744,526);
  390. exit = new AdventureMapButton
  391. (CGI->generaltexth->tcommands[8],"",boost::bind(&CCastleInterface::close,this),pos.x+744,pos.y+544,"TSBTNS.DEF",SDLK_RETURN);
  392. split = new AdventureMapButton
  393. (CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+744,pos.y+382,"TSBTNS.DEF");
  394. statusbar = new CStatusBar(pos.x+7,pos.y+555,"TSTATBAR.bmp",732);
  395. resdatabar = new CResDataBar("ZRESBAR.bmp",pos.x+3,pos.y+575,32,2,85,85);
  396. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  397. townlist->fun = boost::bind(&CCastleInterface::townChange,this);
  398. townlist->genList();
  399. townlist->selected = vstd::findPos(townlist->items,Town);
  400. if((townlist->selected+1) > townlist->SIZE)
  401. townlist->from = townlist->selected - townlist->SIZE + 2;
  402. graphics->blueToPlayersAdv(townInt,LOCPLINT->playerID);
  403. exit->bitmapOffset = 4;
  404. //buildings
  405. recreateBuildings();
  406. std::string defname;
  407. switch (town->subID)
  408. {
  409. case 0:
  410. defname = "HALLCSTL.DEF";
  411. musicID = musicBase::castleTown;
  412. break;
  413. case 1:
  414. defname = "HALLRAMP.DEF";
  415. musicID = musicBase::rampartTown;
  416. break;
  417. case 2:
  418. defname = "HALLTOWR.DEF";
  419. musicID = musicBase::towerTown;
  420. break;
  421. case 3:
  422. defname = "HALLINFR.DEF";
  423. musicID = musicBase::infernoTown;
  424. break;
  425. case 4:
  426. defname = "HALLNECR.DEF";
  427. musicID = musicBase::necroTown;
  428. break;
  429. case 5:
  430. defname = "HALLDUNG.DEF";
  431. musicID = musicBase::dungeonTown;
  432. break;
  433. case 6:
  434. defname = "HALLSTRN.DEF";
  435. musicID = musicBase::strongHoldTown;
  436. break;
  437. case 7:
  438. defname = "HALLFORT.DEF";
  439. musicID = musicBase::fortressTown;
  440. break;
  441. case 8:
  442. defname = "HALLELEM.DEF";
  443. musicID = musicBase::elemTown;
  444. break;
  445. default:
  446. throw new std::string("Wrong town subID");
  447. }
  448. bicons = CDefHandler::giveDefEss(defname);
  449. }
  450. CCastleInterface::~CCastleInterface()
  451. {
  452. delete bars;
  453. delete status;
  454. SDL_FreeSurface(townInt);
  455. SDL_FreeSurface(cityBg);
  456. delete exit;
  457. delete split;
  458. delete hall;
  459. delete fort;
  460. delete garr;
  461. delete townlist;
  462. delete statusbar;
  463. delete resdatabar;
  464. for(size_t i=0;i<buildings.size();i++)
  465. {
  466. delete buildings[i];
  467. }
  468. delete bicons;
  469. }
  470. void CCastleInterface::close()
  471. {
  472. if(town->visitingHero)
  473. LOCPLINT->adventureInt->select(town->visitingHero);
  474. LOCPLINT->castleInt = NULL;
  475. LOCPLINT->popIntTotally(this);
  476. CGI->musich->stopMusic(5000);
  477. }
  478. void CCastleInterface::splitF()
  479. {
  480. }
  481. void CCastleInterface::buildingClicked(int building)
  482. {
  483. tlog5<<"You've clicked on "<<building<<std::endl;
  484. if(building==19 || building==18)
  485. {
  486. building = town->town->hordeLvl[0] + 30;
  487. }
  488. else if(building==24 || building==25)
  489. {
  490. building = town->town->hordeLvl[1] + 30;
  491. }
  492. if(building >= 30)
  493. {
  494. showRecruitmentWindow(building);
  495. }
  496. else
  497. {
  498. switch(building)
  499. {
  500. case 0: case 1: case 2: case 3: case 4: //mage guild
  501. {
  502. if(town->visitingHero && !vstd::contains(town->visitingHero->artifWorn,ui16(17))) //visiting hero doesn't have spellboks
  503. {
  504. if(LOCPLINT->cb->getResourceAmount(6) < 500) //not enough gold to buy spellbook
  505. {
  506. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[213],std::vector<SComponent*>(), soundBase::sound_todo);
  507. }
  508. else
  509. {
  510. CFunctionList<void()> fl = boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,town->visitingHero,0);
  511. fl += boost::bind(&CCastleInterface::enterMageGuild,this);
  512. std::vector<SComponent*> vvv(1,new SComponent(SComponent::artifact,0,0));
  513. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[214],vvv,fl,0,true);
  514. }
  515. }
  516. else
  517. {
  518. enterMageGuild();
  519. }
  520. break;
  521. }
  522. case 5: //tavern
  523. {
  524. enterTavern();
  525. break;
  526. }
  527. //TODO: case 6: //shipyard
  528. case 7: case 8: case 9: //fort/citadel/castle
  529. {
  530. CFortScreen *fs = new CFortScreen(this);
  531. LOCPLINT->pushInt(fs);
  532. break;
  533. }
  534. case 10: case 11: case 12: case 13: //hall
  535. enterHall();
  536. break;
  537. case 14: //marketplace
  538. {
  539. CMarketplaceWindow *cmw = new CMarketplaceWindow(0);
  540. LOCPLINT->pushInt(cmw);
  541. break;
  542. }
  543. case 15: //resource silo
  544. {
  545. LOCPLINT->showInfoDialog(CGI->buildh->buildings[town->subID][15]->Description(),std::vector<SComponent*>(), soundBase::sound_todo);
  546. break;
  547. }
  548. case 16: //blacksmith
  549. {
  550. const CGHeroInstance *hero = town->visitingHero;
  551. if(!hero)
  552. {
  553. std::string pom = CGI->generaltexth->allTexts[273];
  554. boost::algorithm::replace_first(pom,"%s",CGI->buildh->buildings[town->subID][16]->Name());
  555. LOCPLINT->showInfoDialog(pom,std::vector<SComponent*>(), soundBase::sound_todo);
  556. return;
  557. }
  558. int aid = town->town->warMachine;
  559. int price = CGI->arth->artifacts[aid].price;
  560. bool possible = (LOCPLINT->cb->getResourceAmount(6) >= price);
  561. if(vstd::contains(hero->artifWorn,ui16(aid+9))) //hero already has machine
  562. possible = false;
  563. LOCPLINT->pushInt(new CBlacksmithDialog(possible,CArtHandler::convertMachineID(aid,false),aid,hero->id));
  564. break;
  565. }
  566. //TODO: case 17: //special 1
  567. //TODO: case 18: //basic horde 1
  568. //TODO: case 19: //upg horde 1
  569. //TODO: case 20: //ship at shipyard
  570. //TODO: case 21: //special 2
  571. case 22: //special 3
  572. {
  573. switch(town->subID)
  574. {
  575. case 0: //brotherhood of sword
  576. enterTavern();
  577. break;
  578. default:
  579. tlog4<<"This building isn't handled...\n";
  580. break;
  581. }
  582. break;
  583. }
  584. //TODO: case 23: //special 4
  585. //TODO: case 24: //basic horde 2
  586. //TODO: case 25: //upg horde 2
  587. //TODO: case 26: //grail
  588. default:
  589. tlog4<<"This building isn't handled...\n";
  590. }
  591. }
  592. }
  593. void CCastleInterface::enterHall()
  594. {
  595. CHallInterface *h = new CHallInterface(this);
  596. LOCPLINT->pushInt(h);
  597. }
  598. void CCastleInterface::showAll( SDL_Surface * to/*=NULL*/)
  599. {
  600. blitAt(cityBg,pos,to);
  601. blitAt(townInt,pos.x,pos.y+374,to);
  602. LOCPLINT->adventureInt->resdatabar.draw(to);
  603. townlist->draw(to);
  604. statusbar->show(to);
  605. resdatabar->draw(to);
  606. garr->show(to);
  607. int pom;
  608. //draw fort icon
  609. if(town->builtBuildings.find(9)!=town->builtBuildings.end())
  610. pom = 2;
  611. else if(town->builtBuildings.find(8)!=town->builtBuildings.end())
  612. pom = 1;
  613. else if(town->builtBuildings.find(7)!=town->builtBuildings.end())
  614. pom = 0;
  615. else pom = 3;
  616. blitAt(fort->ourImages[pom].bitmap,pos.x+122,pos.y+413,to);
  617. //draw ((village/town/city) hall)/capitol icon
  618. if(town->builtBuildings.find(13)!=town->builtBuildings.end())
  619. pom = 3;
  620. else if(town->builtBuildings.find(12)!=town->builtBuildings.end())
  621. pom = 2;
  622. else if(town->builtBuildings.find(11)!=town->builtBuildings.end())
  623. pom = 1;
  624. else pom = 0;
  625. blitAt(hall->ourImages[pom].bitmap,pos.x+80,pos.y+413,to);
  626. //draw creatures icons and their growths
  627. for(int i=0;i<CREATURES_PER_TOWN;i++)
  628. {
  629. int cid = -1;
  630. if (town->builtBuildings.find(30+i)!=town->builtBuildings.end())
  631. {
  632. if (town->builtBuildings.find(30+CREATURES_PER_TOWN+i)!=town->builtBuildings.end())
  633. cid = town->town->upgradedCreatures[i];
  634. else
  635. cid = town->town->basicCreatures[i];
  636. }
  637. if (cid>=0)
  638. {
  639. int pomx, pomy;
  640. pomx = 22 + (55*((i>3)?(i-4):i));
  641. pomy = (i>3)?(507):(459);
  642. blitAt(graphics->smallImgs[cid],pos.x+pomx,pos.y+pomy,to);
  643. std::ostringstream oss;
  644. oss << '+' << town->creatureGrowth(i);
  645. CSDL_Ext::printAtMiddle(oss.str(),pos.x+pomx+16,pos.y+pomy+37,GEOR13,zwykly,to);
  646. }
  647. }
  648. //print name and income
  649. CSDL_Ext::printAt(town->name,pos.x+85,pos.y+389,GEOR13,zwykly,to);
  650. char temp[10];
  651. SDL_itoa(town->dailyIncome(),temp,10);
  652. CSDL_Ext::printAtMiddle(temp,pos.x+195,pos.y+442,GEOR13,zwykly,to);
  653. //blit town icon
  654. pom = town->subID*2;
  655. if (!town->hasFort())
  656. pom += F_NUMBER*2;
  657. if(town->builded >= MAX_BUILDING_PER_TURN)
  658. pom++;
  659. blitAt(graphics->bigTownPic->ourImages[pom].bitmap,pos.x+15,pos.y+387,to);
  660. hslotup.show(to);
  661. hslotdown.show(to);
  662. show(to);
  663. if(screen->w != 800 || screen->h !=600)
  664. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  665. exit->show(to);
  666. split->show(to);
  667. }
  668. void CCastleInterface::townChange()
  669. {
  670. const CGTownInstance * nt = townlist->items[townlist->selected];
  671. LOCPLINT->popIntTotally(this);
  672. LOCPLINT->pushInt(new CCastleInterface(nt));
  673. }
  674. void CCastleInterface::show(SDL_Surface * to)
  675. {
  676. count++;
  677. if(count==8)
  678. {
  679. count=0;
  680. animval++;
  681. }
  682. blitAt(cityBg,pos,to);
  683. //blit buildings
  684. for(size_t i=0;i<buildings.size();i++)
  685. {
  686. int frame = ((animval)%(buildings[i]->max - buildings[i]->offset)) + buildings[i]->offset;
  687. if(frame)
  688. {
  689. blitAt(buildings[i]->def->ourImages[0].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  690. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  691. }
  692. else
  693. blitAt(buildings[i]->def->ourImages[frame].bitmap,buildings[i]->pos.x,buildings[i]->pos.y,to);
  694. if(hBuild==buildings[i] && hBuild->border) //if this this higlighted structure and has border we'll blit it
  695. blitAt(hBuild->border,hBuild->pos,to);
  696. }
  697. }
  698. void CCastleInterface::activate()
  699. {
  700. showing = true;
  701. townlist->activate();
  702. garr->activate();
  703. LOCPLINT->statusbar = statusbar;
  704. exit->activate();
  705. split->activate();
  706. for(size_t i=0;i<buildings.size();i++) //XXX pls use iterators or at() but not []
  707. {
  708. buildings[i]->activate();
  709. }
  710. hslotdown.activate();
  711. hslotup.activate();
  712. }
  713. void CCastleInterface::deactivate()
  714. {
  715. showing = false;
  716. townlist->deactivate();
  717. garr->deactivate();
  718. exit->deactivate();
  719. split->deactivate();
  720. for(size_t i=0;i<buildings.size();i++) //XXX iterators
  721. {
  722. buildings[i]->deactivate();
  723. }
  724. hslotdown.deactivate();
  725. hslotup.deactivate();
  726. }
  727. void CCastleInterface::addBuilding(int bid)
  728. {
  729. //TODO: lepiej by bylo tylko dodawac co trzeba pamietajac o grupach
  730. deactivate();
  731. recreateBuildings();
  732. activate();
  733. }
  734. void CCastleInterface::removeBuilding(int bid)
  735. {
  736. //TODO: lepiej by bylo tylko usuwac co trzeba pamietajac o grupach
  737. recreateBuildings();
  738. }
  739. void CCastleInterface::recreateBuildings()
  740. {
  741. for(size_t i=0;i<buildings.size();i++)
  742. {
  743. if(showing)
  744. buildings[i]->deactivate();
  745. delete buildings[i];
  746. }
  747. buildings.clear();
  748. hBuild = NULL;
  749. std::set< std::pair<int,int> > s; //group - id
  750. for (std::set<si32>::const_iterator i=town->builtBuildings.begin();i!=town->builtBuildings.end();i++)
  751. {
  752. if(CGI->townh->structures.find(town->subID) != CGI->townh->structures.end()) //we have info about structures in this town
  753. {
  754. if(CGI->townh->structures[town->subID].find(*i)!=CGI->townh->structures[town->subID].end()) //we have info about that structure
  755. {
  756. Structure * st = CGI->townh->structures[town->subID][*i];
  757. if(st->group<0) //no group - just add it
  758. {
  759. buildings.push_back(new CBuildingRect(st));
  760. }
  761. else
  762. {
  763. std::set< std::pair<int,int> >::iterator obecny=s.end();
  764. for(std::set< std::pair<int,int> >::iterator seti = s.begin(); seti!=s.end(); seti++) //check if we have already building from same group
  765. {
  766. if(seti->first == st->group)
  767. {
  768. obecny = seti;
  769. break;
  770. }
  771. }
  772. if(obecny != s.end())
  773. {
  774. if((*(CGI->townh->structures[town->subID][obecny->second])) < (*(CGI->townh->structures[town->subID][st->ID]))) //we have to replace old building with current one
  775. {
  776. for(size_t itpb = 0; itpb<buildings.size(); itpb++)
  777. {
  778. if(buildings[itpb]->str->ID == obecny->second)
  779. {
  780. delete buildings[itpb];
  781. buildings.erase(buildings.begin() + itpb);
  782. #ifndef __GNUC__
  783. obecny->second = st->ID; //XXX look closer!
  784. #else
  785. *(const_cast<int*>(&(obecny->second))) = st->ID;
  786. #endif
  787. buildings.push_back(new CBuildingRect(st));
  788. }
  789. }
  790. }
  791. }
  792. else
  793. {
  794. buildings.push_back(new CBuildingRect(st));
  795. s.insert(std::pair<int,int>(st->group,st->ID));
  796. }
  797. }
  798. }
  799. else continue;
  800. }
  801. else
  802. break;
  803. }
  804. std::sort(buildings.begin(),buildings.end(),srthlp);
  805. //code for Mana Vortex (there are two sets of animation frames - one without mage guild and one with
  806. if((town->subID == 5) && (town->builtBuildings.find(21)!=town->builtBuildings.end()))
  807. {
  808. CBuildingRect *vortex = NULL;
  809. for(size_t i=0;i<buildings.size();i++)
  810. {
  811. if(buildings[i]->str->ID==21)
  812. {
  813. vortex=buildings[i];
  814. break;
  815. }
  816. }
  817. if(town->builtBuildings.find(4)!=town->builtBuildings.end()) //there is mage Guild level 5
  818. {
  819. vortex->offset = 10;
  820. vortex->max = vortex->def->ourImages.size();
  821. }
  822. else
  823. {
  824. vortex->offset = 0;
  825. vortex->max = 10;
  826. }
  827. }
  828. //code for the shipyard in the Castle
  829. else if((town->subID == 0) && (town->builtBuildings.find(6)!=town->builtBuildings.end()))
  830. {
  831. CBuildingRect *shipyard = NULL;
  832. for(size_t i=0;i<buildings.size();i++)
  833. {
  834. if(buildings[i]->str->ID==6)
  835. {
  836. shipyard=buildings[i];
  837. break;
  838. }
  839. }
  840. if(town->builtBuildings.find(8)!=town->builtBuildings.end()) //there is citadel
  841. {
  842. shipyard->offset = 1;
  843. shipyard->max = shipyard->def->ourImages.size();
  844. }
  845. else
  846. {
  847. shipyard->offset = 0;
  848. shipyard->max = 1;
  849. }
  850. }
  851. }
  852. CRecruitmentWindow * CCastleInterface::showRecruitmentWindow( int building )
  853. {
  854. if(building>36)
  855. building-=7;
  856. std::vector<std::pair<int,int > > crs;
  857. int amount = (const_cast<CGTownInstance*>(town))->strInfo.creatures[building-30]; //trzeba odconstowac, bo inaczej operator [] by sypal :(
  858. if(town->builtBuildings.find(building+7) != town->builtBuildings.end()) //check if there is an upgraded building
  859. crs.push_back(std::make_pair(town->town->upgradedCreatures[building-30],amount));
  860. crs.push_back(std::make_pair(town->town->basicCreatures[building-30],amount));
  861. CRecruitmentWindow *rw = new CRecruitmentWindow(crs,boost::bind(&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2));
  862. LOCPLINT->pushInt(rw);
  863. return rw;
  864. }
  865. void CCastleInterface::enterMageGuild()
  866. {
  867. LOCPLINT->pushInt(new CMageGuildScreen(this));
  868. }
  869. void CCastleInterface::enterTavern()
  870. {
  871. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(town);
  872. CTavernWindow *tv = new CTavernWindow(h[0],h[1],"GOSSIP TEST");
  873. LOCPLINT->pushInt(tv);
  874. }
  875. void CHallInterface::CBuildingBox::hover(bool on)
  876. {
  877. Hoverable::hover(on);
  878. if(on)
  879. {
  880. std::string toPrint;
  881. if(state==8)
  882. toPrint = CGI->generaltexth->hcommands[5];
  883. else
  884. toPrint = CGI->generaltexth->hcommands[state];
  885. std::vector<std::string> name;
  886. name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->Name());
  887. LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
  888. }
  889. else
  890. LOCPLINT->statusbar->clear();
  891. }
  892. void CHallInterface::CBuildingBox::clickLeft (tribool down)
  893. {
  894. if(pressedL && (!down))
  895. {
  896. LOCPLINT->pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0));
  897. }
  898. ClickableL::clickLeft(down);
  899. }
  900. void CHallInterface::CBuildingBox::clickRight (tribool down)
  901. {
  902. if(down)
  903. {
  904. LOCPLINT->pushInt(new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1));
  905. }
  906. ClickableR::clickRight(down);
  907. }
  908. void CHallInterface::CBuildingBox::show(SDL_Surface * to)
  909. {
  910. CCastleInterface *ci = LOCPLINT->castleInt;
  911. blitAt(ci->bicons->ourImages[BID].bitmap,pos.x,pos.y,to);
  912. int pom, pom2=-1;
  913. switch (state)
  914. {
  915. case 4:
  916. pom = 0;
  917. pom2 = 0;
  918. break;
  919. case 7:
  920. pom = 1;
  921. break;
  922. case 6:
  923. pom2 = 2;
  924. pom = 2;
  925. break;
  926. case 0: case 5: case 8:
  927. pom2 = 1;
  928. pom = 2;
  929. break;
  930. case 2: case 1: default:
  931. pom = 3;
  932. break;
  933. }
  934. blitAt(ci->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71,to);
  935. if(pom2>=0)
  936. blitAt(ci->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54,to);
  937. 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, GEOR13,zwykly,to);
  938. }
  939. void CHallInterface::CBuildingBox::activate()
  940. {
  941. Hoverable::activate();
  942. ClickableL::activate();
  943. ClickableR::activate();
  944. }
  945. void CHallInterface::CBuildingBox::deactivate()
  946. {
  947. Hoverable::deactivate();
  948. ClickableL::deactivate();
  949. ClickableR::deactivate();
  950. }
  951. CHallInterface::CBuildingBox::~CBuildingBox()
  952. {
  953. }
  954. CHallInterface::CBuildingBox::CBuildingBox(int id)
  955. :BID(id)
  956. {
  957. pos.w = 150;
  958. pos.h = 70;
  959. }
  960. CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
  961. :BID(id)
  962. {
  963. pos.x = x;
  964. pos.y = y;
  965. pos.w = 150;
  966. pos.h = 70;
  967. }
  968. CHallInterface::CHallInterface(CCastleInterface * owner)
  969. {
  970. resdatabar = new CMinorResDataBar;
  971. pos = owner->pos;
  972. resdatabar->pos.x += pos.x;
  973. resdatabar->pos.y += pos.y;
  974. bg = BitmapHandler::loadBitmap(CGI->buildh->hall[owner->town->subID].first);
  975. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  976. exit = new AdventureMapButton
  977. (CGI->generaltexth->tcommands[8],"",boost::bind(&CHallInterface::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  978. exit->assignedKeys.insert(SDLK_ESCAPE);
  979. //preparing boxes with buildings//
  980. boxes.resize(5);
  981. for(size_t i=0;i<5;i++) //for each row
  982. {
  983. std::vector< std::vector< std::vector<int> > > &boxList = CGI->buildh->hall[owner->town->subID].second;
  984. for(size_t j=0; j<boxList[i].size();j++) //for each box
  985. {
  986. size_t k=0;
  987. for(;k<boxList[i][j].size();k++)//we are looking for the first not build structure
  988. {
  989. int bid = boxList[i][j][k];
  990. if(!vstd::contains(owner->town->builtBuildings,bid))
  991. {
  992. int x = 34 + 194*j,
  993. y = 37 + 104*i,
  994. ID = bid;
  995. if(boxList[i].size() == 2) //only two boxes in this row
  996. x+=194;
  997. else if(boxList[i].size() == 3) //only three boxes in this row
  998. x+=97;
  999. boxes[i].push_back(new CBuildingBox(bid,pos.x+x,pos.y+y));
  1000. //if this is horde dwelling for upgraded creatures and we already have one for basic creatures
  1001. if((bid == 25 && vstd::contains(owner->town->builtBuildings,24))
  1002. || (bid == 19 && vstd::contains(owner->town->builtBuildings,18))
  1003. )
  1004. {
  1005. boxes[i].back()->state = 4; //already built
  1006. }
  1007. else
  1008. {
  1009. boxes[i].back()->state = LOCPLINT->cb->canBuildStructure(owner->town,ID);
  1010. }
  1011. break;
  1012. }
  1013. }
  1014. if(k == boxList[i][j].size()) //all buildings built - let's take the last one
  1015. {
  1016. int x = 34 + 194*j,
  1017. y = 37 + 104*i;
  1018. if(boxList[i].size() == 2)
  1019. x+=194;
  1020. else if(boxList[i].size() == 3)
  1021. x+=97;
  1022. boxes[i].push_back(new CBuildingBox(boxList[i][j][k-1],pos.x+x,pos.y+y));
  1023. boxes[i][boxes[i].size()-1]->state = 4; //already exists
  1024. }
  1025. }
  1026. }
  1027. }
  1028. CHallInterface::~CHallInterface()
  1029. {
  1030. SDL_FreeSurface(bg);
  1031. for(size_t i=0;i<boxes.size();i++)
  1032. for(size_t j=0;j<boxes[i].size();j++)
  1033. delete boxes[i][j]; //TODO whats wrong with smartpointers?
  1034. delete exit;
  1035. delete resdatabar;
  1036. }
  1037. void CHallInterface::close()
  1038. {
  1039. LOCPLINT->popIntTotally(this);
  1040. }
  1041. void CHallInterface::show(SDL_Surface * to)
  1042. {
  1043. blitAt(bg,pos,to);
  1044. LOCPLINT->castleInt->statusbar->show(to);
  1045. resdatabar->show(to);
  1046. exit->show(to);
  1047. for(int i=0; i<5; i++)
  1048. {
  1049. for(size_t j=0;j<boxes[i].size(); ++j)
  1050. boxes[i][j]->show(to);
  1051. }
  1052. }
  1053. void CHallInterface::activate()
  1054. {
  1055. for(int i=0;i<5;i++)
  1056. {
  1057. for(size_t j=0; j < boxes[i].size(); ++j)
  1058. {
  1059. boxes[i][j]->activate();
  1060. }
  1061. }
  1062. exit->activate();
  1063. }
  1064. void CHallInterface::deactivate()
  1065. {
  1066. for(int i=0;i<5;i++)
  1067. {
  1068. for(size_t j=0;j<boxes[i].size();++j)
  1069. {
  1070. boxes[i][j]->deactivate();
  1071. }
  1072. }
  1073. exit->deactivate();
  1074. }
  1075. void CHallInterface::CBuildWindow::activate()
  1076. {
  1077. ClickableR::activate();
  1078. if(mode)
  1079. return;
  1080. if(state==7)
  1081. buy->activate();
  1082. cancel->activate();
  1083. }
  1084. void CHallInterface::CBuildWindow::deactivate()
  1085. {
  1086. ClickableR::deactivate();
  1087. if(mode)
  1088. return;
  1089. if(state==7)
  1090. buy->deactivate();
  1091. cancel->deactivate();
  1092. }
  1093. void CHallInterface::CBuildWindow::Buy()
  1094. {
  1095. int building = bid;
  1096. LOCPLINT->popInts(2); //we - build window and hall screen
  1097. LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,building);
  1098. }
  1099. void CHallInterface::CBuildWindow::close()
  1100. {
  1101. LOCPLINT->popIntTotally(this);
  1102. }
  1103. void CHallInterface::CBuildWindow::clickRight (tribool down)
  1104. {
  1105. if((!down || indeterminate(down)) && mode)
  1106. close();
  1107. }
  1108. void CHallInterface::CBuildWindow::show(SDL_Surface * to)
  1109. {
  1110. SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
  1111. SDL_Rect poms = pom; poms.x=0;poms.y=0;
  1112. SDL_BlitSurface(bitmap,&poms,to,&pom);
  1113. if(!mode)
  1114. {
  1115. buy->show(to);
  1116. cancel->show(to);
  1117. }
  1118. }
  1119. std::string CHallInterface::CBuildWindow::getTextForState(int state)
  1120. {
  1121. std::string ret;
  1122. if(state<7)
  1123. ret = CGI->generaltexth->hcommands[state];
  1124. switch (state)
  1125. {
  1126. case 4: case 5: case 6:
  1127. ret.replace(ret.find_first_of("%s"),2,CGI->buildh->buildings[tid][bid]->Name());
  1128. break;
  1129. case 7:
  1130. return CGI->generaltexth->allTexts[219]; //all prereq. are met
  1131. case 8:
  1132. {
  1133. ret = CGI->generaltexth->allTexts[52];
  1134. std::set<int> used;
  1135. used.insert(bid);
  1136. std::set<int> reqs;
  1137. for(std::set<int>::iterator i=CGI->townh->requirements[tid][bid].begin();
  1138. i!=CGI->townh->requirements[tid][bid].end();
  1139. i++
  1140. )
  1141. {
  1142. if (LOCPLINT->castleInt->town->builtBuildings.find(*i) == LOCPLINT->castleInt->town->builtBuildings.end())
  1143. reqs.insert(*i);
  1144. }
  1145. while(true)
  1146. {
  1147. size_t czystych=0;
  1148. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1149. {
  1150. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  1151. {
  1152. used.insert(*i);
  1153. for(
  1154. std::set<int>::iterator j=CGI->townh->requirements[tid][*i].begin();
  1155. j!=CGI->townh->requirements[tid][*i].end();
  1156. j++
  1157. )
  1158. {
  1159. if(LOCPLINT->castleInt->town->builtBuildings.find(*j) == //this building is not built
  1160. LOCPLINT->castleInt->town->builtBuildings.end())
  1161. reqs.insert(*j);
  1162. }
  1163. }
  1164. else
  1165. {
  1166. czystych++;
  1167. }
  1168. }
  1169. if(czystych==reqs.size())
  1170. break;
  1171. }
  1172. bool first=true;
  1173. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  1174. {
  1175. ret+=(((first)?(" "):(", ")) + CGI->buildh->buildings[tid][*i]->Name());
  1176. first = false;
  1177. }
  1178. }
  1179. }
  1180. return ret;
  1181. }
  1182. CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
  1183. :tid(Tid), bid(Bid), state(State), mode(Mode)
  1184. {
  1185. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPUBUILD.bmp");
  1186. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1187. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  1188. SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
  1189. SDL_FreeSurface(hhlp);
  1190. pos.x = screen->w/2 - bitmap->w/2;
  1191. pos.y = screen->h/2 - bitmap->h/2;
  1192. blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
  1193. std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->Name());
  1194. CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->Description(),197,168,GEOR16,40,zwykly,bitmap);
  1195. CSDL_Ext::printAtMiddleWB(getTextForState(state),197,248,GEOR13,50,zwykly,bitmap);
  1196. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->generaltexth->hcommands[7],pom),197,30,GEOR16,tytulowy,bitmap);
  1197. int resamount=0;
  1198. for(int i=0;i<7;i++)
  1199. {
  1200. if(CGI->buildh->buildings[tid][bid]->resources[i])
  1201. {
  1202. resamount++;
  1203. }
  1204. }
  1205. int ah = (resamount>4) ? 304 : 341;
  1206. int cn=-1, it=0;
  1207. int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
  1208. row2w = (resamount-4) * 32 + (resamount-5) * 45;
  1209. char buf[15];
  1210. while(++cn<7)
  1211. {
  1212. if(!CGI->buildh->buildings[tid][bid]->resources[cn])
  1213. continue;
  1214. SDL_itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
  1215. if(it<4)
  1216. {
  1217. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+42,GEOR16,zwykly,bitmap);
  1218. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
  1219. }
  1220. else
  1221. {
  1222. CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+42,GEOR16,zwykly,bitmap);
  1223. blitAt(graphics->resources32->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
  1224. }
  1225. if(it==4)
  1226. ah+=75;
  1227. }
  1228. if(!mode)
  1229. {
  1230. buy = new AdventureMapButton
  1231. ("","",boost::bind(&CBuildWindow::Buy,this),pos.x+45,pos.y+446,"IBUY30.DEF",SDLK_RETURN);
  1232. cancel = new AdventureMapButton
  1233. ("","",boost::bind(&CBuildWindow::close,this),pos.x+290,pos.y+445,"ICANCEL.DEF",SDLK_ESCAPE);
  1234. if(state!=7)
  1235. buy->state=2;
  1236. }
  1237. }
  1238. CHallInterface::CBuildWindow::~CBuildWindow()
  1239. {
  1240. SDL_FreeSurface(bitmap);
  1241. if(!mode)
  1242. {
  1243. delete buy;
  1244. delete cancel;
  1245. }
  1246. }
  1247. CFortScreen::~CFortScreen()
  1248. {
  1249. for(size_t i=0;i<crePics.size();i++)
  1250. delete crePics[i];
  1251. for (size_t i=0;i<recAreas.size();i++)
  1252. delete recAreas[i];
  1253. SDL_FreeSurface(bg);
  1254. delete exit;
  1255. delete resdatabar;
  1256. }
  1257. void CFortScreen::show( SDL_Surface * to)
  1258. {
  1259. blitAt(bg,pos,to);
  1260. static unsigned char anim = 1;
  1261. for (int i=0; i<CREATURES_PER_TOWN; i++)
  1262. {
  1263. crePics[i]->blitPic(to,pos.x+positions[i].x+159,pos.y+positions[i].y+4,!(anim%4));
  1264. }
  1265. anim++;
  1266. exit->show(to);
  1267. resdatabar->show(to);
  1268. LOCPLINT->statusbar->show(to);
  1269. }
  1270. void CFortScreen::activate()
  1271. {
  1272. LOCPLINT->statusbar = LOCPLINT->castleInt->statusbar;
  1273. exit->activate();
  1274. for (size_t i=0;i<recAreas.size(); ++i)
  1275. {
  1276. recAreas[i]->activate();
  1277. }
  1278. }
  1279. void CFortScreen::deactivate()
  1280. {
  1281. exit->deactivate();
  1282. for (size_t i=0;i<recAreas.size();i++)
  1283. {
  1284. recAreas[i]->deactivate();
  1285. }
  1286. }
  1287. void CFortScreen::close()
  1288. {
  1289. LOCPLINT->popIntTotally(this);
  1290. }
  1291. CFortScreen::CFortScreen( CCastleInterface * owner )
  1292. {
  1293. resdatabar = new CMinorResDataBar;
  1294. pos = owner->pos;
  1295. bg = NULL;
  1296. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CFortScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1297. positions += genRect(126,386,10,22),genRect(126,386,404,22),
  1298. genRect(126,386,10,155),genRect(126,386,404,155),
  1299. genRect(126,386,10,288),genRect(126,386,404,288),
  1300. genRect(126,386,206,421);
  1301. draw(owner,true);
  1302. resdatabar->pos += pos;
  1303. }
  1304. void CFortScreen::draw( CCastleInterface * owner, bool first)
  1305. {
  1306. if(bg)
  1307. SDL_FreeSurface(bg);
  1308. char buf[20];
  1309. memset(buf,0,20);
  1310. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPCASTL7.bmp"),
  1311. *icons = BitmapHandler::loadBitmap("ZPCAINFO.bmp");
  1312. SDL_SetColorKey(icons,SDL_SRCCOLORKEY,SDL_MapRGB(icons->format,0,255,255));
  1313. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1314. bg = SDL_ConvertSurface(bg2,screen->format,0);
  1315. SDL_FreeSurface(bg2);
  1316. printAtMiddle(CGI->buildh->buildings[owner->town->subID][owner->town->fortLevel()+6]->Name(),400,13,GEORXX,zwykly,bg);
  1317. for(int i=0;i<CREATURES_PER_TOWN; i++)
  1318. {
  1319. bool upgraded = owner->town->creatureDwelling(i,true);
  1320. bool present = owner->town->creatureDwelling(i,false);
  1321. CCreature *c = &CGI->creh->creatures[upgraded ? owner->town->town->upgradedCreatures[i] : owner->town->town->basicCreatures[i]];
  1322. printAtMiddle(c->namePl,positions[i].x+79,positions[i].y+10,GEOR13,zwykly,bg); //cr. name
  1323. blitAt(owner->bicons->ourImages[30+i+upgraded*7].bitmap,positions[i].x+4,positions[i].y+21,bg); //dwelling pic
  1324. printAtMiddle(CGI->buildh->buildings[owner->town->subID][30+i+upgraded*7]->Name(),positions[i].x+79,positions[i].y+100,GEOR13,zwykly,bg); //dwelling name
  1325. if(present) //if creature is present print avail able quantity
  1326. {
  1327. SDL_itoa(owner->town->strInfo.creatures.find(i)->second,buf,10);
  1328. printAtMiddle(CGI->generaltexth->allTexts[217] + buf,positions[i].x+79,positions[i].y+118,GEOR13,zwykly,bg);
  1329. }
  1330. blitAt(icons,positions[i].x+261,positions[i].y+3,bg);
  1331. //atttack
  1332. printAt(CGI->generaltexth->allTexts[190],positions[i].x+288,positions[i].y+5,GEOR13,zwykly,bg);
  1333. SDL_itoa(c->attack,buf,10);
  1334. printToWR(buf,positions[i].x+381,positions[i].y+18,GEOR13,zwykly,bg);
  1335. //defense
  1336. printAt(CGI->generaltexth->allTexts[191],positions[i].x+288,positions[i].y+25,GEOR13,zwykly,bg);
  1337. SDL_itoa(c->defence,buf,10);
  1338. printToWR(buf,positions[i].x+381,positions[i].y+38,GEOR13,zwykly,bg);
  1339. //damage
  1340. printAt(CGI->generaltexth->allTexts[199],positions[i].x+288,positions[i].y+46,GEOR13,zwykly,bg);
  1341. SDL_itoa(c->damageMin,buf,10);
  1342. int hlp;
  1343. if(c->damageMin > 0)
  1344. hlp = log10f(c->damageMin)+2;
  1345. else
  1346. hlp = 2;
  1347. buf[hlp-1]=' '; buf[hlp]='-'; buf[hlp+1]=' ';
  1348. SDL_itoa(c->damageMax,buf+hlp+2,10);
  1349. printToWR(buf,positions[i].x+381,positions[i].y+59,GEOR13,zwykly,bg);
  1350. //health
  1351. printAt(CGI->generaltexth->zelp[439].first,positions[i].x+288,positions[i].y+66,GEOR13,zwykly,bg);
  1352. SDL_itoa(c->hitPoints,buf,10);
  1353. printToWR(buf,positions[i].x+381,positions[i].y+79,GEOR13,zwykly,bg);
  1354. //speed
  1355. printAt(CGI->generaltexth->zelp[441].first,positions[i].x+288,positions[i].y+87,GEOR13,zwykly,bg);
  1356. SDL_itoa(c->speed,buf,10);
  1357. printToWR(buf,positions[i].x+381,positions[i].y+100,GEOR13,zwykly,bg);
  1358. if(present)//growth
  1359. {
  1360. printAt(CGI->generaltexth->allTexts[194],positions[i].x+288,positions[i].y+107,GEOR13,zwykly,bg);
  1361. SDL_itoa(owner->town->creatureGrowth(i),buf,10);
  1362. printToWR(buf,positions[i].x+381,positions[i].y+120,GEOR13,zwykly,bg);
  1363. }
  1364. if(first)
  1365. {
  1366. crePics.push_back(new CCreaturePic(c,false));
  1367. if(present)
  1368. {
  1369. recAreas.push_back(new RecArea(30+i+upgraded*7));
  1370. recAreas.back()->pos = positions[i] + pos;
  1371. }
  1372. }
  1373. }
  1374. SDL_FreeSurface(icons);
  1375. }
  1376. void CFortScreen::RecArea::clickLeft (tribool down)
  1377. {
  1378. if(!down && pressedL)
  1379. {
  1380. LOCPLINT->castleInt->showRecruitmentWindow(bid);
  1381. }
  1382. ClickableL::clickLeft(down);
  1383. }
  1384. void CFortScreen::RecArea::activate()
  1385. {
  1386. ClickableL::activate();
  1387. ClickableR::activate();
  1388. }
  1389. void CFortScreen::RecArea::deactivate()
  1390. {
  1391. ClickableL::deactivate();
  1392. ClickableR::deactivate();
  1393. }
  1394. void CFortScreen::RecArea::clickRight( tribool down )
  1395. {
  1396. clickLeft(down);; //r-click does same as l-click - opens recr. window
  1397. }
  1398. CMageGuildScreen::CMageGuildScreen(CCastleInterface * owner)
  1399. {
  1400. resdatabar = new CMinorResDataBar;
  1401. pos = owner->pos;
  1402. resdatabar->pos.x += pos.x;
  1403. resdatabar->pos.y += pos.y;
  1404. bg = BitmapHandler::loadBitmap("TPMAGE.bmp");
  1405. exit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CMageGuildScreen::close,this),pos.x+748,pos.y+556,"TPMAGE1.DEF",SDLK_RETURN);
  1406. exit->assignedKeys.insert(SDLK_ESCAPE);
  1407. scrolls2 = CDefHandler::giveDefEss("TPMAGES.DEF");
  1408. SDL_Surface *view = BitmapHandler::loadBitmap(graphics->guildBgs[owner->town->subID]);
  1409. SDL_SetColorKey(view,SDL_SRCCOLORKEY,SDL_MapRGB(view->format,0,255,255));
  1410. positions.resize(5);
  1411. 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);
  1412. 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);
  1413. positions[2] += genRect(61,83,570,82), genRect(61,83,672,82), genRect(61,83,570,157), genRect(61,83,672,157);
  1414. positions[3] += genRect(61,83,183,42), genRect(61,83,183,148), genRect(61,83,183,253);
  1415. positions[4] += genRect(61,83,491,325), genRect(61,83,591,325);
  1416. blitAt(view,332,76,bg);
  1417. for(size_t i=0; i<owner->town->town->mageLevel; i++)
  1418. {
  1419. size_t sp = owner->town->spellsAtLevel(i+1,false); //spell at level with -1 hmmm?
  1420. for(size_t j=0; j<sp; j++)
  1421. {
  1422. if(i<owner->town->mageGuildLevel() && owner->town->spells[i].size()>j)
  1423. {
  1424. spells.push_back(Scroll(&CGI->spellh->spells[owner->town->spells[i][j]]));
  1425. spells[spells.size()-1].pos = positions[i][j];
  1426. blitAt(graphics->spellscr->ourImages[owner->town->spells[i][j]].bitmap,positions[i][j],bg);
  1427. }
  1428. else
  1429. {
  1430. blitAt(scrolls2->ourImages[1].bitmap,positions[i][j],bg);
  1431. }
  1432. }
  1433. }
  1434. SDL_FreeSurface(view);
  1435. for(size_t i=0;i<spells.size();i++)
  1436. {
  1437. spells[i].pos.x += pos.x;
  1438. spells[i].pos.y += pos.y;
  1439. }
  1440. delete scrolls2;
  1441. }
  1442. CMageGuildScreen::~CMageGuildScreen()
  1443. {
  1444. delete exit;
  1445. SDL_FreeSurface(bg);
  1446. delete resdatabar;
  1447. }
  1448. void CMageGuildScreen::close()
  1449. {
  1450. LOCPLINT->popIntTotally(this);
  1451. }
  1452. void CMageGuildScreen::show(SDL_Surface * to)
  1453. {
  1454. blitAt(bg,pos,to);
  1455. resdatabar->show(to);
  1456. LOCPLINT->statusbar->show(to);
  1457. exit->show(to);
  1458. }
  1459. void CMageGuildScreen::activate()
  1460. {
  1461. exit->activate();
  1462. for(size_t i=0;i<spells.size();i++)
  1463. {
  1464. spells[i].activate();
  1465. }
  1466. }
  1467. void CMageGuildScreen::deactivate()
  1468. {
  1469. exit->deactivate();
  1470. for(size_t i=0;i<spells.size();i++)
  1471. {
  1472. spells[i].deactivate();
  1473. }
  1474. }
  1475. void CMageGuildScreen::Scroll::clickLeft (tribool down)
  1476. {
  1477. if(down)
  1478. {
  1479. std::vector<SComponent*> comps(1,
  1480. new CCustomImgComponent(SComponent::spell,spell->id,0,graphics->spellscr->ourImages[spell->id].bitmap,false)
  1481. );
  1482. LOCPLINT->showInfoDialog(spell->descriptions[0],comps, soundBase::sound_todo);
  1483. }
  1484. }
  1485. void CMageGuildScreen::Scroll::clickRight (tribool down)
  1486. {
  1487. if(down)
  1488. {
  1489. CInfoPopup *vinya = new CInfoPopup();
  1490. vinya->free = true;
  1491. vinya->bitmap = CMessage::drawBoxTextBitmapSub
  1492. (LOCPLINT->playerID,
  1493. spell->descriptions[0],graphics->spellscr->ourImages[spell->id].bitmap,
  1494. spell->name,30,30);
  1495. vinya->pos.x = screen->w/2 - vinya->bitmap->w/2;
  1496. vinya->pos.y = screen->h/2 - vinya->bitmap->h/2;
  1497. LOCPLINT->pushInt(vinya);
  1498. }
  1499. }
  1500. void CMageGuildScreen::Scroll::hover(bool on)
  1501. {
  1502. Hoverable::hover(on);
  1503. if(on)
  1504. LOCPLINT->statusbar->print(spell->name);
  1505. else
  1506. LOCPLINT->statusbar->clear();
  1507. }
  1508. void CMageGuildScreen::Scroll::activate()
  1509. {
  1510. ClickableL::activate();
  1511. ClickableR::activate();
  1512. Hoverable::activate();
  1513. }
  1514. void CMageGuildScreen::Scroll::deactivate()
  1515. {
  1516. ClickableL::deactivate();
  1517. ClickableR::deactivate();
  1518. Hoverable::deactivate();
  1519. }
  1520. CBlacksmithDialog::CBlacksmithDialog(bool possible, int creMachineID, int aid, int hid)
  1521. {
  1522. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPSMITH.bmp");
  1523. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  1524. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  1525. bmp = SDL_ConvertSurface(bg2,screen->format,0);
  1526. SDL_FreeSurface(bg2);
  1527. bg2 = BitmapHandler::loadBitmap("TPSMITBK.bmp");
  1528. blitAt(bg2,64,50,bmp);
  1529. SDL_FreeSurface(bg2);
  1530. CCreatureAnimation cra(CGI->creh->creatures[creMachineID].animDefName);
  1531. cra.nextFrameMiddle(bmp,170,120,true,0,false);
  1532. char pom[75];
  1533. sprintf(pom,CGI->generaltexth->allTexts[274].c_str(),CGI->creh->creatures[creMachineID].nameSing.c_str()); //build a new ...
  1534. printAtMiddle(pom,165,28,GEORXX,tytulowy,bmp);
  1535. printAtMiddle(CGI->generaltexth->jktexts[43],165,218,GEOR16,zwykly,bmp); //resource cost
  1536. SDL_itoa(CGI->arth->artifacts[aid].price,pom,10);
  1537. printAtMiddle(pom,165,290,GEOR13,zwykly,bmp);
  1538. pos.w = bmp->w;
  1539. pos.h = bmp->h;
  1540. pos.x = screen->w/2 - pos.w/2;
  1541. pos.y = screen->h/2 - pos.h/2;
  1542. buy = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 42,pos.y + 312,"IBUY30.DEF",SDLK_RETURN);
  1543. cancel = new AdventureMapButton("","",boost::bind(&CBlacksmithDialog::close,this),pos.x + 224,pos.y + 312,"ICANCEL.DEF",SDLK_ESCAPE);
  1544. if(possible)
  1545. buy->callback += boost::bind(&CCallback::buyArtifact,LOCPLINT->cb,LOCPLINT->cb->getHeroInfo(hid,2),aid);
  1546. else
  1547. buy->bitmapOffset = 2;
  1548. blitAt(graphics->resources32->ourImages[6].bitmap,148,244,bmp);
  1549. }
  1550. void CBlacksmithDialog::show( SDL_Surface * to )
  1551. {
  1552. blitAt(bmp,pos,to);
  1553. buy->show(to);
  1554. cancel->show(to);
  1555. }
  1556. void CBlacksmithDialog::activate()
  1557. {
  1558. if(!buy->bitmapOffset)
  1559. buy->activate();
  1560. cancel->activate();
  1561. }
  1562. void CBlacksmithDialog::deactivate()
  1563. {
  1564. if(!buy->bitmapOffset)
  1565. buy->deactivate();
  1566. cancel->deactivate();
  1567. }
  1568. CBlacksmithDialog::~CBlacksmithDialog()
  1569. {
  1570. SDL_FreeSurface(bmp);
  1571. delete cancel;
  1572. delete buy;
  1573. }
  1574. void CBlacksmithDialog::close()
  1575. {
  1576. LOCPLINT->popIntTotally(this);
  1577. }