CCreatureWindow.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. 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. //spell effects
  361. int printed=0; //how many effect pics have been printed
  362. std::vector<si32> spells = battleStack->activeSpells();
  363. for(si32 effect : spells)
  364. {
  365. std::string spellText;
  366. if (effect < graphics->spellEffectsPics->ourImages.size()) //not all effects have graphics (for eg. Acid Breath)
  367. {
  368. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  369. boost::replace_first (spellText, "%s", CGI->spellh->spells[effect]->name);
  370. int duration = battleStack->getBonusLocalFirst(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  371. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  372. new CAnimImage("SpellInt", effect + 1, 0, 20 + 52 * printed, 184);
  373. spellEffects.push_back(new LRClickableAreaWText(Rect(20 + 52 * printed, 184, 50, 38), spellText, spellText));
  374. if (++printed >= 10) //we can fit only 10 effects
  375. break;
  376. }
  377. }
  378. //print current health
  379. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  380. }
  381. if (bonusItems.size() > (bonusRows << 1)) //only after graphics are created
  382. {
  383. slider = new CSlider(528, 231 + commanderOffset, bonusRows*60, boost::bind (&CCreatureWindow::sliderMoved, this, _1),
  384. bonusRows, (bonusItems.size() + 1) >> 1, 0, false, 0);
  385. }
  386. else //slider automatically places bonus Items
  387. recreateSkillList (0);
  388. showAll(screen2);
  389. //AUIDAT.DEF
  390. }
  391. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  392. {
  393. new CLabel(162, 48 + nr*19, FONT_SMALL, TOPLEFT, Colors::WHITE, text);
  394. std::string hlp;
  395. if(range && baseVal != val)
  396. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  397. else if(baseVal != val && val>=0)
  398. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  399. else
  400. hlp = boost::lexical_cast<std::string>(baseVal);
  401. new CLabel(325, 64 + nr*19, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, hlp);
  402. }
  403. void CCreatureWindow::recreateSkillList(int Pos)
  404. {
  405. int commanderOffset = 0;
  406. if (type >= COMMANDER)
  407. commanderOffset = 74;
  408. int n = 0, i = 0, j = 0;
  409. int numSkills = std::min ((bonusRows + Pos) << 1, (int)bonusItems.size());
  410. for (n = 0; n < Pos << 1; ++n)
  411. {
  412. bonusItems[n]->visible = false;
  413. if (n < selectableBonuses.size())
  414. selectableBonuses[n]->deactivate(); //we assume that bonuses are at front of the list
  415. }
  416. for (n = Pos << 1; n < numSkills; ++n)
  417. {
  418. int offsetx = 257*j - (bonusRows == 4 ? 1 : 0);
  419. int offsety = 60*i + (bonusRows > 1 ? 1 : 0) + commanderOffset; //lack of precision :/
  420. bonusItems[n]->moveTo (Point(pos.x + offsetx + 10, pos.y + offsety + 230), true);
  421. bonusItems[n]->visible = true;
  422. if (n < selectableBonuses.size())
  423. {
  424. selectableBonuses[n]->moveTo (Point(bonusItems[n]->pos.x + 12, bonusItems[n]->pos.y + 2)); //for some reason bonusItems have dimensions 0?
  425. //selectableBonuses[n]->pos = bonusItems[n]->bonusGraphics->pos;
  426. selectableBonuses[n]->activate();
  427. }
  428. if (++j > 1) //next line
  429. {
  430. ++i;
  431. j = 0;
  432. }
  433. }
  434. for (n = numSkills; n < bonusItems.size(); ++n)
  435. {
  436. bonusItems[n]->visible = false;
  437. if (n < selectableBonuses.size())
  438. selectableBonuses[n]->deactivate();
  439. }
  440. }
  441. void CCreatureWindow::showAll(SDL_Surface * to)
  442. {
  443. CIntObject::showAll(to);
  444. printAtMiddleLoc((type >= COMMANDER ? c->nameSing : c->namePl), 180, 30, FONT_SMALL, Colors::YELLOW, to); //creature name
  445. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->Attack());
  446. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->Defense());
  447. if (stackNode->valOfBonuses(Bonus::SHOTS) && stackNode->hasBonusOfType(Bonus::SHOOTER))
  448. {//only for shooting units - important with wog exp shooters
  449. if (type == BATTLE)
  450. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS), dynamic_cast<const CStack*>(stackNode)->shots);
  451. else
  452. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  453. }
  454. if (stackNode->valOfBonuses(Bonus::CASTS))
  455. {
  456. printAtMiddleLoc(CGI->generaltexth->allTexts[399], 356, 62, FONT_SMALL, Colors::WHITE, to);
  457. std::string casts;
  458. if (type == BATTLE)
  459. casts = boost::lexical_cast<std::string>((ui16)dynamic_cast<const CStack*>(stackNode)->casts); //ui8 is converted to char :(
  460. else
  461. casts = boost::lexical_cast<std::string>(stackNode->valOfBonuses(Bonus::CASTS));
  462. printAtMiddleLoc(casts, 356, 82, FONT_SMALL, Colors::WHITE, to);
  463. }
  464. //TODO
  465. int dmgMultiply = 1;
  466. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  467. dmgMultiply += heroOwner->Attack();
  468. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  469. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  470. printLine(6, CGI->generaltexth->zelp[441].first, c->Speed(), stackNode->Speed());
  471. for(CBonusItem* b : bonusItems)
  472. b->showAll (to);
  473. for(auto s : selectableSkills)
  474. s->showAll (to);
  475. for (int i = 0; i < skillPictures.size(); i++)
  476. {
  477. skillPictures[i]->bg = BitmapHandler::loadBitmap (skillToFile(i));
  478. skillPictures[i]->showAll (to);
  479. }
  480. if (upgradeOptions.size() && (type == COMMANDER_LEVEL_UP && upgradeOptions[selectedOption] >= 100)) //add frame to selected skill
  481. {
  482. int index = selectedOption - selectableSkills.size(); //this is screwed
  483. CSDL_Ext::drawBorder(to, Rect::around(selectableBonuses[index]->pos), int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  484. }
  485. }
  486. void CCreatureWindow::show(SDL_Surface * to)
  487. {
  488. CIntObject::show(to);
  489. if (!count.empty()) //army stack
  490. graphics->fonts[FONT_TIMES]->renderTextRight(to, count, Colors::WHITE, Point(pos.x + 114, pos.y + 174));
  491. }
  492. void CCreatureWindow::close()
  493. {
  494. if (upgradeOptions.size()) //a skill for commander was chosen
  495. levelUp (selectedOption); //callback value is vector index
  496. GH.popIntTotally(this);
  497. }
  498. void CCreatureWindow::sliderMoved(int newpos)
  499. {
  500. recreateSkillList(newpos); //move components
  501. redraw();
  502. }
  503. std::string CCreatureWindow::skillToFile (int skill)
  504. {
  505. std::string file = "zvs/Lib1.res/_";
  506. switch (skill)
  507. {
  508. case ECommander::ATTACK:
  509. file += "AT";
  510. break;
  511. case ECommander::DEFENSE:
  512. file += "DF";
  513. break;
  514. case ECommander::HEALTH:
  515. file += "HP";
  516. break;
  517. case ECommander::DAMAGE:
  518. file += "DM";
  519. break;
  520. case ECommander::SPEED:
  521. file += "SP";
  522. break;
  523. case ECommander::SPELL_POWER:
  524. file += "MP";
  525. break;
  526. }
  527. std::string sufix = boost::lexical_cast<std::string>((int)(commander->secondarySkills[skill])); //casting ui8 causes ascii char conversion
  528. if (type == COMMANDER_LEVEL_UP)
  529. {
  530. if (upgradeOptions.size() && upgradeOptions[selectedOption] == skill)//that one specific skill is selected
  531. sufix += "="; //level-up highlight
  532. else if (!vstd::contains(upgradeOptions, skill))
  533. sufix = "no"; //not avaliable - no number
  534. }
  535. file += sufix += ".bmp";
  536. return file;
  537. }
  538. void CCreatureWindow::setArt(const CArtifactInstance *art)
  539. {
  540. creatureArtifact = art;
  541. if (creatureArtifact)
  542. {
  543. if (artifactImage == nullptr)
  544. artifactImage = new CAnimImage("ARTIFACT", creatureArtifact->artType->iconIndex, 0, 466, 100);
  545. else
  546. artifactImage->setFrame(creatureArtifact->artType->iconIndex);
  547. }
  548. else
  549. artifactImage = nullptr;
  550. redraw();
  551. }
  552. void CCreatureWindow::scrollArt(int dir)
  553. {
  554. //TODO: get next artifact
  555. int size = stack->artifactsWorn.size();
  556. displayedArtifact = size ? static_cast<ArtifactPosition>((displayedArtifact + dir) % size)
  557. : static_cast<ArtifactPosition>(ArtifactPosition::CREATURE_SLOT);
  558. setArt (stack->getArt(displayedArtifact));
  559. }
  560. void CCreatureWindow::passArtifactToHero()
  561. {
  562. const CGHeroInstance * h = dynamic_cast<const CGHeroInstance *>(stack->armyObj);
  563. if (h && creatureArtifact)
  564. {
  565. LOCPLINT->cb->swapArtifacts (ArtifactLocation (stack, displayedArtifact), ArtifactLocation(h, creatureArtifact->firstBackpackSlot(h)));
  566. }
  567. else
  568. logGlobal->warnStream() << "Pass artifact to hero should be disabled, no hero or no artifact!";
  569. //redraw is handled via CArtifactHolder interface
  570. }
  571. void CCreatureWindow::artifactRemoved (const ArtifactLocation &artLoc)
  572. {
  573. //align artifacts to remove holes
  574. for (auto al : stack->artifactsWorn)
  575. {
  576. ArtifactPosition freeSlot = al.second.artifact->firstAvailableSlot(stack);
  577. if (freeSlot < al.first)
  578. LOCPLINT->cb->swapArtifacts (ArtifactLocation(stack, al.first), ArtifactLocation(stack, freeSlot));
  579. }
  580. int size = stack->artifactsWorn.size();
  581. displayedArtifact = size ? static_cast<ArtifactPosition>(displayedArtifact % size)
  582. : static_cast<ArtifactPosition>(ArtifactPosition::CREATURE_SLOT); //0
  583. setArt (stack->getArt(displayedArtifact));
  584. }
  585. void CCreatureWindow::artifactMoved (const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  586. {
  587. artifactRemoved (artLoc); //same code
  588. }
  589. void CCreatureWindow::selectSkill (ui32 which)
  590. {
  591. selectedOption = which;
  592. redraw();
  593. }
  594. CCreatureWindow::~CCreatureWindow()
  595. {
  596. for (auto & elem : upgResCost)
  597. delete elem;
  598. bonusItems.clear();
  599. }
  600. CBonusItem::CBonusItem()
  601. {
  602. }
  603. CBonusItem::CBonusItem(const Rect &Pos, const std::string &Name, const std::string &Description, const std::string &graphicsName)
  604. {
  605. OBJ_CONSTRUCTION;
  606. visible = false;
  607. name = Name;
  608. description = Description;
  609. if (graphicsName.size())
  610. bonusGraphics = new CPicture(graphicsName, 26, 232);
  611. else
  612. bonusGraphics = nullptr;
  613. removeUsedEvents(ALL); //no actions atm
  614. }
  615. void CBonusItem::showAll (SDL_Surface * to)
  616. {
  617. if (visible)
  618. {
  619. graphics->fonts[FONT_SMALL]->renderTextLeft(to, name, Colors::YELLOW, Point(pos.x + 72, pos.y + 6));
  620. graphics->fonts[FONT_SMALL]->renderTextLeft(to, description, Colors::WHITE, Point(pos.x + 72, pos.y + 30));
  621. if (bonusGraphics && bonusGraphics->bg)
  622. blitAtLoc(bonusGraphics->bg, 12, 2, to);
  623. }
  624. }
  625. CBonusItem::~CBonusItem()
  626. {
  627. //delete bonusGraphics; //automatic destruction
  628. }
  629. void CSelectableSkill::clickLeft(tribool down, bool previousState)
  630. {
  631. if (down)
  632. callback();
  633. }
  634. void CCreInfoWindow::show(SDL_Surface * to)
  635. {
  636. CIntObject::show(to);
  637. creatureCount->showAll(to);
  638. }
  639. CCreInfoWindow::CCreInfoWindow(const CStackInstance &stack, bool LClicked, std::function<void()> upgradeFunc, std::function<void()> dismissFunc, UpgradeInfo *upgradeInfo):
  640. CWindowObject(PLAYER_COLORED | (LClicked ? 0 : RCLICK_POPUP), "CRSTKPU")
  641. {
  642. OBJ_CONSTRUCTION_CAPTURING_ALL;
  643. init(stack.type, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj), stack.count, LClicked);
  644. //additional buttons if opened with left click
  645. if(LClicked)
  646. {
  647. std::function<void()> closeFunc = boost::bind(&CCreInfoWindow::close,this);
  648. if(upgradeFunc && upgradeInfo)
  649. {
  650. TResources upgradeCost = upgradeInfo->cost[0] * stack.count;
  651. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  652. {
  653. BLOCK_CAPTURING;
  654. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  655. }
  656. CFunctionList<void()> onUpgrade;
  657. onUpgrade += upgradeFunc;
  658. onUpgrade += closeFunc;
  659. std::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  660. LOCPLINT,
  661. CGI->generaltexth->allTexts[207],
  662. onUpgrade, 0, false,
  663. boost::ref(upgResCost));
  664. upgrade = new CAdventureMapButton("", CGI->generaltexth->zelp[446].second, dialog, 76, 237, "IVIEWCR", SDLK_u);
  665. upgrade->block(!LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost));
  666. }
  667. if(dismissFunc)
  668. {
  669. CFunctionList<void()> onDismiss;
  670. onDismiss += dismissFunc;
  671. onDismiss += closeFunc;
  672. std::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  673. LOCPLINT,
  674. CGI->generaltexth->allTexts[12],
  675. onDismiss, 0, true, std::vector<CComponent*>());
  676. dismiss = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second, dialog, 21, 237, "IVIEWCR2",SDLK_d);
  677. }
  678. }
  679. }
  680. CCreInfoWindow::CCreInfoWindow(int creatureID, bool LClicked, int creatureCount):
  681. CWindowObject(PLAYER_COLORED | (LClicked ? 0 : RCLICK_POPUP), "CRSTKPU")
  682. {
  683. OBJ_CONSTRUCTION_CAPTURING_ALL;
  684. const CCreature *creature = CGI->creh->creatures[creatureID];
  685. init(creature, nullptr, nullptr, creatureCount, LClicked);
  686. }
  687. CCreInfoWindow::CCreInfoWindow(const CStack &stack, bool LClicked):
  688. CWindowObject(PLAYER_COLORED | (LClicked ? 0 : RCLICK_POPUP), "CRSTKPU")
  689. {
  690. OBJ_CONSTRUCTION_CAPTURING_ALL;
  691. init(stack.getCreature(), &stack, stack.getMyHero(), stack.count, LClicked);
  692. }
  693. CCreInfoWindow::~CCreInfoWindow()
  694. {
  695. for(CComponent* object : upgResCost)
  696. delete object;
  697. }
  698. void CCreInfoWindow::printLine(int position, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  699. {
  700. infoTexts[position].first = new CLabel(155, 48 + position*19, FONT_SMALL, TOPLEFT, Colors::WHITE, text);
  701. std::string valueStr;
  702. if(range && baseVal != val)
  703. valueStr = boost::str(boost::format("%d - %d") % baseVal % val);
  704. else if(baseVal != val && val>=0)
  705. valueStr = boost::str(boost::format("%d (%d)") % baseVal % val);
  706. else
  707. valueStr = boost::lexical_cast<std::string>(baseVal);
  708. infoTexts[position].second = new CLabel(276, 63 + position*19, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, valueStr);
  709. }
  710. void CCreInfoWindow::init(const CCreature *creature, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner, int count, bool LClicked)
  711. {
  712. removeUsedEvents(ALL);
  713. if (!LClicked)
  714. addUsedEvents(RCLICK);
  715. if(!stackNode)
  716. stackNode = creature;
  717. animation = new CCreaturePic(21, 48, creature);
  718. std::string countStr = boost::lexical_cast<std::string>(count);
  719. creatureCount = new CLabel(114, 174, FONT_TIMES, BOTTOMRIGHT, Colors::WHITE, countStr);
  720. creatureName = new CLabel(149, 30, FONT_SMALL, CENTER, Colors::YELLOW, creature->namePl);
  721. printLine(0, CGI->generaltexth->primarySkillNames[0], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  722. printLine(1, CGI->generaltexth->primarySkillNames[1], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  723. if(stackNode->valOfBonuses(Bonus::SHOTS))
  724. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  725. //TODO
  726. int dmgMultiply = 1;
  727. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  728. dmgMultiply += heroOwner->Attack();
  729. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  730. printLine(4, CGI->generaltexth->allTexts[388], creature->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  731. printLine(6, CGI->generaltexth->zelp[441].first, creature->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  732. //setting morale
  733. morale = new MoraleLuckBox(true, genRect(42, 42, 22, 186));
  734. morale->set(stackNode);
  735. //setting luck
  736. luck = new MoraleLuckBox(false, genRect(42, 42, 75, 186));
  737. luck->set(stackNode);
  738. if(!LClicked)
  739. {
  740. abilityText = new CLabel(17, 231, FONT_SMALL, TOPLEFT, Colors::WHITE, creature->abilityText);
  741. }
  742. else
  743. {
  744. abilityText = nullptr;
  745. ok = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second,
  746. boost::bind(&CCreInfoWindow::close,this), 216, 237, "IOKAY.DEF", SDLK_RETURN);
  747. ok->assignedKeys.insert(SDLK_ESCAPE);
  748. }
  749. //if we are displying window fo r stack in battle, there are several more things that we need to display
  750. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  751. {
  752. //print at most 3 spell effects
  753. std::vector<si32> spells = battleStack->activeSpells();
  754. for (size_t i=0; i< std::min(spells.size(), size_t(3)); i++)
  755. effects.push_back(new CAnimImage("SpellInt", spells[i]+1, 0, 127 + 52*i, 186));
  756. //print current health
  757. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  758. }
  759. }
  760. CIntObject * createCreWindow(
  761. const CStack *s, bool lclick/* = false*/)
  762. {
  763. auto c = dynamic_cast<const CCommanderInstance *>(s->base);
  764. if (c)
  765. {
  766. return new CCreatureWindow (c, s);
  767. }
  768. else
  769. {
  770. if(settings["general"]["classicCreatureWindow"].Bool())
  771. return new CCreInfoWindow(*s, lclick);
  772. else
  773. return new CCreatureWindow(*s, LOCPLINT->battleInt ? CCreatureWindow::BATTLE : CCreatureWindow::OTHER);
  774. }
  775. }
  776. CIntObject * createCreWindow(CreatureID Cid, CCreatureWindow::CreWinType Type, int creatureCount)
  777. {
  778. if(settings["general"]["classicCreatureWindow"].Bool())
  779. return new CCreInfoWindow(Cid, Type, creatureCount);
  780. else
  781. return new CCreatureWindow(Cid, Type, creatureCount);
  782. }
  783. CIntObject * createCreWindow(const CStackInstance *s, CCreatureWindow::CreWinType type, std::function<void()> Upg, std::function<void()> Dsm, UpgradeInfo *ui)
  784. {
  785. if(settings["general"]["classicCreatureWindow"].Bool())
  786. return new CCreInfoWindow(*s, type==CCreatureWindow::HERO, Upg, Dsm, ui);
  787. else
  788. return new CCreatureWindow(*s, type, Upg, Dsm, ui);
  789. }