CKingdomInterface.cpp 32 KB

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