CKingdomInterface.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. #include "CKingdomInterface.h"
  2. #include "AdventureMapButton.h"
  3. #include "CAdvmapInterface.h"
  4. #include "../CCallback.h"
  5. #include "../global.h"
  6. #include "CConfigHandler.h"
  7. #include "CGameInfo.h"
  8. #include "CHeroWindow.h"
  9. #include "CSpellWindow.h"
  10. #include "CMessage.h"
  11. #include "SDL_Extensions.h"
  12. #include "Graphics.h"
  13. #include "../hch/CArtHandler.h"
  14. #include "../hch/CBuildingHandler.h"
  15. #include "../hch/CDefHandler.h"
  16. #include "../hch/CHeroHandler.h"
  17. #include "../hch/CGeneralTextHandler.h"
  18. #include "../hch/CObjectHandler.h"
  19. #include "../hch/CTownHandler.h"
  20. #include "../lib/map.h"
  21. #include "../lib/NetPacks.h"
  22. #include <boost/algorithm/string/replace.hpp>
  23. #include <boost/assign/std/vector.hpp>
  24. #include <boost/assign/list_of.hpp>
  25. #include <boost/lexical_cast.hpp>
  26. #include <boost/format.hpp>
  27. #include <sstream>
  28. using namespace boost::assign;
  29. using namespace CSDL_Ext;
  30. /*
  31. * CKingdomInterface.cpp, part of VCMI engine
  32. *
  33. * Authors: listed in file AUTHORS in main folder
  34. *
  35. * License: GNU General Public License v2.0 or later
  36. * Full text of license available in license.txt file, in main folder
  37. *
  38. */
  39. #define ADVOPT (conf.go()->ac)
  40. CDefEssential* CKingdomInterface::slots;
  41. CDefEssential* CKingdomInterface::fort;
  42. CDefEssential* CKingdomInterface::hall;
  43. CKingdomInterface::CKingdomInterface()
  44. {
  45. OBJ_CONSTRUCTION_CAPTURING_ALL;
  46. defActions = SHARE_POS | DISPOSE;
  47. PicCount = ADVOPT.overviewPics;
  48. size = ADVOPT.overviewSize;
  49. pos.x = screen->w/2 - 400;
  50. pos.y = screen->h/2 - (68+58*size);
  51. showHarrisoned = false;//set to true if you want to see garrisoned heroes
  52. heroPos = townPos = objPos = state = 0;
  53. bg = BitmapHandler::loadBitmap(ADVOPT.overviewBg);
  54. graphics->blueToPlayersAdv(bg, LOCPLINT->playerID);
  55. mines = CDefHandler::giveDefEss("OVMINES.DEF");
  56. title = CDefHandler::giveDefEss("OVTITLE.DEF");
  57. hall = CDefHandler::giveDefEss("ITMTL.DEF");
  58. fort = CDefHandler::giveDefEss("ITMCL.DEF");
  59. objPics = CDefHandler::giveDefEss("FLAGPORT.DEF");
  60. slots = CDefHandler::giveDefEss("OVSLOT.DEF");
  61. toHeroes = new AdventureMapButton (CGI->generaltexth->overview[11],"",
  62. boost::bind(&CKingdomInterface::listToHeroes,this),748,28+size*116,"OVBUTN1.DEF");
  63. toHeroes->block(2);
  64. toTowns = new AdventureMapButton (CGI->generaltexth->overview[12],"",
  65. boost::bind(&CKingdomInterface::listToTowns,this),748,64+size*116,"OVBUTN6.DEF");
  66. toTowns->block(0);
  67. exit = new AdventureMapButton (CGI->generaltexth->allTexts[600],"",
  68. boost::bind(&CKingdomInterface::close,this),748,99+size*116,"OVBUTN1.DEF");
  69. exit->bitmapOffset = 3;
  70. statusbar = new CStatusBar(pos.x+7,pos.y+91+size*116,"TSTATBAR.bmp",732);
  71. resdatabar = new CResDataBar("KRESBAR.bmp",pos.x+3,pos.y+111+size*116,32,2,76,76);
  72. for(size_t i=0;i<size;i++)//creating empty hero/town lists for input
  73. {
  74. heroes.push_back( new CHeroItem(i,this));
  75. towns.push_back( new CTownItem(i,this));
  76. }
  77. slider = new CSlider(4, 4, size*116+19, boost::bind (&CKingdomInterface::sliderMoved, this, _1),
  78. size, LOCPLINT->cb->howManyHeroes(showHarrisoned), 0, false, 0);
  79. //creating objects list
  80. ObjTop = new AdventureMapButton ("","", boost::bind(&CKingdomInterface::moveObjectList,this,0),
  81. 733,4,"OVBUTN4.DEF");
  82. ObjUp = new AdventureMapButton ("","", boost::bind(&CKingdomInterface::moveObjectList,this,1),
  83. 733,24,"OVBUTN4.DEF");
  84. ObjUp->bitmapOffset = 4;
  85. ObjDown = new AdventureMapButton ("","", boost::bind(&CKingdomInterface::moveObjectList,this,2),
  86. 733,size*116-18,"OVBUTN4.DEF");
  87. ObjDown->bitmapOffset = 6;
  88. ObjBottom = new AdventureMapButton ("","", boost::bind(&CKingdomInterface::moveObjectList,this,3),
  89. 733,size*116+2,"OVBUTN4.DEF");
  90. ObjBottom->bitmapOffset = 2;
  91. for (size_t i=0; i<SKILL_PER_HERO; i++)
  92. {
  93. incomes.push_back(new HoverableArea());//bottom panel with mines
  94. incomes[i]->pos = genRect(54,68,pos.x+20+i*80,pos.y+31+size*116);
  95. incomes[i]->hoverText = CGI->generaltexth->mines[i].first;
  96. }
  97. incomes[7]->pos.w = 136;
  98. incomes[7]->hoverText = CGI->generaltexth->allTexts[255];
  99. incomesVal+=0,0,0,0,0,0,0,0;//for mines images
  100. std::map<std::pair<int,int>,int> addObjects;//objects to print, except 17th dwelling
  101. //format: (id,subID),image index
  102. #define INSERT_MAP addObjects.insert(std::pair<std::pair<int,int>,int>(std::pair<int,int>
  103. INSERT_MAP (20,1) ,81));//Golem factory
  104. INSERT_MAP (42,0) ,82));//Lighthouse
  105. INSERT_MAP (33,0) ,83));//Garrison
  106. INSERT_MAP (219,0),83));//Garrison
  107. INSERT_MAP (33,1) ,84));//Anti-magic Garrison
  108. INSERT_MAP (219,1),84));//Anti-magic Garrison
  109. INSERT_MAP (53,7) ,85));//Abandoned mine
  110. INSERT_MAP (20,0) ,86));//Conflux
  111. INSERT_MAP (87,0) ,87));//Harbor
  112. #undef INSERT_MAP
  113. for(size_t i = 0; i<CGI->state->map->objects.size(); i++)
  114. {
  115. CGObjectInstance* obj = CGI->state->map->objects[i];//current object
  116. if (obj)
  117. {
  118. if (obj->tempOwner == CGI->state->currentPlayer)//if object is our
  119. {
  120. std::pair<int,int > curElm = std::pair<int,int >(obj->ID, obj->subID);
  121. if ( obj->ID == 17 )//dwelling, text is a plural name of a creature
  122. {
  123. objList[obj->subID].first += 1;
  124. objList[obj->subID].second = & CGI->creh->creatures[CGI->objh->cregens[obj->subID]].namePl;
  125. }
  126. else if (addObjects.find(curElm) != addObjects.end())
  127. {//object from addObjects map, text is name of the object
  128. objList[addObjects[curElm]].first += 1;
  129. objList[addObjects[curElm]].second = & obj->hoverName;
  130. }
  131. else if ( obj->ID == 53 )//TODO: abandoned mines
  132. incomesVal[obj->subID]+=1;
  133. }
  134. }
  135. }
  136. addObjects.clear();
  137. objSize = (size*116-64)/57; //in object list will fit (height of panel)/(height of one element) items
  138. ObjList.resize(objSize);
  139. for(size_t i=0;i<objSize;i++)
  140. {
  141. ObjList[i] = new HoverableArea();
  142. ObjList[i]->pos = genRect(50,50,pos.x+740,pos.y+44+i*57);
  143. }
  144. incomesVal[7] = incomesVal[6]*1000;//gold mines -> total income
  145. std::vector<const CGHeroInstance*> heroes = LOCPLINT->cb->getHeroesInfo(true);
  146. for(size_t i=0; i<heroes.size();i++)
  147. switch(heroes[i]->getSecSkillLevel(13))//some heroes may have estates
  148. {
  149. case 1: //basic
  150. incomesVal[7] += 125;
  151. break;
  152. case 2: //advanced
  153. incomesVal[7] += 250;
  154. break;
  155. case 3: //expert
  156. incomesVal[7] += 500;
  157. break;
  158. }
  159. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  160. for(size_t i=0; i<towns.size();i++)
  161. incomesVal[7] += towns[i]->dailyIncome();
  162. }
  163. void CKingdomInterface::moveObjectList(int newPos)
  164. {
  165. int top = objList.size() > objSize ? 0 : objList.size() - objSize ;
  166. int bottom = objList.size() > objSize ? objList.size() - objSize : 0 ;
  167. switch (newPos)//checking what button was pressed
  168. {
  169. /* Top */ case 0: objPos = top;
  170. break;
  171. /* Up */ case 1: objPos = objPos==top?top:(objPos-1);
  172. break;
  173. /* Down */ case 2: objPos = objPos==bottom?bottom:(objPos+1);
  174. break;
  175. /*Bottom*/ case 3: objPos = bottom;
  176. break;
  177. }
  178. GH.totalRedraw();
  179. }
  180. CKingdomInterface::~CKingdomInterface()
  181. {
  182. SDL_FreeSurface(bg);
  183. delete title;//deleting .def
  184. delete slots;
  185. delete fort;
  186. delete hall;
  187. delete objPics;
  188. delete mines;
  189. towns.clear();//deleting lists
  190. heroes.clear();
  191. incomes.clear();
  192. ObjList.clear();
  193. objList.clear();
  194. }
  195. void CKingdomInterface::close()
  196. {
  197. GH.popIntTotally(this);
  198. }
  199. void CKingdomInterface::showAll( SDL_Surface * to/*=NULL*/)
  200. {
  201. LOCPLINT->adventureInt->resdatabar.draw(to);
  202. blitAt(bg,pos,to);
  203. resdatabar->draw(to);
  204. toTowns->show(to);
  205. toHeroes->show(to);
  206. exit->show(to);
  207. ObjTop->show(to);
  208. ObjUp->show(to);
  209. ObjDown->show(to);
  210. ObjBottom->show(to);
  211. for (size_t i=0; i<ObjList.size(); i++)//list may be moved, recreate hover text
  212. ObjList[i]->hoverText = "";
  213. int skipCount=0, curPos=objPos<0?(-objPos):0;
  214. for (std::map<int,std::pair<int, std::string*> >::iterator it=objList.begin(); it!= objList.end(); it++)
  215. {
  216. if (skipCount<objPos)//we will show only objects from objPos
  217. {
  218. skipCount++;
  219. continue;
  220. }
  221. blitAt(objPics->ourImages[(*it).first].bitmap,pos.x+740,pos.y+44+curPos*57,to);
  222. std::ostringstream ostrs;//objects count
  223. ostrs << (*it).second.first;
  224. CSDL_Ext::printTo(ostrs.str(),pos.x+790,pos.y+94+curPos*57,GEOR13,zwykly,to);
  225. ObjList[curPos]->hoverText = * (*it).second.second;
  226. curPos++;
  227. if (curPos == objSize)
  228. break;
  229. }
  230. if (state == 1)
  231. {//printing text "Town", "Harrisoned hero", "Visiting hero"
  232. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[3],pos.x+145,pos.y+12,TNRB16,zwykly,to);
  233. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[4],pos.x+370,pos.y+12,TNRB16,zwykly,to);
  234. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[5],pos.x+600,pos.y+12,TNRB16,zwykly,to);
  235. for (size_t i=0; i<size; i++)
  236. towns[i]->showAll(to);//show town list
  237. }
  238. else
  239. {//text "Hero/stats" and "Skills"
  240. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[0],pos.x+150,pos.y+12,TNRB16,zwykly,to);
  241. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[1],pos.x+500,pos.y+12,TNRB16,zwykly,to);
  242. for (size_t i=0; i<size; i++)
  243. heroes[i]->showAll(to);//show hero list
  244. }
  245. for (int i = 0; i<7; i++)
  246. blitAt(mines->ourImages[i].bitmap,pos.x + 20 + i*80,pos.y + 31+size*116,to);
  247. for(size_t i=0;i<incomes.size();i++)
  248. {
  249. std::ostringstream oss;
  250. oss << incomesVal[i];
  251. CSDL_Ext::printAtMiddle(oss.str(),incomes[i]->pos.x+incomes[i]->pos.w/2,incomes[i]->pos.y+50,GEOR13,zwykly,to);
  252. }
  253. slider->showAll(to);
  254. if(screen->w != 800 || screen->h !=600)
  255. CMessage::drawBorder(LOCPLINT->playerID,to,828,136+116*size+29,pos.x-14,pos.y-15);
  256. show(to);
  257. }
  258. void CKingdomInterface::show(SDL_Surface * to)
  259. {
  260. statusbar->show(to);
  261. }
  262. void CKingdomInterface::activate()
  263. {
  264. LOCPLINT->statusbar = statusbar;
  265. exit->activate();
  266. toTowns->activate();
  267. toHeroes->activate();
  268. ObjTop->activate();
  269. ObjUp->activate();
  270. ObjDown->activate();
  271. ObjBottom->activate();
  272. for (size_t i=0; i<ObjList.size(); i++)
  273. ObjList[i]->activate();
  274. for (size_t i=0; i<incomes.size(); i++)
  275. incomes[i]->activate();
  276. if (state == 1)
  277. for (size_t i=0; i<size; i++)
  278. towns[i]->activate();
  279. else
  280. for (size_t i=0; i<size; i++)
  281. heroes[i]->activate();
  282. slider->activate();
  283. }
  284. void CKingdomInterface::deactivate()
  285. {
  286. exit->deactivate();
  287. toTowns->deactivate();
  288. toHeroes->deactivate();
  289. ObjTop->deactivate();
  290. ObjUp->deactivate();
  291. ObjDown->deactivate();
  292. ObjBottom->deactivate();
  293. for (size_t i=0; i<ObjList.size(); i++)
  294. ObjList[i]->deactivate();
  295. for (size_t i=0; i<incomes.size(); i++)
  296. incomes[i]->deactivate();
  297. if (state == 1)
  298. for (size_t i=0; i<size; i++)
  299. towns[i]->deactivate();
  300. else
  301. for (size_t i=0; i<size; i++)
  302. heroes[i]->deactivate();
  303. slider->deactivate();
  304. }
  305. void CKingdomInterface::keyPressed(const SDL_KeyboardEvent & key)
  306. {
  307. }
  308. void CKingdomInterface::recreateHeroList(int pos)
  309. {
  310. std::vector<const CGHeroInstance*> Heroes = LOCPLINT->cb->getHeroesInfo(true);
  311. int i=0, cnt=0;
  312. for (size_t j = 0; ((j<Heroes.size()) && (i<size));j++)
  313. {
  314. if (Heroes[j]->inTownGarrison && (!showHarrisoned))//if hero in garrison and we don't show them
  315. continue;
  316. if (cnt<pos)//skipping heroes
  317. {
  318. cnt++;
  319. continue;
  320. }//this hero will be added
  321. heroes[i]->setHero(Heroes[j]);
  322. i++;
  323. }
  324. for (i;i<size;i++)//if we still have empty pieces
  325. heroes[i]->setHero(NULL);//empty pic
  326. GH.totalRedraw();
  327. }
  328. void CKingdomInterface::recreateTownList(int pos)
  329. {
  330. std::vector<const CGTownInstance*> Towns = LOCPLINT->cb->getTownsInfo(true);
  331. for(size_t i=0;i<size;i++)
  332. {
  333. if (i+pos<Towns.size())
  334. towns[i]->setTown(Towns[i+pos]);//replace town
  335. else
  336. towns[i]->setTown(NULL);//only empty pic
  337. }
  338. GH.totalRedraw();
  339. }
  340. void CKingdomInterface::listToTowns()
  341. {
  342. state = 1;
  343. toHeroes->block(0);
  344. toTowns->block(2);
  345. heroPos = slider->value;
  346. slider->setAmount(LOCPLINT->cb->howManyTowns());
  347. slider->value=townPos;//moving slider
  348. recreateTownList(townPos);
  349. for (size_t i=0;i<size;i++)
  350. {
  351. heroes[i]->deactivate();
  352. towns[i]->activate();
  353. }
  354. }
  355. void CKingdomInterface::listToHeroes()
  356. {
  357. state = 2;
  358. toHeroes->block(2);
  359. toTowns->block(0);
  360. townPos = slider->value;
  361. slider->setAmount(LOCPLINT->cb->howManyHeroes(showHarrisoned));
  362. slider->value=heroPos;//moving slider
  363. recreateHeroList(heroPos);
  364. for (size_t i=0;i<size;i++)
  365. {
  366. towns[i]->deactivate();
  367. heroes[i]->activate();
  368. }
  369. }
  370. void CKingdomInterface::sliderMoved(int newpos)
  371. {
  372. if (state == 0)
  373. {
  374. townPos = newpos;
  375. recreateHeroList(newpos);
  376. state = 2;
  377. }
  378. else if ( state == 1 )//towns
  379. {
  380. for (size_t i=0; i<size; i++)
  381. towns[i]->deactivate();
  382. townPos = newpos;
  383. recreateTownList(newpos);
  384. for (size_t i=0; i<size; i++)
  385. towns[i]->activate();
  386. }
  387. else//heroes
  388. {
  389. for (size_t i=0; i<size; i++)
  390. heroes[i]->deactivate();
  391. heroPos = newpos;
  392. recreateHeroList(newpos);
  393. for (size_t i=0; i<size; i++)
  394. heroes[i]->activate();
  395. }
  396. }
  397. CKingdomInterface::CTownItem::CTownItem(int num, CKingdomInterface * Owner)
  398. {
  399. recActions = DISPOSE | SHARE_POS;
  400. owner = Owner;
  401. numb = num;
  402. pos.x += 23;
  403. pos.y += 25+num*116;
  404. pos.w = 702;
  405. pos.h = 114;
  406. town = NULL;
  407. garr = NULL;
  408. garrHero = new HoverableArea();
  409. garrHero->pos = genRect(64, 58, pos.x+244, pos.y + 6);
  410. visitHero = new HoverableArea();
  411. visitHero->pos = genRect(64, 58, pos.x+476, pos.y + 6);
  412. for (int i=0; i<CREATURES_PER_TOWN;i++)
  413. {//creatures info
  414. creaGrowth.push_back(new HoverableArea());
  415. creaGrowth[i]->pos = genRect(32, 32, pos.x+56+i*37, pos.y + 78);
  416. creaCount.push_back(new CCreaPlace());
  417. creaCount[i]->pos = genRect(32, 32, pos.x+409+i*37, pos.y + 78);
  418. creaCount[i]->type = i;
  419. }
  420. hallArea = new HoverableArea();
  421. hallArea->pos = genRect(38, 38, pos.x+69, pos.y + 31);
  422. fortArea = new HoverableArea();
  423. fortArea->pos = genRect(38, 38, pos.x+111, pos.y + 31);
  424. townImage = new HoverableArea();
  425. townImage->pos = genRect(64, 58, pos.x+5, pos.y + 6);
  426. incomeArea = new HoverableArea();
  427. incomeArea->pos = genRect(42, 64, pos.x+154, pos.y + 31);
  428. incomeArea->hoverText = CGI->generaltexth->allTexts[255];
  429. }
  430. CKingdomInterface::CTownItem::~CTownItem()
  431. {
  432. creaGrowth.clear();
  433. creaCount.clear();
  434. delete garr;
  435. }
  436. void CKingdomInterface::CTownItem::setTown(const CGTownInstance * newTown)
  437. {
  438. BLOCK_CAPTURING;
  439. delete garr;
  440. town = newTown;
  441. if (!town)
  442. {
  443. return;
  444. garr = NULL;
  445. }
  446. garr = new CGarrisonInt(pos.x+313,pos.y+3,4,Point(232,0),slots->ourImages[owner->PicCount+2].bitmap,Point(313,2),town,town->visitingHero,false,true, 4,Point(-126,37));
  447. garr->update = false;
  448. /*for (int i=4; i<7; i++)
  449. {
  450. garr->sup[0][i][0].pos.x -= 126;
  451. garr->sup[0][i][0].pos.y += 37;
  452. garr->sdown[0][i][0].pos.x -= 126;
  453. garr->sdown[0][i][0].pos.y += 37;
  454. }*/
  455. for (int i=0; i<CREATURES_PER_TOWN;i++)
  456. {
  457. creaCount[i]->town = NULL;
  458. int crid = -1;
  459. if (!vstd::contains(town->builtBuildings,30+i))
  460. continue;
  461. if (vstd::contains(town->builtBuildings,30+i+CREATURES_PER_TOWN))
  462. crid = town->town->upgradedCreatures[i];
  463. else
  464. crid = town->town->basicCreatures[i];
  465. std::string descr=CGI->generaltexth->allTexts[588];
  466. boost::algorithm::replace_first(descr,"%s",CGI->creh->creatures[crid].namePl);
  467. creaGrowth[i]->hoverText = descr;
  468. descr=CGI->generaltexth->heroscrn[1];
  469. boost::algorithm::replace_first(descr,"%s",CGI->creh->creatures[crid].namePl);
  470. creaCount[i]->hoverText = descr;
  471. creaCount[i]->town = town;
  472. }
  473. townImage->hoverText = town->name;
  474. hallArea->hoverText = CGI->buildh->buildings[town->subID][10+town->hallLevel()]->Name();
  475. if (town->hasFort())
  476. fortArea->hoverText = CGI->buildh->buildings[town->subID][6+town->fortLevel()]->Name();
  477. else
  478. fortArea->hoverText = "";
  479. }
  480. void CKingdomInterface::CTownItem::activate()
  481. {
  482. if (!town)
  483. return;
  484. garr->activate();
  485. hallArea->activate();
  486. fortArea->activate();
  487. incomeArea->activate();
  488. townImage->activate();
  489. if (town->garrisonHero)
  490. garrHero->activate();
  491. if (town->visitingHero)
  492. visitHero->activate();
  493. for (int i=0; i<CREATURES_PER_TOWN;i++)
  494. if (vstd::contains(town->builtBuildings,30+i))
  495. {
  496. creaGrowth[i]->activate();
  497. creaCount [i]->activate();
  498. }
  499. }
  500. void CKingdomInterface::CTownItem::deactivate()
  501. {
  502. if (!town)
  503. return;
  504. garr->deactivate();
  505. hallArea->deactivate();
  506. fortArea->deactivate();
  507. incomeArea->deactivate();
  508. townImage->deactivate();
  509. if (town->garrisonHero)
  510. garrHero->deactivate();
  511. if (town->visitingHero)
  512. visitHero->deactivate();
  513. for (int i=0; i<CREATURES_PER_TOWN;i++)
  514. if (vstd::contains(town->builtBuildings,30+i))
  515. {
  516. creaGrowth[i]->deactivate();
  517. creaCount [i]->deactivate();
  518. }
  519. }
  520. void CKingdomInterface::CTownItem::showAll(SDL_Surface * to)
  521. {
  522. if (!town)
  523. {//if NULL - print background & exit
  524. blitAt(slots->ourImages[numb % owner->PicCount].bitmap,pos.x,pos.y,to);
  525. return;
  526. }//background
  527. blitAt(slots->ourImages[owner->PicCount+2].bitmap,pos.x,pos.y,to);
  528. //town pic/name
  529. int townPic = town->subID*2;
  530. if (!town->hasFort())
  531. townPic += F_NUMBER*2;
  532. if(town->builded >= MAX_BUILDING_PER_TURN)
  533. townPic++;
  534. blitAt(graphics->bigTownPic->ourImages[townPic].bitmap,pos.x+5,pos.y+6,to);
  535. CSDL_Ext::printAt(town->name,pos.x+73,pos.y+7,GEOR13,zwykly,to);
  536. //fort pic
  537. townPic = town->fortLevel()-1;
  538. if (townPic==-1) townPic = 3;
  539. blitAt(fort->ourImages[townPic].bitmap,pos.x+111,pos.y+31,to);
  540. //hall pic
  541. townPic = town->hallLevel();
  542. blitAt(hall->ourImages[townPic].bitmap,pos.x+69,pos.y+31,to);
  543. //income pic
  544. std::ostringstream oss;
  545. oss << town->dailyIncome();
  546. CSDL_Ext::printAtMiddle(oss.str(),pos.x+188,pos.y+60,GEOR13,zwykly,to);
  547. // Creature bonuses/ Creature available texts - need to find text wrapper thingy (have "\n" inside text)
  548. // CSDL_Ext::printAtWR(CGI->generaltexth->allTexts[265],pos.x,pos.y+80,GEOR13,zwykly,to);
  549. // CSDL_Ext::printTo(CGI->generaltexth->allTexts[266],pos.x+350,pos.y+80,GEOR13,zwykly,to);
  550. for (int i=0; i<CREATURES_PER_TOWN;i++)
  551. {//creatures info
  552. int crid = -1;
  553. int bid = 30+i;
  554. if (!vstd::contains(town->builtBuildings,bid))
  555. continue;
  556. if (vstd::contains(town->builtBuildings,bid+CREATURES_PER_TOWN))
  557. {
  558. crid = town->town->upgradedCreatures[i];
  559. bid += CREATURES_PER_TOWN;
  560. }
  561. else
  562. crid = town->town->basicCreatures[i];
  563. //creature growth
  564. blitAt(graphics->smallImgs[crid],pos.x+56+i*37,pos.y+78,to);
  565. std::ostringstream oss;
  566. oss << '+' << town->creatureGrowth(i);
  567. CSDL_Ext::printTo(oss.str(),pos.x+87+i*37,pos.y+110,GEORM,zwykly,to);
  568. //creature available
  569. blitAt(graphics->smallImgs[crid],pos.x+409+i*37,pos.y+78,to);
  570. std::ostringstream ostrs;
  571. ostrs << town->creatures[i].first;
  572. CSDL_Ext::printTo(ostrs.str(),pos.x+440+i*37,pos.y+110,GEORM,zwykly,to);
  573. }
  574. garr->show(to);
  575. if (town->garrisonHero)
  576. blitAt(graphics->portraitLarge[town->garrisonHero->portrait],pos.x+244,pos.y+6,to);
  577. if (town->visitingHero)
  578. blitAt(graphics->portraitLarge[town->visitingHero->portrait],pos.x+476,pos.y+6,to);
  579. }
  580. CKingdomInterface::CHeroItem::CHeroItem(int num, CKingdomInterface * Owner)
  581. {
  582. OBJ_CONSTRUCTION_CAPTURING_ALL;
  583. recActions = DISPOSE | SHARE_POS;
  584. defActions = SHARE_POS;
  585. owner = Owner;
  586. numb = num;
  587. pos.x += 23;
  588. pos.y += 25+num*116;
  589. pos.w = 702;
  590. pos.h = 114;
  591. hero = NULL;
  592. garr = NULL;
  593. artGroup = 0;
  594. backpackPos = 0;
  595. artButtons = new CHighlightableButtonsGroup(0);
  596. for (size_t it = 0; it<3; it++)
  597. {
  598. artButtons->addButton(boost::assign::map_list_of(0,CGI->generaltexth->overview[13+it]),
  599. CGI->generaltexth->overview[8+it], "OVBUTN3.DEF",364+it*112, 46, it);
  600. std::string str = CGI->generaltexth->overview[8+it];//TODO:find function for this if any
  601. str = str.substr(str.find_first_of("{"), str.find_first_of("}")-str.find_first_of("{"));
  602. artButtons->buttons[it]->addTextOverlay(str, FONT_SMALL, tytulowy);
  603. }
  604. artButtons->onChange = boost::bind(&CKingdomInterface::CHeroItem::onArtChange, this, _1);
  605. artButtons->select(0,0);
  606. artLeft = new AdventureMapButton("", "", boost::bind
  607. (&CKingdomInterface::CHeroItem::scrollArts,this,-1), 269, 66, "hsbtns3.def", SDLK_LEFT);
  608. artRight = new AdventureMapButton("", "", boost::bind
  609. (&CKingdomInterface::CHeroItem::scrollArts,this,+1), 675, 66, "hsbtns5.def", SDLK_RIGHT);
  610. portrait = new LRClickableAreaWText();
  611. portrait->pos = genRect(64, 58, pos.x+5, pos.y + 5);
  612. char bufor[400];
  613. for(int i=0; i<PRIMARY_SKILLS; i++)
  614. {
  615. primarySkills.push_back(new LRClickableAreaWTextComp());
  616. primarySkills[i]->pos = genRect(45, 32, pos.x+77 + 36*i, pos.y+26);
  617. primarySkills[i]->text = CGI->generaltexth->arraytxt[2+i];
  618. primarySkills[i]->type = i;
  619. primarySkills[i]->baseType = 0;
  620. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[i].c_str());
  621. primarySkills[i]->hoverText = std::string(bufor);
  622. };
  623. experience = new LRClickableAreaWText();
  624. experience->pos = genRect(33, 49, pos.x+322, pos.y+5);
  625. experience->hoverText = CGI->generaltexth->heroscrn[9];
  626. morale = new LRClickableAreaWTextComp();
  627. morale->pos = genRect(20,32,pos.x+221,pos.y+52);
  628. luck = new LRClickableAreaWTextComp();
  629. luck->pos = genRect(20,32,pos.x+221,pos.y+28);
  630. spellPoints = new LRClickableAreaWText();
  631. spellPoints->pos = genRect(33, 49, pos.x+270, pos.y+5);
  632. spellPoints->hoverText = CGI->generaltexth->heroscrn[22];
  633. speciality = new LRClickableAreaWText();
  634. speciality->pos = genRect(32, 32, pos.x+374, pos.y+5);
  635. speciality->hoverText = CGI->generaltexth->heroscrn[27];
  636. for(int i=0; i<SKILL_PER_HERO; ++i)
  637. {
  638. secondarySkills.push_back(new LRClickableAreaWTextComp());
  639. secondarySkills[i]->pos = genRect(32, 32, pos.x+410+i*37, pos.y+5);
  640. secondarySkills[i]->baseType = 1;
  641. };
  642. for (int i=0; i<18;i++)
  643. {
  644. artifacts.push_back(new CArtPlace(this));
  645. artifacts[i]->pos = genRect(44, 44, pos.x+268+(i%9)*48, pos.y+66);
  646. artifacts[i]->baseType = SComponent::artifact;
  647. };
  648. for (int i=0; i<8;i++)
  649. {
  650. backpack.push_back(new CArtPlace(this));
  651. backpack[i]->pos = genRect(44, 44, pos.x+293+(i%9)*48, pos.y+66);
  652. backpack[i]->baseType = SComponent::artifact;
  653. };
  654. }
  655. CKingdomInterface::CHeroItem::~CHeroItem()
  656. {
  657. delete garr;
  658. delete artButtons;
  659. primarySkills.clear();
  660. secondarySkills.clear();
  661. artifacts.clear();
  662. backpack.clear();
  663. }
  664. void CKingdomInterface::CHeroItem::setHero(const CGHeroInstance * newHero)
  665. {
  666. BLOCK_CAPTURING;
  667. delete garr;
  668. hero = newHero;
  669. if (!hero)
  670. {
  671. return;
  672. garr = NULL;
  673. }
  674. char bufor[4000];
  675. artLeft->block(hero->artifacts.size() <= 8);
  676. artRight->block(hero->artifacts.size() <= 8);
  677. garr = new CGarrisonInt(pos.x+6, pos.y+78, 4, Point(), slots->ourImages[owner->PicCount].bitmap,
  678. Point(6,78), hero, NULL, false, true);
  679. garr->update = false;
  680. for (int i=0; i<artifacts.size(); i++)
  681. {
  682. artifacts[i]->type = hero->getArtAtPos(i);
  683. if (artifacts[i]->type<0)
  684. artifacts[i]->hoverText = CGI->generaltexth->heroscrn[11];
  685. else
  686. {
  687. artifacts[i]->text = CGI->generaltexth->artifDescriptions[artifacts[i]->type];
  688. artifacts[i]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % CGI->arth->artifacts[artifacts[i]->type].Name());
  689. }
  690. }
  691. for (int i=0; i<backpack.size(); i++)
  692. {
  693. backpack[i]->type = hero->getArtAtPos(19+i);
  694. if (backpack[i]->type<0)
  695. backpack[i]->hoverText ="";
  696. else
  697. {
  698. backpack[i]->text = CGI->generaltexth->artifDescriptions[backpack[i]->type];
  699. backpack[i]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % CGI->arth->artifacts[backpack[i]->type].Name());
  700. }
  701. }
  702. sprintf(bufor, CGI->generaltexth->allTexts[15].c_str(), hero->name.c_str(), hero->type->heroClass->name.c_str());
  703. portrait->hoverText = std::string(bufor);
  704. portrait->text = hero->getBiography();
  705. speciality->text = CGI->generaltexth->hTxts[hero->subID].longBonus;
  706. //primary skills
  707. for(size_t g=0; g<primarySkills.size(); ++g)
  708. primarySkills[g]->bonus = hero->getPrimSkillLevel(g);
  709. //secondary skills
  710. for(size_t g=0; g<std::min(secondarySkills.size(),hero->secSkills.size()); ++g)
  711. {
  712. int skill = hero->secSkills[g].first,
  713. level = hero->secSkills[g].second;
  714. secondarySkills[g]->type = skill;
  715. secondarySkills[g]->bonus = level;
  716. secondarySkills[g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  717. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level-1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  718. secondarySkills[g]->hoverText = std::string(bufor);
  719. }
  720. //experience
  721. experience->text = CGI->generaltexth->allTexts[2].c_str();
  722. boost::replace_first(experience->text, "%d", boost::lexical_cast<std::string>(hero->level));
  723. boost::replace_first(experience->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  724. boost::replace_first(experience->text, "%d", boost::lexical_cast<std::string>(hero->exp));
  725. //spell points
  726. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), hero->name.c_str(), hero->mana, hero->manaLimit());
  727. spellPoints->text = std::string(bufor);
  728. //setting morale
  729. std::vector<std::pair<int,std::string> > mrl = hero->getCurrentMoraleModifiers();
  730. int mrlv = hero->getCurrentMorale();
  731. int mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad morale, 0 - neutral, 1 - good
  732. morale->hoverText = CGI->generaltexth->heroscrn[4 - mrlt];
  733. morale->baseType = SComponent::morale;
  734. morale->bonus = mrlv;
  735. morale->text = CGI->generaltexth->arraytxt[88];
  736. boost::algorithm::replace_first(morale->text,"%s",CGI->generaltexth->arraytxt[86-mrlt]);
  737. if (!mrl.size())
  738. morale->text += CGI->generaltexth->arraytxt[108];
  739. else
  740. for(int it=0; it < mrl.size(); it++)
  741. morale->text += mrl[it].second;
  742. //setting luck
  743. mrl = hero->getCurrentLuckModifiers();
  744. mrlv = hero->getCurrentLuck();
  745. mrlt = (mrlv>0)-(mrlv<0); //signum: -1 - bad luck, 0 - neutral, 1 - good
  746. luck->hoverText = CGI->generaltexth->heroscrn[7 - mrlt];
  747. luck->baseType = SComponent::luck;
  748. luck->bonus = mrlv;
  749. luck->text = CGI->generaltexth->arraytxt[62];
  750. boost::algorithm::replace_first(luck->text,"%s",CGI->generaltexth->arraytxt[60-mrlt]);
  751. if (!mrl.size())
  752. luck->text += CGI->generaltexth->arraytxt[77];
  753. else
  754. for(int it=0; it < mrl.size(); it++)
  755. luck->text += mrl[it].second;
  756. }
  757. void CKingdomInterface::CHeroItem::scrollArts(int move)
  758. {
  759. backpackPos = ( backpackPos + move + hero->artifacts.size()) % hero->artifacts.size();
  760. for (int i=0; i<backpack.size(); i++)
  761. {
  762. backpack[i]->type = hero->getArtAtPos(19+(backpackPos + i)%hero->artifacts.size());
  763. if (backpack[i]->type<0)
  764. backpack[i]->hoverText ="";
  765. else
  766. {
  767. backpack[i]->text = CGI->generaltexth->artifDescriptions[backpack[i]->type];
  768. backpack[i]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % CGI->arth->artifacts[backpack[i]->type].Name());
  769. }
  770. }
  771. }
  772. void CKingdomInterface::CHeroItem::showAll(SDL_Surface * to)
  773. {
  774. if (!hero)
  775. {//if we have no hero for this slot - print background & exit
  776. blitAt(slots->ourImages[numb % owner->PicCount].bitmap,pos.x,pos.y,to);
  777. return;
  778. }//print background, different for arts view/backpack mode
  779. blitAt(slots->ourImages[(artGroup!=2)?owner->PicCount:(owner->PicCount+1)].bitmap,pos.x,pos.y,to);
  780. //text "Artifacts"
  781. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[2],pos.x+320,pos.y+55,GEOR13,zwykly,to);
  782. blitAt(graphics->portraitLarge[hero->portrait],pos.x+5,pos.y+6,to);
  783. garr->show(to);
  784. //hero name
  785. CSDL_Ext::printAt(hero->name,pos.x+73,pos.y+7,GEOR13,zwykly,to);
  786. for (int i = 0; i<6; i++)
  787. {//primary skills, mana and exp. pics
  788. blitAt(graphics->pskillst->ourImages[i].bitmap,(i<4)?(pos.x+78+36*i):(pos.x+539-52*i),
  789. (i<4)?(pos.y+26):(pos.y+6),to);
  790. if (i>3) continue;//primary skills text
  791. std::ostringstream str;
  792. str << (hero->primSkills[i]);
  793. CSDL_Ext::printAtMiddle(str.str(),pos.x+95+36*i,pos.y+65,GEOR13,zwykly,to);
  794. }
  795. {//luck and morale pics, experience and mana text
  796. blitAt(graphics->luck30->ourImages[hero->getCurrentLuck()+3].bitmap,pos.x+222,pos.y+30,to);
  797. blitAt(graphics->morale30->ourImages[hero->getCurrentMorale()+3].bitmap,pos.x+222,pos.y+54,to);
  798. std::ostringstream str;
  799. str << (hero->exp);
  800. CSDL_Ext::printAtMiddle(str.str(),(pos.x+348),(pos.y+31),GEORM,zwykly,to);
  801. std::ostringstream strnew;
  802. strnew << (hero->mana)<<"/"<<(hero->manaLimit());
  803. CSDL_Ext::printAtMiddle(strnew.str(),(pos.x+298),(pos.y+31),GEORM,zwykly,to);
  804. }
  805. //hero speciality
  806. blitAt(graphics->un32->ourImages[hero->subID].bitmap, pos.x+375, pos.y+6, to);
  807. for(int i=0; i<hero->secSkills.size(); i++)
  808. {//secondary skills
  809. int skill = hero->secSkills[i].first,
  810. level = hero->secSkills[i].second;
  811. blitAt(graphics->abils32->ourImages[skill*3+level+2].bitmap,pos.x+411+i*36,pos.y+6,to);
  812. }
  813. artButtons->show(to);
  814. int iter=0;
  815. switch (artGroup)
  816. {//arts
  817. case 1:iter = 9;//misc. arts, spellbook, war machines
  818. case 0://equipped arts
  819. for (int i = iter ; i<iter+9;i++)
  820. {
  821. int artID = hero->getArtAtPos(i);
  822. if (artID>=0)
  823. blitAt(graphics->artDefs->ourImages[artID].bitmap,pos.x+268+48*(i%9),pos.y+66,to);
  824. }
  825. break;
  826. case 2:
  827. artLeft->show(to);
  828. artRight->show(to);
  829. int max = hero->artifacts.size();
  830. iter = std::min(8, max);
  831. for (size_t it = 0 ; it<iter;it++)
  832. blitAt(graphics->artDefs->ourImages[hero->artifacts[(it+backpackPos)%max]].bitmap,pos.x+293+48*it,pos.y+66,to);
  833. break;
  834. }
  835. show(to);
  836. }
  837. void CKingdomInterface::CHeroItem::onArtChange(int newstate)
  838. {
  839. if (!hero)
  840. return;
  841. deactivate();
  842. artGroup = newstate;
  843. activate();
  844. GH.totalRedraw();
  845. }
  846. void CKingdomInterface::CHeroItem::activate()
  847. {
  848. if (!hero)
  849. return;
  850. artButtons->activate();
  851. garr->activate();
  852. if ( artGroup == 2 )
  853. {
  854. artLeft->activate();
  855. artRight->activate();
  856. for (size_t i=0; i<8;i++)
  857. backpack[i]->activate();
  858. }
  859. else
  860. {
  861. for (size_t i=artGroup*9; i<9+artGroup*9;i++)
  862. artifacts[i]->activate();
  863. }
  864. portrait->activate();
  865. experience->activate();
  866. morale->activate();
  867. luck->activate();
  868. spellPoints->activate();
  869. speciality->activate();
  870. for (size_t i=0; i<primarySkills.size();i++)
  871. primarySkills[i]->activate();
  872. for (size_t i=0; i<std::min(secondarySkills.size(),hero->secSkills.size());i++)
  873. secondarySkills[i]->activate();
  874. }
  875. void CKingdomInterface::CHeroItem::deactivate()
  876. {
  877. if (!hero)
  878. return;
  879. artButtons->deactivate();
  880. garr->deactivate();
  881. if ( artGroup == 2 )
  882. {
  883. artLeft->deactivate();
  884. artRight->deactivate();
  885. for (size_t i=0; i<8;i++)
  886. backpack[i]->deactivate();
  887. }
  888. else
  889. {
  890. for (size_t i=artGroup*9; i<9+artGroup*9;i++)
  891. artifacts[i]->deactivate();
  892. }
  893. portrait->deactivate();
  894. experience->deactivate();
  895. morale->deactivate();
  896. luck->deactivate();
  897. spellPoints->deactivate();
  898. speciality->deactivate();
  899. for (size_t i=0; i<primarySkills.size();i++)
  900. primarySkills[i]->deactivate();
  901. for (size_t i=0; i<std::min(secondarySkills.size(),hero->secSkills.size());i++)
  902. secondarySkills[i]->deactivate();
  903. }
  904. CKingdomInterface::CHeroItem::CArtPlace::CArtPlace(CHeroItem * owner)
  905. {
  906. hero = owner;
  907. used = LCLICK | RCLICK | HOVER;
  908. }
  909. void CKingdomInterface::CHeroItem::CArtPlace::activate()
  910. {
  911. LRClickableAreaWTextComp::activate();
  912. }
  913. void CKingdomInterface::CHeroItem::CArtPlace::clickLeft(tribool down, bool previousState)
  914. {
  915. if (!down && previousState && type>=0)
  916. {
  917. if(type == 0)
  918. {
  919. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), hero->hero);
  920. GH.pushInt(spellWindow);
  921. }
  922. else
  923. LRClickableAreaWTextComp::clickLeft(down,previousState);
  924. }
  925. }
  926. void CKingdomInterface::CHeroItem::CArtPlace::clickRight(tribool down, bool previousState)
  927. {
  928. if (type>=0)
  929. LRClickableAreaWTextComp::clickRight(down, previousState);
  930. }
  931. void CKingdomInterface::CHeroItem::CArtPlace::deactivate()
  932. {
  933. LRClickableAreaWTextComp::deactivate();
  934. }
  935. CKingdomInterface::CTownItem::CCreaPlace::CCreaPlace()
  936. {
  937. town = NULL;
  938. used = LCLICK | RCLICK | HOVER;
  939. }
  940. void CKingdomInterface::CTownItem::CCreaPlace::activate()
  941. {
  942. LRClickableAreaWTextComp::activate();
  943. }
  944. void CKingdomInterface::CTownItem::CCreaPlace::clickLeft(tribool down, bool previousState)
  945. {
  946. if (!down && previousState && town)
  947. {
  948. GH.pushInt (new CRecruitmentWindow(town, type, town, boost::bind
  949. (&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2)));
  950. }
  951. }
  952. void CKingdomInterface::CTownItem::CCreaPlace::clickRight(tribool down, bool previousState)
  953. {
  954. //TODO
  955. }
  956. void CKingdomInterface::CTownItem::CCreaPlace::deactivate()
  957. {
  958. LRClickableAreaWTextComp::deactivate();
  959. }