CCreatureWindow.cpp 22 KB

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