2
0

CCreatureWindow.cpp 32 KB

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