CCreatureWindow.cpp 15 KB

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