CKingdomInterface.cpp 33 KB

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