CCreatureWindow.cpp 24 KB

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