CCreatureWindow.cpp 23 KB

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