CCreatureWindow.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 BattleInterface* battleInterface = GAME->interface()->battleInt.get();
  505. const CStack* battleStack = parent->info->stack;
  506. int dmgMultiply = 1;
  507. if (battleInterface && battleInterface->getBattle() != nullptr && battleStack->hasBonusOfType(BonusType::SIEGE_WEAPON))
  508. {
  509. // Determine the relevant hero based on the unit side
  510. const auto hero = (battleStack->unitSide() == BattleSide::ATTACKER)
  511. ? battleInterface->attackingHeroInstance
  512. : battleInterface->defendingHeroInstance;
  513. dmgMultiply += hero->getPrimSkillLevel(PrimarySkill::ATTACK);
  514. }
  515. icons = std::make_shared<CPicture>(ImagePath::builtin("stackWindow/icons"), 117, 32);
  516. morale = std::make_shared<MoraleLuckBox>(true, Rect(Point(321, 110), Point(42, 42) ));
  517. luck = std::make_shared<MoraleLuckBox>(false, Rect(Point(375, 110), Point(42, 42) ));
  518. if(battleStack != nullptr) // in battle
  519. {
  520. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(battleStack->isShooter()), battleStack->getAttack(battleStack->isShooter()));
  521. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(battleStack->isShooter()), battleStack->getDefense(battleStack->isShooter()));
  522. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(battleStack->isShooter()) * dmgMultiply, battleStack->getMaxDamage(battleStack->isShooter()) * dmgMultiply);
  523. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), battleStack->getMaxHealth());
  524. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), battleStack->getMovementRange());
  525. if(battleStack->isShooter())
  526. addStatLabel(EStat::SHOTS, battleStack->shots.total(), battleStack->shots.available());
  527. if(battleStack->isCaster())
  528. addStatLabel(EStat::MANA, battleStack->casts.total(), battleStack->casts.available());
  529. addStatLabel(EStat::HEALTH_LEFT, battleStack->getFirstHPleft());
  530. morale->set(battleStack);
  531. luck->set(battleStack);
  532. }
  533. else
  534. {
  535. const bool shooter = parent->info->stackNode->hasBonusOfType(BonusType::SHOOTER) && parent->info->stackNode->valOfBonuses(BonusType::SHOTS);
  536. const bool caster = parent->info->stackNode->valOfBonuses(BonusType::CASTS);
  537. addStatLabel(EStat::ATTACK, parent->info->creature->getAttack(shooter), parent->info->stackNode->getAttack(shooter));
  538. addStatLabel(EStat::DEFENCE, parent->info->creature->getDefense(shooter), parent->info->stackNode->getDefense(shooter));
  539. addStatLabel(EStat::DAMAGE, parent->info->stackNode->getMinDamage(shooter), parent->info->stackNode->getMaxDamage(shooter));
  540. addStatLabel(EStat::HEALTH, parent->info->creature->getMaxHealth(), parent->info->stackNode->getMaxHealth());
  541. addStatLabel(EStat::SPEED, parent->info->creature->getMovementRange(), parent->info->stackNode->getMovementRange());
  542. if(shooter)
  543. addStatLabel(EStat::SHOTS, parent->info->stackNode->valOfBonuses(BonusType::SHOTS));
  544. if(caster)
  545. addStatLabel(EStat::MANA, parent->info->stackNode->valOfBonuses(BonusType::CASTS));
  546. morale->set(parent->info->stackNode);
  547. luck->set(parent->info->stackNode);
  548. }
  549. if(showExp)
  550. {
  551. const CStackInstance * stack = parent->info->stackNode;
  552. Point pos = showArt ? Point(321, 32) : Point(347, 32);
  553. if(parent->info->commander)
  554. {
  555. const CCommanderInstance * commander = parent->info->commander;
  556. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("PSKIL42"), 4, 0, pos.x, pos.y);
  557. auto area = std::make_shared<LRClickableAreaWTextComp>(Rect(pos.x, pos.y, 44, 44), ComponentType::EXPERIENCE);
  558. expArea = area;
  559. area->text = LIBRARY->generaltexth->allTexts[2];
  560. area->component.value = commander->getExpRank();
  561. boost::replace_first(area->text, "%d", std::to_string(commander->getExpRank()));
  562. boost::replace_first(area->text, "%d", std::to_string(LIBRARY->heroh->reqExp(commander->getExpRank() + 1)));
  563. boost::replace_first(area->text, "%d", std::to_string(commander->experience));
  564. }
  565. else
  566. {
  567. expRankIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("stackWindow/levels"), stack->getExpRank(), 0, pos.x, pos.y);
  568. expArea = std::make_shared<LRClickableAreaWText>(Rect(pos.x, pos.y, 44, 44));
  569. expArea->text = parent->generateStackExpDescription();
  570. }
  571. expLabel = std::make_shared<CLabel>(
  572. pos.x + 21, pos.y + 55, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  573. TextOperations::formatMetric(stack->experience, 6));
  574. }
  575. if(showArt)
  576. {
  577. Point pos = showExp ? Point(375, 32) : Point(347, 32);
  578. // ALARMA: do not refactor this into a separate function
  579. // otherwise, artifact icon is drawn near the hero's portrait
  580. // this is really strange
  581. auto art = parent->info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  582. if(art)
  583. {
  584. parent->stackArtifact = std::make_shared<CArtPlace>(pos, art->getTypeId());
  585. parent->stackArtifact->setShowPopupCallback([](CComponentHolder & artPlace, const Point & cursorPosition)
  586. {
  587. artPlace.LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
  588. });
  589. if(parent->info->owner)
  590. {
  591. parent->stackArtifactButton = std::make_shared<CButton>(
  592. Point(pos.x - 2 , pos.y + 46), AnimationPath::builtin("stackWindow/cancelButton"),
  593. CButton::tooltipLocalized("vcmi.creatureWindow.returnArtifact"), [this]()
  594. {
  595. parent->removeStackArtifact(ArtifactPosition::CREATURE_SLOT);
  596. });
  597. }
  598. }
  599. }
  600. }
  601. ImagePath CStackWindow::MainSection::getBackgroundName(bool showExp, bool showArt)
  602. {
  603. if(showExp && showArt)
  604. return ImagePath::builtin("stackWindow/info-panel-2");
  605. else if(showExp || showArt)
  606. return ImagePath::builtin("stackWindow/info-panel-1");
  607. else
  608. return ImagePath::builtin("stackWindow/info-panel-0");
  609. }
  610. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value1, int64_t value2)
  611. {
  612. const auto title = statNames.at(static_cast<size_t>(index));
  613. stats.push_back(std::make_shared<CLabel>(145, 32 + (int)index*19, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, title));
  614. const bool useRange = value1 != value2;
  615. std::string formatStr = useRange ? statFormats.at(static_cast<size_t>(index)) : "%d";
  616. boost::format fmt(formatStr);
  617. fmt % value1;
  618. if(useRange)
  619. fmt % value2;
  620. stats.push_back(std::make_shared<CLabel>(307, 48 + (int)index*19, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, fmt.str()));
  621. }
  622. void CStackWindow::MainSection::addStatLabel(EStat index, int64_t value)
  623. {
  624. addStatLabel(index, value, value);
  625. }
  626. CStackWindow::CStackWindow(const CStack * stack, bool popup)
  627. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  628. info(new UnitView())
  629. {
  630. info->stack = stack;
  631. info->stackNode = stack->base;
  632. info->commander = dynamic_cast<const CCommanderInstance*>(stack->base);
  633. info->creature = stack->unitType();
  634. info->creatureCount = stack->getCount();
  635. info->popupWindow = popup;
  636. init();
  637. }
  638. CStackWindow::CStackWindow(const CCreature * creature, bool popup)
  639. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  640. info(new UnitView())
  641. {
  642. info->creature = creature;
  643. info->popupWindow = popup;
  644. init();
  645. }
  646. CStackWindow::CStackWindow(const CStackInstance * stack, bool popup)
  647. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  648. info(new UnitView())
  649. {
  650. info->stackNode = stack;
  651. info->creature = stack->getCreature();
  652. info->creatureCount = stack->count;
  653. info->popupWindow = popup;
  654. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  655. init();
  656. }
  657. CStackWindow::CStackWindow(const CStackInstance * stack, std::function<void()> dismiss, const UpgradeInfo & upgradeInfo, std::function<void(CreatureID)> callback)
  658. : CWindowObject(BORDERED),
  659. info(new UnitView())
  660. {
  661. info->stackNode = stack;
  662. info->creature = stack->getCreature();
  663. info->creatureCount = stack->count;
  664. if(upgradeInfo.canUpgrade())
  665. {
  666. info->upgradeInfo = std::make_optional(UnitView::StackUpgradeInfo(upgradeInfo));
  667. info->upgradeInfo->callback = callback;
  668. }
  669. info->dismissInfo = std::make_optional(UnitView::StackDismissInfo());
  670. info->dismissInfo->callback = dismiss;
  671. info->owner = dynamic_cast<const CGHeroInstance *> (stack->armyObj);
  672. init();
  673. }
  674. CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup)
  675. : CWindowObject(BORDERED | (popup ? RCLICK_POPUP : 0)),
  676. info(new UnitView())
  677. {
  678. info->stackNode = commander;
  679. info->creature = commander->getCreature();
  680. info->commander = commander;
  681. info->creatureCount = 1;
  682. info->popupWindow = popup;
  683. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  684. init();
  685. }
  686. CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vector<ui32> &skills, std::function<void(ui32)> callback)
  687. : CWindowObject(BORDERED),
  688. info(new UnitView())
  689. {
  690. info->stackNode = commander;
  691. info->creature = commander->getCreature();
  692. info->commander = commander;
  693. info->creatureCount = 1;
  694. info->levelupInfo = std::make_optional(UnitView::CommanderLevelInfo());
  695. info->levelupInfo->skills = skills;
  696. info->levelupInfo->callback = callback;
  697. info->owner = dynamic_cast<const CGHeroInstance *> (commander->armyObj);
  698. init();
  699. }
  700. CStackWindow::~CStackWindow()
  701. {
  702. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  703. info->levelupInfo->callback(vstd::find_pos(info->levelupInfo->skills, selectedSkill));
  704. }
  705. void CStackWindow::init()
  706. {
  707. OBJECT_CONSTRUCTION;
  708. if(!info->stackNode)
  709. info->stackNode = new CStackInstance(info->creature, 1, true);// FIXME: free data
  710. selectedIcon = nullptr;
  711. selectedSkill = -1;
  712. if(info->levelupInfo && !info->levelupInfo->skills.empty())
  713. selectedSkill = info->levelupInfo->skills.front();
  714. activeTab = 0;
  715. initBonusesList();
  716. initSections();
  717. }
  718. void CStackWindow::initBonusesList()
  719. {
  720. auto inputPtr = info->stackNode->getBonuses(CSelector(Bonus::Permanent), Selector::all);
  721. BonusList output;
  722. BonusList input = *inputPtr;
  723. std::sort(input.begin(), input.end(), [this](std::shared_ptr<Bonus> v1, std::shared_ptr<Bonus> & v2){
  724. if (v1->source != v2->source)
  725. {
  726. int priorityV1 = v1->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v1->source);
  727. int priorityV2 = v2->source == BonusSource::CREATURE_ABILITY ? -1 : static_cast<int>(v2->source);
  728. return priorityV1 < priorityV2;
  729. }
  730. else
  731. return info->stackNode->bonusToString(v1, false) < info->stackNode->bonusToString(v2, false);
  732. });
  733. while(!input.empty())
  734. {
  735. auto b = input.front();
  736. output.push_back(std::make_shared<Bonus>(*b));
  737. output.back()->val = input.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  738. input.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  739. }
  740. BonusInfo bonusInfo;
  741. for(auto b : output)
  742. {
  743. bonusInfo.name = info->stackNode->bonusToString(b, false);
  744. bonusInfo.description = info->stackNode->bonusToString(b, true);
  745. bonusInfo.imagePath = info->stackNode->bonusToGraphics(b);
  746. bonusInfo.bonusSource = b->source;
  747. if(b->sid.getNum() != info->stackNode->getId() && b->propagator && b->propagator->getPropagatorType() == CBonusSystemNode::HERO) // Shows bonus with "propagator":"HERO" only at creature with bonus
  748. continue;
  749. //if it's possible to give any description or image for this kind of bonus
  750. //TODO: figure out why half of bonuses don't have proper description
  751. if(!bonusInfo.name.empty() || !bonusInfo.imagePath.empty())
  752. activeBonuses.push_back(bonusInfo);
  753. }
  754. }
  755. void CStackWindow::initSections()
  756. {
  757. OBJECT_CONSTRUCTION;
  758. bool showArt = GAME->interface()->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_ARTIFACT) && info->commander == nullptr && info->stackNode;
  759. bool showExp = (GAME->interface()->cb->getSettings().getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE) || info->commander != nullptr) && info->stackNode;
  760. mainSection = std::make_shared<MainSection>(this, pos.h, showExp, showArt);
  761. pos.w = mainSection->pos.w;
  762. pos.h += mainSection->pos.h;
  763. if(info->stack) // in battle
  764. {
  765. activeSpellsSection = std::make_shared<ActiveSpellsSection>(this, pos.h);
  766. pos.h += activeSpellsSection->pos.h;
  767. }
  768. if(info->commander)
  769. {
  770. auto onCreate = [this](size_t index) -> std::shared_ptr<CIntObject>
  771. {
  772. auto obj = switchTab(index);
  773. if(obj)
  774. {
  775. obj->activate();
  776. obj->recActions |= (UPDATE | SHOWALL);
  777. }
  778. return obj;
  779. };
  780. auto deactivateObj = [=](std::shared_ptr<CIntObject> obj)
  781. {
  782. obj->deactivate();
  783. obj->recActions &= ~(UPDATE | SHOWALL);
  784. };
  785. commanderMainSection = std::make_shared<CommanderMainSection>(this, 0);
  786. auto size = std::make_optional<size_t>((info->levelupInfo) ? 4 : 3);
  787. commanderBonusesSection = std::make_shared<BonusesSection>(this, 0, size);
  788. deactivateObj(commanderBonusesSection);
  789. commanderTab = std::make_shared<CTabbedInt>(onCreate, Point(0, pos.h), 0);
  790. pos.h += commanderMainSection->pos.h;
  791. }
  792. if(!info->commander && !activeBonuses.empty())
  793. {
  794. bonusesSection = std::make_shared<BonusesSection>(this, pos.h);
  795. pos.h += bonusesSection->pos.h;
  796. }
  797. if(!info->popupWindow)
  798. {
  799. buttonsSection = std::make_shared<ButtonsSection>(this, pos.h);
  800. pos.h += buttonsSection->pos.h;
  801. //FIXME: add status bar to image?
  802. }
  803. updateShadow();
  804. pos = center(pos);
  805. }
  806. std::string CStackWindow::generateStackExpDescription()
  807. {
  808. const CStackInstance * stack = info->stackNode;
  809. const CCreature * creature = info->creature;
  810. int tier = stack->getType()->getLevel();
  811. int rank = stack->getExpRank();
  812. if (!vstd::iswithin(tier, 1, 7))
  813. tier = 0;
  814. int number;
  815. std::string expText = LIBRARY->generaltexth->translate("vcmi.stackExperience.description");
  816. boost::replace_first(expText, "%s", creature->getNamePluralTranslated());
  817. boost::replace_first(expText, "%s", LIBRARY->generaltexth->translate("vcmi.stackExperience.rank", rank));
  818. boost::replace_first(expText, "%i", std::to_string(rank));
  819. boost::replace_first(expText, "%i", std::to_string(stack->experience));
  820. number = static_cast<int>(LIBRARY->creh->expRanks[tier][rank] - stack->experience);
  821. boost::replace_first(expText, "%i", std::to_string(number));
  822. number = LIBRARY->creh->maxExpPerBattle[tier]; //percent
  823. boost::replace_first(expText, "%i%", std::to_string(number));
  824. number *= LIBRARY->creh->expRanks[tier].back() / 100; //actual amount
  825. boost::replace_first(expText, "%i", std::to_string(number));
  826. boost::replace_first(expText, "%i", std::to_string(stack->count)); //Number of Creatures in stack
  827. int expmin = std::max(LIBRARY->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  828. number = static_cast<int>((stack->count * (stack->experience - expmin)) / expmin); //Maximum New Recruits without losing current Rank
  829. boost::replace_first(expText, "%i", std::to_string(number)); //TODO
  830. boost::replace_first(expText, "%.2f", std::to_string(1)); //TODO Experience Multiplier
  831. number = LIBRARY->creh->expAfterUpgrade;
  832. boost::replace_first(expText, "%.2f", std::to_string(number) + "%"); //Upgrade Multiplier
  833. expmin = LIBRARY->creh->expRanks[tier][9];
  834. int expmax = LIBRARY->creh->expRanks[tier][10];
  835. number = expmax - expmin;
  836. boost::replace_first(expText, "%i", std::to_string(number)); //Experience after Rank 10
  837. number = (stack->count * (expmax - expmin)) / expmin;
  838. boost::replace_first(expText, "%i", std::to_string(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  839. return expText;
  840. }
  841. std::string CStackWindow::getCommanderSkillDescription(int skillIndex, int skillLevel)
  842. {
  843. constexpr std::array skillNames = {
  844. "attack",
  845. "defence",
  846. "health",
  847. "damage",
  848. "speed",
  849. "magic"
  850. };
  851. std::string textID = TextIdentifier("vcmi", "commander", "skill", skillNames.at(skillIndex), skillLevel).get();
  852. return LIBRARY->generaltexth->translate(textID);
  853. }
  854. void CStackWindow::setSelection(si32 newSkill, std::shared_ptr<CCommanderSkillIcon> newIcon)
  855. {
  856. auto getSkillDescription = [this](int skillIndex, bool selected) -> std::string
  857. {
  858. if(selected)
  859. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex] + 1); //upgrade description
  860. else
  861. return getCommanderSkillDescription(skillIndex, info->commander->secondarySkills[skillIndex]);
  862. };
  863. auto getSkillImage = [this](int skillIndex)
  864. {
  865. bool selected = ((selectedSkill == skillIndex) && info->levelupInfo );
  866. return skillToFile(skillIndex, info->commander->secondarySkills[skillIndex], selected);
  867. };
  868. OBJECT_CONSTRUCTION;
  869. int oldSelection = selectedSkill; // update selection
  870. selectedSkill = newSkill;
  871. if(selectedIcon && oldSelection < 100) // recreate image on old selection, only for skills
  872. selectedIcon->setObject(std::make_shared<CPicture>(getSkillImage(oldSelection)));
  873. if(selectedIcon)
  874. {
  875. if(!selectedIcon->getIsMasterAbility()) //unlike WoG, in VCMI master skill descriptions are taken from bonus descriptions
  876. {
  877. selectedIcon->text = getSkillDescription(oldSelection, false); //update previously selected icon's message to existing skill level
  878. }
  879. selectedIcon->deselect();
  880. }
  881. selectedIcon = newIcon; // update new selection
  882. if(newSkill < 100)
  883. {
  884. newIcon->setObject(std::make_shared<CPicture>(getSkillImage(newSkill)));
  885. if(!newIcon->getIsMasterAbility())
  886. {
  887. newIcon->text = getSkillDescription(newSkill, true); //update currently selected icon's message to show upgrade description
  888. }
  889. }
  890. }
  891. std::shared_ptr<CIntObject> CStackWindow::switchTab(size_t index)
  892. {
  893. std::shared_ptr<CIntObject> ret;
  894. switch(index)
  895. {
  896. case 0:
  897. {
  898. activeTab = 0;
  899. ret = commanderMainSection;
  900. }
  901. break;
  902. case 1:
  903. {
  904. activeTab = 1;
  905. ret = commanderBonusesSection;
  906. }
  907. break;
  908. default:
  909. break;
  910. }
  911. return ret;
  912. }
  913. void CStackWindow::removeStackArtifact(ArtifactPosition pos)
  914. {
  915. auto art = info->stackNode->getArt(ArtifactPosition::CREATURE_SLOT);
  916. if(!art)
  917. {
  918. logGlobal->error("Attempt to remove missing artifact");
  919. return;
  920. }
  921. const auto slot = ArtifactUtils::getArtBackpackPosition(info->owner, art->getTypeId());
  922. if(slot != ArtifactPosition::PRE_FIRST)
  923. {
  924. auto artLoc = ArtifactLocation(info->owner->id, pos);
  925. artLoc.creature = info->stackNode->armyObj->findStack(info->stackNode);
  926. GAME->interface()->cb->swapArtifacts(artLoc, ArtifactLocation(info->owner->id, slot));
  927. stackArtifactButton.reset();
  928. stackArtifact.reset();
  929. redraw();
  930. }
  931. }