CKingdomInterface.cpp 32 KB

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