CCreatureWindow.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. #include "StdInc.h"
  2. #include "CCreatureWindow.h"
  3. #include "../lib/CCreatureSet.h"
  4. #include "CGameInfo.h"
  5. #include "../lib/CGeneralTextHandler.h"
  6. #include "../lib/BattleState.h"
  7. #include "../CCallback.h"
  8. #include <SDL.h>
  9. #include "gui/SDL_Extensions.h"
  10. #include "CBitmapHandler.h"
  11. #include "CDefHandler.h"
  12. #include "Graphics.h"
  13. #include "CPlayerInterface.h"
  14. #include "../lib/CConfigHandler.h"
  15. #include "CAnimation.h"
  16. #include "../lib/CGameState.h"
  17. #include "../lib/BattleState.h"
  18. #include "../lib/CSpellHandler.h"
  19. #include "../lib/CArtHandler.h"
  20. #include "../lib/NetPacksBase.h" //ArtifactLocation
  21. #include "../lib/CModHandler.h"
  22. #include "../lib/IBonusTypeHandler.h"
  23. #include "gui/CGuiHandler.h"
  24. #include "gui/CIntObjectClasses.h"
  25. using namespace CSDL_Ext;
  26. class CCreatureArtifactInstance;
  27. class CSelectableSkill;
  28. /*
  29. * CCreatureWindow.cpp, part of VCMI engine
  30. *
  31. * Authors: listed in file AUTHORS in main folder
  32. *
  33. * License: GNU General Public License v2.0 or later
  34. * Full text of license available in license.txt file, in main folder
  35. *
  36. */
  37. CCreatureWindow::CCreatureWindow (const CStack &stack, CreWinType Type):
  38. CWindowObject(PLAYER_COLORED | (Type == OTHER ? RCLICK_POPUP : 0 ) ),
  39. type(Type)
  40. {
  41. OBJ_CONSTRUCTION_CAPTURING_ALL;
  42. if (stack.base)
  43. init(stack.base, &stack, dynamic_cast<const CGHeroInstance*>(stack.base->armyObj));
  44. else
  45. {
  46. auto s = new CStackInstance(stack.type, 1); //TODO: war machines and summons should be regular stacks
  47. init(s, &stack, nullptr);
  48. delete s;
  49. }
  50. }
  51. CCreatureWindow::CCreatureWindow (const CStackInstance &stack, CreWinType Type):
  52. CWindowObject(PLAYER_COLORED | (Type == OTHER ? RCLICK_POPUP : 0 ) ),
  53. type(Type)
  54. {
  55. OBJ_CONSTRUCTION_CAPTURING_ALL;
  56. init(&stack, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj));
  57. }
  58. CCreatureWindow::CCreatureWindow(CreatureID Cid, CreWinType Type, int creatureCount):
  59. CWindowObject(PLAYER_COLORED | (Type == OTHER ? RCLICK_POPUP : 0 ) ),
  60. type(Type)
  61. {
  62. OBJ_CONSTRUCTION_CAPTURING_ALL;
  63. auto stack = new CStackInstance(Cid, creatureCount); //TODO: simplify?
  64. init(stack, CGI->creh->creatures[Cid], nullptr);
  65. delete stack;
  66. }
  67. CCreatureWindow::CCreatureWindow(const CStackInstance &st, CreWinType Type, std::function<void()> Upg, std::function<void()> Dsm, UpgradeInfo *ui):
  68. CWindowObject(PLAYER_COLORED | (Type == OTHER ? RCLICK_POPUP : 0 ) ),
  69. type(Type),
  70. dismiss(nullptr),
  71. upgrade(nullptr),
  72. ok(nullptr),
  73. dsm(Dsm)
  74. {
  75. OBJ_CONSTRUCTION_CAPTURING_ALL;
  76. init(&st, &st,dynamic_cast<const CGHeroInstance*>(st.armyObj));
  77. //print abilities text - if r-click popup
  78. if(type)
  79. {
  80. if(Upg && ui)
  81. {
  82. TResources upgradeCost = ui->cost[0] * st.count;
  83. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  84. {
  85. BLOCK_CAPTURING;
  86. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  87. }
  88. if(LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost))
  89. {
  90. CFunctionList<void()> fs;
  91. fs += Upg;
  92. fs += boost::bind(&CCreatureWindow::close,this);
  93. CFunctionList<void()> cfl;
  94. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,
  95. LOCPLINT, CGI->generaltexth->allTexts[207], fs, nullptr, false, upgResCost);
  96. upgrade = new CAdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,385, 148,"IVIEWCR.DEF",SDLK_u);
  97. }
  98. else
  99. {
  100. upgrade = new CAdventureMapButton("",CGI->generaltexth->zelp[446].second,std::function<void()>(),385, 148,"IVIEWCR.DEF");
  101. upgrade->callback.funcs.clear();
  102. upgrade->setOffset(2);
  103. }
  104. }
  105. if(Dsm)
  106. {
  107. CFunctionList<void()> fs[2];
  108. //on dismiss confirmed
  109. fs[0] += Dsm; //dismiss
  110. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  111. CFunctionList<void()> cfl;
  112. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],fs[0],fs[1],false,std::vector<CComponent*>());
  113. dismiss = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,333, 148,"IVIEWCR2.DEF",SDLK_d);
  114. }
  115. }
  116. }
  117. CCreatureWindow::CCreatureWindow (const CCommanderInstance * Commander, const CStack * stack):
  118. CWindowObject(PLAYER_COLORED),
  119. commander (Commander)
  120. {
  121. OBJ_CONSTRUCTION_CAPTURING_ALL;
  122. if (stack)
  123. {
  124. type = COMMANDER_BATTLE;
  125. init(commander, stack, dynamic_cast<const CGHeroInstance*>(commander->armyObj));
  126. }
  127. else
  128. {
  129. type = COMMANDER;
  130. init(commander, commander, dynamic_cast<const CGHeroInstance*>(commander->armyObj));
  131. }
  132. std::function<void()> Dsm;
  133. CFunctionList<void()> fs[2];
  134. //on dismiss confirmed
  135. fs[0] += Dsm; //dismiss
  136. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  137. CFunctionList<void()> cfl;
  138. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],fs[0],fs[1],false,std::vector<CComponent*>());
  139. if (type < COMMANDER_LEVEL_UP) //can dismiss only in regular window
  140. dismiss = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, cfl, 333, 148,"IVIEWCR2.DEF", SDLK_d);
  141. }
  142. CCreatureWindow::CCreatureWindow (std::vector<ui32> &skills, const CCommanderInstance * Commander, std::function<void(ui32)> callback):
  143. CWindowObject(PLAYER_COLORED),
  144. type(COMMANDER_LEVEL_UP),
  145. commander (Commander),
  146. selectedOption (0), //choose something before drawing
  147. upgradeOptions(skills), //copy skills to choose from
  148. levelUp (callback)
  149. {
  150. OBJ_CONSTRUCTION_CAPTURING_ALL;
  151. init(commander, commander, dynamic_cast<const CGHeroInstance*>(commander->armyObj));
  152. std::function<void()> Dsm;
  153. CFunctionList<void()> fs[2];
  154. //on dismiss confirmed
  155. fs[0] += Dsm; //dismiss
  156. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  157. CFunctionList<void()> cfl;
  158. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],fs[0],fs[1],false,std::vector<CComponent*>());
  159. if (type < COMMANDER_LEVEL_UP) //can dismiss only in regular window
  160. dismiss = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, cfl, 333, 148,"IVIEWCR2.DEF", SDLK_d);
  161. }
  162. void CCreatureWindow::init(const CStackInstance *Stack, const CBonusSystemNode *StackNode, const CGHeroInstance *HeroOwner)
  163. {
  164. creatureArtifact = nullptr; //may be set later
  165. artifactImage = nullptr;
  166. spellEffectsPics = nullptr;
  167. stack = Stack;
  168. c = stack->type;
  169. if(!StackNode)
  170. stackNode = c;
  171. else
  172. stackNode = StackNode;
  173. const CStack *battleStack = dynamic_cast<const CStack*>(stackNode); //only during battle
  174. heroOwner = HeroOwner;
  175. if (battleStack)
  176. count = boost::lexical_cast<std::string>(battleStack->count);
  177. else if (Stack->count)
  178. count = boost::lexical_cast<std::string>(Stack->count);
  179. if (type < COMMANDER)
  180. commander = nullptr;
  181. bool creArt = false;
  182. displayedArtifact = ArtifactPosition::CREATURE_SLOT; // 0
  183. //Basic graphics - need to calculate size
  184. int commanderOffset = 0;
  185. if (type >= COMMANDER)
  186. commanderOffset = 74;
  187. if (commander) //secondary skills
  188. {
  189. creArt = true;
  190. for (int i = ECommander::ATTACK; i <= ECommander::SPELL_POWER; ++i)
  191. {
  192. if (commander->secondarySkills[i] || vstd::contains(upgradeOptions, i))
  193. {
  194. std::string file = skillToFile(i);
  195. skillPictures.push_back(new CPicture(file, 0,0));
  196. }
  197. }
  198. if (type == COMMANDER_LEVEL_UP)
  199. {
  200. for (auto option : upgradeOptions)
  201. {
  202. ui32 index = selectableSkills.size();
  203. auto selectableSkill = new CSelectableSkill();
  204. selectableSkill->callback = boost::bind(&CCreatureWindow::selectSkill, this, index);
  205. if (option < 100)
  206. {
  207. selectableSkill->pos = skillPictures[index]->pos; //resize
  208. selectableSkills.push_back (selectableSkill);
  209. }
  210. else
  211. {
  212. selectableSkill->pos = Rect (95, 256, 55, 55); //TODO: scroll
  213. const Bonus *b = CGI->creh->skillRequirements[option-100].first;
  214. bonusItems.push_back (new CBonusItem (genRect(0, 0, 251, 57), stack->bonusToString(b, false), stack->bonusToString(b, true), stack->bonusToGraphics(b)));
  215. selectableBonuses.push_back (selectableSkill); //insert these before other bonuses
  216. }
  217. }
  218. }
  219. }
  220. BonusList bl, blTemp;
  221. blTemp = (*(stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT).And(Selector::anyRange()))));
  222. while (blTemp.size())
  223. {
  224. Bonus * b = blTemp.front();
  225. bl.push_back (new Bonus(*b));
  226. bl.back()->val = blTemp.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  227. blTemp.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  228. }
  229. std::string text, img;
  230. for(Bonus* b : bl)
  231. {
  232. text = stack->bonusToString(b, false);
  233. img = stack->bonusToGraphics(b);
  234. if (text.size() || img.size()) //if it's possible to give any description or image for this kind of bonus
  235. {
  236. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, stack->bonusToString(b, true), img));
  237. }
  238. }
  239. //handle Magic resistance separately :/
  240. const IBonusBearer *temp = stack;
  241. if (battleStack)
  242. {
  243. temp = battleStack;
  244. }
  245. int magicResistance = temp->magicResistance();
  246. if (magicResistance)
  247. {
  248. Bonus b;
  249. b.type = Bonus::MAGIC_RESISTANCE;
  250. text = VLC->getBth()->bonusToString(&b,temp,false);
  251. const std::string description = VLC->getBth()->bonusToString(&b,temp,true);
  252. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, description, stack->bonusToGraphics(&b)));
  253. }
  254. bonusRows = std::min ((int)((bonusItems.size() + 1) / 2), (screen->h - 230) / 60);
  255. if (type >= COMMANDER)
  256. vstd::amin(bonusRows, 3);
  257. else
  258. vstd::amin(bonusRows, 4);
  259. vstd::amax(bonusRows, 1);
  260. if (type >= COMMANDER)
  261. {
  262. setBackground("CommWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx");
  263. for (int i = 0; i < skillPictures.size(); ++i)
  264. {
  265. skillPictures[i]->moveTo (Point (pos.x + 37 + i * 84, pos.y + 224));
  266. }
  267. for (int i = 0; i < selectableSkills.size(); ++i)
  268. {
  269. if (upgradeOptions[i] < skillPictures.size()) // it's secondary skill
  270. {
  271. selectableSkills[i]->pos = skillPictures[upgradeOptions[i]]->pos; //dirty workaround
  272. }
  273. else
  274. break;
  275. }
  276. //print commander level
  277. new CLabel(488, 62, FONT_MEDIUM, CENTER, Colors::YELLOW,
  278. boost::lexical_cast<std::string>((ui16)(commander->level)));
  279. new CLabel(488, 82, FONT_SMALL, CENTER, Colors::WHITE,
  280. boost::lexical_cast<std::string>(stack->experience));
  281. }
  282. else
  283. setBackground("CreWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx"); //1 to 4 rows for now
  284. //Buttons
  285. ok = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CCreatureWindow::close,this), 489, 148, "hsbtns.def", SDLK_RETURN);
  286. ok->assignedKeys.insert(SDLK_ESCAPE);
  287. if (type <= BATTLE) //in battle or info window
  288. {
  289. upgrade = nullptr;
  290. dismiss = nullptr;
  291. }
  292. anim = new CCreaturePic(22, 48, c);
  293. //Stats
  294. morale = new MoraleLuckBox(true, genRect(42, 42, 335, 100));
  295. morale->set(stackNode);
  296. luck = new MoraleLuckBox(false, genRect(42, 42, 387, 100));
  297. luck->set(stackNode);
  298. new CAnimImage("PSKIL42", 4, 0, 387, 51); //exp icon - Print it always?
  299. if (type) //not in fort window
  300. {
  301. if (CGI->modh->modules.STACK_EXP && type < COMMANDER)
  302. {
  303. int rank = std::min(stack->getExpRank(), 10); //hopefully nobody adds more
  304. new CLabel(488, 82, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(stack->experience));
  305. new CLabel(488, 62, FONT_MEDIUM, CENTER, Colors::YELLOW,
  306. CGI->generaltexth->zcrexp[rank] + " [" + boost::lexical_cast<std::string>(rank) + "]");
  307. if (type > BATTLE) //we need it only on adv. map
  308. {
  309. int tier = stack->type->level;
  310. if (!vstd::iswithin(tier, 1, 7))
  311. tier = 0;
  312. int number;
  313. std::string expText = CGI->generaltexth->zcrexp[324];
  314. boost::replace_first (expText, "%s", c->namePl);
  315. boost::replace_first (expText, "%s", CGI->generaltexth->zcrexp[rank]);
  316. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(rank));
  317. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->experience));
  318. number = CGI->creh->expRanks[tier][rank] - stack->experience;
  319. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  320. number = CGI->creh->maxExpPerBattle[tier]; //percent
  321. boost::replace_first (expText, "%i%", boost::lexical_cast<std::string>(number));
  322. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  323. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  324. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->count)); //Number of Creatures in stack
  325. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  326. number = (stack->count * (stack->experience - expmin)) / expmin; //Maximum New Recruits without losing current Rank
  327. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //TODO
  328. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(1)); //TODO Experience Multiplier
  329. number = CGI->creh->expAfterUpgrade;
  330. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(number) + "%"); //Upgrade Multiplier
  331. expmin = CGI->creh->expRanks[tier][9];
  332. int expmax = CGI->creh->expRanks[tier][10];
  333. number = expmax - expmin;
  334. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Experience after Rank 10
  335. number = (stack->count * (expmax - expmin)) / expmin;
  336. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  337. expArea = new LRClickableAreaWTextComp(Rect(334, 49, 160, 44),CComponent::experience);
  338. expArea->text = expText;
  339. expArea->bonusValue = 0; //TDO: some specific value or no number at all
  340. }
  341. }
  342. if (CGI->modh->modules.STACK_ARTIFACT)
  343. {
  344. creArt = true;
  345. }
  346. }
  347. if (creArt) //stack or commander artifacts
  348. {
  349. setArt (stack->getArt(ArtifactPosition::CREATURE_SLOT));
  350. if (type > BATTLE && type < COMMANDER_BATTLE) //artifact buttons inactive in battle
  351. {
  352. //TODO: disable buttons if no artifact is equipped
  353. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, -1), 437, 98, "hsbtns3.def", SDLK_LEFT);
  354. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, +1), 516, 98, "hsbtns5.def", SDLK_RIGHT);
  355. if (heroOwner)
  356. passArtToHero = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::passArtifactToHero, this), 437, 148, "OVBUTN1.DEF", SDLK_HOME);
  357. }
  358. }
  359. if (battleStack) //only during battle
  360. {
  361. spellEffectsPics = new CAnimation("SpellInt.def");
  362. //spell effects
  363. int printed=0; //how many effect pics have been printed
  364. std::vector<si32> spells = battleStack->activeSpells();
  365. for(si32 effect : spells)
  366. {
  367. const si32 imageIndex = effect+1; //there is "null" frame with index 0 in SpellInt.def
  368. std::string spellText;
  369. spellEffectsPics->load(imageIndex);
  370. IImage * effectIcon = spellEffectsPics->getImage(imageIndex,0,false); //todo: better way to determine presence of icon
  371. spellEffectsPics->unload(imageIndex);
  372. if (effectIcon != nullptr) //not all effects have graphics (for eg. Acid Breath)
  373. {
  374. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  375. boost::replace_first (spellText, "%s", CGI->spellh->objects[effect]->name);
  376. int duration = battleStack->getBonusLocalFirst(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  377. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  378. new CAnimImage("SpellInt.def", imageIndex, 0, 20 + 52 * printed, 184);
  379. spellEffects.push_back(new LRClickableAreaWText(Rect(20 + 52 * printed, 184, 50, 38), spellText, spellText));
  380. if (++printed >= 10) //we can fit only 10 effects
  381. break;
  382. }
  383. }
  384. //print current health
  385. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  386. }
  387. if (bonusItems.size() > (bonusRows << 1)) //only after graphics are created
  388. {
  389. slider = new CSlider(528, 231 + commanderOffset, bonusRows*60, boost::bind (&CCreatureWindow::sliderMoved, this, _1),
  390. bonusRows, (bonusItems.size() + 1) >> 1, 0, false, 0);
  391. }
  392. else //slider automatically places bonus Items
  393. recreateSkillList (0);
  394. showAll(screen2);
  395. //AUIDAT.DEF
  396. }
  397. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  398. {
  399. new CLabel(162, 48 + nr*19, FONT_SMALL, TOPLEFT, Colors::WHITE, text);
  400. std::string hlp;
  401. if(range && baseVal != val)
  402. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  403. else if(baseVal != val && val>=0)
  404. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  405. else
  406. hlp = boost::lexical_cast<std::string>(baseVal);
  407. new CLabel(325, 64 + nr*19, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, hlp);
  408. }
  409. void CCreatureWindow::recreateSkillList(int Pos)
  410. {
  411. int commanderOffset = 0;
  412. if (type >= COMMANDER)
  413. commanderOffset = 74;
  414. int n = 0, i = 0, j = 0;
  415. int numSkills = std::min ((bonusRows + Pos) << 1, (int)bonusItems.size());
  416. for (n = 0; n < Pos << 1; ++n)
  417. {
  418. bonusItems[n]->visible = false;
  419. if (n < selectableBonuses.size())
  420. selectableBonuses[n]->deactivate(); //we assume that bonuses are at front of the list
  421. }
  422. for (n = Pos << 1; n < numSkills; ++n)
  423. {
  424. int offsetx = 257*j - (bonusRows == 4 ? 1 : 0);
  425. int offsety = 60*i + (bonusRows > 1 ? 1 : 0) + commanderOffset; //lack of precision :/
  426. bonusItems[n]->moveTo (Point(pos.x + offsetx + 10, pos.y + offsety + 230), true);
  427. bonusItems[n]->visible = true;
  428. if (n < selectableBonuses.size())
  429. {
  430. selectableBonuses[n]->moveTo (Point(bonusItems[n]->pos.x + 12, bonusItems[n]->pos.y + 2)); //for some reason bonusItems have dimensions 0?
  431. //selectableBonuses[n]->pos = bonusItems[n]->bonusGraphics->pos;
  432. selectableBonuses[n]->activate();
  433. }
  434. if (++j > 1) //next line
  435. {
  436. ++i;
  437. j = 0;
  438. }
  439. }
  440. for (n = numSkills; n < bonusItems.size(); ++n)
  441. {
  442. bonusItems[n]->visible = false;
  443. if (n < selectableBonuses.size())
  444. selectableBonuses[n]->deactivate();
  445. }
  446. }
  447. void CCreatureWindow::showAll(SDL_Surface * to)
  448. {
  449. CIntObject::showAll(to);
  450. printAtMiddleLoc((type >= COMMANDER ? c->nameSing : c->namePl), 180, 30, FONT_SMALL, Colors::YELLOW, to); //creature name
  451. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->Attack());
  452. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->Defense());
  453. if (stackNode->valOfBonuses(Bonus::SHOTS) && stackNode->hasBonusOfType(Bonus::SHOOTER))
  454. {//only for shooting units - important with wog exp shooters
  455. if (type == BATTLE)
  456. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS), dynamic_cast<const CStack*>(stackNode)->shots);
  457. else
  458. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  459. }
  460. if (stackNode->valOfBonuses(Bonus::CASTS))
  461. {
  462. printAtMiddleLoc(CGI->generaltexth->allTexts[399], 356, 62, FONT_SMALL, Colors::WHITE, to);
  463. std::string casts;
  464. if (type == BATTLE)
  465. casts = boost::lexical_cast<std::string>((ui16)dynamic_cast<const CStack*>(stackNode)->casts); //ui8 is converted to char :(
  466. else
  467. casts = boost::lexical_cast<std::string>(stackNode->valOfBonuses(Bonus::CASTS));
  468. printAtMiddleLoc(casts, 356, 82, FONT_SMALL, Colors::WHITE, to);
  469. }
  470. //TODO
  471. int dmgMultiply = 1;
  472. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  473. dmgMultiply += heroOwner->Attack();
  474. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  475. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  476. printLine(6, CGI->generaltexth->zelp[441].first, c->Speed(), stackNode->Speed());
  477. for(CBonusItem* b : bonusItems)
  478. b->showAll (to);
  479. for(auto s : selectableSkills)
  480. s->showAll (to);
  481. for (int i = 0; i < skillPictures.size(); i++)
  482. {
  483. skillPictures[i]->bg = BitmapHandler::loadBitmap (skillToFile(i));
  484. skillPictures[i]->showAll (to);
  485. }
  486. if (upgradeOptions.size() && (type == COMMANDER_LEVEL_UP && upgradeOptions[selectedOption] >= 100)) //add frame to selected skill
  487. {
  488. int index = selectedOption - selectableSkills.size(); //this is screwed
  489. CSDL_Ext::drawBorder(to, Rect::around(selectableBonuses[index]->pos), int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  490. }
  491. }
  492. void CCreatureWindow::show(SDL_Surface * to)
  493. {
  494. CIntObject::show(to);
  495. if (!count.empty()) //army stack
  496. graphics->fonts[FONT_TIMES]->renderTextRight(to, count, Colors::WHITE, Point(pos.x + 114, pos.y + 174));
  497. }
  498. void CCreatureWindow::close()
  499. {
  500. if (upgradeOptions.size()) //a skill for commander was chosen
  501. levelUp (selectedOption); //callback value is vector index
  502. GH.popIntTotally(this);
  503. }
  504. void CCreatureWindow::sliderMoved(int newpos)
  505. {
  506. recreateSkillList(newpos); //move components
  507. redraw();
  508. }
  509. std::string CCreatureWindow::skillToFile (int skill)
  510. {
  511. std::string file = "zvs/Lib1.res/_";
  512. switch (skill)
  513. {
  514. case ECommander::ATTACK:
  515. file += "AT";
  516. break;
  517. case ECommander::DEFENSE:
  518. file += "DF";
  519. break;
  520. case ECommander::HEALTH:
  521. file += "HP";
  522. break;
  523. case ECommander::DAMAGE:
  524. file += "DM";
  525. break;
  526. case ECommander::SPEED:
  527. file += "SP";
  528. break;
  529. case ECommander::SPELL_POWER:
  530. file += "MP";
  531. break;
  532. }
  533. std::string sufix = boost::lexical_cast<std::string>((int)(commander->secondarySkills[skill])); //casting ui8 causes ascii char conversion
  534. if (type == COMMANDER_LEVEL_UP)
  535. {
  536. if (upgradeOptions.size() && upgradeOptions[selectedOption] == skill)//that one specific skill is selected
  537. sufix += "="; //level-up highlight
  538. else if (!vstd::contains(upgradeOptions, skill))
  539. sufix = "no"; //not avaliable - no number
  540. }
  541. file += sufix += ".bmp";
  542. return file;
  543. }
  544. void CCreatureWindow::setArt(const CArtifactInstance *art)
  545. {
  546. creatureArtifact = art;
  547. if (creatureArtifact)
  548. {
  549. if (artifactImage == nullptr)
  550. artifactImage = new CAnimImage("ARTIFACT", creatureArtifact->artType->iconIndex, 0, 466, 100);
  551. else
  552. artifactImage->setFrame(creatureArtifact->artType->iconIndex);
  553. }
  554. else
  555. artifactImage = nullptr;
  556. redraw();
  557. }
  558. void CCreatureWindow::scrollArt(int dir)
  559. {
  560. //TODO: get next artifact
  561. int size = stack->artifactsWorn.size();
  562. displayedArtifact = size ? static_cast<ArtifactPosition>((displayedArtifact + dir) % size)
  563. : static_cast<ArtifactPosition>(ArtifactPosition::CREATURE_SLOT);
  564. setArt (stack->getArt(displayedArtifact));
  565. }
  566. void CCreatureWindow::passArtifactToHero()
  567. {
  568. const CGHeroInstance * h = dynamic_cast<const CGHeroInstance *>(stack->armyObj);
  569. if (h && creatureArtifact)
  570. {
  571. LOCPLINT->cb->swapArtifacts (ArtifactLocation (stack, displayedArtifact), ArtifactLocation(h, creatureArtifact->firstBackpackSlot(h)));
  572. }
  573. else
  574. logGlobal->warnStream() << "Pass artifact to hero should be disabled, no hero or no artifact!";
  575. //redraw is handled via CArtifactHolder interface
  576. }
  577. void CCreatureWindow::artifactRemoved (const ArtifactLocation &artLoc)
  578. {
  579. //align artifacts to remove holes
  580. for (auto al : stack->artifactsWorn)
  581. {
  582. ArtifactPosition freeSlot = al.second.artifact->firstAvailableSlot(stack);
  583. if (freeSlot < al.first)
  584. LOCPLINT->cb->swapArtifacts (ArtifactLocation(stack, al.first), ArtifactLocation(stack, freeSlot));
  585. }
  586. int size = stack->artifactsWorn.size();
  587. displayedArtifact = size ? static_cast<ArtifactPosition>(displayedArtifact % size)
  588. : static_cast<ArtifactPosition>(ArtifactPosition::CREATURE_SLOT); //0
  589. setArt (stack->getArt(displayedArtifact));
  590. }
  591. void CCreatureWindow::artifactMoved (const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  592. {
  593. artifactRemoved (artLoc); //same code
  594. }
  595. void CCreatureWindow::selectSkill (ui32 which)
  596. {
  597. selectedOption = which;
  598. redraw();
  599. }
  600. CCreatureWindow::~CCreatureWindow()
  601. {
  602. for (auto & elem : upgResCost)
  603. delete elem;
  604. bonusItems.clear();
  605. if(spellEffectsPics!=nullptr)
  606. delete spellEffectsPics;
  607. }
  608. CBonusItem::CBonusItem()
  609. {
  610. }
  611. CBonusItem::CBonusItem(const Rect &Pos, const std::string &Name, const std::string &Description, const std::string &graphicsName)
  612. {
  613. OBJ_CONSTRUCTION;
  614. visible = false;
  615. name = Name;
  616. description = Description;
  617. if (graphicsName.size())
  618. bonusGraphics = new CPicture(graphicsName, 26, 232);
  619. else
  620. bonusGraphics = nullptr;
  621. removeUsedEvents(ALL); //no actions atm
  622. }
  623. void CBonusItem::showAll (SDL_Surface * to)
  624. {
  625. if (visible)
  626. {
  627. graphics->fonts[FONT_SMALL]->renderTextLeft(to, name, Colors::YELLOW, Point(pos.x + 72, pos.y + 6));
  628. graphics->fonts[FONT_SMALL]->renderTextLeft(to, description, Colors::WHITE, Point(pos.x + 72, pos.y + 30));
  629. if (bonusGraphics && bonusGraphics->bg)
  630. blitAtLoc(bonusGraphics->bg, 12, 2, to);
  631. }
  632. }
  633. CBonusItem::~CBonusItem()
  634. {
  635. //delete bonusGraphics; //automatic destruction
  636. }
  637. void CSelectableSkill::clickLeft(tribool down, bool previousState)
  638. {
  639. if (down)
  640. callback();
  641. }
  642. void CCreInfoWindow::show(SDL_Surface * to)
  643. {
  644. CIntObject::show(to);
  645. creatureCount->showAll(to);
  646. }
  647. CCreInfoWindow::CCreInfoWindow(const CStackInstance &stack, bool LClicked, std::function<void()> upgradeFunc, std::function<void()> dismissFunc, UpgradeInfo *upgradeInfo):
  648. CWindowObject(PLAYER_COLORED | (LClicked ? 0 : RCLICK_POPUP), "CRSTKPU")
  649. {
  650. OBJ_CONSTRUCTION_CAPTURING_ALL;
  651. init(stack.type, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj), stack.count, LClicked);
  652. //additional buttons if opened with left click
  653. if(LClicked)
  654. {
  655. std::function<void()> closeFunc = boost::bind(&CCreInfoWindow::close,this);
  656. if(upgradeFunc && upgradeInfo)
  657. {
  658. TResources upgradeCost = upgradeInfo->cost[0] * stack.count;
  659. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  660. {
  661. BLOCK_CAPTURING;
  662. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  663. }
  664. CFunctionList<void()> onUpgrade;
  665. onUpgrade += upgradeFunc;
  666. onUpgrade += closeFunc;
  667. std::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  668. LOCPLINT,
  669. CGI->generaltexth->allTexts[207],
  670. onUpgrade, 0, false,
  671. boost::ref(upgResCost));
  672. upgrade = new CAdventureMapButton("", CGI->generaltexth->zelp[446].second, dialog, 76, 237, "IVIEWCR", SDLK_u);
  673. upgrade->block(!LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost));
  674. }
  675. if(dismissFunc)
  676. {
  677. CFunctionList<void()> onDismiss;
  678. onDismiss += dismissFunc;
  679. onDismiss += closeFunc;
  680. std::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  681. LOCPLINT,
  682. CGI->generaltexth->allTexts[12],
  683. onDismiss, 0, true, std::vector<CComponent*>());
  684. dismiss = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second, dialog, 21, 237, "IVIEWCR2",SDLK_d);
  685. }
  686. }
  687. }
  688. CCreInfoWindow::CCreInfoWindow(int creatureID, bool LClicked, int creatureCount):
  689. CWindowObject(PLAYER_COLORED | (LClicked ? 0 : RCLICK_POPUP), "CRSTKPU")
  690. {
  691. OBJ_CONSTRUCTION_CAPTURING_ALL;
  692. const CCreature *creature = CGI->creh->creatures[creatureID];
  693. init(creature, nullptr, nullptr, creatureCount, LClicked);
  694. }
  695. CCreInfoWindow::CCreInfoWindow(const CStack &stack, bool LClicked):
  696. CWindowObject(PLAYER_COLORED | (LClicked ? 0 : RCLICK_POPUP), "CRSTKPU")
  697. {
  698. OBJ_CONSTRUCTION_CAPTURING_ALL;
  699. init(stack.getCreature(), &stack, stack.getMyHero(), stack.count, LClicked);
  700. }
  701. CCreInfoWindow::~CCreInfoWindow()
  702. {
  703. for(CComponent* object : upgResCost)
  704. delete object;
  705. }
  706. void CCreInfoWindow::printLine(int position, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  707. {
  708. infoTexts[position].first = new CLabel(155, 48 + position*19, FONT_SMALL, TOPLEFT, Colors::WHITE, text);
  709. std::string valueStr;
  710. if(range && baseVal != val)
  711. valueStr = boost::str(boost::format("%d - %d") % baseVal % val);
  712. else if(baseVal != val && val>=0)
  713. valueStr = boost::str(boost::format("%d (%d)") % baseVal % val);
  714. else
  715. valueStr = boost::lexical_cast<std::string>(baseVal);
  716. infoTexts[position].second = new CLabel(276, 63 + position*19, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, valueStr);
  717. }
  718. void CCreInfoWindow::init(const CCreature *creature, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner, int count, bool LClicked)
  719. {
  720. removeUsedEvents(ALL);
  721. if (!LClicked)
  722. addUsedEvents(RCLICK);
  723. if(!stackNode)
  724. stackNode = creature;
  725. animation = new CCreaturePic(21, 48, creature);
  726. std::string countStr = boost::lexical_cast<std::string>(count);
  727. creatureCount = new CLabel(114, 174, FONT_TIMES, BOTTOMRIGHT, Colors::WHITE, countStr);
  728. creatureName = new CLabel(149, 30, FONT_SMALL, CENTER, Colors::YELLOW, creature->namePl);
  729. printLine(0, CGI->generaltexth->primarySkillNames[0], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  730. printLine(1, CGI->generaltexth->primarySkillNames[1], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  731. if(stackNode->valOfBonuses(Bonus::SHOTS))
  732. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  733. //TODO
  734. int dmgMultiply = 1;
  735. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  736. dmgMultiply += heroOwner->Attack();
  737. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  738. printLine(4, CGI->generaltexth->allTexts[388], creature->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  739. printLine(6, CGI->generaltexth->zelp[441].first, creature->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  740. //setting morale
  741. morale = new MoraleLuckBox(true, genRect(42, 42, 22, 186));
  742. morale->set(stackNode);
  743. //setting luck
  744. luck = new MoraleLuckBox(false, genRect(42, 42, 75, 186));
  745. luck->set(stackNode);
  746. if(!LClicked)
  747. {
  748. abilityText = new CLabel(17, 231, FONT_SMALL, TOPLEFT, Colors::WHITE, creature->abilityText);
  749. }
  750. else
  751. {
  752. abilityText = nullptr;
  753. ok = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second,
  754. boost::bind(&CCreInfoWindow::close,this), 216, 237, "IOKAY.DEF", SDLK_RETURN);
  755. ok->assignedKeys.insert(SDLK_ESCAPE);
  756. }
  757. //if we are displying window fo r stack in battle, there are several more things that we need to display
  758. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  759. {
  760. //print at most 3 spell effects
  761. std::vector<si32> spells = battleStack->activeSpells();
  762. for (size_t i=0; i< std::min(spells.size(), size_t(3)); i++)
  763. effects.push_back(new CAnimImage("SpellInt", spells[i]+1, 0, 127 + 52*i, 186));
  764. //print current health
  765. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  766. }
  767. }
  768. CIntObject * createCreWindow(
  769. const CStack *s, bool lclick/* = false*/)
  770. {
  771. auto c = dynamic_cast<const CCommanderInstance *>(s->base);
  772. if (c)
  773. {
  774. return new CCreatureWindow (c, s);
  775. }
  776. else
  777. {
  778. if(settings["general"]["classicCreatureWindow"].Bool())
  779. return new CCreInfoWindow(*s, lclick);
  780. else
  781. return new CCreatureWindow(*s, LOCPLINT->battleInt ? CCreatureWindow::BATTLE : CCreatureWindow::OTHER);
  782. }
  783. }
  784. CIntObject * createCreWindow(CreatureID Cid, CCreatureWindow::CreWinType Type, int creatureCount)
  785. {
  786. if(settings["general"]["classicCreatureWindow"].Bool())
  787. return new CCreInfoWindow(Cid, Type, creatureCount);
  788. else
  789. return new CCreatureWindow(Cid, Type, creatureCount);
  790. }
  791. CIntObject * createCreWindow(const CStackInstance *s, CCreatureWindow::CreWinType type, std::function<void()> Upg, std::function<void()> Dsm, UpgradeInfo *ui)
  792. {
  793. if(settings["general"]["classicCreatureWindow"].Bool())
  794. return new CCreInfoWindow(*s, type==CCreatureWindow::HERO, Upg, Dsm, ui);
  795. else
  796. return new CCreatureWindow(*s, type, Upg, Dsm, ui);
  797. }