CCreatureWindow.cpp 36 KB

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