CHeroWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * CHeroWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CHeroWindow.h"
  12. #include "CAdvmapInterface.h"
  13. #include "CCreatureWindow.h"
  14. #include "CKingdomInterface.h"
  15. #include "GUIClasses.h"
  16. #include "../CBitmapHandler.h"
  17. #include "../CGameInfo.h"
  18. #include "../CMessage.h"
  19. #include "../CMT.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../Graphics.h"
  22. #include "../gui/SDL_Extensions.h"
  23. #include "../gui/CGuiHandler.h"
  24. #include "../gui/CAnimation.h"
  25. #include "../widgets/MiscWidgets.h"
  26. #include "../widgets/CComponent.h"
  27. #include "../../CCallback.h"
  28. #include "../lib/CArtHandler.h"
  29. #include "../lib/CConfigHandler.h"
  30. #include "../lib/CGeneralTextHandler.h"
  31. #include "../lib/CHeroHandler.h"
  32. #include "../lib/CSkillHandler.h"
  33. #include "../lib/mapObjects/CGHeroInstance.h"
  34. #include "../lib/NetPacksBase.h"
  35. #include "../mapHandler.h"
  36. const TBonusListPtr CHeroWithMaybePickedArtifact::getAllBonuses(const CSelector & selector, const CSelector & limit, const CBonusSystemNode * root, const std::string & cachingStr) const
  37. {
  38. TBonusListPtr out(new BonusList());
  39. TBonusListPtr heroBonuses = hero->getAllBonuses(selector, limit, hero, cachingStr);
  40. TBonusListPtr bonusesFromPickedUpArtifact;
  41. std::shared_ptr<CArtifactsOfHero::SCommonPart> cp = cww->getCommonPart();
  42. if(cp && cp->src.art && cp->src.valid() && cp->src.AOH && cp->src.AOH->getHero() == hero)
  43. bonusesFromPickedUpArtifact = cp->src.art->getAllBonuses(selector, limit, hero);
  44. else
  45. bonusesFromPickedUpArtifact = TBonusListPtr(new BonusList());
  46. for(auto b : *bonusesFromPickedUpArtifact)
  47. *heroBonuses -= b;
  48. for(auto b : *heroBonuses)
  49. out->push_back(b);
  50. return out;
  51. }
  52. int64_t CHeroWithMaybePickedArtifact::getTreeVersion() const
  53. {
  54. return hero->getTreeVersion(); //this assumes that hero and artifact belongs to main bonus tree
  55. }
  56. CHeroWithMaybePickedArtifact::CHeroWithMaybePickedArtifact(CWindowWithArtifacts * Cww, const CGHeroInstance * Hero)
  57. : hero(Hero), cww(Cww)
  58. {
  59. }
  60. void CHeroSwitcher::clickLeft(tribool down, bool previousState)
  61. {
  62. if(!down)
  63. {
  64. //TODO: do not recreate window
  65. #if 0
  66. owner->update(hero, true);
  67. #else
  68. UNUSED(owner);
  69. const CGHeroInstance * buf = hero;
  70. GH.popInts(1);
  71. GH.pushIntT<CHeroWindow>(buf);
  72. #endif // 0
  73. }
  74. }
  75. CHeroSwitcher::CHeroSwitcher(CHeroWindow * owner_, Point pos_, const CGHeroInstance * hero_)
  76. : CIntObject(LCLICK),
  77. owner(owner_),
  78. hero(hero_)
  79. {
  80. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  81. pos += pos_;
  82. image = std::make_shared<CAnimImage>("PortraitsSmall", hero->portrait);
  83. pos.w = image->pos.w;
  84. pos.h = image->pos.h;
  85. }
  86. CHeroWindow::CHeroWindow(const CGHeroInstance * hero)
  87. : CWindowObject(PLAYER_COLORED, "HeroScr4"),
  88. heroWArt(this, hero)
  89. {
  90. auto & heroscrn = CGI->generaltexth->heroscrn;
  91. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  92. curHero = hero;
  93. banner = std::make_shared<CAnimImage>("CREST58", LOCPLINT->playerID.getNum(), 0, 606, 8);
  94. name = std::make_shared<CLabel>(190, 38, EFonts::FONT_BIG, EAlignment::CENTER, Colors::YELLOW);
  95. title = std::make_shared<CLabel>(190, 65, EFonts::FONT_MEDIUM, EAlignment::CENTER, Colors::WHITE);
  96. statusBar = CGStatusBar::create(7, 559, "ADROLLVR.bmp", 660);
  97. quitButton = std::make_shared<CButton>(Point(609, 516), "hsbtns.def", CButton::tooltip(heroscrn[17]), [=](){ close(); }, SDLK_RETURN);
  98. quitButton->assignedKeys.insert(SDLK_ESCAPE);
  99. dismissLabel = std::make_shared<CTextBox>(CGI->generaltexth->jktexts[8], Rect(370, 430, 65, 35), 0, FONT_SMALL, TOPLEFT, Colors::WHITE);
  100. dismissButton = std::make_shared<CButton>(Point(454, 429), "hsbtns2.def", CButton::tooltip(heroscrn[28]), [=](){ dismissCurrent(); }, SDLK_d);
  101. questlogLabel = std::make_shared<CTextBox>(CGI->generaltexth->jktexts[9], Rect(510, 430, 65, 35), 0, FONT_SMALL, TOPLEFT, Colors::WHITE);
  102. questlogButton = std::make_shared<CButton>(Point(314, 429), "hsbtns4.def", CButton::tooltip(heroscrn[0]), [=](){ LOCPLINT->showQuestLog(); }, SDLK_q);
  103. formations = std::make_shared<CToggleGroup>(0);
  104. formations->addToggle(0, std::make_shared<CToggleButton>(Point(481, 483), "hsbtns6.def", std::make_pair(heroscrn[23], heroscrn[29]), 0, SDLK_t));
  105. formations->addToggle(1, std::make_shared<CToggleButton>(Point(481, 519), "hsbtns7.def", std::make_pair(heroscrn[24], heroscrn[30]), 0, SDLK_l));
  106. if(hero->commander)
  107. {
  108. auto texts = CGI->generaltexth->localizedTexts["heroWindow"]["openCommander"];
  109. commanderButton = std::make_shared<CButton>(Point(317, 18), "buttons/commander", CButton::tooltip(texts), [&](){ commanderWindow(); }, SDLK_c);
  110. }
  111. //right list of heroes
  112. for(int i=0; i < std::min(LOCPLINT->cb->howManyHeroes(false), 8); i++)
  113. heroList.push_back(std::make_shared<CHeroSwitcher>(this, Point(612, 87 + i * 54), LOCPLINT->cb->getHeroBySerial(i, false)));
  114. //areas
  115. portraitArea = std::make_shared<LRClickableAreaWText>(Rect(18, 18, 58, 64));
  116. portraitImage = std::make_shared<CAnimImage>("PortraitsLarge", 0, 0, 19, 19);
  117. for(int v = 0; v < GameConstants::PRIMARY_SKILLS; ++v)
  118. {
  119. auto area = std::make_shared<LRClickableAreaWTextComp>(Rect(30 + 70 * v, 109, 42, 64), CComponent::primskill);
  120. area->text = CGI->generaltexth->arraytxt[2+v];
  121. area->type = v;
  122. area->hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % CGI->generaltexth->primarySkillNames[v]);
  123. primSkillAreas.push_back(area);
  124. auto value = std::make_shared<CLabel>(53 + 70 * v, 166, FONT_SMALL, CENTER);
  125. primSkillValues.push_back(value);
  126. }
  127. auto primSkills = std::make_shared<CAnimation>("PSKIL42");
  128. primSkills->preload();
  129. primSkillImages.push_back(std::make_shared<CAnimImage>(primSkills, 0, 0, 32, 111));
  130. primSkillImages.push_back(std::make_shared<CAnimImage>(primSkills, 1, 0, 102, 111));
  131. primSkillImages.push_back(std::make_shared<CAnimImage>(primSkills, 2, 0, 172, 111));
  132. primSkillImages.push_back(std::make_shared<CAnimImage>(primSkills, 3, 0, 162, 230));
  133. primSkillImages.push_back(std::make_shared<CAnimImage>(primSkills, 4, 0, 20, 230));
  134. primSkillImages.push_back(std::make_shared<CAnimImage>(primSkills, 5, 0, 242, 111));
  135. specImage = std::make_shared<CAnimImage>("UN44", 0, 0, 18, 180);
  136. specArea = std::make_shared<LRClickableAreaWText>(Rect(18, 180, 136, 42), CGI->generaltexth->heroscrn[27]);
  137. specName = std::make_shared<CLabel>(69, 205);
  138. expArea = std::make_shared<LRClickableAreaWText>(Rect(18, 228, 136, 42), CGI->generaltexth->heroscrn[9]);
  139. morale = std::make_shared<MoraleLuckBox>(true, Rect(175, 179, 53, 45));
  140. luck = std::make_shared<MoraleLuckBox>(false, Rect(233, 179, 53, 45));
  141. spellPointsArea = std::make_shared<LRClickableAreaWText>(Rect(162,228, 136, 42), CGI->generaltexth->heroscrn[22]);
  142. expValue = std::make_shared<CLabel>(68, 252);
  143. manaValue = std::make_shared<CLabel>(211, 252);
  144. auto secSkills = std::make_shared<CAnimation>("SECSKILL");
  145. for(int i = 0; i < std::min<size_t>(hero->secSkills.size(), 8u); ++i)
  146. {
  147. Rect r = Rect(i%2 == 0 ? 18 : 162, 276 + 48 * (i/2), 136, 42);
  148. secSkillAreas.push_back(std::make_shared<LRClickableAreaWTextComp>(r, CComponent::secskill));
  149. secSkillImages.push_back(std::make_shared<CAnimImage>(secSkills, 0, 0, r.x, r.y));
  150. int x = (i % 2) ? 212 : 68;
  151. int y = 280 + 48 * (i/2);
  152. secSkillValues.push_back(std::make_shared<CLabel>(x, y, FONT_SMALL, TOPLEFT));
  153. secSkillNames.push_back(std::make_shared<CLabel>(x, y+20, FONT_SMALL, TOPLEFT));
  154. }
  155. // various texts
  156. labels.push_back(std::make_shared<CLabel>(52, 99, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[1]));
  157. labels.push_back(std::make_shared<CLabel>(123, 99, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[2]));
  158. labels.push_back(std::make_shared<CLabel>(193, 99, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[3]));
  159. labels.push_back(std::make_shared<CLabel>(262, 99, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[4]));
  160. labels.push_back(std::make_shared<CLabel>(69, 183, FONT_SMALL, TOPLEFT, Colors::YELLOW, CGI->generaltexth->jktexts[5]));
  161. labels.push_back(std::make_shared<CLabel>(69, 232, FONT_SMALL, TOPLEFT, Colors::YELLOW, CGI->generaltexth->jktexts[6]));
  162. labels.push_back(std::make_shared<CLabel>(213, 232, FONT_SMALL, TOPLEFT, Colors::YELLOW, CGI->generaltexth->jktexts[7]));
  163. update(hero);
  164. }
  165. void CHeroWindow::update(const CGHeroInstance * hero, bool redrawNeeded)
  166. {
  167. auto & heroscrn = CGI->generaltexth->heroscrn;
  168. if(!hero) //something strange... no hero? it shouldn't happen
  169. {
  170. logGlobal->error("Set nullptr hero? no way...");
  171. return;
  172. }
  173. assert(hero == curHero);
  174. name->setText(curHero->name);
  175. title->setText((boost::format(CGI->generaltexth->allTexts[342]) % curHero->level % curHero->type->heroClass->name).str());
  176. specArea->text = curHero->type->specDescr;
  177. specImage->setFrame(curHero->type->imageIndex);
  178. specName->setText(curHero->type->specName);
  179. tacticsButton = std::make_shared<CToggleButton>(Point(539, 483), "hsbtns8.def", std::make_pair(heroscrn[26], heroscrn[31]), 0, SDLK_b);
  180. tacticsButton->addHoverText(CButton::HIGHLIGHTED, CGI->generaltexth->heroscrn[25]);
  181. dismissButton->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->heroscrn[16]) % curHero->name % curHero->type->heroClass->name));
  182. portraitArea->hoverText = boost::str(boost::format(CGI->generaltexth->allTexts[15]) % curHero->name % curHero->type->heroClass->name);
  183. portraitArea->text = curHero->getBiography();
  184. portraitImage->setFrame(curHero->portrait);
  185. {
  186. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  187. if(!garr)
  188. {
  189. std::string helpBox = heroscrn[32];
  190. boost::algorithm::replace_first(helpBox, "%s", CGI->generaltexth->allTexts[43]);
  191. garr = std::make_shared<CGarrisonInt>(15, 485, 8, Point(), curHero);
  192. auto split = std::make_shared<CButton>(Point(539, 519), "hsbtns9.def", CButton::tooltip(CGI->generaltexth->allTexts[256], helpBox), [&](){ garr->splitClick(); });
  193. garr->addSplitBtn(split);
  194. }
  195. if(!arts)
  196. {
  197. arts = std::make_shared<CArtifactsOfHero>(Point(-65, -8), true);
  198. arts->setHero(curHero);
  199. addSet(arts);
  200. }
  201. int serial = LOCPLINT->cb->getHeroSerial(curHero, false);
  202. listSelection.reset();
  203. if(serial >= 0)
  204. listSelection = std::make_shared<CPicture>("HPSYYY", 612, 33 + serial * 54);
  205. }
  206. //primary skills support
  207. for(size_t g=0; g<primSkillAreas.size(); ++g)
  208. {
  209. primSkillAreas[g]->bonusValue = heroWArt.getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(g));
  210. primSkillValues[g]->setText(boost::lexical_cast<std::string>(primSkillAreas[g]->bonusValue));
  211. }
  212. //secondary skills support
  213. for(size_t g=0; g< secSkillAreas.size(); ++g)
  214. {
  215. int skill = curHero->secSkills[g].first;
  216. int level = curHero->getSecSkillLevel(SecondarySkill(curHero->secSkills[g].first));
  217. std::string skillName = CGI->skillh->skillName(skill);
  218. std::string skillValue = CGI->generaltexth->levels[level-1];
  219. secSkillAreas[g]->type = skill;
  220. secSkillAreas[g]->bonusValue = level;
  221. secSkillAreas[g]->text = CGI->skillh->skillInfo(skill, level);
  222. secSkillAreas[g]->hoverText = boost::str(boost::format(heroscrn[21]) % skillValue % skillName);
  223. secSkillImages[g]->setFrame(skill*3 + level + 2);
  224. secSkillNames[g]->setText(skillName);
  225. secSkillValues[g]->setText(skillValue);
  226. }
  227. std::ostringstream expstr;
  228. expstr << curHero->exp;
  229. expValue->setText(expstr.str());
  230. std::ostringstream manastr;
  231. manastr << curHero->mana << '/' << heroWArt.manaLimit();
  232. manaValue->setText(manastr.str());
  233. //printing experience - original format does not support ui64
  234. expArea->text = CGI->generaltexth->allTexts[2];
  235. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(curHero->level));
  236. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(curHero->level+1)));
  237. boost::replace_first(expArea->text, "%d", boost::lexical_cast<std::string>(curHero->exp));
  238. //printing spell points, boost::format can't be used due to locale issues
  239. spellPointsArea->text = CGI->generaltexth->allTexts[205];
  240. boost::replace_first(spellPointsArea->text, "%s", boost::lexical_cast<std::string>(curHero->name));
  241. boost::replace_first(spellPointsArea->text, "%d", boost::lexical_cast<std::string>(curHero->mana));
  242. boost::replace_first(spellPointsArea->text, "%d", boost::lexical_cast<std::string>(heroWArt.manaLimit()));
  243. //if we have exchange window with this curHero open
  244. bool noDismiss=false;
  245. for(auto isa : GH.listInt)
  246. {
  247. if(CExchangeWindow * cew = dynamic_cast<CExchangeWindow*>(isa.get()))
  248. {
  249. for(int g=0; g < cew->heroInst.size(); ++g)
  250. if(cew->heroInst[g] == curHero)
  251. noDismiss = true;
  252. }
  253. if(dynamic_cast<CKingdomInterface*>(isa.get()))
  254. noDismiss = true;
  255. }
  256. //if player only have one hero and no towns
  257. if(!LOCPLINT->cb->howManyTowns() && LOCPLINT->cb->howManyHeroes() == 1)
  258. noDismiss = true;
  259. if(curHero->isMissionCritical())
  260. noDismiss = true;
  261. dismissButton->block(!!curHero->visitedTown || noDismiss);
  262. if(curHero->getSecSkillLevel(SecondarySkill::TACTICS) == 0)
  263. {
  264. tacticsButton->block(true);
  265. }
  266. else
  267. {
  268. tacticsButton->block(false);
  269. tacticsButton->addCallback([&](bool on){curHero->tacticFormationEnabled = on;});
  270. }
  271. formations->resetCallback();
  272. //setting formations
  273. formations->setSelected(curHero->formation);
  274. formations->addCallback([=](int value){ LOCPLINT->cb->setFormation(curHero, value);});
  275. morale->set(&heroWArt);
  276. luck->set(&heroWArt);
  277. if(redrawNeeded)
  278. redraw();
  279. }
  280. void CHeroWindow::dismissCurrent()
  281. {
  282. CFunctionList<void()> ony = [=](){ close(); };
  283. ony += [=](){ LOCPLINT->cb->dismissHero(curHero); };
  284. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[22], ony, nullptr);
  285. }
  286. void CHeroWindow::commanderWindow()
  287. {
  288. //bool artSelected = false;
  289. const std::shared_ptr<CArtifactsOfHero::SCommonPart> commonInfo = getCommonPart();
  290. if(const CArtifactInstance *art = commonInfo->src.art)
  291. {
  292. const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
  293. //artSelected = true;
  294. ArtifactPosition freeSlot = art->firstAvailableSlot (curHero->commander);
  295. if(freeSlot < ArtifactPosition::COMMANDER_AFTER_LAST) //we don't want to put it in commander's backpack!
  296. {
  297. ArtifactLocation src(srcHero, commonInfo->src.slotID);
  298. ArtifactLocation dst(curHero->commander.get(), freeSlot);
  299. if(art->canBePutAt(dst, true))
  300. { //equip clicked stack
  301. if(dst.getArt())
  302. {
  303. LOCPLINT->cb->swapArtifacts (dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
  304. }
  305. LOCPLINT->cb->swapArtifacts(src, dst);
  306. }
  307. }
  308. }
  309. else
  310. {
  311. GH.pushIntT<CStackWindow>(curHero->commander, false);
  312. }
  313. }
  314. void CHeroWindow::updateGarrisons()
  315. {
  316. garr->recreateSlots();
  317. morale->set(&heroWArt);
  318. }