CCreatureWindow.cpp 14 KB

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