CKingdomInterface.cpp 32 KB

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