CCastleInterface.cpp 42 KB

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