CKingdomInterface.cpp 32 KB

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