CCastleInterface.cpp 47 KB

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