CKingdomInterface.cpp 32 KB

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