CCastleInterface.cpp 45 KB

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