CCreatureWindow.cpp 32 KB

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