CKingdomInterface.cpp 33 KB

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