CCreatureWindow.cpp 27 KB

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