CCreatureWindow.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * CCreatureWindow.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 "CCreatureWindow.h"
  12. #include <vcmi/spells/Spell.h>
  13. #include <vcmi/spells/Service.h>
  14. #include "../CPlayerInterface.h"
  15. #include "../render/Canvas.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/CComponent.h"
  18. #include "../widgets/CComponentHolder.h"
  19. #include "../widgets/Images.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../widgets/ObjectLists.h"
  22. #include "../widgets/GraphicalPrimitiveCanvas.h"
  23. #include "../windows/InfoWindows.h"
  24. #include "../GameEngine.h"
  25. #include "../GameInstance.h"
  26. #include "../gui/Shortcut.h"
  27. #include "../battle/BattleInterface.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CBonusTypeHandler.h"
  30. #include "../../lib/CStack.h"
  31. #include "../../lib/GameLibrary.h"
  32. #include "../../lib/IGameSettings.h"
  33. #include "../../lib/bonuses/Propagators.h"
  34. #include "../../lib/entities/artifact/ArtifactUtils.h"
  35. #include "../../lib/entities/hero/CHeroHandler.h"
  36. #include "../../lib/gameState/CGameState.h"
  37. #include "../../lib/gameState/UpgradeInfo.h"
  38. #include "../../lib/networkPacks/ArtifactLocation.h"
  39. #include "../../lib/texts/CGeneralTextHandler.h"
  40. #include "../../lib/texts/TextOperations.h"
  41. class CCreatureArtifactInstance;
  42. class CSelectableSkill;
  43. class UnitView
  44. {
  45. public:
  46. // helper structs
  47. struct CommanderLevelInfo
  48. {
  49. std::vector<ui32> skills;
  50. std::function<void(ui32)> callback;
  51. };
  52. struct StackDismissInfo
  53. {
  54. std::function<void()> callback;
  55. };
  56. struct StackUpgradeInfo
  57. {
  58. StackUpgradeInfo() = delete;
  59. StackUpgradeInfo(const UpgradeInfo & upgradeInfo)
  60. : info(upgradeInfo)
  61. { }
  62. UpgradeInfo info;
  63. std::function<void(CreatureID)> callback;
  64. };
  65. // pointers to permanent objects in game state
  66. const CCreature * creature;
  67. const CCommanderInstance * commander;
  68. const CStackInstance * stackNode;
  69. const CStack * stack;
  70. const CGHeroInstance * owner;
  71. // temporary objects which should be kept as copy if needed
  72. std::optional<CommanderLevelInfo> levelupInfo;
  73. std::optional<StackDismissInfo> dismissInfo;
  74. std::optional<StackUpgradeInfo> upgradeInfo;
  75. // misc fields
  76. unsigned int creatureCount;
  77. bool popupWindow;
  78. UnitView()
  79. : creature(nullptr),
  80. commander(nullptr),
  81. stackNode(nullptr),
  82. stack(nullptr),
  83. owner(nullptr),
  84. creatureCount(0),
  85. popupWindow(false)
  86. {
  87. }
  88. std::string getName() const
  89. {
  90. if(commander)
  91. return commander->getType()->getNameSingularTranslated();
  92. else
  93. return creature->getNamePluralTranslated();
  94. }
  95. private:
  96. };
  97. CCommanderSkillIcon::CCommanderSkillIcon(std::shared_ptr<CIntObject> object_, bool isMasterAbility_, std::function<void()> callback)
  98. : object(),
  99. isMasterAbility(isMasterAbility_),
  100. isSelected(false),
  101. callback(callback)
  102. {
  103. pos = object_->pos;
  104. this->isMasterAbility = isMasterAbility_;
  105. setObject(object_);
  106. }
  107. void CCommanderSkillIcon::setObject(std::shared_ptr<CIntObject> newObject)
  108. {
  109. if(object)
  110. removeChild(object.get());
  111. object = newObject;
  112. addChild(object.get());
  113. object->moveTo(pos.topLeft());
  114. redraw();
  115. }
  116. void CCommanderSkillIcon::clickPressed(const Point & cursorPosition)
  117. {
  118. callback();
  119. isSelected = true;
  120. }
  121. void CCommanderSkillIcon::deselect()
  122. {
  123. isSelected = false;
  124. }
  125. bool CCommanderSkillIcon::getIsMasterAbility()
  126. {
  127. return isMasterAbility;
  128. }
  129. void CCommanderSkillIcon::show(Canvas &to)
  130. {
  131. CIntObject::show(to);
  132. if(isMasterAbility && isSelected)
  133. to.drawBorder(pos, Colors::YELLOW, 2);
  134. }
  135. static ImagePath skillToFile(int skill, int level, bool selected)
  136. {
  137. // FIXME: is this a correct handling?
  138. // level 0 = skill not present, use image with "no" suffix
  139. // level 1-5 = skill available, mapped to images indexed as 0-4
  140. // selecting skill means that it will appear one level higher (as if already upgraded)
  141. std::string file = "zvs/Lib1.res/_";
  142. switch (skill)
  143. {
  144. case ECommander::ATTACK:
  145. file += "AT";
  146. break;
  147. case ECommander::DEFENSE:
  148. file += "DF";
  149. break;
  150. case ECommander::HEALTH:
  151. file += "HP";
  152. break;
  153. case ECommander::DAMAGE:
  154. file += "DM";
  155. break;
  156. case ECommander::SPEED:
  157. file += "SP";
  158. break;
  159. case ECommander::SPELL_POWER:
  160. file += "MP";
  161. break;
  162. }
  163. std::string suffix;
  164. if (selected)
  165. level++; // UI will display resulting level
  166. if (level == 0)
  167. suffix = "no"; //not available - no number
  168. else
  169. suffix = std::to_string(level-1);
  170. if (selected)
  171. suffix += "="; //level-up highlight
  172. return ImagePath::builtin(file + suffix + ".bmp");
  173. }
  174. CStackWindow::CWindowSection::CWindowSection(CStackWindow * parent, const ImagePath & backgroundPath, int yOffset)
  175. : parent(parent)
  176. {
  177. pos.y += yOffset;
  178. OBJECT_CONSTRUCTION;
  179. if(!backgroundPath.empty())
  180. {
  181. background = std::make_shared<CPicture>(backgroundPath);
  182. pos.w = background->pos.w;
  183. pos.h = background->pos.h;
  184. }
  185. }
  186. CStackWindow::ActiveSpellsSection::ActiveSpellsSection(CStackWindow * owner, int yOffset)
  187. : CWindowSection(owner, ImagePath::builtin("stackWindow/spell-effects"), yOffset)
  188. {
  189. static const Point firstPos(6, 2); // position of 1st spell box
  190. static const Point offset(54, 0); // offset of each spell box from previous
  191. OBJECT_CONSTRUCTION;
  192. const CStack * battleStack = parent->info->stack;
  193. assert(battleStack); // Section should be created only for battles
  194. //spell effects
  195. int printed=0; //how many effect pics have been printed
  196. std::vector<SpellID> spells = battleStack->activeSpells();
  197. for(SpellID effect : spells)
  198. {
  199. const spells::Spell * spell = LIBRARY->spells()->getById(effect);
  200. std::string spellText;
  201. //not all effects have graphics (for eg. Acid Breath)
  202. //for modded spells iconEffect is added to SpellInt.def
  203. const bool hasGraphics = (effect < SpellID::THUNDERBOLT) || (effect >= SpellID::AFTER_LAST);
  204. if (hasGraphics)
  205. {
  206. spellText = LIBRARY->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  207. boost::replace_first(spellText, "%s", spell->getNameTranslated());
  208. //FIXME: support permanent duration
  209. auto spellBonuses = battleStack->getBonuses(Selector::source(BonusSource::SPELL_EFFECT, BonusSourceID(effect)));
  210. if (spellBonuses->empty())
  211. throw std::runtime_error("Failed to find effects for spell " + effect.toSpell()->getJsonKey());
  212. int duration = spellBonuses->front()->turnsRemain;
  213. boost::replace_first(spellText, "%d", std::to_string(duration));
  214. spellIcons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("SpellInt"), effect + 1, 0, firstPos.x + offset.x * printed, firstPos.y + offset.y * printed));
  215. labels.push_back(std::make_shared<CLabel>(firstPos.x + offset.x * printed + 46, firstPos.y + offset.y * printed + 36, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(duration)));
  216. clickableAreas.push_back(std::make_shared<LRClickableAreaWText>(Rect(firstPos + offset * printed, Point(50, 38)), spellText, spellText));
  217. if(++printed >= 8) // interface limit reached
  218. break;
  219. }
  220. }
  221. }
  222. CStackWindow::BonusLineSection::BonusLineSection(CStackWindow * owner, size_t lineIndex)
  223. : CWindowSection(owner, ImagePath::builtin("stackWindow/bonus-effects"), 0)
  224. {
  225. OBJECT_CONSTRUCTION;
  226. static const std::array<Point, 2> offset =
  227. {
  228. Point(6, 4),
  229. Point(214, 4)
  230. };
  231. auto drawBonusSource = [this](int leftRight, Point p, BonusInfo & bi)
  232. {
  233. std::map<BonusSource, ColorRGBA> bonusColors = {
  234. {BonusSource::ARTIFACT, Colors::GREEN},
  235. {BonusSource::ARTIFACT_INSTANCE, Colors::GREEN},
  236. {BonusSource::CREATURE_ABILITY, Colors::YELLOW},
  237. {BonusSource::SPELL_EFFECT, Colors::ORANGE},
  238. {BonusSource::SECONDARY_SKILL, Colors::PURPLE},
  239. {BonusSource::HERO_SPECIAL, Colors::PURPLE},
  240. {BonusSource::STACK_EXPERIENCE, Colors::CYAN},
  241. {BonusSource::COMMANDER, Colors::CYAN},
  242. };
  243. std::map<BonusSource, std::string> bonusNames = {
  244. {BonusSource::ARTIFACT, LIBRARY->generaltexth->translate("vcmi.bonusSource.artifact")},
  245. {BonusSource::ARTIFACT_INSTANCE, LIBRARY->generaltexth->translate("vcmi.bonusSource.artifact")},
  246. {BonusSource::CREATURE_ABILITY, LIBRARY->generaltexth->translate("vcmi.bonusSource.creature")},
  247. {BonusSource::SPELL_EFFECT, LIBRARY->generaltexth->translate("vcmi.bonusSource.spell")},
  248. {BonusSource::SECONDARY_SKILL, LIBRARY->generaltexth->translate("vcmi.bonusSource.hero")},
  249. {BonusSource::HERO_SPECIAL, LIBRARY->generaltexth->translate("vcmi.bonusSource.hero")},
  250. {BonusSource::STACK_EXPERIENCE, LIBRARY->generaltexth->translate("vcmi.bonusSource.commander")},
  251. {BonusSource::COMMANDER, LIBRARY->generaltexth->translate("vcmi.bonusSource.commander")},
  252. };
  253. auto c = bonusColors.count(bi.bonusSource) ? bonusColors[bi.bonusSource] : ColorRGBA(192, 192, 192);
  254. std::string t = bonusNames.count(bi.bonusSource) ? bonusNames[bi.bonusSource] : LIBRARY->generaltexth->translate("vcmi.bonusSource.other");
  255. int maxLen = 50;
  256. EFonts f = FONT_TINY;
  257. Point pText = p + Point(4, 38);
  258. // 1px Black border
  259. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x - 1, pText.y, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  260. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x + 1, pText.y, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  261. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x, pText.y - 1, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  262. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x, pText.y + 1, f, ETextAlignment::TOPLEFT, Colors::BLACK, t, maxLen));
  263. bonusSource[leftRight].push_back(std::make_shared<CLabel>(pText.x, pText.y, f, ETextAlignment::TOPLEFT, c, t, maxLen));
  264. frame[leftRight] = std::make_shared<GraphicalPrimitiveCanvas>(Rect(p.x, p.y, 52, 52));
  265. frame[leftRight]->addRectangle(Point(0, 0), Point(52, 52), c);
  266. };
  267. for(size_t leftRight : {0, 1})
  268. {
  269. auto position = offset[leftRight];
  270. size_t bonusIndex = lineIndex * 2 + leftRight;
  271. if(parent->activeBonuses.size() > bonusIndex)
  272. {
  273. BonusInfo & bi = parent->activeBonuses[bonusIndex];
  274. if (!bi.imagePath.empty())
  275. icon[leftRight] = std::make_shared<CPicture>(bi.imagePath, position.x, position.y);
  276. name[leftRight] = std::make_shared<CLabel>(position.x + 60, position.y + 2, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, bi.name, 137);
  277. description[leftRight] = std::make_shared<CMultiLineLabel>(Rect(position.x + 60, position.y + 20, 137, 26), FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, bi.description);
  278. drawBonusSource(leftRight, Point(position.x - 1, position.y - 1), bi);
  279. }
  280. }
  281. }
  282. CStackWindow::BonusesSection::BonusesSection(CStackWindow * owner, int yOffset, std::optional<size_t> preferredSize):
  283. CWindowSection(owner, {}, yOffset)
  284. {
  285. OBJECT_CONSTRUCTION;
  286. // size of single image for an item
  287. static const int itemHeight = 59;
  288. size_t totalSize = (owner->activeBonuses.size() + 1) / 2;
  289. size_t visibleSize = preferredSize.value_or(std::min<size_t>(3, totalSize));
  290. pos.w = owner->pos.w;
  291. pos.h = itemHeight * (int)visibleSize;
  292. auto onCreate = [=](size_t index) -> std::shared_ptr<CIntObject>
  293. {
  294. return std::make_shared<BonusLineSection>(owner, index);
  295. };
  296. lines = std::make_shared<CListBox>(onCreate, Point(0, 0), Point(0, itemHeight), visibleSize, totalSize, 0, totalSize > 3 ? 1 : 0, Rect(pos.w - 15, 0, pos.h, pos.h));
  297. }
  298. CStackWindow::ButtonsSection::ButtonsSection(CStackWindow * owner, int yOffset)
  299. : CWindowSection(owner, ImagePath::builtin("stackWindow/button-panel"), yOffset)
  300. {
  301. OBJECT_CONSTRUCTION;
  302. if(parent->info->dismissInfo && parent->info->dismissInfo->callback)
  303. {
  304. auto onDismiss = [this]()
  305. {
  306. parent->info->dismissInfo->callback();
  307. parent->close();
  308. };
  309. auto onClick = [=] ()
  310. {
  311. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[12], onDismiss, nullptr);
  312. };
  313. dismiss = std::make_shared<CButton>(Point(5, 5),AnimationPath::builtin("IVIEWCR2.DEF"), LIBRARY->generaltexth->zelp[445], onClick, EShortcut::HERO_DISMISS);
  314. }
  315. if(parent->info->upgradeInfo && !parent->info->commander)
  316. {
  317. // used space overlaps with commander switch button
  318. // besides - should commander really be upgradeable?
  319. auto & upgradeInfo = parent->info->upgradeInfo.value();
  320. const size_t buttonsToCreate = std::min<size_t>(upgradeInfo.info.size(), upgrade.size());
  321. for(size_t buttonIndex = 0; buttonIndex < buttonsToCreate; buttonIndex++)
  322. {
  323. TResources totalCost = upgradeInfo.info.getAvailableUpgradeCosts().at(buttonIndex) * parent->info->creatureCount;
  324. auto onUpgrade = [this, upgradeInfo, buttonIndex]()
  325. {
  326. upgradeInfo.callback(upgradeInfo.info.getAvailableUpgrades().at(buttonIndex));
  327. parent->close();
  328. };
  329. auto onClick = [totalCost, onUpgrade]()
  330. {
  331. std::vector<std::shared_ptr<CComponent>> resComps;
  332. for(TResources::nziterator i(totalCost); i.valid(); i++)
  333. {
  334. resComps.push_back(std::make_shared<CComponent>(ComponentType::RESOURCE, i->resType, i->resVal));
  335. }
  336. if(GAME->interface()->cb->getResourceAmount().canAfford(totalCost))
  337. {
  338. GAME->interface()->showYesNoDialog(LIBRARY->generaltexth->allTexts[207], onUpgrade, nullptr, resComps);
  339. }
  340. else
  341. {
  342. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->allTexts[314], resComps);
  343. }
  344. };
  345. auto upgradeBtn = std::make_shared<CButton>(Point(221 + (int)buttonIndex * 40, 5), AnimationPath::builtin("stackWindow/upgradeButton"), LIBRARY->generaltexth->zelp[446], onClick);
  346. upgradeBtn->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), LIBRARY->creh->objects[upgradeInfo.info.getAvailableUpgrades()[buttonIndex]]->getIconIndex()));
  347. if(buttonsToCreate == 1) // single upgrade available
  348. upgradeBtn->assignedKey = EShortcut::RECRUITMENT_UPGRADE;
  349. upgrade[buttonIndex] = upgradeBtn;
  350. }
  351. }
  352. if(parent->info->commander)
  353. {
  354. for(size_t buttonIndex = 0; buttonIndex < 2; buttonIndex++)
  355. {
  356. std::string btnIDs[2] = { "showSkills", "showBonuses" };
  357. auto onSwitch = [buttonIndex, this]()
  358. {
  359. logAnim->debug("Switch %d->%d", parent->activeTab, buttonIndex);
  360. parent->switchButtons[parent->activeTab]->enable();
  361. parent->commanderTab->setActive(buttonIndex);
  362. parent->switchButtons[buttonIndex]->disable();
  363. parent->redraw(); // FIXME: enable/disable don't redraw screen themselves
  364. };
  365. std::string tooltipText = "vcmi.creatureWindow." + btnIDs[buttonIndex];
  366. parent->switchButtons[buttonIndex] = std::make_shared<CButton>(Point(302 + (int)buttonIndex*40, 5), AnimationPath::builtin("stackWindow/upgradeButton"), CButton::tooltipLocalized(tooltipText), onSwitch);
  367. parent->switchButtons[buttonIndex]->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/switchModeIcons"), buttonIndex));
  368. }
  369. parent->switchButtons[parent->activeTab]->disable();
  370. }
  371. exit = std::make_shared<CButton>(Point(382, 5), AnimationPath::builtin("hsbtns.def"), LIBRARY->generaltexth->zelp[447], [this](){ parent->close(); }, EShortcut::GLOBAL_RETURN);
  372. }
  373. CStackWindow::CommanderMainSection::CommanderMainSection(CStackWindow * owner, int yOffset)
  374. : CWindowSection(owner, ImagePath::builtin("stackWindow/commander-bg"), yOffset)
  375. {
  376. OBJECT_CONSTRUCTION;
  377. auto getSkillPos = [](int index)
  378. {
  379. return Point(10 + 80 * (index%3), 20 + 80 * (index/3));
  380. };
  381. auto getSkillImage = [this](int skillIndex)
  382. {
  383. bool selected = ((parent->selectedSkill == skillIndex) && parent->info->levelupInfo );
  384. return skillToFile(skillIndex, parent->info->commander->secondarySkills[skillIndex], selected);
  385. };
  386. auto getSkillDescription = [this](int skillIndex) -> std::string
  387. {
  388. return parent->getCommanderSkillDescription(skillIndex, parent->info->commander->secondarySkills[skillIndex]);
  389. };
  390. for(int index = ECommander::ATTACK; index <= ECommander::SPELL_POWER; ++index)
  391. {
  392. Point skillPos = getSkillPos(index);
  393. auto icon = std::make_shared<CCommanderSkillIcon>(std::make_shared<CPicture>(getSkillImage(index), skillPos.x, skillPos.y), false, [=]()
  394. {
  395. GAME->interface()->showInfoDialog(getSkillDescription(index));
  396. });
  397. icon->text = getSkillDescription(index); //used to handle right click description via LRClickableAreaWText::ClickRight()
  398. if(parent->selectedSkill == index)
  399. parent->selectedIcon = icon;
  400. if(parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch
  401. {
  402. if(parent->selectedSkill == index)
  403. parent->setSelection(index, icon);
  404. icon->callback = [this, index, icon]()
  405. {
  406. parent->setSelection(index, icon);
  407. };
  408. }
  409. skillIcons.push_back(icon);
  410. }
  411. auto getArtifactPos = [](int index)
  412. {
  413. return Point(269 + 47 * (index % 3), 22 + 47 * (index / 3));
  414. };
  415. for(auto equippedArtifact : parent->info->commander->artifactsWorn)
  416. {
  417. Point artPos = getArtifactPos(equippedArtifact.first);
  418. const auto commanderArt = equippedArtifact.second.getArt();
  419. assert(commanderArt);
  420. auto artPlace = std::make_shared<CCommanderArtPlace>(artPos, parent->info->owner, equippedArtifact.first, commanderArt->getTypeId());
  421. artifacts.push_back(artPlace);
  422. }
  423. if(parent->info->levelupInfo)
  424. {
  425. abilitiesBackground = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/commander-abilities.png"));
  426. abilitiesBackground->moveBy(Point(0, pos.h));
  427. size_t abilitiesCount = boost::range::count_if(parent->info->levelupInfo->skills, [](ui32 skillID)
  428. {
  429. return skillID >= 100;
  430. });
  431. auto onCreate = [this](size_t index)->std::shared_ptr<CIntObject>
  432. {
  433. for(auto skillID : parent->info->levelupInfo->skills)
  434. {
  435. if(index == 0 && skillID >= 100)
  436. {
  437. const auto bonuses = LIBRARY->creh->skillRequirements[skillID-100].first;
  438. const CStackInstance * stack = parent->info->commander;
  439. auto icon = std::make_shared<CCommanderSkillIcon>(std::make_shared<CPicture>(stack->bonusToGraphics(bonuses[0])), true, [](){});
  440. icon->callback = [this, skillID, icon]()
  441. {
  442. parent->setSelection(skillID, icon);
  443. };
  444. for(int i = 0; i < bonuses.size(); i++)
  445. {
  446. icon->text += stack->bonusToString(bonuses[i], true) + "\n";
  447. icon->hoverText += stack->bonusToString(bonuses[i], false) + "\n";
  448. }
  449. return icon;
  450. }
  451. if(skillID >= 100)
  452. index--;
  453. }
  454. return nullptr;
  455. };
  456. abilities = std::make_shared<CListBox>(onCreate, Point(38, 3+pos.h), Point(63, 0), 6, abilitiesCount);
  457. abilities->setRedrawParent(true);
  458. leftBtn = std::make_shared<CButton>(Point(10, pos.h + 6), AnimationPath::builtin("hsbtns3.def"), CButton::tooltip(), [this](){ abilities->moveToPrev(); }, EShortcut::MOVE_LEFT);
  459. rightBtn = std::make_shared<CButton>(Point(411, pos.h + 6), AnimationPath::builtin("hsbtns5.def"), CButton::tooltip(), [this](){ abilities->moveToNext(); }, EShortcut::MOVE_RIGHT);
  460. if(abilitiesCount <= 6)
  461. {
  462. leftBtn->block(true);
  463. rightBtn->block(true);
  464. }
  465. pos.h += abilitiesBackground->pos.h;
  466. }
  467. }
  468. CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool showExp, bool showArt)
  469. : CWindowSection(owner, getBackgroundName(showExp, showArt), yOffset)
  470. {
  471. OBJECT_CONSTRUCTION;
  472. statNames =
  473. {
  474. LIBRARY->generaltexth->primarySkillNames[0], //ATTACK
  475. LIBRARY->generaltexth->primarySkillNames[1],//DEFENCE
  476. LIBRARY->generaltexth->allTexts[198],//SHOTS
  477. LIBRARY->generaltexth->allTexts[199],//DAMAGE
  478. LIBRARY->generaltexth->allTexts[388],//HEALTH
  479. LIBRARY->generaltexth->allTexts[200],//HEALTH_LEFT
  480. LIBRARY->generaltexth->zelp[441].first,//SPEED
  481. LIBRARY->generaltexth->allTexts[399]//MANA
  482. };
  483. statFormats =
  484. {
  485. "%d (%d)",
  486. "%d (%d)",
  487. "%d (%d)",
  488. "%d - %d",
  489. "%d (%d)",
  490. "%d (%d)",
  491. "%d (%d)",
  492. "%d (%d)"
  493. };
  494. animation = std::make_shared<CCreaturePic>(5, 41, parent->info->creature);
  495. animationArea = std::make_shared<LRClickableArea>(Rect(5, 41, 100, 130), nullptr, [&]{
  496. if(!parent->info->creature->getDescriptionTranslated().empty())
  497. CRClickPopup::createAndPush(parent->info->creature->getDescriptionTranslated());
  498. });
  499. if(parent->info->stackNode != nullptr && parent->info->commander == nullptr)
  500. {
  501. //normal stack, not a commander and not non-existing stack (e.g. recruitment dialog)
  502. animation->setAmount(parent->info->creatureCount);
  503. }
  504. name = std::make_shared<CLabel>(215, 13, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, parent->info->getName());
  505. const CStack* battleStack = parent->info->stack;
  506. int dmgMultiply = 1;
  507. if (battleStack != nullptr && battleStack->hasBonusOfType(BonusType::SIEGE_WEAPON))
  508. {
  509. static const auto bonusSelector =
  510. Selector::sourceTypeSel(BonusSource::ARTIFACT).Or(
  511. Selector::sourceTypeSel(BonusSource::HERO_BASE_SKILL)).And(
  512. Selector::typeSubtype(BonusType::PRIMARY_SKILL, BonusSubtypeID(PrimarySkill::ATTACK)));
  513. dmgMultiply += battleStack->valOfBonuses(bonusSelector);
  514. }
  515. icons = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/icons"), 117, 32);
  516. morale = std::make_shared<MoraleLuckBox>(true, Rect(Point(321, 110), Point(42, 42) ));
  517. luck = std::make_shared<MoraleLuckBox>(false, Rect(Point(375, 110), Point(42, 42) ));
  518. if(battleStack != nullptr) // in battle
  519. {
  520. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(battleStack->isShooter()), battleStack->getAttack(battleStack->isShooter()));
  521. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(battleStack->isShooter()), battleStack->getDefense(battleStack->isShooter()));
  522. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(battleStack->isShooter()) * dmgMultiply, battleStack->getMaxDamage(battleStack->isShooter()) * dmgMultiply);
  523. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), battleStack->getMaxHealth());
  524. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), battleStack->getMovementRange());
  525. if(battleStack->isShooter())
  526. addStatLabel(EStat::SHOTS, battleStack->shots.total(), battleStack->shots.available());
  527. if(battleStack->isCaster())
  528. addStatLabel(EStat::MANA, battleStack->casts.total(), battleStack->casts.available());
  529. addStatLabel(EStat::HEALTH_LEFT, battleStack->getFirstHPleft());
  530. morale->set(battleStack);
  531. luck->set(battleStack);
  532. }
  533. else
  534. {
  535. const bool shooter = parent->info->stackNode->hasBonusOfType(BonusType::SHOOTER) && parent->info->stackNode->valOfBonuses(BonusType::SHOTS);
  536. const bool caster = parent->info->stackNode->valOfBonuses(BonusType::CASTS);
  537. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(shooter), parent->info->stackNode->getAttack(shooter));
  538. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(shooter), parent->info->stackNode->getDefense(shooter));
  539. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(shooter), parent->info->stackNode->getMaxDamage(shooter));
  540. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), parent->info->stackNode->getMaxHealth());
  541. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), parent->info->stackNode->getMovementRange());
  542. if(shooter)
  543. addStatLabel(EStat::SHOTS, parent->info->stackNode->valOfBonuses(BonusType::SHOTS));
  544. if(caster)
  545. addStatLabel(EStat::MANA, parent->info->stackNode->valOfBonuses(BonusType::CASTS));
  546. morale->set(parent->info->stackNode);
  547. luck->set(parent->info->stackNode);
  548. }
  549. if(showExp)
  550. {
  551. const CStackInstance * stack = parent->info->stackNode;
  552. Point pos = showArt ? Point(321, 32) : Point(347, 32);
  553. if(parent->info->commander)
  554. {
  555. const CCommanderInstance * commander = parent->info->commander;
  556. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 4, 0, pos.x, pos.y);
  557. auto area = std::make_shared<LRClickableAreaWTextComp>(Rect(pos.x, pos.y, 44, 44), ComponentType::EXPERIENCE);
  558. expArea = area;
  559. area->text = LIBRARY->generaltexth->allTexts[2];
  560. area->component.value = commander->getExpRank();
  561. boost::replace_first(area->text, "%d", std::to_string(commander->getExpRank()));
  562. boost::replace_first(area->text, "%d", std::to_string(LIBRARY->heroh->reqExp(commander->getExpRank() + 1)));
  563. boost::replace_first(area->text, "%d", std::to_string(commander->getAverageExperience()));
  564. }
  565. else
  566. {
  567. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/levels"), stack->getExpRank(), 0, pos.x, pos.y);
  568. expArea = std::make_shared<LRClickableAreaWText>(Rect(pos.x, pos.y, 44, 44));
  569. expArea->text = parent->generateStackExpDescription();
  570. }
  571. expLabel = std::make_shared<CLabel>(
  572. pos.x + 21, pos.y + 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  573. TextOperations::formatMetric(stack->getAverageExperience(), 6));
  574. }
  575. if(showArt)
  576. {
  577. Point pos = showExp ? Point(375, 32) : Point(347, 32);
  578. // ALARMA: do not refactor this into a separate function
  579. // otherwise, artifact icon is drawn near the hero's portrait
  580. // this is really strange
  581. auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  582. if(art)
  583. {
  584. parent->stackArtifact = std::make_shared<CArtPlace>(pos, art->getTypeId());
  585. parent->stackArtifact->setShowPopupCallback([](CComponentHolder & artPlace, const Point & cursorPosition)
  586. {
  587. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  588. });
  589. if(parent->info->owner)
  590. {
  591. parent->stackArtifactButton = std::make_shared<CButton>(
  592. Point(pos.x - 2 , pos.y + 46), AnimationPath::builtin("stackWindow/cancelButton"),
  593. CButton::tooltipLocalized("vcmi.creatureWindow.returnArtifact"), [this]()
  594. {
  595. parent->removeStackArtifact(ArtifactPosition::CREATURE_SLOT);
  596. });
  597. }
  598. }
  599. }
  600. }
  601. ImagePath CStackWindow::MainSection::getBackgroundName(bool showExp, bool showArt)
  602. {
  603. if(showExp && showArt)
  604. return ImagePath::builtin("stackWindow/info-panel-2");
  605. else if(showExp || showArt)
  606. return ImagePath::builtin("stackWindow/info-panel-1");
  607. else
  608. return ImagePath::builtin("stackWindow/info-panel-0");
  609. }
  610. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value1, int64_t value2)
  611. {
  612. const auto title = statNames.at(static_cast<size_t>(index));
  613. stats.push_back(std::make_shared<CLabel>(145, 32 + (int)index*19, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, title));
  614. const bool useRange = value1 != value2;
  615. std::string formatStr = useRange ? statFormats.at(static_cast<size_t>(index)) : "%d";
  616. boost::format fmt(formatStr);
  617. fmt % value1;
  618. if(useRange)
  619. fmt % value2;
  620. stats.push_back(std::make_shared<CLabel>(307, 48 + (int)index*19, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, fmt.str()));
  621. }
  622. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value)
  623. {
  624. addStatLabel(index, value, value);
  625. }
  626. CStackWindow::CStackWindow(const CStack * stack, bool popup)
  627. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  628. info(std::make_unique<UnitView>())
  629. {
  630. info->stack = stack;
  631. info->stackNode = stack->base;
  632. info->commander = dynamic_cast<const CCommanderInstance*>(stack->base);
  633. info->creature = stack->unitType();
  634. info->creatureCount = stack->getCount();
  635. info->popupWindow = popup;
  636. init();
  637. }
  638. CStackWindow::CStackWindow(const CCreature * creature, bool popup)
  639. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  640. info(std::make_unique<UnitView>())
  641. {
  642. info->creature = creature;
  643. info->popupWindow = popup;
  644. init();
  645. }
  646. CStackWindow::CStackWindow(const CStackInstance * stack, bool popup)
  647. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  648. info(std::make_unique<UnitView>())
  649. {
  650. info->stackNode = stack;
  651. info->creature = stack->getCreature();
  652. info->creatureCount = stack->getCount();
  653. info->popupWindow = popup;
  654. info->owner = dynamic_cast<const CGHeroInstance *> (stack->getArmy());
  655. init();
  656. }
  657. CStackWindow::CStackWindow(const CStackInstance * stack, std::function<void()> dismiss, const UpgradeInfo & upgradeInfo, std::function<void(CreatureID)> callback)
  658. : CWindowObject(BORDERED),
  659. info(std::make_unique<UnitView>())
  660. {
  661. info->stackNode = stack;
  662. info->creature = stack->getCreature();
  663. info->creatureCount = stack->getCount();
  664. if(upgradeInfo.canUpgrade())
  665. {
  666. info->upgradeInfo = std::make_optional(UnitView::StackUpgradeInfo(upgradeInfo));
  667. info->upgradeInfo->callback = callback;
  668. }
  669. info->dismissInfo = std::make_optional(UnitView::StackDismissInfo());
  670. info->dismissInfo->callback = dismiss;
  671. info->owner = dynamic_cast<const CGHeroInstance *> (stack->getArmy());
  672. init();
  673. }
  674. CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup)
  675. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  676. info(std::make_unique<UnitView>())
  677. {
  678. info->stackNode = commander;
  679. info->creature = commander->getCreature();
  680. info->commander = commander;
  681. info->creatureCount = 1;
  682. info->popupWindow = popup;
  683. info->owner = dynamic_cast<const CGHeroInstance *> (commander->getArmy());
  684. init();
  685. }
  686. CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vector<ui32> &skills, std::function<void(ui32)> callback)
  687. : CWindowObject(BORDERED),
  688. info(std::make_unique<UnitView>())
  689. {
  690. info->stackNode = commander;
  691. info->creature = commander->getCreature();
  692. info->commander = commander;
  693. info->creatureCount = 1;
  694. info->levelupInfo = std::make_optional(UnitView::CommanderLevelInfo());
  695. info->levelupInfo->skills = skills;
  696. info->levelupInfo->callback = callback;
  697. info->owner = dynamic_cast<const CGHeroInstance *> (commander->getArmy());
  698. init();
  699. }
  700. CStackWindow::~CStackWindow()
  701. {
  702. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  703. info->levelupInfo->callback(vstd::find_pos(info->levelupInfo->skills, selectedSkill));
  704. }
  705. void CStackWindow::init()
  706. {
  707. OBJECT_CONSTRUCTION;
  708. if(!info->stackNode)
  709. {
  710. fakeNode = std::make_unique<CStackInstance>(nullptr, info->creature->getId(), 1, true);
  711. info->stackNode = fakeNode.get();
  712. }
  713. selectedIcon = nullptr;
  714. selectedSkill = -1;
  715. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  716. selectedSkill = info->levelupInfo->skills.front();
  717. activeTab = 0;
  718. initBonusesList();
  719. initSections();
  720. }
  721. void CStackWindow::initBonusesList()
  722. {
  723. auto inputPtr = info->stackNode->getBonuses(CSelector(Bonus::Permanent), Selector::all);
  724. BonusList output;
  725. BonusList input = *inputPtr;
  726. std::sort(input.begin(), input.end(), [this](std::shared_ptr<Bonus> v1, std::shared_ptr<Bonus> & v2){
  727. if (v1->source != v2->source)
  728. {
  729. int priorityV1 = v1->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v1->source);
  730. int priorityV2 = v2->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v2->source);
  731. return priorityV1 < priorityV2;
  732. }
  733. else
  734. return info->stackNode->bonusToString(v1, false) < info->stackNode->bonusToString(v2, false);
  735. });
  736. while(!input.empty())
  737. {
  738. auto b = input.front();
  739. output.push_back(std::make_shared<Bonus>(*b));
  740. output.back()->val = input.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  741. input.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  742. }
  743. BonusInfo bonusInfo;
  744. for(auto b : output)
  745. {
  746. bonusInfo.name = info->stackNode->bonusToString(b, false);
  747. bonusInfo.description = info->stackNode->bonusToString(b, true);
  748. bonusInfo.imagePath = info->stackNode->bonusToGraphics(b);
  749. bonusInfo.bonusSource = b->source;
  750. if(b->sid.as<CreatureID>() != info->stackNode->getId() && b->propagator && b->propagator->getPropagatorType() == CBonusSystemNode::HERO) // Shows bonus with "propagator":"HERO" only at creature with bonus
  751. continue;
  752. //if it's possible to give any description or image for this kind of bonus
  753. //TODO: figure out why half of bonuses don't have proper description
  754. if(!bonusInfo.name.empty() || !bonusInfo.imagePath.empty())
  755. activeBonuses.push_back(bonusInfo);
  756. }
  757. }
  758. void CStackWindow::initSections()
  759. {
  760. OBJECT_CONSTRUCTION;
  761. bool showArt = GAME->interface()->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_ARTIFACT) && info->commander == nullptr && info->stackNode;
  762. bool showExp = (GAME->interface()->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE) || info->commander != nullptr) && info->stackNode;
  763. mainSection = std::make_shared<MainSection>(this, pos.h, showExp, showArt);
  764. pos.w = mainSection->pos.w;
  765. pos.h += mainSection->pos.h;
  766. if(info->stack) // in battle
  767. {
  768. activeSpellsSection = std::make_shared<ActiveSpellsSection>(this, pos.h);
  769. pos.h += activeSpellsSection->pos.h;
  770. }
  771. if(info->commander)
  772. {
  773. auto onCreate = [this](size_t index) -> std::shared_ptr<CIntObject>
  774. {
  775. auto obj = switchTab(index);
  776. if(obj)
  777. {
  778. obj->activate();
  779. obj->recActions |= (UPDATE | SHOWALL);
  780. }
  781. return obj;
  782. };
  783. auto deactivateObj = [=](std::shared_ptr<CIntObject> obj)
  784. {
  785. obj->deactivate();
  786. obj->recActions &= ~(UPDATE | SHOWALL);
  787. };
  788. commanderMainSection = std::make_shared<CommanderMainSection>(this, 0);
  789. auto size = std::make_optional<size_t>((info->levelupInfo) ? 4 : 3);
  790. commanderBonusesSection = std::make_shared<BonusesSection>(this, 0, size);
  791. deactivateObj(commanderBonusesSection);
  792. commanderTab = std::make_shared<CTabbedInt>(onCreate, Point(0, pos.h), 0);
  793. pos.h += commanderMainSection->pos.h;
  794. }
  795. if(!info->commander && !activeBonuses.empty())
  796. {
  797. bonusesSection = std::make_shared<BonusesSection>(this, pos.h);
  798. pos.h += bonusesSection->pos.h;
  799. }
  800. if(!info->popupWindow)
  801. {
  802. buttonsSection = std::make_shared<ButtonsSection>(this, pos.h);
  803. pos.h += buttonsSection->pos.h;
  804. //FIXME: add status bar to image?
  805. }
  806. updateShadow();
  807. pos = center(pos);
  808. }
  809. std::string CStackWindow::generateStackExpDescription()
  810. {
  811. const CStackInstance * stack = info->stackNode;
  812. const CCreature * creature = info->creature;
  813. int tier = stack->getType()->getLevel();
  814. int rank = stack->getExpRank();
  815. if (!vstd::iswithin(tier, 1, 7))
  816. tier = 0;
  817. int number;
  818. std::string expText = LIBRARY->generaltexth->translate("vcmi.stackExperience.description");
  819. boost::replace_first(expText, "%s", creature->getNamePluralTranslated());
  820. boost::replace_first(expText, "%s", LIBRARY->generaltexth->translate("vcmi.stackExperience.rank", rank));
  821. boost::replace_first(expText, "%i", std::to_string(rank));
  822. boost::replace_first(expText, "%i", std::to_string(stack->getAverageExperience()));
  823. number = static_cast<int>(LIBRARY->creh->expRanks[tier][rank] - stack->getAverageExperience());
  824. boost::replace_first(expText, "%i", std::to_string(number));
  825. number = LIBRARY->creh->maxExpPerBattle[tier]; //percent
  826. boost::replace_first(expText, "%i%", std::to_string(number));
  827. number *= LIBRARY->creh->expRanks[tier].back() / 100; //actual amount
  828. boost::replace_first(expText, "%i", std::to_string(number));
  829. boost::replace_first(expText, "%i", std::to_string(stack->getCount())); //Number of Creatures in stack
  830. int expmin = std::max(LIBRARY->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  831. number = stack->getTotalExperience() / expmin - stack->getCount(); //Maximum New Recruits without losing current Rank
  832. boost::replace_first(expText, "%i", std::to_string(number)); //TODO
  833. boost::replace_first(expText, "%.2f", std::to_string(1)); //TODO Experience Multiplier
  834. number = LIBRARY->creh->expAfterUpgrade;
  835. boost::replace_first(expText, "%.2f", std::to_string(number) + "%"); //Upgrade Multiplier
  836. expmin = LIBRARY->creh->expRanks[tier][9];
  837. int expmax = LIBRARY->creh->expRanks[tier][10];
  838. number = expmax - expmin;
  839. boost::replace_first(expText, "%i", std::to_string(number)); //Experience after Rank 10
  840. number = (stack->getCount() * (expmax - expmin)) / expmin;
  841. boost::replace_first(expText, "%i", std::to_string(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  842. return expText;
  843. }
  844. std::string CStackWindow::getCommanderSkillDescription(int skillIndex, int skillLevel)
  845. {
  846. constexpr std::array skillNames = {
  847. "attack",
  848. "defence",
  849. "health",
  850. "damage",
  851. "speed",
  852. "magic"
  853. };
  854. std::string textID = TextIdentifier("vcmi", "commander", "skill", skillNames.at(skillIndex), skillLevel).get();
  855. return LIBRARY->generaltexth->translate(textID);
  856. }
  857. void CStackWindow::setSelection(si32 newSkill, std::shared_ptr<CCommanderSkillIcon> newIcon)
  858. {
  859. auto getSkillDescription = [this](int skillIndex, bool selected) -> std::string
  860. {
  861. if(selected)
  862. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex] + 1); //upgrade description
  863. else
  864. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex]);
  865. };
  866. auto getSkillImage = [this](int skillIndex)
  867. {
  868. bool selected = ((selectedSkill == skillIndex) && info->levelupInfo );
  869. return skillToFile(skillIndex, info->commander->secondarySkills[skillIndex], selected);
  870. };
  871. OBJECT_CONSTRUCTION;
  872. int oldSelection = selectedSkill; // update selection
  873. selectedSkill = newSkill;
  874. if(selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills
  875. selectedIcon->setObject(std::make_shared<CPicture>(getSkillImage(oldSelection)));
  876. if(selectedIcon)
  877. {
  878. if(!selectedIcon->getIsMasterAbility()) //unlike WoG, in VCMI master skill descriptions are taken from bonus descriptions
  879. {
  880. selectedIcon->text = getSkillDescription(oldSelection, false); //update previously selected icon's message to existing skill level
  881. }
  882. selectedIcon->deselect();
  883. }
  884. selectedIcon = newIcon; // update new selection
  885. if(newSkill < 100)
  886. {
  887. newIcon->setObject(std::make_shared<CPicture>(getSkillImage(newSkill)));
  888. if(!newIcon->getIsMasterAbility())
  889. {
  890. newIcon->text = getSkillDescription(newSkill, true); //update currently selected icon's message to show upgrade description
  891. }
  892. }
  893. }
  894. std::shared_ptr<CIntObject> CStackWindow::switchTab(size_t index)
  895. {
  896. std::shared_ptr<CIntObject> ret;
  897. switch(index)
  898. {
  899. case 0:
  900. {
  901. activeTab = 0;
  902. ret = commanderMainSection;
  903. }
  904. break;
  905. case 1:
  906. {
  907. activeTab = 1;
  908. ret = commanderBonusesSection;
  909. }
  910. break;
  911. default:
  912. break;
  913. }
  914. return ret;
  915. }
  916. void CStackWindow::removeStackArtifact(ArtifactPosition pos)
  917. {
  918. auto art = info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  919. if(!art)
  920. {
  921. logGlobal->error("Attempt to remove missing artifact");
  922. return;
  923. }
  924. const auto slot = ArtifactUtils::getArtBackpackPosition(info->owner, art->getTypeId());
  925. if(slot != ArtifactPosition::PRE_FIRST)
  926. {
  927. auto artLoc = ArtifactLocation(info->owner->id, pos);
  928. artLoc.creature = info->stackNode->getArmy()->findStack(info->stackNode);
  929. GAME->interface()->cb->swapArtifacts(artLoc, ArtifactLocation(info->owner->id, slot));
  930. stackArtifactButton.reset();
  931. stackArtifact.reset();
  932. redraw();
  933. }
  934. }