CCreatureWindow.cpp 15 KB

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