CHeroWindow.cpp 14 KB

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