CCreatureWindow.cpp 31 KB

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