CCreatureWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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 "UIFramework/SDL_Extensions.h"
  10. #include "CBitmapHandler.h"
  11. #include "CDefHandler.h"
  12. #include "Graphics.h"
  13. #include "CPlayerInterface.h"
  14. #include "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 "UIFramework/CGuiHandler.h"
  21. #include "UIFramework/CIntObjectClasses.h"
  22. using namespace CSDL_Ext;
  23. class CBonusItem;
  24. class CCreatureArtifactInstance;
  25. /*
  26. * CCreatureWindow.cpp, part of VCMI engine
  27. *
  28. * Authors: listed in file AUTHORS in main folder
  29. *
  30. * License: GNU General Public License v2.0 or later
  31. * Full text of license available in license.txt file, in main folder
  32. *
  33. */
  34. CCreatureWindow::CCreatureWindow (const CStack &stack, int Type)
  35. : type(Type)
  36. {
  37. OBJ_CONSTRUCTION_CAPTURING_ALL;
  38. if (stack.base)
  39. init(stack.base, &stack, dynamic_cast<const CGHeroInstance*>(stack.base->armyObj));
  40. else
  41. {
  42. CStackInstance * s = new CStackInstance(stack.type, 1); //TODO: war machines and summons should be regular stacks
  43. init(s, &stack, NULL);
  44. delete s;
  45. }
  46. }
  47. CCreatureWindow::CCreatureWindow (const CStackInstance &stack, int Type)
  48. : type(Type)
  49. {
  50. OBJ_CONSTRUCTION_CAPTURING_ALL;
  51. init(&stack, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj));
  52. }
  53. CCreatureWindow::CCreatureWindow(int Cid, int Type, int creatureCount)
  54. :type(Type)
  55. {
  56. OBJ_CONSTRUCTION_CAPTURING_ALL;
  57. CStackInstance * stack = new CStackInstance(Cid, creatureCount); //TODO: simplify?
  58. init(stack, CGI->creh->creatures[Cid], NULL);
  59. delete stack;
  60. }
  61. CCreatureWindow::CCreatureWindow(const CStackInstance &st, int Type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  62. : type(Type), dismiss(0), upgrade(0), ok(0), dsm(Dsm)
  63. {
  64. OBJ_CONSTRUCTION_CAPTURING_ALL;
  65. init(&st, &st,dynamic_cast<const CGHeroInstance*>(st.armyObj));
  66. //print abilities text - if r-click popup
  67. if(type)
  68. {
  69. if(Upg && ui)
  70. {
  71. TResources upgradeCost = ui->cost[0] * st.count;
  72. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  73. {
  74. BLOCK_CAPTURING;
  75. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  76. }
  77. if(LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost))
  78. {
  79. CFunctionList<void()> fs;
  80. fs += Upg;
  81. fs += boost::bind(&CCreatureWindow::close,this);
  82. CFunctionList<void()> cfl;
  83. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], boost::ref(upgResCost), fs, 0, false);
  84. upgrade = new CAdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,385, 148,"IVIEWCR.DEF",SDLK_u);
  85. }
  86. else
  87. {
  88. upgrade = new CAdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),385, 148,"IVIEWCR.DEF");
  89. upgrade->callback.funcs.clear();
  90. upgrade->setOffset(2);
  91. }
  92. }
  93. if(Dsm)
  94. {
  95. CFunctionList<void()> fs[2];
  96. //on dismiss confirmed
  97. fs[0] += Dsm; //dismiss
  98. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  99. CFunctionList<void()> cfl;
  100. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<CComponent*>(),fs[0],fs[1],false);
  101. dismiss = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,333, 148,"IVIEWCR2.DEF",SDLK_d);
  102. }
  103. }
  104. }
  105. void CCreatureWindow::init(const CStackInstance *Stack, const CBonusSystemNode *StackNode, const CGHeroInstance *HeroOwner)
  106. {
  107. stack = Stack;
  108. c = stack->type;
  109. if(!StackNode)
  110. stackNode = c;
  111. else
  112. stackNode = StackNode;
  113. const CStack *battleStack = dynamic_cast<const CStack*>(stackNode); //only during battle
  114. heroOwner = HeroOwner;
  115. if (battleStack)
  116. count = boost::lexical_cast<std::string>(battleStack->count);
  117. else if (Stack->count)
  118. count = boost::lexical_cast<std::string>(Stack->count);
  119. //Basic graphics - need to calculate size
  120. BonusList bl, blTemp;
  121. blTemp = (*(stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT))));
  122. while (blTemp.size())
  123. {
  124. Bonus * b = blTemp.front();
  125. bl.push_back (new Bonus(*b));
  126. bl.back()->val = blTemp.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  127. blTemp.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  128. }
  129. std::string text;
  130. BOOST_FOREACH(Bonus* b, bl)
  131. {
  132. text = stack->bonusToString(b, false);
  133. if (text.size()) //if it's possible to give any description for this kind of bonus
  134. {
  135. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, stack->bonusToString(b, true), stack->bonusToGraphics(b)));
  136. }
  137. }
  138. int magicResistance = 0; //handle it separately :/
  139. if (battleStack)
  140. {
  141. magicResistance = battleStack->magicResistance(); //include Aura of Resistance
  142. }
  143. else
  144. {
  145. magicResistance = stack->magicResistance(); //include Resiatance hero skill
  146. }
  147. if (magicResistance)
  148. {
  149. std::map<TBonusType, std::pair<std::string, std::string> >::const_iterator it = CGI->creh->stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  150. std::string description;
  151. text = it->second.first;
  152. description = it->second.second;
  153. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(magicResistance));
  154. Bonus b;
  155. b.type = Bonus::MAGIC_RESISTANCE;
  156. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, description, stack->bonusToGraphics(&b)));
  157. }
  158. bonusRows = std::min ((int)((bonusItems.size() + 1) / 2), (screen->h - 230) / 60);
  159. vstd::amin(bonusRows, 4);
  160. vstd::amax(bonusRows, 1);
  161. bitmap = new CPicture("CreWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx"); //1 to 4 rows for now
  162. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  163. pos = bitmap->center();
  164. //Buttons
  165. ok = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CCreatureWindow::close,this), 489, 148, "hsbtns.def", SDLK_RETURN);
  166. if (type <= BATTLE) //in battle or info window
  167. {
  168. upgrade = NULL;
  169. dismiss = NULL;
  170. }
  171. anim = new CCreaturePic(22, 48, c);
  172. //Stats
  173. morale = new MoraleLuckBox(true, genRect(42, 42, 335, 100));
  174. morale->set(stack);
  175. luck = new MoraleLuckBox(false, genRect(42, 42, 387, 100));
  176. luck->set(stack);
  177. new CPicture(graphics->pskillsm->ourImages[4].bitmap, 335, 50, false); //exp icon - Print it always?
  178. if (type) //not in fort window
  179. {
  180. if (GameConstants::STACK_EXP)
  181. {
  182. int rank = std::min(stack->getExpRank(), 10); //hopefully nobody adds more
  183. printAtMiddle(CGI->generaltexth->zcrexp[rank] + " [" + boost::lexical_cast<std::string>(rank) + "]", 436, 62, FONT_MEDIUM, Colors::Jasmine,*bitmap);
  184. printAtMiddle(boost::lexical_cast<std::string>(stack->experience), 436, 82, FONT_SMALL, Colors::Cornsilk,*bitmap);
  185. if (type > BATTLE) //we need it only on adv. map
  186. {
  187. int tier = stack->type->level;
  188. if (!vstd::iswithin(tier, 1, 7))
  189. tier = 0;
  190. int number;
  191. std::string expText = CGI->generaltexth->zcrexp[324];
  192. boost::replace_first (expText, "%s", c->namePl);
  193. boost::replace_first (expText, "%s", CGI->generaltexth->zcrexp[rank]);
  194. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(rank));
  195. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->experience));
  196. number = CGI->creh->expRanks[tier][rank] - stack->experience;
  197. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  198. number = CGI->creh->maxExpPerBattle[tier]; //percent
  199. boost::replace_first (expText, "%i%", boost::lexical_cast<std::string>(number));
  200. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  201. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  202. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->count)); //Number of Creatures in stack
  203. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  204. number = (stack->count * (stack->experience - expmin)) / expmin; //Maximum New Recruits without losing current Rank
  205. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //TODO
  206. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(1)); //TODO Experience Multiplier
  207. number = CGI->creh->expAfterUpgrade;
  208. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(number) + "%"); //Upgrade Multiplier
  209. expmin = CGI->creh->expRanks[tier][9];
  210. int expmax = CGI->creh->expRanks[tier][10];
  211. number = expmax - expmin;
  212. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Experience after Rank 10
  213. number = (stack->count * (expmax - expmin)) / expmin;
  214. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  215. expArea = new LRClickableAreaWTextComp(Rect(334, 49, 160, 44),CComponent::experience);
  216. expArea->text = expText;
  217. expArea->bonusValue = 0; //TDO: some specific value or no number at all
  218. }
  219. }
  220. if (GameConstants::STACK_ARTIFACT)
  221. {
  222. if (type > BATTLE) //artifact buttons inactive in battle
  223. {
  224. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, -1), 437, 98, "hsbtns3.def", SDLK_LEFT);
  225. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, +1), 516, 98, "hsbtns5.def", SDLK_RIGHT);
  226. if (heroOwner)
  227. passArtToHero = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, 0), 437, 148, "OVBUTN1.DEF", SDLK_HOME);
  228. }
  229. if (const CArtifactInstance * art = stack->getArt(GameConstants::CREATURE_ART))
  230. blitAt(graphics->artDefs->ourImages[art->id].bitmap, 466, 161, *bitmap);
  231. }
  232. else
  233. creatureArtifact = NULL;
  234. }
  235. if (battleStack) //only during battle
  236. {
  237. //spell effects
  238. int printed=0; //how many effect pics have been printed
  239. std::vector<si32> spells = battleStack->activeSpells();
  240. BOOST_FOREACH(si32 effect, spells)
  241. {
  242. std::string spellText;
  243. if (effect < graphics->spellEffectsPics->ourImages.size()) //not all effects have graphics (for eg. Acid Breath)
  244. {
  245. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  246. boost::replace_first (spellText, "%s", CGI->spellh->spells[effect]->name);
  247. int duration = battleStack->getBonus(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  248. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  249. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 20 + 52 * printed, 184, *bitmap);
  250. spellEffects.push_back(new LRClickableAreaWText(Rect(20 + 52 * printed, 184, 50, 38), spellText, spellText));
  251. if (++printed >= 10) //we can fit only 10 effects
  252. break;
  253. }
  254. }
  255. //print current health
  256. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  257. }
  258. if (bonusItems.size() > (bonusRows << 1)) //only after graphics are created
  259. {
  260. slider = new CSlider(528, 231, bonusRows*60, boost::bind (&CCreatureWindow::sliderMoved, this, _1),
  261. bonusRows, (bonusItems.size() + 1) >> 1, 0, false, 0);
  262. }
  263. else //slider automatically places bonus Items
  264. recreateSkillList (0);
  265. showAll(screen2);
  266. //AUIDAT.DEF
  267. }
  268. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  269. {
  270. printAt(text, 162, 48 + nr*19, FONT_SMALL, Colors::Cornsilk, *bitmap);
  271. std::string hlp;
  272. if(range && baseVal != val)
  273. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  274. else if(baseVal != val && val>=0)
  275. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  276. else
  277. hlp = boost::lexical_cast<std::string>(baseVal);
  278. printTo(hlp, 325, 64 + nr*19, FONT_SMALL, Colors::Cornsilk, *bitmap);
  279. }
  280. void CCreatureWindow::recreateSkillList(int Pos)
  281. {
  282. int n = 0, i = 0, j = 0;
  283. int numSkills = std::min ((bonusRows + Pos) << 1, (int)bonusItems.size());
  284. std::string gfxName;
  285. for (n = 0; n < Pos << 1; ++n)
  286. {
  287. bonusItems[n]->visible = false;
  288. }
  289. for (n = Pos << 1; n < numSkills; ++n)
  290. {
  291. int offsetx = 257*j - (bonusRows == 4 ? 1 : 0);
  292. int offsety = 60*i + (bonusRows > 1 ? 1 : 0); //lack of precision :/
  293. bonusItems[n]->moveTo (Point(pos.x + offsetx + 10, pos.y + offsety + 230), true);
  294. bonusItems[n]->visible = true;
  295. if (++j > 1) //next line
  296. {
  297. ++i;
  298. j = 0;
  299. }
  300. }
  301. for (n = numSkills; n < bonusItems.size(); ++n)
  302. {
  303. bonusItems[n]->visible = false;
  304. }
  305. }
  306. void CCreatureWindow::showAll(SDL_Surface * to)
  307. {
  308. CIntObject::showAll(to);
  309. printAtMiddle(c->namePl, 180, 30, FONT_SMALL, Colors::Jasmine,*bitmap); //creature name
  310. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->Attack());
  311. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->Defense());
  312. if(stackNode->valOfBonuses(Bonus::SHOTS) && stackNode->hasBonusOfType(Bonus::SHOOTER))
  313. {//only for shooting units - important with wog exp shooters
  314. if (type == BATTLE)
  315. printLine(2, CGI->generaltexth->allTexts[198], dynamic_cast<const CStack*>(stackNode)->shots);
  316. else
  317. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  318. }
  319. //TODO
  320. int dmgMultiply = 1;
  321. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  322. dmgMultiply += heroOwner->Attack();
  323. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  324. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  325. printLine(6, CGI->generaltexth->zelp[441].first, c->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  326. BOOST_FOREACH(CBonusItem* b, bonusItems)
  327. b->showAll (to);
  328. }
  329. void CCreatureWindow::show(SDL_Surface * to)
  330. {
  331. if (count.size()) //army stack
  332. printTo(count, pos.x + 114, pos.y + 174,FONT_TIMES, Colors::Cornsilk, to);
  333. }
  334. void CCreatureWindow::sliderMoved(int newpos)
  335. {
  336. recreateSkillList(newpos); //move components
  337. redraw();
  338. }
  339. void CCreatureWindow::scrollArt(int dir)
  340. {
  341. //TODO: get next artifact
  342. creatureArtifact = const_cast<CArtifactInstance*>(stack->getArt(GameConstants::CREATURE_ART));
  343. }
  344. void CCreatureWindow::passArtifactToHero()
  345. {
  346. //creatureArtifact->artType; //FIXME
  347. }
  348. void CCreatureWindow::clickRight(tribool down, bool previousState)
  349. {
  350. if(down)
  351. return;
  352. if (type < 3)
  353. close();
  354. }
  355. //void CCreatureWindow::activate()
  356. //{
  357. // CIntObject::activate();
  358. // if(type < 3)
  359. // activateRClick();
  360. //}
  361. //void CCreatureWindow::deactivate()
  362. //{
  363. //
  364. //}
  365. void CCreatureWindow::close()
  366. {
  367. GH.popIntTotally(this);
  368. }
  369. CCreatureWindow::~CCreatureWindow()
  370. {
  371. for (int i=0; i<upgResCost.size(); ++i)
  372. delete upgResCost[i];
  373. bonusItems.clear();
  374. }
  375. CBonusItem::CBonusItem()
  376. {
  377. }
  378. CBonusItem::CBonusItem(const Rect &Pos, const std::string &Name, const std::string &Description, const std::string &graphicsName)
  379. {
  380. OBJ_CONSTRUCTION;
  381. visible = false;
  382. name = Name;
  383. description = Description;
  384. if (graphicsName.size())
  385. bonusGraphics = new CPicture(graphicsName, 26, 232);
  386. else
  387. bonusGraphics = NULL;
  388. used = 0; //no actions atm
  389. }
  390. void CBonusItem::showAll (SDL_Surface * to)
  391. {
  392. if (visible)
  393. {
  394. printAt(name, pos.x + 72, pos.y + 6, FONT_SMALL, Colors::Jasmine, to);
  395. printAt(description, pos.x + 72, pos.y + 30, FONT_SMALL, Colors::Cornsilk, to);
  396. if (bonusGraphics && bonusGraphics->bg)
  397. blitAtLoc(bonusGraphics->bg, 12, 2, to);
  398. }
  399. }
  400. CBonusItem::~CBonusItem()
  401. {
  402. //delete bonusGraphics; //automatic destruction
  403. }
  404. void CCreInfoWindow::show(SDL_Surface * to)
  405. {
  406. CIntObject::show(to);
  407. creatureCount->showAll(to);
  408. }
  409. CCreInfoWindow::CCreInfoWindow(const CStackInstance &stack, bool LClicked, boost::function<void()> upgradeFunc, boost::function<void()> dismissFunc, UpgradeInfo *upgradeInfo)
  410. {
  411. OBJ_CONSTRUCTION_CAPTURING_ALL;
  412. init(stack.type, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj), stack.count, LClicked);
  413. //additional buttons if opened with left click
  414. if(LClicked)
  415. {
  416. boost::function<void()> closeFunc = boost::bind(&CCreInfoWindow::close,this);
  417. if(upgradeFunc && upgradeInfo)
  418. {
  419. TResources upgradeCost = upgradeInfo->cost[0] * stack.count;
  420. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  421. {
  422. BLOCK_CAPTURING;
  423. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  424. }
  425. CFunctionList<void()> onUpgrade;
  426. onUpgrade += upgradeFunc;
  427. onUpgrade += closeFunc;
  428. boost::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  429. LOCPLINT,
  430. CGI->generaltexth->allTexts[207],
  431. boost::ref(upgResCost),
  432. onUpgrade, 0, false);
  433. upgrade = new CAdventureMapButton("", CGI->generaltexth->zelp[446].second, dialog, 76, 237, "IVIEWCR", SDLK_u);
  434. upgrade->block(!LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost));
  435. }
  436. if(dismissFunc)
  437. {
  438. CFunctionList<void()> onDismiss;
  439. onDismiss += dismissFunc;
  440. onDismiss += closeFunc;
  441. boost::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  442. LOCPLINT,
  443. CGI->generaltexth->allTexts[12],
  444. std::vector<CComponent*>(),
  445. onDismiss, 0, true);
  446. dismiss = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second, dialog, 21, 237, "IVIEWCR2",SDLK_d);
  447. }
  448. }
  449. }
  450. CCreInfoWindow::CCreInfoWindow(int creatureID, bool LClicked, int creatureCount)
  451. {
  452. OBJ_CONSTRUCTION_CAPTURING_ALL;
  453. const CCreature *creature = CGI->creh->creatures[creatureID];
  454. init(creature, NULL, NULL, creatureCount, LClicked);
  455. }
  456. CCreInfoWindow::CCreInfoWindow(const CStack &stack, bool LClicked)
  457. {
  458. OBJ_CONSTRUCTION_CAPTURING_ALL;
  459. init(stack.getCreature(), &stack, stack.getMyHero(), stack.count, LClicked);
  460. }
  461. CCreInfoWindow::~CCreInfoWindow()
  462. {
  463. BOOST_FOREACH(CComponent* object, upgResCost)
  464. delete object;
  465. }
  466. void CCreInfoWindow::printLine(int position, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  467. {
  468. infoTexts[position].first = new CLabel(155, 48 + position*19, FONT_SMALL, TOPLEFT, Colors::Cornsilk, text);
  469. std::string valueStr;
  470. if(range && baseVal != val)
  471. valueStr = boost::str(boost::format("%d - %d") % baseVal % val);
  472. else if(baseVal != val && val>=0)
  473. valueStr = boost::str(boost::format("%d (%d)") % baseVal % val);
  474. else
  475. valueStr = boost::lexical_cast<std::string>(baseVal);
  476. infoTexts[position].second = new CLabel(276, 63 + position*19, FONT_SMALL, BOTTOMRIGHT, Colors::Cornsilk, valueStr);
  477. }
  478. void CCreInfoWindow::init(const CCreature *creature, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner, int count, bool LClicked)
  479. {
  480. used = 0;
  481. if (!LClicked)
  482. used |= RCLICK;
  483. if(!stackNode)
  484. stackNode = creature;
  485. background = new CPicture("CRSTKPU");
  486. background->colorize(LOCPLINT->playerID);
  487. pos = background->center();
  488. animation = new CCreaturePic(21, 48, creature);
  489. std::string countStr = boost::lexical_cast<std::string>(count);
  490. creatureCount = new CLabel(114, 174, FONT_TIMES, BOTTOMRIGHT, Colors::Cornsilk, countStr);
  491. creatureName = new CLabel(149, 30, FONT_SMALL, CENTER, Colors::Jasmine, creature->namePl);
  492. printLine(0, CGI->generaltexth->primarySkillNames[0], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  493. printLine(1, CGI->generaltexth->primarySkillNames[1], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  494. if(stackNode->valOfBonuses(Bonus::SHOTS))
  495. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  496. //TODO
  497. int dmgMultiply = 1;
  498. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  499. dmgMultiply += heroOwner->Attack();
  500. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  501. printLine(4, CGI->generaltexth->allTexts[388], creature->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  502. printLine(6, CGI->generaltexth->zelp[441].first, creature->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  503. //setting morale
  504. morale = new MoraleLuckBox(true, genRect(42, 42, 22, 186));
  505. morale->set(stackNode);
  506. //setting luck
  507. luck = new MoraleLuckBox(false, genRect(42, 42, 75, 186));
  508. luck->set(stackNode);
  509. if(!LClicked)
  510. {
  511. abilityText = new CLabel(17, 231, FONT_SMALL, TOPLEFT, Colors::Cornsilk, creature->abilityText);
  512. }
  513. else
  514. {
  515. abilityText = NULL;
  516. ok = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second,
  517. boost::bind(&CCreInfoWindow::close,this), 216, 237, "IOKAY.DEF", SDLK_RETURN);
  518. }
  519. //if we are displying window fo r stack in battle, there are several more things that we need to display
  520. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  521. {
  522. //print at most 3 spell effects
  523. std::vector<si32> spells = battleStack->activeSpells();
  524. for (size_t i=0; i< std::min(spells.size(), size_t(3)); i++)
  525. effects.push_back(new CAnimImage("SpellInt", spells[i]+1, 0, 127 + 52*i, 186));
  526. //print current health
  527. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  528. }
  529. }
  530. void CCreInfoWindow::close()
  531. {
  532. GH.popIntTotally(this);
  533. }
  534. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  535. {
  536. close();
  537. }
  538. CIntObject * createCreWindow(const CStack *s, bool lclick/* = false*/)
  539. {
  540. if(settings["general"]["classicCreatureWindow"].Bool())
  541. return new CCreInfoWindow(*s, lclick);
  542. else
  543. return new CCreatureWindow(*s, CCreatureWindow::BATTLE);
  544. }
  545. CIntObject * createCreWindow(int Cid, int Type, int creatureCount)
  546. {
  547. if(settings["general"]["classicCreatureWindow"].Bool())
  548. return new CCreInfoWindow(Cid, Type, creatureCount);
  549. else
  550. return new CCreatureWindow(Cid, Type, creatureCount);
  551. }
  552. CIntObject * createCreWindow(const CStackInstance *s, int type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  553. {
  554. if(settings["general"]["classicCreatureWindow"].Bool())
  555. return new CCreInfoWindow(*s, type==3, Upg, Dsm, ui);
  556. else
  557. return new CCreatureWindow(*s, type, Upg, Dsm, ui);
  558. }