CCreatureWindow.cpp 32 KB

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