CCreatureWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 (Stack->count)
  116. count = boost::lexical_cast<std::string>(Stack->count);
  117. //Basic graphics - need to calculate size
  118. BonusList bl, blTemp;
  119. blTemp = (*(stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT))));
  120. while (blTemp.size())
  121. {
  122. Bonus * b = blTemp.front();
  123. bl.push_back (new Bonus(*b));
  124. bl.back()->val = blTemp.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  125. blTemp.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  126. }
  127. std::string text;
  128. BOOST_FOREACH(Bonus* b, bl)
  129. {
  130. text = stack->bonusToString(b, false);
  131. if (text.size()) //if it's possible to give any description for this kind of bonus
  132. {
  133. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, stack->bonusToString(b, true), stack->bonusToGraphics(b)));
  134. }
  135. }
  136. int magicResistance = 0; //handle it separately :/
  137. if (battleStack)
  138. {
  139. magicResistance = battleStack->magicResistance(); //include Aura of Resistance
  140. }
  141. else
  142. {
  143. magicResistance = stack->magicResistance(); //include Resiatance hero skill
  144. }
  145. if (magicResistance)
  146. {
  147. std::map<TBonusType, std::pair<std::string, std::string> >::const_iterator it = CGI->creh->stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  148. std::string description;
  149. text = it->second.first;
  150. description = it->second.second;
  151. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(magicResistance));
  152. Bonus b;
  153. b.type = Bonus::MAGIC_RESISTANCE;
  154. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, description, stack->bonusToGraphics(&b)));
  155. }
  156. bonusRows = std::min ((int)((bonusItems.size() + 1) / 2), (screen->h - 230) / 60);
  157. vstd::amin(bonusRows, 4);
  158. vstd::amax(bonusRows, 1);
  159. bitmap = new CPicture("CreWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx"); //1 to 4 rows for now
  160. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  161. pos = bitmap->center();
  162. //Buttons
  163. ok = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CCreatureWindow::close,this), 489, 148, "hsbtns.def", SDLK_RETURN);
  164. if (type <= BATTLE) //in battle or info window
  165. {
  166. upgrade = NULL;
  167. dismiss = NULL;
  168. }
  169. anim = new CCreaturePic(22, 48, c);
  170. //Stats
  171. morale = new MoraleLuckBox(true, genRect(42, 42, 335, 100));
  172. morale->set(stack);
  173. luck = new MoraleLuckBox(false, genRect(42, 42, 387, 100));
  174. luck->set(stack);
  175. new CPicture(graphics->pskillsm->ourImages[4].bitmap, 335, 50, false); //exp icon - Print it always?
  176. if (type) //not in fort window
  177. {
  178. if (GameConstants::STACK_EXP)
  179. {
  180. int rank = std::min(stack->getExpRank(), 10); //hopefully nobody adds more
  181. printAtMiddle(CGI->generaltexth->zcrexp[rank] + " [" + boost::lexical_cast<std::string>(rank) + "]", 436, 62, FONT_MEDIUM, Colors::Jasmine,*bitmap);
  182. printAtMiddle(boost::lexical_cast<std::string>(stack->experience), 436, 82, FONT_SMALL, Colors::Cornsilk,*bitmap);
  183. if (type > BATTLE) //we need it only on adv. map
  184. {
  185. int tier = stack->type->level;
  186. if (!vstd::iswithin(tier, 1, 7))
  187. tier = 0;
  188. int number;
  189. std::string expText = CGI->generaltexth->zcrexp[324];
  190. boost::replace_first (expText, "%s", c->namePl);
  191. boost::replace_first (expText, "%s", CGI->generaltexth->zcrexp[rank]);
  192. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(rank));
  193. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->experience));
  194. number = CGI->creh->expRanks[tier][rank] - stack->experience;
  195. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  196. number = CGI->creh->maxExpPerBattle[tier]; //percent
  197. boost::replace_first (expText, "%i%", boost::lexical_cast<std::string>(number));
  198. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  199. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  200. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->count)); //Number of Creatures in stack
  201. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  202. number = (stack->count * (stack->experience - expmin)) / expmin; //Maximum New Recruits without losing current Rank
  203. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //TODO
  204. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(1)); //TODO Experience Multiplier
  205. number = CGI->creh->expAfterUpgrade;
  206. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(number) + "%"); //Upgrade Multiplier
  207. expmin = CGI->creh->expRanks[tier][9];
  208. int expmax = CGI->creh->expRanks[tier][10];
  209. number = expmax - expmin;
  210. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Experience after Rank 10
  211. number = (stack->count * (expmax - expmin)) / expmin;
  212. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  213. expArea = new LRClickableAreaWTextComp(Rect(334, 49, 160, 44),CComponent::experience);
  214. expArea->text = expText;
  215. expArea->bonusValue = 0; //TDO: some specific value or no number at all
  216. }
  217. }
  218. if (GameConstants::STACK_ARTIFACT)
  219. {
  220. if (type > BATTLE) //artifact buttons inactive in battle
  221. {
  222. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, -1), 437, 98, "hsbtns3.def", SDLK_LEFT);
  223. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, +1), 516, 98, "hsbtns5.def", SDLK_RIGHT);
  224. if (heroOwner)
  225. passArtToHero = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, 0), 437, 148, "OVBUTN1.DEF", SDLK_HOME);
  226. }
  227. if (const CArtifactInstance * art = stack->getArt(GameConstants::CREATURE_ART))
  228. blitAt(graphics->artDefs->ourImages[art->id].bitmap, 466, 161, *bitmap);
  229. }
  230. else
  231. creatureArtifact = NULL;
  232. }
  233. if (battleStack) //only during battle
  234. {
  235. //spell effects
  236. int printed=0; //how many effect pics have been printed
  237. std::vector<si32> spells = battleStack->activeSpells();
  238. BOOST_FOREACH(si32 effect, spells)
  239. {
  240. std::string spellText;
  241. if (effect < graphics->spellEffectsPics->ourImages.size()) //not all effects have graphics (for eg. Acid Breath)
  242. {
  243. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  244. boost::replace_first (spellText, "%s", CGI->spellh->spells[effect]->name);
  245. int duration = battleStack->getBonus(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  246. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  247. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 20 + 52 * printed, 184, *bitmap);
  248. spellEffects.push_back(new LRClickableAreaWText(Rect(20 + 52 * printed, 184, 50, 38), spellText, spellText));
  249. if (++printed >= 10) //we can fit only 10 effects
  250. break;
  251. }
  252. }
  253. //print current health
  254. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  255. }
  256. if (bonusItems.size() > (bonusRows << 1)) //only after graphics are created
  257. {
  258. slider = new CSlider(528, 231, bonusRows*60, boost::bind (&CCreatureWindow::sliderMoved, this, _1),
  259. bonusRows, (bonusItems.size() + 1) >> 1, 0, false, 0);
  260. }
  261. else //slider automatically places bonus Items
  262. recreateSkillList (0);
  263. showAll(screen2);
  264. //AUIDAT.DEF
  265. }
  266. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  267. {
  268. printAt(text, 162, 48 + nr*19, FONT_SMALL, Colors::Cornsilk, *bitmap);
  269. std::string hlp;
  270. if(range && baseVal != val)
  271. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  272. else if(baseVal != val && val>=0)
  273. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  274. else
  275. hlp = boost::lexical_cast<std::string>(baseVal);
  276. printTo(hlp, 325, 64 + nr*19, FONT_SMALL, Colors::Cornsilk, *bitmap);
  277. }
  278. void CCreatureWindow::recreateSkillList(int Pos)
  279. {
  280. int n = 0, i = 0, j = 0;
  281. int numSkills = std::min ((bonusRows + Pos) << 1, (int)bonusItems.size());
  282. std::string gfxName;
  283. for (n = 0; n < Pos << 1; ++n)
  284. {
  285. bonusItems[n]->visible = false;
  286. }
  287. for (n = Pos << 1; n < numSkills; ++n)
  288. {
  289. int offsetx = 257*j - (bonusRows == 4 ? 1 : 0);
  290. int offsety = 60*i + (bonusRows > 1 ? 1 : 0); //lack of precision :/
  291. bonusItems[n]->moveTo (Point(pos.x + offsetx + 10, pos.y + offsety + 230), true);
  292. bonusItems[n]->visible = true;
  293. if (++j > 1) //next line
  294. {
  295. ++i;
  296. j = 0;
  297. }
  298. }
  299. for (n = numSkills; n < bonusItems.size(); ++n)
  300. {
  301. bonusItems[n]->visible = false;
  302. }
  303. }
  304. void CCreatureWindow::showAll(SDL_Surface * to)
  305. {
  306. CIntObject::showAll(to);
  307. printAtMiddle(c->namePl, 180, 30, FONT_SMALL, Colors::Jasmine,*bitmap); //creature name
  308. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->Attack());
  309. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->Defense());
  310. if(stackNode->valOfBonuses(Bonus::SHOTS) && stackNode->hasBonusOfType(Bonus::SHOOTER))
  311. {//only for shooting units - important with wog exp shooters
  312. if (type == BATTLE)
  313. printLine(2, CGI->generaltexth->allTexts[198], dynamic_cast<const CStack*>(stackNode)->shots);
  314. else
  315. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  316. }
  317. //TODO
  318. int dmgMultiply = 1;
  319. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  320. dmgMultiply += heroOwner->Attack();
  321. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  322. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  323. printLine(6, CGI->generaltexth->zelp[441].first, c->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  324. BOOST_FOREACH(CBonusItem* b, bonusItems)
  325. b->showAll (to);
  326. }
  327. void CCreatureWindow::show(SDL_Surface * to)
  328. {
  329. if (count.size()) //army stack
  330. printTo(count, pos.x + 114, pos.y + 174,FONT_TIMES, Colors::Cornsilk, to);
  331. }
  332. void CCreatureWindow::sliderMoved(int newpos)
  333. {
  334. recreateSkillList(newpos); //move components
  335. redraw();
  336. }
  337. void CCreatureWindow::scrollArt(int dir)
  338. {
  339. //TODO: get next artifact
  340. creatureArtifact = const_cast<CArtifactInstance*>(stack->getArt(GameConstants::CREATURE_ART));
  341. }
  342. void CCreatureWindow::passArtifactToHero()
  343. {
  344. creatureArtifact->artType;
  345. }
  346. void CCreatureWindow::clickRight(tribool down, bool previousState)
  347. {
  348. if(down)
  349. return;
  350. if (type < 3)
  351. close();
  352. }
  353. //void CCreatureWindow::activate()
  354. //{
  355. // CIntObject::activate();
  356. // if(type < 3)
  357. // activateRClick();
  358. //}
  359. //void CCreatureWindow::deactivate()
  360. //{
  361. //
  362. //}
  363. void CCreatureWindow::close()
  364. {
  365. GH.popIntTotally(this);
  366. }
  367. CCreatureWindow::~CCreatureWindow()
  368. {
  369. for (int i=0; i<upgResCost.size(); ++i)
  370. delete upgResCost[i];
  371. bonusItems.clear();
  372. }
  373. CBonusItem::CBonusItem()
  374. {
  375. }
  376. CBonusItem::CBonusItem(const Rect &Pos, const std::string &Name, const std::string &Description, const std::string &graphicsName)
  377. {
  378. OBJ_CONSTRUCTION;
  379. visible = false;
  380. name = Name;
  381. description = Description;
  382. if (graphicsName.size())
  383. bonusGraphics = new CPicture(graphicsName, 26, 232);
  384. else
  385. bonusGraphics = NULL;
  386. used = 0; //no actions atm
  387. }
  388. void CBonusItem::showAll (SDL_Surface * to)
  389. {
  390. if (visible)
  391. {
  392. printAt(name, pos.x + 72, pos.y + 6, FONT_SMALL, Colors::Jasmine, to);
  393. printAt(description, pos.x + 72, pos.y + 30, FONT_SMALL, Colors::Cornsilk, to);
  394. if (bonusGraphics && bonusGraphics->bg)
  395. blitAtLoc(bonusGraphics->bg, 12, 2, to);
  396. }
  397. }
  398. CBonusItem::~CBonusItem()
  399. {
  400. //delete bonusGraphics; //automatic destruction
  401. }
  402. void CCreInfoWindow::show(SDL_Surface * to)
  403. {
  404. CIntObject::show(to);
  405. creatureCount->showAll(to);
  406. }
  407. CCreInfoWindow::CCreInfoWindow(const CStackInstance &stack, bool LClicked, boost::function<void()> upgradeFunc, boost::function<void()> dismissFunc, UpgradeInfo *upgradeInfo)
  408. {
  409. OBJ_CONSTRUCTION_CAPTURING_ALL;
  410. init(stack.type, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj), stack.count, LClicked);
  411. //additional buttons if opened with left click
  412. if(LClicked)
  413. {
  414. boost::function<void()> closeFunc = boost::bind(&CCreInfoWindow::close,this);
  415. if(upgradeFunc && upgradeInfo)
  416. {
  417. TResources upgradeCost = upgradeInfo->cost[0] * stack.count;
  418. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  419. {
  420. BLOCK_CAPTURING;
  421. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  422. }
  423. CFunctionList<void()> onUpgrade;
  424. onUpgrade += upgradeFunc;
  425. onUpgrade += closeFunc;
  426. boost::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  427. LOCPLINT,
  428. CGI->generaltexth->allTexts[207],
  429. boost::ref(upgResCost),
  430. onUpgrade, 0, false);
  431. upgrade = new CAdventureMapButton("", CGI->generaltexth->zelp[446].second, dialog, 76, 237, "IVIEWCR", SDLK_u);
  432. upgrade->block(!LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost));
  433. }
  434. if(dismissFunc)
  435. {
  436. CFunctionList<void()> onDismiss;
  437. onDismiss += dismissFunc;
  438. onDismiss += closeFunc;
  439. boost::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  440. LOCPLINT,
  441. CGI->generaltexth->allTexts[12],
  442. std::vector<CComponent*>(),
  443. onDismiss, 0, true);
  444. dismiss = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second, dialog, 21, 237, "IVIEWCR2",SDLK_d);
  445. }
  446. ok = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second,
  447. boost::bind(&CCreInfoWindow::close,this), 216, 237, "IOKAY.DEF", SDLK_RETURN);
  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. abilityText = new CLabel(17, 231, FONT_SMALL, TOPLEFT, Colors::Cornsilk, creature->abilityText);
  511. else
  512. abilityText = NULL;
  513. //if we are displying window fo r stack in battle, there are several more things that we need to display
  514. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  515. {
  516. //print at most 3 spell effects
  517. std::vector<si32> spells = battleStack->activeSpells();
  518. for (size_t i=0; i< std::min(spells.size(), size_t(3)); i++)
  519. effects.push_back(new CAnimImage("SpellInt", spells[i]+1, 0, 127 + 52*i, 186));
  520. //print current health
  521. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  522. }
  523. }
  524. void CCreInfoWindow::close()
  525. {
  526. GH.popIntTotally(this);
  527. }
  528. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  529. {
  530. close();
  531. }
  532. CIntObject * createCreWindow(const CStack *s)
  533. {
  534. if(settings["general"]["classicCreatureWindow"].Bool())
  535. return new CCreInfoWindow(*s);
  536. else
  537. return new CCreatureWindow(*s, CCreatureWindow::BATTLE);
  538. }
  539. CIntObject * createCreWindow(int Cid, int Type, int creatureCount)
  540. {
  541. if(settings["general"]["classicCreatureWindow"].Bool())
  542. return new CCreInfoWindow(Cid, Type, creatureCount);
  543. else
  544. return new CCreatureWindow(Cid, Type, creatureCount);
  545. }
  546. CIntObject * createCreWindow(const CStackInstance *s, int type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  547. {
  548. if(settings["general"]["classicCreatureWindow"].Bool())
  549. return new CCreInfoWindow(*s, type==3, Upg, Dsm, ui);
  550. else
  551. return new CCreatureWindow(*s, type, Upg, Dsm, ui);
  552. }