CCreatureWindow.cpp 37 KB

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