CCreatureWindow.cpp 24 KB

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