CCreatureWindow.cpp 28 KB

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