CCreatureWindow.cpp 32 KB

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