CKingdomInterface.cpp 32 KB

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