CHeroWindow.cpp 13 KB

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