CKingdomInterface.cpp 32 KB

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