CCreatureWindow.cpp 37 KB

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