CCreatureWindow.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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 "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../render/Canvas.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/CComponent.h"
  19. #include "../widgets/CComponentHolder.h"
  20. #include "../widgets/Images.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../widgets/GraphicalPrimitiveCanvas.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../gui/CGuiHandler.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 = CGI->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 = CGI->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, CGI->generaltexth->translate("vcmi.bonusSource.artifact")},
  244. {BonusSource::ARTIFACT_INSTANCE, CGI->generaltexth->translate("vcmi.bonusSource.artifact")},
  245. {BonusSource::CREATURE_ABILITY, CGI->generaltexth->translate("vcmi.bonusSource.creature")},
  246. {BonusSource::SPELL_EFFECT, CGI->generaltexth->translate("vcmi.bonusSource.spell")},
  247. {BonusSource::SECONDARY_SKILL, CGI->generaltexth->translate("vcmi.bonusSource.hero")},
  248. {BonusSource::HERO_SPECIAL, CGI->generaltexth->translate("vcmi.bonusSource.hero")},
  249. {BonusSource::STACK_EXPERIENCE, CGI->generaltexth->translate("vcmi.bonusSource.commander")},
  250. {BonusSource::COMMANDER, CGI->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] : CGI->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. icon[leftRight] = std::make_shared<CPicture>(bi.imagePath, position.x, position.y);
  274. name[leftRight] = std::make_shared<CLabel>(position.x + 60, position.y + 2, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, bi.name, 137);
  275. description[leftRight] = std::make_shared<CMultiLineLabel>(Rect(position.x + 60, position.y + 20, 137, 26), FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, bi.description);
  276. drawBonusSource(leftRight, Point(position.x - 1, position.y - 1), bi);
  277. }
  278. }
  279. }
  280. CStackWindow::BonusesSection::BonusesSection(CStackWindow * owner, int yOffset, std::optional<size_t> preferredSize):
  281. CWindowSection(owner, {}, yOffset)
  282. {
  283. OBJECT_CONSTRUCTION;
  284. // size of single image for an item
  285. static const int itemHeight = 59;
  286. size_t totalSize = (owner->activeBonuses.size() + 1) / 2;
  287. size_t visibleSize = preferredSize.value_or(std::min<size_t>(3, totalSize));
  288. pos.w = owner->pos.w;
  289. pos.h = itemHeight * (int)visibleSize;
  290. auto onCreate = [=](size_t index) -> std::shared_ptr<CIntObject>
  291. {
  292. return std::make_shared<BonusLineSection>(owner, index);
  293. };
  294. 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));
  295. }
  296. CStackWindow::ButtonsSection::ButtonsSection(CStackWindow * owner, int yOffset)
  297. : CWindowSection(owner, ImagePath::builtin("stackWindow/button-panel"), yOffset)
  298. {
  299. OBJECT_CONSTRUCTION;
  300. if(parent->info->dismissInfo && parent->info->dismissInfo->callback)
  301. {
  302. auto onDismiss = [=]()
  303. {
  304. parent->info->dismissInfo->callback();
  305. parent->close();
  306. };
  307. auto onClick = [=] ()
  308. {
  309. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[12], onDismiss, nullptr);
  310. };
  311. dismiss = std::make_shared<CButton>(Point(5, 5),AnimationPath::builtin("IVIEWCR2.DEF"), CGI->generaltexth->zelp[445], onClick, EShortcut::HERO_DISMISS);
  312. }
  313. if(parent->info->upgradeInfo && !parent->info->commander)
  314. {
  315. // used space overlaps with commander switch button
  316. // besides - should commander really be upgradeable?
  317. auto & upgradeInfo = parent->info->upgradeInfo.value();
  318. const size_t buttonsToCreate = std::min<size_t>(upgradeInfo.info.size(), upgrade.size());
  319. for(size_t buttonIndex = 0; buttonIndex < buttonsToCreate; buttonIndex++)
  320. {
  321. TResources totalCost = upgradeInfo.info.getAvailableUpgradeCosts().at(buttonIndex) * parent->info->creatureCount;
  322. auto onUpgrade = [=]()
  323. {
  324. upgradeInfo.callback(upgradeInfo.info.getAvailableUpgrades().at(buttonIndex));
  325. parent->close();
  326. };
  327. auto onClick = [=]()
  328. {
  329. std::vector<std::shared_ptr<CComponent>> resComps;
  330. for(TResources::nziterator i(totalCost); i.valid(); i++)
  331. {
  332. resComps.push_back(std::make_shared<CComponent>(ComponentType::RESOURCE, i->resType, i->resVal));
  333. }
  334. if(LOCPLINT->cb->getResourceAmount().canAfford(totalCost))
  335. {
  336. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[207], onUpgrade, nullptr, resComps);
  337. }
  338. else
  339. {
  340. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314], resComps);
  341. }
  342. };
  343. auto upgradeBtn = std::make_shared<CButton>(Point(221 + (int)buttonIndex * 40, 5), AnimationPath::builtin("stackWindow/upgradeButton"), CGI->generaltexth->zelp[446], onClick);
  344. upgradeBtn->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), VLC->creh->objects[upgradeInfo.info.getAvailableUpgrades()[buttonIndex]]->getIconIndex()));
  345. if(buttonsToCreate == 1) // single upgrade available
  346. upgradeBtn->assignedKey = EShortcut::RECRUITMENT_UPGRADE;
  347. upgrade[buttonIndex] = upgradeBtn;
  348. }
  349. }
  350. if(parent->info->commander)
  351. {
  352. for(size_t buttonIndex = 0; buttonIndex < 2; buttonIndex++)
  353. {
  354. std::string btnIDs[2] = { "showSkills", "showBonuses" };
  355. auto onSwitch = [buttonIndex, this]()
  356. {
  357. logAnim->debug("Switch %d->%d", parent->activeTab, buttonIndex);
  358. parent->switchButtons[parent->activeTab]->enable();
  359. parent->commanderTab->setActive(buttonIndex);
  360. parent->switchButtons[buttonIndex]->disable();
  361. parent->redraw(); // FIXME: enable/disable don't redraw screen themselves
  362. };
  363. std::string tooltipText = "vcmi.creatureWindow." + btnIDs[buttonIndex];
  364. parent->switchButtons[buttonIndex] = std::make_shared<CButton>(Point(302 + (int)buttonIndex*40, 5), AnimationPath::builtin("stackWindow/upgradeButton"), CButton::tooltipLocalized(tooltipText), onSwitch);
  365. parent->switchButtons[buttonIndex]->setOverlay(std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/switchModeIcons"), buttonIndex));
  366. }
  367. parent->switchButtons[parent->activeTab]->disable();
  368. }
  369. exit = std::make_shared<CButton>(Point(382, 5), AnimationPath::builtin("hsbtns.def"), CGI->generaltexth->zelp[447], [=](){ parent->close(); }, EShortcut::GLOBAL_RETURN);
  370. }
  371. CStackWindow::CommanderMainSection::CommanderMainSection(CStackWindow * owner, int yOffset)
  372. : CWindowSection(owner, ImagePath::builtin("stackWindow/commander-bg"), yOffset)
  373. {
  374. OBJECT_CONSTRUCTION;
  375. auto getSkillPos = [](int index)
  376. {
  377. return Point(10 + 80 * (index%3), 20 + 80 * (index/3));
  378. };
  379. auto getSkillImage = [this](int skillIndex)
  380. {
  381. bool selected = ((parent->selectedSkill == skillIndex) && parent->info->levelupInfo );
  382. return skillToFile(skillIndex, parent->info->commander->secondarySkills[skillIndex], selected);
  383. };
  384. auto getSkillDescription = [this](int skillIndex) -> std::string
  385. {
  386. return parent->getCommanderSkillDescription(skillIndex, parent->info->commander->secondarySkills[skillIndex]);
  387. };
  388. for(int index = ECommander::ATTACK; index <= ECommander::SPELL_POWER; ++index)
  389. {
  390. Point skillPos = getSkillPos(index);
  391. auto icon = std::make_shared<CCommanderSkillIcon>(std::make_shared<CPicture>(getSkillImage(index), skillPos.x, skillPos.y), false, [=]()
  392. {
  393. LOCPLINT->showInfoDialog(getSkillDescription(index));
  394. });
  395. icon->text = getSkillDescription(index); //used to handle right click description via LRClickableAreaWText::ClickRight()
  396. if(parent->selectedSkill == index)
  397. parent->selectedIcon = icon;
  398. if(parent->info->levelupInfo && vstd::contains(parent->info->levelupInfo->skills, index)) // can be upgraded - enable selection switch
  399. {
  400. if(parent->selectedSkill == index)
  401. parent->setSelection(index, icon);
  402. icon->callback = [=]()
  403. {
  404. parent->setSelection(index, icon);
  405. };
  406. }
  407. skillIcons.push_back(icon);
  408. }
  409. auto getArtifactPos = [](int index)
  410. {
  411. return Point(269 + 47 * (index % 3), 22 + 47 * (index / 3));
  412. };
  413. for(auto equippedArtifact : parent->info->commander->artifactsWorn)
  414. {
  415. Point artPos = getArtifactPos(equippedArtifact.first);
  416. const auto commanderArt = equippedArtifact.second.artifact;
  417. assert(commanderArt);
  418. auto artPlace = std::make_shared<CCommanderArtPlace>(artPos, parent->info->owner, equippedArtifact.first, commanderArt->getTypeId());
  419. artifacts.push_back(artPlace);
  420. }
  421. if(parent->info->levelupInfo)
  422. {
  423. abilitiesBackground = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/commander-abilities.png"));
  424. abilitiesBackground->moveBy(Point(0, pos.h));
  425. size_t abilitiesCount = boost::range::count_if(parent->info->levelupInfo->skills, [](ui32 skillID)
  426. {
  427. return skillID >= 100;
  428. });
  429. auto onCreate = [=](size_t index)->std::shared_ptr<CIntObject>
  430. {
  431. for(auto skillID : parent->info->levelupInfo->skills)
  432. {
  433. if(index == 0 && skillID >= 100)
  434. {
  435. const auto bonus = CGI->creh->skillRequirements[skillID-100].first;
  436. const CStackInstance * stack = parent->info->commander;
  437. auto icon = std::make_shared<CCommanderSkillIcon>(std::make_shared<CPicture>(stack->bonusToGraphics(bonus)), true, [](){});
  438. icon->callback = [=]()
  439. {
  440. parent->setSelection(skillID, icon);
  441. };
  442. icon->text = stack->bonusToString(bonus, true);
  443. icon->hoverText = stack->bonusToString(bonus, false);
  444. return icon;
  445. }
  446. if(skillID >= 100)
  447. index--;
  448. }
  449. return nullptr;
  450. };
  451. abilities = std::make_shared<CListBox>(onCreate, Point(38, 3+pos.h), Point(63, 0), 6, abilitiesCount);
  452. abilities->setRedrawParent(true);
  453. leftBtn = std::make_shared<CButton>(Point(10, pos.h + 6), AnimationPath::builtin("hsbtns3.def"), CButton::tooltip(), [=](){ abilities->moveToPrev(); }, EShortcut::MOVE_LEFT);
  454. rightBtn = std::make_shared<CButton>(Point(411, pos.h + 6), AnimationPath::builtin("hsbtns5.def"), CButton::tooltip(), [=](){ abilities->moveToNext(); }, EShortcut::MOVE_RIGHT);
  455. if(abilitiesCount <= 6)
  456. {
  457. leftBtn->block(true);
  458. rightBtn->block(true);
  459. }
  460. pos.h += abilitiesBackground->pos.h;
  461. }
  462. }
  463. CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool showExp, bool showArt)
  464. : CWindowSection(owner, getBackgroundName(showExp, showArt), yOffset)
  465. {
  466. OBJECT_CONSTRUCTION;
  467. statNames =
  468. {
  469. CGI->generaltexth->primarySkillNames[0], //ATTACK
  470. CGI->generaltexth->primarySkillNames[1],//DEFENCE
  471. CGI->generaltexth->allTexts[198],//SHOTS
  472. CGI->generaltexth->allTexts[199],//DAMAGE
  473. CGI->generaltexth->allTexts[388],//HEALTH
  474. CGI->generaltexth->allTexts[200],//HEALTH_LEFT
  475. CGI->generaltexth->zelp[441].first,//SPEED
  476. CGI->generaltexth->allTexts[399]//MANA
  477. };
  478. statFormats =
  479. {
  480. "%d (%d)",
  481. "%d (%d)",
  482. "%d (%d)",
  483. "%d - %d",
  484. "%d (%d)",
  485. "%d (%d)",
  486. "%d (%d)",
  487. "%d (%d)"
  488. };
  489. animation = std::make_shared<CCreaturePic>(5, 41, parent->info->creature);
  490. animationArea = std::make_shared<LRClickableArea>(Rect(5, 41, 100, 130), nullptr, [&]{
  491. if(!parent->info->creature->getDescriptionTranslated().empty())
  492. CRClickPopup::createAndPush(parent->info->creature->getDescriptionTranslated());
  493. });
  494. if(parent->info->stackNode != nullptr && parent->info->commander == nullptr)
  495. {
  496. //normal stack, not a commander and not non-existing stack (e.g. recruitment dialog)
  497. animation->setAmount(parent->info->creatureCount);
  498. }
  499. name = std::make_shared<CLabel>(215, 13, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, parent->info->getName());
  500. const BattleInterface* battleInterface = LOCPLINT->battleInt.get();
  501. const CStack* battleStack = parent->info->stack;
  502. int dmgMultiply = 1;
  503. if (battleInterface && battleInterface->getBattle() != nullptr && battleStack->hasBonusOfType(BonusType::SIEGE_WEAPON))
  504. {
  505. // Determine the relevant hero based on the unit side
  506. const auto hero = (battleStack->unitSide() == BattleSide::ATTACKER)
  507. ? battleInterface->attackingHeroInstance
  508. : battleInterface->defendingHeroInstance;
  509. dmgMultiply += hero->getPrimSkillLevel(PrimarySkill::ATTACK);
  510. }
  511. icons = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/icons"), 117, 32);
  512. morale = std::make_shared<MoraleLuckBox>(true, Rect(Point(321, 110), Point(42, 42) ));
  513. luck = std::make_shared<MoraleLuckBox>(false, Rect(Point(375, 110), Point(42, 42) ));
  514. if(battleStack != nullptr) // in battle
  515. {
  516. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(battleStack->isShooter()), battleStack->getAttack(battleStack->isShooter()));
  517. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(battleStack->isShooter()), battleStack->getDefense(battleStack->isShooter()));
  518. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(battleStack->isShooter()) * dmgMultiply, battleStack->getMaxDamage(battleStack->isShooter()) * dmgMultiply);
  519. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), battleStack->getMaxHealth());
  520. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), battleStack->getMovementRange());
  521. if(battleStack->isShooter())
  522. addStatLabel(EStat::SHOTS, battleStack->shots.total(), battleStack->shots.available());
  523. if(battleStack->isCaster())
  524. addStatLabel(EStat::MANA, battleStack->casts.total(), battleStack->casts.available());
  525. addStatLabel(EStat::HEALTH_LEFT, battleStack->getFirstHPleft());
  526. morale->set(battleStack);
  527. luck->set(battleStack);
  528. }
  529. else
  530. {
  531. const bool shooter = parent->info->stackNode->hasBonusOfType(BonusType::SHOOTER) && parent->info->stackNode->valOfBonuses(BonusType::SHOTS);
  532. const bool caster = parent->info->stackNode->valOfBonuses(BonusType::CASTS);
  533. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(shooter), parent->info->stackNode->getAttack(shooter));
  534. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(shooter), parent->info->stackNode->getDefense(shooter));
  535. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(shooter), parent->info->stackNode->getMaxDamage(shooter));
  536. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), parent->info->stackNode->getMaxHealth());
  537. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), parent->info->stackNode->getMovementRange());
  538. if(shooter)
  539. addStatLabel(EStat::SHOTS, parent->info->stackNode->valOfBonuses(BonusType::SHOTS));
  540. if(caster)
  541. addStatLabel(EStat::MANA, parent->info->stackNode->valOfBonuses(BonusType::CASTS));
  542. morale->set(parent->info->stackNode);
  543. luck->set(parent->info->stackNode);
  544. }
  545. if(showExp)
  546. {
  547. const CStackInstance * stack = parent->info->stackNode;
  548. Point pos = showArt ? Point(321, 32) : Point(347, 32);
  549. if(parent->info->commander)
  550. {
  551. const CCommanderInstance * commander = parent->info->commander;
  552. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 4, 0, pos.x, pos.y);
  553. auto area = std::make_shared<LRClickableAreaWTextComp>(Rect(pos.x, pos.y, 44, 44), ComponentType::EXPERIENCE);
  554. expArea = area;
  555. area->text = CGI->generaltexth->allTexts[2];
  556. area->component.value = commander->getExpRank();
  557. boost::replace_first(area->text, "%d", std::to_string(commander->getExpRank()));
  558. boost::replace_first(area->text, "%d", std::to_string(CGI->heroh->reqExp(commander->getExpRank() + 1)));
  559. boost::replace_first(area->text, "%d", std::to_string(commander->experience));
  560. }
  561. else
  562. {
  563. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/levels"), stack->getExpRank(), 0, pos.x, pos.y);
  564. expArea = std::make_shared<LRClickableAreaWText>(Rect(pos.x, pos.y, 44, 44));
  565. expArea->text = parent->generateStackExpDescription();
  566. }
  567. expLabel = std::make_shared<CLabel>(
  568. pos.x + 21, pos.y + 52, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  569. TextOperations::formatMetric(stack->experience, 6));
  570. }
  571. if(showArt)
  572. {
  573. Point pos = showExp ? Point(375, 32) : Point(347, 32);
  574. // ALARMA: do not refactor this into a separate function
  575. // otherwise, artifact icon is drawn near the hero's portrait
  576. // this is really strange
  577. auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  578. if(art)
  579. {
  580. parent->stackArtifact = std::make_shared<CArtPlace>(pos, art->getTypeId());
  581. parent->stackArtifact->setShowPopupCallback([](CComponentHolder & artPlace, const Point & cursorPosition)
  582. {
  583. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  584. });
  585. if(parent->info->owner)
  586. {
  587. parent->stackArtifactButton = std::make_shared<CButton>(
  588. Point(pos.x - 2 , pos.y + 46), AnimationPath::builtin("stackWindow/cancelButton"),
  589. CButton::tooltipLocalized("vcmi.creatureWindow.returnArtifact"), [=]()
  590. {
  591. parent->removeStackArtifact(ArtifactPosition::CREATURE_SLOT);
  592. });
  593. }
  594. }
  595. }
  596. }
  597. ImagePath CStackWindow::MainSection::getBackgroundName(bool showExp, bool showArt)
  598. {
  599. if(showExp && showArt)
  600. return ImagePath::builtin("stackWindow/info-panel-2");
  601. else if(showExp || showArt)
  602. return ImagePath::builtin("stackWindow/info-panel-1");
  603. else
  604. return ImagePath::builtin("stackWindow/info-panel-0");
  605. }
  606. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value1, int64_t value2)
  607. {
  608. const auto title = statNames.at(static_cast<size_t>(index));
  609. stats.push_back(std::make_shared<CLabel>(145, 32 + (int)index*19, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, title));
  610. const bool useRange = value1 != value2;
  611. std::string formatStr = useRange ? statFormats.at(static_cast<size_t>(index)) : "%d";
  612. boost::format fmt(formatStr);
  613. fmt % value1;
  614. if(useRange)
  615. fmt % value2;
  616. stats.push_back(std::make_shared<CLabel>(307, 48 + (int)index*19, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, fmt.str()));
  617. }
  618. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value)
  619. {
  620. addStatLabel(index, value, value);
  621. }
  622. CStackWindow::CStackWindow(const CStack * stack, bool popup)
  623. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  624. info(new UnitView())
  625. {
  626. info->stack = stack;
  627. info->stackNode = stack->base;
  628. info->commander = dynamic_cast<const CCommanderInstance*>(stack->base);
  629. info->creature = stack->unitType();
  630. info->creatureCount = stack->getCount();
  631. info->popupWindow = popup;
  632. init();
  633. }
  634. CStackWindow::CStackWindow(const CCreature * creature, bool popup)
  635. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  636. info(new UnitView())
  637. {
  638. info->creature = creature;
  639. info->popupWindow = popup;
  640. init();
  641. }
  642. CStackWindow::CStackWindow(const CStackInstance * stack, bool popup)
  643. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  644. info(new UnitView())
  645. {
  646. info->stackNode = stack;
  647. info->creature = stack->getCreature();
  648. info->creatureCount = stack->count;
  649. info->popupWindow = popup;
  650. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  651. init();
  652. }
  653. CStackWindow::CStackWindow(const CStackInstance * stack, std::function<void()> dismiss, const UpgradeInfo & upgradeInfo, std::function<void(CreatureID)> callback)
  654. : CWindowObject(BORDERED),
  655. info(new UnitView())
  656. {
  657. info->stackNode = stack;
  658. info->creature = stack->getCreature();
  659. info->creatureCount = stack->count;
  660. if(upgradeInfo.canUpgrade())
  661. {
  662. info->upgradeInfo = std::make_optional(UnitView::StackUpgradeInfo(upgradeInfo));
  663. info->upgradeInfo->callback = callback;
  664. }
  665. info->dismissInfo = std::make_optional(UnitView::StackDismissInfo());
  666. info->dismissInfo->callback = dismiss;
  667. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  668. init();
  669. }
  670. CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup)
  671. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  672. info(new UnitView())
  673. {
  674. info->stackNode = commander;
  675. info->creature = commander->getCreature();
  676. info->commander = commander;
  677. info->creatureCount = 1;
  678. info->popupWindow = popup;
  679. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  680. init();
  681. }
  682. CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vector<ui32> &skills, std::function<void(ui32)> callback)
  683. : CWindowObject(BORDERED),
  684. info(new UnitView())
  685. {
  686. info->stackNode = commander;
  687. info->creature = commander->getCreature();
  688. info->commander = commander;
  689. info->creatureCount = 1;
  690. info->levelupInfo = std::make_optional(UnitView::CommanderLevelInfo());
  691. info->levelupInfo->skills = skills;
  692. info->levelupInfo->callback = callback;
  693. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  694. init();
  695. }
  696. CStackWindow::~CStackWindow()
  697. {
  698. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  699. info->levelupInfo->callback(vstd::find_pos(info->levelupInfo->skills, selectedSkill));
  700. }
  701. void CStackWindow::init()
  702. {
  703. OBJECT_CONSTRUCTION;
  704. if(!info->stackNode)
  705. info->stackNode = new CStackInstance(info->creature, 1, true);// FIXME: free data
  706. selectedIcon = nullptr;
  707. selectedSkill = -1;
  708. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  709. selectedSkill = info->levelupInfo->skills.front();
  710. activeTab = 0;
  711. initBonusesList();
  712. initSections();
  713. }
  714. void CStackWindow::initBonusesList()
  715. {
  716. auto inputPtr = info->stackNode->getBonuses(CSelector(Bonus::Permanent), Selector::all);
  717. BonusList output;
  718. BonusList input = *inputPtr;
  719. std::sort(input.begin(), input.end(), [this](std::shared_ptr<Bonus> v1, std::shared_ptr<Bonus> & v2){
  720. if (v1->source != v2->source)
  721. {
  722. int priorityV1 = v1->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v1->source);
  723. int priorityV2 = v2->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v2->source);
  724. return priorityV1 < priorityV2;
  725. }
  726. else
  727. return info->stackNode->bonusToString(v1, false) < info->stackNode->bonusToString(v2, false);
  728. });
  729. while(!input.empty())
  730. {
  731. auto b = input.front();
  732. output.push_back(std::make_shared<Bonus>(*b));
  733. output.back()->val = input.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  734. input.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  735. }
  736. BonusInfo bonusInfo;
  737. for(auto b : output)
  738. {
  739. bonusInfo.name = info->stackNode->bonusToString(b, false);
  740. bonusInfo.description = info->stackNode->bonusToString(b, true);
  741. bonusInfo.imagePath = info->stackNode->bonusToGraphics(b);
  742. bonusInfo.bonusSource = b->source;
  743. if(b->sid.getNum() != info->stackNode->getId() && b->propagator && b->propagator->getPropagatorType() == CBonusSystemNode::HERO) // Shows bonus with "propagator":"HERO" only at creature with bonus
  744. continue;
  745. //if it's possible to give any description or image for this kind of bonus
  746. //TODO: figure out why half of bonuses don't have proper description
  747. if(!bonusInfo.name.empty() || !bonusInfo.imagePath.empty())
  748. activeBonuses.push_back(bonusInfo);
  749. }
  750. }
  751. void CStackWindow::initSections()
  752. {
  753. OBJECT_CONSTRUCTION;
  754. bool showArt = LOCPLINT->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_ARTIFACT) && info->commander == nullptr && info->stackNode;
  755. bool showExp = (LOCPLINT->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE) || info->commander != nullptr) && info->stackNode;
  756. mainSection = std::make_shared<MainSection>(this, pos.h, showExp, showArt);
  757. pos.w = mainSection->pos.w;
  758. pos.h += mainSection->pos.h;
  759. if(info->stack) // in battle
  760. {
  761. activeSpellsSection = std::make_shared<ActiveSpellsSection>(this, pos.h);
  762. pos.h += activeSpellsSection->pos.h;
  763. }
  764. if(info->commander)
  765. {
  766. auto onCreate = [=](size_t index) -> std::shared_ptr<CIntObject>
  767. {
  768. auto obj = switchTab(index);
  769. if(obj)
  770. {
  771. obj->activate();
  772. obj->recActions |= (UPDATE | SHOWALL);
  773. }
  774. return obj;
  775. };
  776. auto deactivateObj = [=](std::shared_ptr<CIntObject> obj)
  777. {
  778. obj->deactivate();
  779. obj->recActions &= ~(UPDATE | SHOWALL);
  780. };
  781. commanderMainSection = std::make_shared<CommanderMainSection>(this, 0);
  782. auto size = std::make_optional<size_t>((info->levelupInfo) ? 4 : 3);
  783. commanderBonusesSection = std::make_shared<BonusesSection>(this, 0, size);
  784. deactivateObj(commanderBonusesSection);
  785. commanderTab = std::make_shared<CTabbedInt>(onCreate, Point(0, pos.h), 0);
  786. pos.h += commanderMainSection->pos.h;
  787. }
  788. if(!info->commander && !activeBonuses.empty())
  789. {
  790. bonusesSection = std::make_shared<BonusesSection>(this, pos.h);
  791. pos.h += bonusesSection->pos.h;
  792. }
  793. if(!info->popupWindow)
  794. {
  795. buttonsSection = std::make_shared<ButtonsSection>(this, pos.h);
  796. pos.h += buttonsSection->pos.h;
  797. //FIXME: add status bar to image?
  798. }
  799. updateShadow();
  800. pos = center(pos);
  801. }
  802. std::string CStackWindow::generateStackExpDescription()
  803. {
  804. const CStackInstance * stack = info->stackNode;
  805. const CCreature * creature = info->creature;
  806. int tier = stack->getType()->getLevel();
  807. int rank = stack->getExpRank();
  808. if (!vstd::iswithin(tier, 1, 7))
  809. tier = 0;
  810. int number;
  811. std::string expText = CGI->generaltexth->translate("vcmi.stackExperience.description");
  812. boost::replace_first(expText, "%s", creature->getNamePluralTranslated());
  813. boost::replace_first(expText, "%s", CGI->generaltexth->translate("vcmi.stackExperience.rank", rank));
  814. boost::replace_first(expText, "%i", std::to_string(rank));
  815. boost::replace_first(expText, "%i", std::to_string(stack->experience));
  816. number = static_cast<int>(CGI->creh->expRanks[tier][rank] - stack->experience);
  817. boost::replace_first(expText, "%i", std::to_string(number));
  818. number = CGI->creh->maxExpPerBattle[tier]; //percent
  819. boost::replace_first(expText, "%i%", std::to_string(number));
  820. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  821. boost::replace_first(expText, "%i", std::to_string(number));
  822. boost::replace_first(expText, "%i", std::to_string(stack->count)); //Number of Creatures in stack
  823. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  824. number = static_cast<int>((stack->count * (stack->experience - expmin)) / expmin); //Maximum New Recruits without losing current Rank
  825. boost::replace_first(expText, "%i", std::to_string(number)); //TODO
  826. boost::replace_first(expText, "%.2f", std::to_string(1)); //TODO Experience Multiplier
  827. number = CGI->creh->expAfterUpgrade;
  828. boost::replace_first(expText, "%.2f", std::to_string(number) + "%"); //Upgrade Multiplier
  829. expmin = CGI->creh->expRanks[tier][9];
  830. int expmax = CGI->creh->expRanks[tier][10];
  831. number = expmax - expmin;
  832. boost::replace_first(expText, "%i", std::to_string(number)); //Experience after Rank 10
  833. number = (stack->count * (expmax - expmin)) / expmin;
  834. boost::replace_first(expText, "%i", std::to_string(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  835. return expText;
  836. }
  837. std::string CStackWindow::getCommanderSkillDescription(int skillIndex, int skillLevel)
  838. {
  839. constexpr std::array skillNames = {
  840. "attack",
  841. "defence",
  842. "health",
  843. "damage",
  844. "speed",
  845. "magic"
  846. };
  847. std::string textID = TextIdentifier("vcmi", "commander", "skill", skillNames.at(skillIndex), skillLevel).get();
  848. return CGI->generaltexth->translate(textID);
  849. }
  850. void CStackWindow::setSelection(si32 newSkill, std::shared_ptr<CCommanderSkillIcon> newIcon)
  851. {
  852. auto getSkillDescription = [this](int skillIndex, bool selected) -> std::string
  853. {
  854. if(selected)
  855. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex] + 1); //upgrade description
  856. else
  857. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex]);
  858. };
  859. auto getSkillImage = [this](int skillIndex)
  860. {
  861. bool selected = ((selectedSkill == skillIndex) && info->levelupInfo );
  862. return skillToFile(skillIndex, info->commander->secondarySkills[skillIndex], selected);
  863. };
  864. OBJECT_CONSTRUCTION;
  865. int oldSelection = selectedSkill; // update selection
  866. selectedSkill = newSkill;
  867. if(selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills
  868. selectedIcon->setObject(std::make_shared<CPicture>(getSkillImage(oldSelection)));
  869. if(selectedIcon)
  870. {
  871. if(!selectedIcon->getIsMasterAbility()) //unlike WoG, in VCMI master skill descriptions are taken from bonus descriptions
  872. {
  873. selectedIcon->text = getSkillDescription(oldSelection, false); //update previously selected icon's message to existing skill level
  874. }
  875. selectedIcon->deselect();
  876. }
  877. selectedIcon = newIcon; // update new selection
  878. if(newSkill < 100)
  879. {
  880. newIcon->setObject(std::make_shared<CPicture>(getSkillImage(newSkill)));
  881. if(!newIcon->getIsMasterAbility())
  882. {
  883. newIcon->text = getSkillDescription(newSkill, true); //update currently selected icon's message to show upgrade description
  884. }
  885. }
  886. }
  887. std::shared_ptr<CIntObject> CStackWindow::switchTab(size_t index)
  888. {
  889. std::shared_ptr<CIntObject> ret;
  890. switch(index)
  891. {
  892. case 0:
  893. {
  894. activeTab = 0;
  895. ret = commanderMainSection;
  896. }
  897. break;
  898. case 1:
  899. {
  900. activeTab = 1;
  901. ret = commanderBonusesSection;
  902. }
  903. break;
  904. default:
  905. break;
  906. }
  907. return ret;
  908. }
  909. void CStackWindow::removeStackArtifact(ArtifactPosition pos)
  910. {
  911. auto art = info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  912. if(!art)
  913. {
  914. logGlobal->error("Attempt to remove missing artifact");
  915. return;
  916. }
  917. const auto slot = ArtifactUtils::getArtBackpackPosition(info->owner, art->getTypeId());
  918. if(slot != ArtifactPosition::PRE_FIRST)
  919. {
  920. auto artLoc = ArtifactLocation(info->owner->id, pos);
  921. artLoc.creature = info->stackNode->armyObj->findStack(info->stackNode);
  922. LOCPLINT->cb->swapArtifacts(artLoc, ArtifactLocation(info->owner->id, slot));
  923. stackArtifactButton.reset();
  924. stackArtifact.reset();
  925. redraw();
  926. }
  927. }