CCreatureWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 "SDL_Extensions.h"
  10. #include "CBitmapHandler.h"
  11. #include "CDefHandler.h"
  12. #include "Graphics.h"
  13. #include "AdventureMapButton.h"
  14. #include "CPlayerInterface.h"
  15. #include "CConfigHandler.h"
  16. #include "../lib/CGameState.h"
  17. #include "../lib/BattleState.h"
  18. #include "../lib/CSpellHandler.h"
  19. #include "UIFramework/CGuiHandler.h"
  20. using namespace CSDL_Ext;
  21. class CBonusItem;
  22. /*
  23. * CCreatureWindow.cpp, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  31. CCreatureWindow::CCreatureWindow (const CStack &stack, int Type)
  32. : type(Type)
  33. {
  34. OBJ_CONSTRUCTION_CAPTURING_ALL;
  35. if (stack.base)
  36. init(stack.base, &stack, dynamic_cast<const CGHeroInstance*>(stack.base->armyObj));
  37. else
  38. {
  39. CStackInstance * s = new CStackInstance(stack.type, 1); //TODO: war machines and summons should be regular stacks
  40. init(s, &stack, NULL);
  41. delete s;
  42. }
  43. }
  44. CCreatureWindow::CCreatureWindow (const CStackInstance &stack, int Type)
  45. : type(Type)
  46. {
  47. OBJ_CONSTRUCTION_CAPTURING_ALL;
  48. init(&stack, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj));
  49. }
  50. CCreatureWindow::CCreatureWindow(int Cid, int Type, int creatureCount)
  51. :type(Type)
  52. {
  53. OBJ_CONSTRUCTION_CAPTURING_ALL;
  54. CStackInstance * stack = new CStackInstance(Cid, creatureCount); //TODO: simplify?
  55. init(stack, CGI->creh->creatures[Cid], NULL);
  56. delete stack;
  57. }
  58. CCreatureWindow::CCreatureWindow(const CStackInstance &st, int Type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  59. : type(Type), dismiss(0), upgrade(0), ok(0), dsm(Dsm)
  60. {
  61. OBJ_CONSTRUCTION_CAPTURING_ALL;
  62. init(&st, &st,dynamic_cast<const CGHeroInstance*>(st.armyObj));
  63. //print abilities text - if r-click popup
  64. if(type)
  65. {
  66. if(Upg && ui)
  67. {
  68. TResources upgradeCost = ui->cost[0] * st.count;
  69. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  70. {
  71. BLOCK_CAPTURING;
  72. upgResCost.push_back(new SComponent(SComponent::resource, i->resType, i->resVal));
  73. }
  74. if(LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost))
  75. {
  76. CFunctionList<void()> fs;
  77. fs += Upg;
  78. fs += boost::bind(&CCreatureWindow::close,this);
  79. CFunctionList<void()> cfl;
  80. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], boost::ref(upgResCost), fs, 0, false);
  81. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,385, 148,"IVIEWCR.DEF",SDLK_u);
  82. }
  83. else
  84. {
  85. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),385, 148,"IVIEWCR.DEF");
  86. upgrade->callback.funcs.clear();
  87. upgrade->setOffset(2);
  88. }
  89. }
  90. if(Dsm)
  91. {
  92. CFunctionList<void()> fs[2];
  93. //on dismiss confirmed
  94. fs[0] += Dsm; //dismiss
  95. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  96. CFunctionList<void()> cfl;
  97. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  98. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,333, 148,"IVIEWCR2.DEF",SDLK_d);
  99. }
  100. }
  101. }
  102. void CCreatureWindow::init(const CStackInstance *Stack, const CBonusSystemNode *StackNode, const CGHeroInstance *HeroOwner)
  103. {
  104. stack = Stack;
  105. c = stack->type;
  106. if(!StackNode)
  107. stackNode = c;
  108. else
  109. stackNode = StackNode;
  110. const CStack *battleStack = dynamic_cast<const CStack*>(stackNode); //only during battle
  111. heroOwner = HeroOwner;
  112. if (Stack->count)
  113. count = boost::lexical_cast<std::string>(Stack->count);
  114. //Basic graphics - need to calculate size
  115. BonusList bl, blTemp;
  116. blTemp = (*(stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT))));
  117. while (blTemp.size())
  118. {
  119. Bonus * b = blTemp.front();
  120. bl.push_back (new Bonus(*b));
  121. bl.back()->val = blTemp.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  122. blTemp.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  123. }
  124. std::string text;
  125. BOOST_FOREACH(Bonus* b, bl)
  126. {
  127. text = stack->bonusToString(b, false);
  128. if (text.size()) //if it's possible to give any description for this kind of bonus
  129. {
  130. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, stack->bonusToString(b, true), stack->bonusToGraphics(b)));
  131. }
  132. }
  133. int magicResistance = 0; //handle it separately :/
  134. if (battleStack)
  135. {
  136. magicResistance = battleStack->magicResistance(); //include Aura of Resistance
  137. }
  138. else
  139. {
  140. magicResistance = stack->magicResistance(); //include Resiatance hero skill
  141. }
  142. if (magicResistance)
  143. {
  144. std::map<TBonusType, std::pair<std::string, std::string> >::const_iterator it = CGI->creh->stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  145. std::string description;
  146. text = it->second.first;
  147. description = it->second.second;
  148. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(magicResistance));
  149. Bonus b;
  150. b.type = Bonus::MAGIC_RESISTANCE;
  151. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, description, stack->bonusToGraphics(&b)));
  152. }
  153. bonusRows = std::min ((int)((bonusItems.size() + 1) / 2), (conf.cc.resy - 230) / 60);
  154. vstd::amin(bonusRows, 4);
  155. vstd::amax(bonusRows, 1);
  156. bitmap = new CPicture("CreWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx"); //1 to 4 rows for now
  157. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  158. pos = bitmap->center();
  159. //Buttons
  160. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CCreatureWindow::close,this), 489, 148, "hsbtns.def", SDLK_RETURN);
  161. if (type <= BATTLE) //in battle or info window
  162. {
  163. upgrade = NULL;
  164. dismiss = NULL;
  165. }
  166. anim = new CCreaturePic(22, 48, c);
  167. //Stats
  168. morale = new MoraleLuckBox(true, genRect(42, 42, 335, 100));
  169. morale->set(stack);
  170. luck = new MoraleLuckBox(false, genRect(42, 42, 387, 100));
  171. luck->set(stack);
  172. new CPicture(graphics->pskillsm->ourImages[4].bitmap, 335, 50, false); //exp icon - Print it always?
  173. if (type) //not in fort window
  174. {
  175. if (GameConstants::STACK_EXP)
  176. {
  177. int rank = std::min(stack->getExpRank(), 10); //hopefully nobody adds more
  178. printAtMiddle(CGI->generaltexth->zcrexp[rank] + " [" + boost::lexical_cast<std::string>(rank) + "]", 436, 62, FONT_MEDIUM, tytulowy,*bitmap);
  179. printAtMiddle(boost::lexical_cast<std::string>(stack->experience), 436, 82, FONT_SMALL, zwykly,*bitmap);
  180. if (type > BATTLE) //we need it only on adv. map
  181. {
  182. int tier = stack->type->level;
  183. if (!vstd::iswithin(tier, 1, 7))
  184. tier = 0;
  185. int number;
  186. std::string expText = CGI->generaltexth->zcrexp[324];
  187. boost::replace_first (expText, "%s", c->namePl);
  188. boost::replace_first (expText, "%s", CGI->generaltexth->zcrexp[rank]);
  189. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(rank));
  190. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->experience));
  191. number = CGI->creh->expRanks[tier][rank] - stack->experience;
  192. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  193. number = CGI->creh->maxExpPerBattle[tier]; //percent
  194. boost::replace_first (expText, "%i%", boost::lexical_cast<std::string>(number));
  195. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  196. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  197. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->count)); //Number of Creatures in stack
  198. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  199. number = (stack->count * (stack->experience - expmin)) / expmin; //Maximum New Recruits without losing current Rank
  200. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //TODO
  201. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(1)); //TODO Experience Multiplier
  202. number = CGI->creh->expAfterUpgrade;
  203. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(number) + "%"); //Upgrade Multiplier
  204. expmin = CGI->creh->expRanks[tier][9];
  205. int expmax = CGI->creh->expRanks[tier][10];
  206. number = expmax - expmin;
  207. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Experience after Rank 10
  208. number = (stack->count * (expmax - expmin)) / expmin;
  209. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  210. expArea = new LRClickableAreaWTextComp(SRect(334, 49, 160, 44),SComponent::experience);
  211. expArea->text = expText;
  212. expArea->bonusValue = 0; //TDO: some specific value or no number at all
  213. }
  214. }
  215. if (GameConstants::STACK_ARTIFACT && type > BATTLE)
  216. {
  217. //SDL_Rect rect = genRect(44,44,465,98);
  218. //creatureArtifact = new CArtPlace(NULL);
  219. //creatureArtifact->pos = rect;
  220. //creatureArtifact->ourOwner = NULL; //hmm?
  221. leftArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, -1), 437, 98, "hsbtns3.def", SDLK_LEFT);
  222. rightArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, +1), 516, 98, "hsbtns5.def", SDLK_RIGHT);
  223. }
  224. else
  225. creatureArtifact = NULL;
  226. }
  227. if (battleStack) //only during battle
  228. {
  229. //spell effects
  230. int printed=0; //how many effect pics have been printed
  231. std::vector<si32> spells = battleStack->activeSpells();
  232. BOOST_FOREACH(si32 effect, spells)
  233. {
  234. std::string spellText;
  235. if (effect < graphics->spellEffectsPics->ourImages.size()) //not all effects have graphics (for eg. Acid Breath)
  236. {
  237. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  238. boost::replace_first (spellText, "%s", CGI->spellh->spells[effect]->name);
  239. int duration = battleStack->getBonus(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  240. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  241. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 20 + 52 * printed, 184, *bitmap);
  242. spellEffects.push_back(new LRClickableAreaWText(SRect(20 + 52 * printed, 184, 50, 38), spellText, spellText));
  243. if (++printed >= 10) //we can fit only 10 effects
  244. break;
  245. }
  246. }
  247. //print current health
  248. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  249. }
  250. if (bonusItems.size() > (bonusRows << 1)) //only after graphics are created
  251. {
  252. slider = new CSlider(528, 231, bonusRows*60, boost::bind (&CCreatureWindow::sliderMoved, this, _1),
  253. bonusRows, (bonusItems.size() + 1) >> 1, 0, false, 0);
  254. }
  255. else //slider automatically places bonus Items
  256. recreateSkillList (0);
  257. showAll(screen2);
  258. //AUIDAT.DEF
  259. }
  260. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  261. {
  262. printAt(text, 162, 48 + nr*19, FONT_SMALL, zwykly, *bitmap);
  263. std::string hlp;
  264. if(range && baseVal != val)
  265. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  266. else if(baseVal != val && val>=0)
  267. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  268. else
  269. hlp = boost::lexical_cast<std::string>(baseVal);
  270. printTo(hlp, 325, 64 + nr*19, FONT_SMALL, zwykly, *bitmap);
  271. }
  272. void CCreatureWindow::recreateSkillList(int Pos)
  273. {
  274. int n = 0, i = 0, j = 0;
  275. int numSkills = std::min ((bonusRows + Pos) << 1, (int)bonusItems.size());
  276. std::string gfxName;
  277. for (n = 0; n < Pos << 1; ++n)
  278. {
  279. bonusItems[n]->visible = false;
  280. }
  281. for (n = Pos << 1; n < numSkills; ++n)
  282. {
  283. int offsetx = 257*j - (bonusRows == 4 ? 1 : 0);
  284. int offsety = 60*i + (bonusRows > 1 ? 1 : 0); //lack of precision :/
  285. bonusItems[n]->moveTo (SPoint(pos.x + offsetx + 10, pos.y + offsety + 230), true);
  286. bonusItems[n]->visible = true;
  287. if (++j > 1) //next line
  288. {
  289. ++i;
  290. j = 0;
  291. }
  292. }
  293. for (n = numSkills; n < bonusItems.size(); ++n)
  294. {
  295. bonusItems[n]->visible = false;
  296. }
  297. }
  298. void CCreatureWindow::showAll(SDL_Surface * to)
  299. {
  300. CIntObject::showAll(to);
  301. printAtMiddle(c->namePl, 180, 30, FONT_SMALL, tytulowy,*bitmap); //creature name
  302. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->Attack());
  303. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->Defense());
  304. if(stackNode->valOfBonuses(Bonus::SHOTS) && stackNode->hasBonusOfType(Bonus::SHOOTER))
  305. {//only for shooting units - important with wog exp shooters
  306. if (type == BATTLE)
  307. printLine(2, CGI->generaltexth->allTexts[198], dynamic_cast<const CStack*>(stackNode)->shots);
  308. else
  309. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  310. }
  311. //TODO
  312. int dmgMultiply = 1;
  313. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  314. dmgMultiply += heroOwner->Attack();
  315. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  316. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  317. printLine(6, CGI->generaltexth->zelp[441].first, c->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  318. BOOST_FOREACH(CBonusItem* b, bonusItems)
  319. b->showAll (to);
  320. }
  321. void CCreatureWindow::show(SDL_Surface * to)
  322. {
  323. if (count.size()) //army stack
  324. printTo(count, pos.x + 114, pos.y + 174,FONT_TIMES, zwykly, to);
  325. }
  326. void CCreatureWindow::sliderMoved(int newpos)
  327. {
  328. recreateSkillList(newpos); //move components
  329. redraw();
  330. }
  331. void CCreatureWindow::scrollArt(int dir)
  332. {
  333. }
  334. void CCreatureWindow::clickRight(tribool down, bool previousState)
  335. {
  336. if(down)
  337. return;
  338. if (type < 3)
  339. close();
  340. }
  341. //void CCreatureWindow::activate()
  342. //{
  343. // CIntObject::activate();
  344. // if(type < 3)
  345. // activateRClick();
  346. //}
  347. //void CCreatureWindow::deactivate()
  348. //{
  349. //
  350. //}
  351. void CCreatureWindow::close()
  352. {
  353. GH.popIntTotally(this);
  354. }
  355. CCreatureWindow::~CCreatureWindow()
  356. {
  357. for (int i=0; i<upgResCost.size(); ++i)
  358. delete upgResCost[i];
  359. bonusItems.clear();
  360. }
  361. CBonusItem::CBonusItem()
  362. {
  363. }
  364. CBonusItem::CBonusItem(const SRect &Pos, const std::string &Name, const std::string &Description, const std::string &graphicsName)
  365. {
  366. OBJ_CONSTRUCTION;
  367. visible = false;
  368. name = Name;
  369. description = Description;
  370. if (graphicsName.size())
  371. bonusGraphics = new CPicture(graphicsName, 26, 232);
  372. else
  373. bonusGraphics = NULL;
  374. used = 0; //no actions atm
  375. }
  376. void CBonusItem::showAll (SDL_Surface * to)
  377. {
  378. if (visible)
  379. {
  380. printAt(name, pos.x + 72, pos.y + 6, FONT_SMALL, tytulowy, to);
  381. printAt(description, pos.x + 72, pos.y + 30, FONT_SMALL, zwykly, to);
  382. if (bonusGraphics && bonusGraphics->bg)
  383. blitAtLoc(bonusGraphics->bg, 12, 2, to);
  384. }
  385. }
  386. CBonusItem::~CBonusItem()
  387. {
  388. //delete bonusGraphics; //automatic destruction
  389. }