CCreatureWindow.cpp 32 KB

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