CKingdomInterface.cpp 32 KB

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