CHeroWindow.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "../stdafx.h"
  2. #include "AdventureMapButton.h"
  3. #include "CAdvmapInterface.h"
  4. #include "../CCallback.h"
  5. #include "CGameInfo.h"
  6. #include "CHeroWindow.h"
  7. #include "CMessage.h"
  8. #include "CKingdomInterface.h"
  9. #include "SDL.h"
  10. #include "SDL_Extensions.h"
  11. #include "CBitmapHandler.h"
  12. #include "Graphics.h"
  13. #include "CSpellWindow.h"
  14. #include "CConfigHandler.h"
  15. #include "../global.h"
  16. #include "../hch/CArtHandler.h"
  17. #include "../hch/CDefHandler.h"
  18. #include "../hch/CGeneralTextHandler.h"
  19. #include "../hch/CHeroHandler.h"
  20. #include "../hch/CLodHandler.h"
  21. #include "../hch/CObjectHandler.h"
  22. #include <boost/algorithm/string/replace.hpp>
  23. #include <boost/assign/list_of.hpp>
  24. #include <boost/assign/std/vector.hpp>
  25. #include <cstdlib>
  26. #include <sstream>
  27. #include <boost/lexical_cast.hpp>
  28. #include <boost/format.hpp>
  29. #include <boost/foreach.hpp>
  30. #undef min
  31. /*
  32. * CHeroWindow.cpp, part of VCMI engine
  33. *
  34. * Authors: listed in file AUTHORS in main folder
  35. *
  36. * License: GNU General Public License v2.0 or later
  37. * Full text of license available in license.txt file, in main folder
  38. *
  39. */
  40. extern SDL_Surface * screen;
  41. using namespace boost::assign;
  42. void CHeroSwitcher::clickLeft(tribool down, bool previousState)
  43. {
  44. if(!down)
  45. {
  46. getOwner()->deactivate();
  47. const CGHeroInstance * buf = LOCPLINT->getWHero(id);
  48. getOwner()->setHero(buf);
  49. getOwner()->activate();
  50. }
  51. }
  52. CHeroWindow * CHeroSwitcher::getOwner()
  53. {
  54. return dynamic_cast<CHeroWindow*>(parent);
  55. }
  56. CHeroSwitcher::CHeroSwitcher(int serial)
  57. {
  58. pos = Rect(677, 95 + serial * 54, 48, 32) + pos;
  59. id = serial;
  60. used = LCLICK;
  61. }
  62. CHeroWindow::CHeroWindow(const CGHeroInstance *hero)
  63. {
  64. OBJ_CONSTRUCTION_CAPTURING_ALL;
  65. garr = NULL;
  66. curBack = NULL;
  67. curHero = NULL;
  68. player = hero->tempOwner;
  69. background = new CPicture("HeroScr4.BMP");
  70. background->colorizeAndConvert(player);
  71. pos = background->center();
  72. //artifs = new CArtifactsOfHero(pos.topLeft(), true);
  73. ourBar = new CGStatusBar(7, 559, "ADROLLVR.bmp", 660); // new CStatusBar(pos.x+72, pos.y+567, "ADROLLVR.bmp", 660);
  74. quitButton = new AdventureMapButton(CGI->generaltexth->heroscrn[17], std::string(),boost::bind(&CHeroWindow::quit,this), 609, 516, "hsbtns.def", SDLK_RETURN);
  75. dismissButton = new AdventureMapButton(std::string(), CGI->generaltexth->heroscrn[28], boost::bind(&CHeroWindow::dismissCurrent,this), 454, 429, "hsbtns2.def", SDLK_d);
  76. questlogButton = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CHeroWindow::questlog,this), 314, 429, "hsbtns4.def", SDLK_q);
  77. formations = new CHighlightableButtonsGroup(0);
  78. {
  79. BLOCK_CAPTURING;
  80. formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[23]),CGI->generaltexth->heroscrn[29], "hsbtns6.def", pos.x+481, pos.y+483, 0, 0, SDLK_t);
  81. formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[24]),CGI->generaltexth->heroscrn[30], "hsbtns7.def", pos.x+481, pos.y+519, 1, 0, SDLK_l);
  82. }
  83. tacticsButton = new CHighlightableButton(0, 0, map_list_of(0,CGI->generaltexth->heroscrn[26])(3,CGI->generaltexth->heroscrn[25]), CGI->generaltexth->heroscrn[31], false, "hsbtns8.def", NULL, 539, 483, SDLK_b);
  84. //right list of heroes
  85. for(int g=0; g<8; ++g)
  86. heroListMi.push_back(new CHeroSwitcher(g));
  87. flags = CDefHandler::giveDefEss("CREST58.DEF");
  88. //areas
  89. portraitArea = new LRClickableAreaWText(Rect(18, 18, 58, 64));
  90. for(int v=0; v<PRIMARY_SKILLS; ++v)
  91. {
  92. LRClickableAreaWTextComp *area = new LRClickableAreaWTextComp(Rect(30 + 70*v, 109, 42, 64), SComponent::primskill);
  93. area->text = CGI->generaltexth->arraytxt[2+v];
  94. area->type = v;
  95. area->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[v]);
  96. primSkillAreas.push_back(area);
  97. }
  98. specArea = new LRClickableAreaWText(Rect(18, 180, 136, 42), CGI->generaltexth->heroscrn[27]);
  99. expArea = new LRClickableAreaWText(Rect(18, 228, 136, 42), CGI->generaltexth->heroscrn[9]);
  100. morale = new MoraleLuckBox(true, Rect(175,179,53,45));
  101. luck = new MoraleLuckBox(false, Rect(233,179,53,45));
  102. spellPointsArea = new LRClickableAreaWText(Rect(162,228, 136, 42), CGI->generaltexth->heroscrn[22]);
  103. for(int i=0; i<SKILL_PER_HERO; ++i)
  104. {
  105. Rect r = Rect(i%2 == 0 ? 18 : 162, 276 + 48 * (i/2), 136, 42);
  106. secSkillAreas.push_back(new LRClickableAreaWTextComp(r, SComponent::secskill));
  107. }
  108. //////////////////////////////////////////////////////////////////////////???????????????
  109. // pos.x += 65;
  110. // pos.y += 8;
  111. //
  112. //primary skills & exp and mana
  113. new CPicture(graphics->pskillsm->ourImages[0].bitmap, 32, 111, false);
  114. new CPicture(graphics->pskillsm->ourImages[1].bitmap, 102, 111, false);
  115. new CPicture(graphics->pskillsm->ourImages[2].bitmap, 172, 111, false);
  116. new CPicture(graphics->pskillsm->ourImages[5].bitmap, 242, 111, false);
  117. new CPicture(graphics->pskillsm->ourImages[4].bitmap, 20, 230, false);
  118. new CPicture(graphics->pskillsm->ourImages[3].bitmap, 162, 230, false);
  119. setHero(hero);
  120. }
  121. CHeroWindow::~CHeroWindow()
  122. {
  123. delete flags;
  124. //SDL_FreeSurface(curBack);
  125. //curBack = NULL;
  126. curHero = NULL;
  127. //artifs->dispose();
  128. }
  129. void CHeroWindow::setHero(const CGHeroInstance *hero)
  130. {
  131. if(!hero) //something strange... no hero? it shouldn't happen
  132. {
  133. tlog1 << "Set NULL hero? no way...\n";
  134. return;
  135. }
  136. if(hero == curHero)
  137. {
  138. return;
  139. }
  140. assert(hero->tempOwner == LOCPLINT->playerID); //for now we won't show hero windows for non-our heroes
  141. curHero = hero;
  142. specArea->text = CGI->generaltexth->hTxts[hero->subID].longBonus;
  143. tacticsButton->callback.clear();
  144. tacticsButton->callback2.clear();
  145. dismissButton->hoverTexts[0] = boost::str(boost::format(CGI->generaltexth->heroscrn[16]) % curHero->name % curHero->type->heroClass->name);
  146. portraitArea->hoverText = boost::str(boost::format(CGI->generaltexth->allTexts[15]) % curHero->name % curHero->type->heroClass->name);
  147. portraitArea->text = hero->getBiography();
  148. {
  149. delete garr;
  150. OBJ_CONSTRUCTION_CAPTURING_ALL;
  151. garr = new CGarrisonInt(80, 493, 8, Point(), curBack->bg, Point(16,486), curHero);
  152. }
  153. AdventureMapButton * split = NULL;
  154. {
  155. BLOCK_CAPTURING;
  156. split = new AdventureMapButton(CGI->generaltexth->allTexts[256], CGI->generaltexth->heroscrn[32], boost::bind(&CGarrisonInt::splitClick,garr), pos.x+604, pos.y+527, "hsbtns9.def", false, NULL, false); //deleted by garrison destructor
  157. }
  158. boost::algorithm::replace_first(split->hoverTexts[0],"%s",CGI->generaltexth->allTexts[43]);
  159. garr->addSplitBtn(split);
  160. //primary skills support
  161. for(size_t g=0; g<primSkillAreas.size(); ++g)
  162. {
  163. primSkillAreas[g]->bonusValue = hero->getPrimSkillLevel(g);
  164. }
  165. //secondary skills support
  166. for(size_t g=0; g<std::min(secSkillAreas.size(),hero->secSkills.size()); ++g)
  167. {
  168. int skill = hero->secSkills[g].first,
  169. level = hero->getSecSkillLevel(hero->secSkills[g].first);
  170. secSkillAreas[g]->type = skill;
  171. secSkillAreas[g]->bonusValue = level;
  172. secSkillAreas[g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  173. secSkillAreas[g]->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[21]) % CGI->generaltexth->levels[level-1] % CGI->generaltexth->skillName[skill]);
  174. }
  175. //printing experience - original format does not support ui64
  176. expArea->text = CGI->generaltexth->allTexts[2];
  177. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(hero->level));
  178. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(hero->level+1)));
  179. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(hero->exp));
  180. //printing spell points
  181. spellPointsArea->text = boost::str(boost::format(CGI->generaltexth->allTexts[205]) % hero->name % hero->mana % hero->manaLimit());
  182. //if we have exchange window with this hero open
  183. bool noDismiss=false;
  184. BOOST_FOREACH(IShowActivable *isa, GH.listInt)
  185. {
  186. if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(isa))
  187. for(int g=0; g < ARRAY_COUNT(cew->heroInst); ++g)
  188. if(cew->heroInst[g] == hero)
  189. noDismiss = true;
  190. if (CKingdomInterface * cki = dynamic_cast<CKingdomInterface*>(isa))
  191. noDismiss = true;
  192. }
  193. dismissButton->block(!!hero->visitedTown || noDismiss);
  194. if(hero->getSecSkillLevel(19) == 0)
  195. tacticsButton->block(true);
  196. else
  197. {
  198. tacticsButton->block(false);
  199. tacticsButton->callback = vstd::assigno(hero->tacticFormationEnabled,true);
  200. tacticsButton->callback2 = vstd::assigno(hero->tacticFormationEnabled,false);
  201. }
  202. //setting formations
  203. formations->onChange = 0;
  204. formations->select(hero->formation,true);
  205. formations->onChange = boost::bind(&CCallback::setFormation, LOCPLINT->cb, hero, _1);
  206. morale->set(hero);
  207. luck->set(hero);
  208. }
  209. void CHeroWindow::quit()
  210. {
  211. adventureInt->heroWindow = NULL;
  212. GH.popIntTotally(this);
  213. }
  214. void CHeroWindow::dismissCurrent()
  215. {
  216. CFunctionList<void()> ony = boost::bind(&CHeroWindow::quit,this);
  217. ony += boost::bind(&CCallback::dismissHero,LOCPLINT->cb,curHero);
  218. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[22],std::vector<SComponent*>(), ony, 0, false);
  219. }
  220. void CHeroWindow::questlog()
  221. {
  222. }
  223. void CHeroWindow::showAll(SDL_Surface * to)
  224. {
  225. CIntObject::showAll(to);
  226. //blitting portrait
  227. blitAtLoc(graphics->portraitLarge[curHero->portrait], 19, 19, to);
  228. //printing hero's name
  229. printAtMiddleLoc(curHero->name, 190, 38, FONT_BIG, tytulowy, to);
  230. //printing hero's level
  231. std::string secondLine= CGI->generaltexth->allTexts[342];
  232. boost::algorithm::replace_first(secondLine,"%d",boost::lexical_cast<std::string>(curHero->level));
  233. boost::algorithm::replace_first(secondLine,"%s",curHero->type->heroClass->name);
  234. printAtMiddleLoc(secondLine, 190, 65, FONT_MEDIUM, zwykly, to);
  235. //primary skills names
  236. printAtMiddleLoc(CGI->generaltexth->jktexts[1], 52, 99, FONT_SMALL, tytulowy, to);
  237. printAtMiddleLoc(CGI->generaltexth->jktexts[2], 123, 99, FONT_SMALL, tytulowy, to);
  238. printAtMiddleLoc(CGI->generaltexth->jktexts[3], 193, 99, FONT_SMALL, tytulowy, to);
  239. printAtMiddleLoc(CGI->generaltexth->jktexts[4], 262, 99, FONT_SMALL, tytulowy, to);
  240. //dismiss / quest log
  241. std::vector<std::string> toPrin = CMessage::breakText(CGI->generaltexth->jktexts[8].substr(1, CGI->generaltexth->jktexts[8].size()-2));
  242. if(toPrin.size()==1)
  243. {
  244. printAtLoc(toPrin[0], 372, 439, FONT_SMALL, zwykly, to);
  245. }
  246. else
  247. {
  248. printAtLoc(toPrin[0], 372, 430, FONT_SMALL, zwykly, to);
  249. printAtLoc(toPrin[1], 372, 446, FONT_SMALL, zwykly, to);
  250. }
  251. toPrin = CMessage::breakText(CGI->generaltexth->jktexts[9].substr(1, CGI->generaltexth->jktexts[9].size()-2));
  252. if(toPrin.size()==1)
  253. {
  254. printAtLoc(toPrin[0], 512, 439, FONT_SMALL, zwykly, to);
  255. }
  256. else
  257. {
  258. printAtLoc(toPrin[0], 512, 430, FONT_SMALL, zwykly, to);
  259. printAtLoc(toPrin[1], 512, 446, FONT_SMALL, zwykly, to);
  260. }
  261. //printing primary skills' amounts
  262. for(int m=0; m<4; ++m)
  263. {
  264. std::ostringstream primarySkill;
  265. primarySkill<<curHero->getPrimSkillLevel(m);
  266. printAtMiddleLoc(primarySkill.str(), 53 + 70 * m, 166, FONT_SMALL, zwykly, to);
  267. }
  268. //morale and luck printing
  269. blitAt(graphics->luck42->ourImages[curHero->LuckVal()+3].bitmap, 239, 182, to);
  270. blitAt(graphics->morale42->ourImages[curHero->MoraleVal()+3].bitmap, 181, 182, to);
  271. blitAt(flags->ourImages[player].bitmap, 606, 8, to);
  272. //hero list blitting
  273. for(int pos=0, g=0; g<LOCPLINT->wanderingHeroes.size(); ++g)
  274. {
  275. const CGHeroInstance * cur = LOCPLINT->wanderingHeroes[g];
  276. if (cur->inTownGarrison)
  277. // Only display heroes that are not in garrison
  278. continue;
  279. blitAt(graphics->portraitSmall[cur->portrait], 611, 87+pos*54, to);
  280. //printing yellow border
  281. if(cur->name == curHero->name)
  282. {
  283. for(int f=0; f<graphics->portraitSmall[cur->portrait]->w; ++f)
  284. {
  285. for(int h=0; h<graphics->portraitSmall[cur->portrait]->h; ++h)
  286. if(f==0 || h==0 || f==graphics->portraitSmall[cur->portrait]->w-1 || h==graphics->portraitSmall[cur->portrait]->h-1)
  287. {
  288. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, 611+f, 87+pos*54+h, 240, 220, 120);
  289. }
  290. }
  291. }
  292. pos ++;
  293. }
  294. //secondary skills
  295. for(size_t v=0; v<std::min(secSkillAreas.size(), curHero->secSkills.size()); ++v)
  296. {
  297. blitAt(graphics->abils44->ourImages[curHero->secSkills[v].first*3+3+curHero->secSkills[v].second-1].bitmap, v%2 ? 161 : 18, 276 + 48 * (v/2), to);
  298. printAtLoc(CGI->generaltexth->levels[curHero->secSkills[v].second-1], v%2 ? 212 : 68, 280 + 48 * (v/2), FONT_SMALL, zwykly, to);
  299. printAtLoc(CGI->generaltexth->skillName[curHero->secSkills[v].first], v%2 ? 212 : 68, 300 + 48 * (v/2), FONT_SMALL, zwykly, to);
  300. }
  301. //printing special ability
  302. blitAt(graphics->un44->ourImages[curHero->subID].bitmap, 18, 180, to);
  303. printAtLoc(CGI->generaltexth->jktexts[5].substr(1, CGI->generaltexth->jktexts[5].size()-2), 69, 183, FONT_SMALL, tytulowy, to);
  304. printAtLoc(CGI->generaltexth->hTxts[curHero->subID].bonusName, 69, 205, FONT_SMALL, zwykly, to);
  305. //printing necessery texts
  306. printAtLoc(CGI->generaltexth->jktexts[6].substr(1, CGI->generaltexth->jktexts[6].size()-2), 69, 232, FONT_SMALL, tytulowy, to);
  307. std::ostringstream expstr;
  308. expstr<<curHero->exp;
  309. printAtLoc(expstr.str(), 68, 252, FONT_SMALL, zwykly, to);
  310. printAtLoc(CGI->generaltexth->jktexts[7].substr(1, CGI->generaltexth->jktexts[7].size()-2), 213, 232, FONT_SMALL, tytulowy, to);
  311. std::ostringstream manastr;
  312. manastr << curHero->mana << '/' << curHero->manaLimit();
  313. printAtLoc(manastr.str(), 211, 252, FONT_SMALL, zwykly, to);
  314. }