CKingdomInterface.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 "../lib/CArtHandler.h"
  14. #include "../lib/CBuildingHandler.h"
  15. #include "CDefHandler.h"
  16. #include "../lib/CHeroHandler.h"
  17. #include "../lib/CGeneralTextHandler.h"
  18. #include "../lib/CObjectHandler.h"
  19. #include "../lib/CTownHandler.h"
  20. #include "../lib/CCreatureHandler.h"
  21. #include "../lib/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(CGHeroInstance::ESTATES))//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, const 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(genRect(45, 32, pos.x+77 + 36*i, pos.y+26), SComponent::primskill));
  623. primarySkills[i]->text = CGI->generaltexth->arraytxt[2+i];
  624. primarySkills[i]->type = i;
  625. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[i].c_str());
  626. primarySkills[i]->hoverText = std::string(bufor);
  627. };
  628. experience = new LRClickableAreaWText();
  629. experience->pos = genRect(33, 49, pos.x+322, pos.y+5);
  630. experience->hoverText = CGI->generaltexth->heroscrn[9];
  631. morale = new MoraleLuckBox(true, genRect(20,32,pos.x+221,pos.y+52));
  632. luck = new MoraleLuckBox(false, genRect(20,32,pos.x+221,pos.y+28));
  633. spellPoints = new LRClickableAreaWText();
  634. spellPoints->pos = genRect(33, 49, pos.x+270, pos.y+5);
  635. spellPoints->hoverText = CGI->generaltexth->heroscrn[22];
  636. speciality = new LRClickableAreaWText();
  637. speciality->pos = genRect(32, 32, pos.x+374, pos.y+5);
  638. speciality->hoverText = CGI->generaltexth->heroscrn[27];
  639. for(int i=0; i<SKILL_PER_HERO; ++i)
  640. {
  641. secondarySkills.push_back(new LRClickableAreaWTextComp(genRect(32, 32, pos.x+410+i*37, pos.y+5), SComponent::secskill));
  642. }
  643. /*
  644. for (int i=0; i<18;i++)
  645. {
  646. artifacts.push_back(new CArtPlace(this, genRect(44, 44, pos.x+268+(i%9)*48, pos.y+66)));
  647. }
  648. for (int i=0; i<8;i++)
  649. {
  650. backpack.push_back(new CArtPlace(this, genRect(44, 44, pos.x+293+(i%9)*48, pos.y+66)));
  651. }*/
  652. }
  653. CKingdomInterface::CHeroItem::~CHeroItem()
  654. {
  655. delete garr;
  656. delete artButtons;
  657. primarySkills.clear();
  658. secondarySkills.clear();
  659. artifacts.clear();
  660. backpack.clear();
  661. }
  662. void CKingdomInterface::CHeroItem::setHero(const CGHeroInstance * newHero)
  663. {
  664. BLOCK_CAPTURING;
  665. delete garr;
  666. hero = newHero;
  667. if (!hero)
  668. {
  669. return;
  670. garr = NULL;
  671. }
  672. char bufor[4000];
  673. artLeft->block(hero->artifactsInBackpack.size() <= 8);
  674. artRight->block(hero->artifactsInBackpack.size() <= 8);
  675. garr = new CGarrisonInt(pos.x+6, pos.y+78, 4, Point(), parent->slots->ourImages[parent->PicCount].bitmap,
  676. Point(6,78), hero, NULL, true, true, true);
  677. for (int i=0; i<artifacts.size(); i++)
  678. {
  679. artifacts[i]->type = hero->getArtTypeId(i);
  680. if (artifacts[i]->type<0 || artifacts[i]->type == 145 )
  681. artifacts[i]->hoverText = CGI->generaltexth->heroscrn[11];
  682. else
  683. {
  684. artifacts[i]->text = CGI->generaltexth->artifDescriptions[artifacts[i]->type];
  685. artifacts[i]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % CGI->arth->artifacts[artifacts[i]->type]->Name());
  686. }
  687. }
  688. for (int i=0; i<backpack.size(); i++)
  689. {
  690. backpack[i]->type = hero->getArtTypeId(19+i);
  691. if (backpack[i]->type<0)
  692. backpack[i]->hoverText ="";
  693. else
  694. {
  695. backpack[i]->text = CGI->generaltexth->artifDescriptions[backpack[i]->type];
  696. backpack[i]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % CGI->arth->artifacts[backpack[i]->type]->Name());
  697. }
  698. }
  699. //sprintf(bufor, CGI->generaltexth->allTexts[15].c_str(), hero->name.c_str(), hero->type->heroClass->name.c_str());
  700. //portrait->hoverText = std::string(bufor);
  701. portrait->hero = hero;
  702. speciality->text = CGI->generaltexth->hTxts[hero->subID].longBonus;
  703. //primary skills
  704. for(size_t g=0; g<primarySkills.size(); ++g)
  705. primarySkills[g]->bonusValue = hero->getPrimSkillLevel(g);
  706. //secondary skills
  707. for(size_t g=0; g<std::min(secondarySkills.size(),hero->secSkills.size()); ++g)
  708. {
  709. int skill = hero->secSkills[g].first,
  710. level = hero->secSkills[g].second;
  711. secondarySkills[g]->type = skill;
  712. secondarySkills[g]->bonusValue = level;
  713. secondarySkills[g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  714. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level-1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  715. secondarySkills[g]->hoverText = std::string(bufor);
  716. }
  717. //experience
  718. experience->text = CGI->generaltexth->allTexts[2].c_str();
  719. boost::replace_first(experience->text, "%d", boost::lexical_cast<std::string>(hero->level));
  720. boost::replace_first(experience->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  721. boost::replace_first(experience->text, "%d", boost::lexical_cast<std::string>(hero->exp));
  722. //spell points
  723. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), hero->name.c_str(), hero->mana, hero->manaLimit());
  724. spellPoints->text = std::string(bufor);
  725. //setting morale and luck
  726. morale->set(hero);
  727. luck->set(hero);
  728. }
  729. void CKingdomInterface::CHeroItem::scrollArts(int move)
  730. {
  731. backpackPos = ( backpackPos + move + hero->artifactsInBackpack.size()) % hero->artifactsInBackpack.size();
  732. for (int i=0; i<backpack.size(); i++)
  733. {
  734. backpack[i]->type = hero->getArtTypeId(19+(backpackPos + i)%hero->artifactsInBackpack.size());
  735. if (backpack[i]->type<0)
  736. backpack[i]->hoverText ="";
  737. else
  738. {
  739. backpack[i]->text = CGI->generaltexth->artifDescriptions[backpack[i]->type];
  740. backpack[i]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1].c_str()) % CGI->arth->artifacts[backpack[i]->type]->Name());
  741. }
  742. }
  743. showAll(screen2);
  744. }
  745. void CKingdomInterface::CHeroItem::showAll(SDL_Surface * to)
  746. {
  747. if (!hero)
  748. {//if we have no hero for this slot - print background & exit
  749. blitAt(parent->slots->ourImages[numb % parent->PicCount].bitmap,pos.x,pos.y,to);
  750. return;
  751. }//print background, different for arts view/backpack mode
  752. blitAt(parent->slots->ourImages[(artGroup!=2)?parent->PicCount:(parent->PicCount+1)].bitmap,pos.x,pos.y,to);
  753. //text "Artifacts"
  754. CSDL_Ext::printAtMiddle(CGI->generaltexth->overview[2],pos.x+320,pos.y+55,FONT_SMALL,zwykly,to);
  755. portrait->showAll(to);
  756. garr->show(to);
  757. //hero name
  758. CSDL_Ext::printAt(hero->name,pos.x+73,pos.y+7,FONT_SMALL,zwykly,to);
  759. for (int i = 0; i<6; i++)
  760. {//primary skills, mana and exp. pics
  761. blitAt(graphics->pskillst->ourImages[i].bitmap,(i<4)?(pos.x+78+36*i):(pos.x+539-52*i),
  762. (i<4)?(pos.y+26):(pos.y+6),to);
  763. if (i>3) continue;//primary skills text
  764. std::ostringstream str;
  765. str << (hero->getPrimSkillLevel(i));
  766. CSDL_Ext::printAtMiddle(str.str(),pos.x+94+36*i,pos.y+66,FONT_SMALL,zwykly,to);
  767. }
  768. {//luck and morale pics, experience and mana text
  769. blitAt(graphics->luck30->ourImages[hero->LuckVal()+3].bitmap,pos.x+222,pos.y+30,to);
  770. blitAt(graphics->morale30->ourImages[hero->MoraleVal()+3].bitmap,pos.x+222,pos.y+54,to);
  771. std::ostringstream str;
  772. str << (hero->exp);
  773. CSDL_Ext::printAtMiddle(str.str(),(pos.x+348),(pos.y+31),FONT_TINY,zwykly,to);
  774. std::ostringstream strnew;
  775. strnew << (hero->mana)<<"/"<<(hero->manaLimit());
  776. CSDL_Ext::printAtMiddle(strnew.str(),(pos.x+295),(pos.y+30),FONT_TINY,zwykly,to);
  777. }
  778. //hero speciality
  779. blitAt(graphics->un32->ourImages[hero->subID].bitmap, pos.x+375, pos.y+6, to);
  780. for(int i=0; i<hero->secSkills.size(); i++)
  781. {//secondary skills
  782. int skill = hero->secSkills[i].first,
  783. level = hero->secSkills[i].second;
  784. blitAt(graphics->abils32->ourImages[skill*3+level+2].bitmap,pos.x+411+i*36,pos.y+6,to);
  785. }
  786. artButtons->show(to);
  787. int iter=0;
  788. switch (artGroup)
  789. {//arts
  790. case 1:iter = 9;//misc. arts, spellbook, war machines
  791. case 0://equipped arts
  792. /*for (int i = iter ; i<iter+9;i++)
  793. {
  794. int artID = hero->getArtTypeId(i);
  795. if (artID>=0)
  796. blitAt(graphics->artDefs->ourImages[artID].bitmap,pos.x+268+48*(i%9),pos.y+66,to);
  797. }*/
  798. break;
  799. case 2:
  800. artLeft->show(to);
  801. artRight->show(to);
  802. int max = hero->artifactsInBackpack.size();
  803. iter = std::min(8, max);
  804. /*for (size_t it = 0 ; it<iter;it++)
  805. blitAt(graphics->artDefs->ourImages[hero->artifacts[(it+backpackPos)%max]->id].bitmap,pos.x+293+48*it,pos.y+66,to);
  806. */break;
  807. }
  808. show(to);
  809. }
  810. void CKingdomInterface::CHeroItem::onArtChange(int newstate)
  811. {
  812. if (!hero)
  813. return;
  814. deactivate();
  815. artGroup = newstate;
  816. activate();
  817. showAll(screen2);
  818. }
  819. void CKingdomInterface::CHeroItem::activate()
  820. {
  821. setHero(hero);
  822. if (!hero)
  823. return;
  824. artButtons->activate();
  825. garr->activate();/*
  826. if ( artGroup == 2 )
  827. {
  828. artLeft->activate();
  829. artRight->activate();
  830. for (size_t i=0; i<8;i++)
  831. backpack[i]->activate();
  832. }
  833. else
  834. {
  835. for (size_t i=artGroup*9; i<9+artGroup*9;i++)
  836. artifacts[i]->activate();
  837. }*/
  838. portrait->activate();
  839. experience->activate();
  840. morale->activate();
  841. luck->activate();
  842. spellPoints->activate();
  843. speciality->activate();
  844. for (size_t i=0; i<primarySkills.size();i++)
  845. primarySkills[i]->activate();
  846. for (size_t i=0; i<std::min(secondarySkills.size(),hero->secSkills.size());i++)
  847. secondarySkills[i]->activate();
  848. }
  849. void CKingdomInterface::CHeroItem::deactivate()
  850. {
  851. if (!hero)
  852. return;
  853. artButtons->deactivate();
  854. garr->deactivate();/*
  855. if ( artGroup == 2 )
  856. {
  857. artLeft->deactivate();
  858. artRight->deactivate();
  859. for (size_t i=0; i<8;i++)
  860. backpack[i]->deactivate();
  861. }
  862. else
  863. {
  864. for (size_t i=artGroup*9; i<9+artGroup*9;i++)
  865. artifacts[i]->deactivate();
  866. }*/
  867. portrait->deactivate();
  868. experience->deactivate();
  869. morale->deactivate();
  870. luck->deactivate();
  871. spellPoints->deactivate();
  872. speciality->deactivate();
  873. for (size_t i=0; i<primarySkills.size();i++)
  874. primarySkills[i]->deactivate();
  875. for (size_t i=0; i<std::min(secondarySkills.size(),hero->secSkills.size());i++)
  876. secondarySkills[i]->deactivate();
  877. }
  878. CKingdomInterface::CHeroItem::CArtPlace::CArtPlace(CHeroItem * owner, const Rect &r)
  879. : LRClickableAreaWTextComp(r, SComponent::artifact)
  880. {
  881. parent = owner;
  882. used = LCLICK | RCLICK | HOVER;
  883. }
  884. void CKingdomInterface::CHeroItem::CArtPlace::activate()
  885. {
  886. LRClickableAreaWTextComp::activate();
  887. }
  888. void CKingdomInterface::CHeroItem::CArtPlace::clickLeft(tribool down, bool previousState)
  889. {
  890. if (!down && previousState && type>=0 && type < 145)
  891. {tlog1<<type;
  892. if(type == 0)
  893. {
  894. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), parent->hero, LOCPLINT, false);
  895. GH.pushInt(spellWindow);
  896. }
  897. else
  898. LRClickableAreaWTextComp::clickLeft(down,previousState);
  899. }
  900. }
  901. void CKingdomInterface::CHeroItem::CArtPlace::clickRight(tribool down, bool previousState)
  902. {
  903. if (type>=0 && type < 145)
  904. LRClickableAreaWTextComp::clickRight(down, previousState);
  905. }
  906. void CKingdomInterface::CHeroItem::CArtPlace::deactivate()
  907. {
  908. LRClickableAreaWTextComp::deactivate();
  909. }
  910. CKingdomInterface::CTownItem::CCreaPlace::CCreaPlace()
  911. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  912. {
  913. town = NULL;
  914. used = LCLICK | RCLICK | HOVER;
  915. }
  916. void CKingdomInterface::CTownItem::CCreaPlace::activate()
  917. {
  918. LRClickableAreaWTextComp::activate();
  919. }
  920. void CKingdomInterface::CTownItem::CCreaPlace::clickLeft(tribool down, bool previousState)
  921. {
  922. if (!down && previousState && town)
  923. {
  924. GH.pushInt (new CRecruitmentWindow(town, type, town, boost::bind
  925. (&CCallback::recruitCreatures,LOCPLINT->cb,town,_1,_2, type)));
  926. }
  927. }
  928. void CKingdomInterface::CTownItem::CCreaPlace::clickRight(tribool down, bool previousState)
  929. {
  930. if (down && town)
  931. {
  932. int crid;
  933. if (town->builtBuildings.find(30+type+CREATURES_PER_TOWN)!=town->builtBuildings.end())
  934. crid = town->town->upgradedCreatures[type];
  935. else
  936. crid = town->town->basicCreatures[type];
  937. GH.pushInt(new CCreInfoWindow(crid, 0, town->creatures[type].first));
  938. }
  939. }
  940. void CKingdomInterface::CTownItem::CCreaPlace::deactivate()
  941. {
  942. LRClickableAreaWTextComp::deactivate();
  943. }