CCreatureWindow.cpp 32 KB

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