CCreatureWindow.cpp 37 KB

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