CCreatureWindow.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. using namespace CSDL_Ext;
  23. /*
  24. * CCreatureWindow.cpp, part of VCMI engine
  25. *
  26. * Authors: listed in file AUTHORS in main folder
  27. *
  28. * License: GNU General Public License v2.0 or later
  29. * Full text of license available in license.txt file, in main folder
  30. *
  31. */
  32. CCreatureWindow::CCreatureWindow (const CStack &stack, int Type)
  33. : type(Type)
  34. {
  35. OBJ_CONSTRUCTION_CAPTURING_ALL;
  36. init(stack.base, &stack, dynamic_cast<const CGHeroInstance*>(stack.base->armyObj));
  37. }
  38. CCreatureWindow::CCreatureWindow (const CStackInstance &stack, int Type)
  39. : type(Type)
  40. {
  41. OBJ_CONSTRUCTION_CAPTURING_ALL;
  42. init(&stack, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj));
  43. }
  44. CCreatureWindow::CCreatureWindow(int Cid, int Type, int creatureCount)
  45. :type(Type)
  46. {
  47. OBJ_CONSTRUCTION_CAPTURING_ALL;
  48. CStackInstance * stack = new CStackInstance(Type,creatureCount); //TODO: simplify?
  49. init(stack, CGI->creh->creatures[Cid], NULL);
  50. delete stack;
  51. }
  52. void CCreatureWindow::init(const CStackInstance *stack, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner)
  53. {
  54. c = stack->type;
  55. if(!stackNode) stackNode = c;
  56. //Basic graphics - need to calculate size
  57. CBonusSystemNode node = CBonusSystemNode() ;
  58. node.bonuses = stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT));
  59. BonusList bl;
  60. while (node.bonuses.size())
  61. {
  62. Bonus * b = node.bonuses.front();
  63. bl.push_back (new Bonus(*b));
  64. bl.back()->val = node.valOfBonuses(Selector::typeSybtype(b->type, b->subtype)); //merge multiple bonuses into one
  65. node.bonuses.remove_if (Selector::typeSybtype(b->type, b->subtype)); //remove used bonuses
  66. }
  67. std::vector<std::pair<std::string, std::string>> descriptions; //quick, yet slow solution
  68. std::string text, text2;
  69. BOOST_FOREACH(Bonus* b, bl)
  70. {
  71. text = stack->bonusToString(b, false);
  72. if (text.size())
  73. {
  74. text2 = stack->bonusToString(b, true);
  75. descriptions.push_back(std::pair<std::string,std::string>(text, text2));
  76. }
  77. }
  78. int bonusRows = std::min ((int)((descriptions.size() + 1) / 2), (conf.cc.resy - 230) / 60);
  79. amin(bonusRows, 4);
  80. amax(bonusRows, 1);
  81. //TODO: Scroll them
  82. bitmap = new CPicture("CreWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx"); //1 to 4 rows for now
  83. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  84. pos = bitmap->center();
  85. //Buttons
  86. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CCreatureWindow::close,this), 489, 148, "hsbtns.def", SDLK_RETURN);
  87. if (type <= BATTLE) //in battle or info window
  88. {
  89. upgrade = NULL;
  90. dismiss = NULL;
  91. }
  92. anim = new CCreaturePic(22, 48, c);
  93. count = boost::lexical_cast<std::string>(stack->count);
  94. if (count.size()) //TODO
  95. printTo(count, 117, 174, FONT_SMALL, tytulowy,*bitmap);
  96. printAtMiddle(c->namePl, 180, 30, FONT_SMALL, tytulowy,*bitmap); //creature name
  97. //Stats
  98. morale = new MoraleLuckBox(true, genRect(42, 42, 335, 100));
  99. morale->set(stack);
  100. luck = new MoraleLuckBox(false, genRect(42, 42, 387, 100));
  101. luck->set(stack);
  102. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  103. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  104. if(c->shots)
  105. printLine(2, CGI->generaltexth->allTexts[198], c->shots);
  106. //TODO
  107. int dmgMultiply = 1;
  108. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  109. dmgMultiply += heroOwner->Attack();
  110. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  111. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  112. printLine(6, CGI->generaltexth->zelp[441].first, c->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  113. if (stack)
  114. {
  115. if (STACK_EXP)
  116. {
  117. printAtMiddle("Rank " + boost::lexical_cast<std::string>(stack->getExpRank()), 436, 62, FONT_MEDIUM, tytulowy,*bitmap);
  118. printAtMiddle(boost::lexical_cast<std::string>(stack->experience), 436, 82, FONT_SMALL, zwykly,*bitmap);
  119. }
  120. if (STACK_ARTIFACT && type > BATTLE)
  121. {
  122. //SDL_Rect rect = genRect(44,44,465,98);
  123. //creatureArtifact = new CArtPlace(NULL);
  124. //creatureArtifact->pos = rect;
  125. //creatureArtifact->ourOwner = NULL; //hmm?
  126. leftArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, -1), 437, 98, "hsbtns3.def", SDLK_LEFT);
  127. rightArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, +1), 516, 98, "hsbtns5.def", SDLK_RIGHT);
  128. }
  129. else
  130. creatureArtifact = NULL;
  131. }
  132. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode)) //only during battle
  133. {
  134. //spell effects
  135. int printed=0; //how many effect pics have been printed
  136. std::vector<si32> spells = battleStack->activeSpells();
  137. BOOST_FOREACH(si32 effect, spells)
  138. {
  139. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 20 + 52 * printed, 184, *bitmap);
  140. if (++printed >= 10) //we can fit only 10 effects
  141. break;
  142. }
  143. //print current health
  144. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  145. }
  146. //All bonuses - abilities
  147. int i = 0, j = 0;
  148. typedef std::pair<std::string, std::string> stringpair; //jeez
  149. BOOST_FOREACH(stringpair p, descriptions)
  150. {
  151. int offsetx = 257*j;
  152. int offsety = 60*i;
  153. printAt(p.first, 84 + offsetx, 238 + offsety, FONT_SMALL, tytulowy, *bitmap);
  154. printAt(p.second, 84 + offsetx, 258 + offsety, FONT_SMALL, zwykly, *bitmap);
  155. if (++j > 1) //next line
  156. {
  157. ++i;
  158. j = 0;
  159. }
  160. //text = stack->bonusToString(*it, true);
  161. //if (text.size())
  162. // printAt(text, 80 + offsety, 262 + offsetx, FONT_SMALL, zwykly, *bitmap);
  163. }
  164. //AUIDAT.DEF
  165. }
  166. CCreatureWindow::CCreatureWindow(const CStackInstance &st, int Type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  167. : type(Type), dsm(Dsm), dismiss(0), upgrade(0), ok(0)
  168. {
  169. OBJ_CONSTRUCTION_CAPTURING_ALL;
  170. init(&st, &st,dynamic_cast<const CGHeroInstance*>(st.armyObj));
  171. //print abilities text - if r-click popup
  172. if(type)
  173. {
  174. if(Upg && ui)
  175. {
  176. bool enough = true;
  177. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  178. {
  179. BLOCK_CAPTURING;
  180. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*st.count)
  181. enough = false;
  182. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*st.count));
  183. }
  184. if(enough)
  185. {
  186. CFunctionList<void()> fs;
  187. fs += Upg;
  188. fs += boost::bind(&CCreatureWindow::close,this);
  189. CFunctionList<void()> cfl;
  190. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], boost::ref(upgResCost), fs, 0, false);
  191. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,385, 148,"IVIEWCR.DEF",SDLK_u);
  192. }
  193. else
  194. {
  195. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),385, 148,"IVIEWCR.DEF");
  196. upgrade->callback.funcs.clear();
  197. upgrade->setOffset(2);
  198. }
  199. }
  200. if(Dsm)
  201. {
  202. CFunctionList<void()> fs[2];
  203. //on dismiss confirmed
  204. fs[0] += Dsm; //dismiss
  205. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  206. CFunctionList<void()> cfl;
  207. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  208. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,333, 148,"IVIEWCR2.DEF",SDLK_d);
  209. }
  210. }
  211. }
  212. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  213. {
  214. printAt(text, 162, 48 + nr*19, FONT_SMALL, zwykly, *bitmap);
  215. std::string hlp;
  216. if(range && baseVal != val)
  217. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  218. else if(baseVal != val && val>=0)
  219. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  220. else
  221. hlp = boost::lexical_cast<std::string>(baseVal);
  222. printTo(hlp, 325, 64 + nr*19, FONT_SMALL, zwykly, *bitmap);
  223. }
  224. //void CCreatureWindow::activate()
  225. //{
  226. // CIntObject::activate();
  227. // if(type < 3)
  228. // activateRClick();
  229. //}
  230. void CCreatureWindow::scrollArt(int dir)
  231. {
  232. }
  233. void CCreatureWindow::clickRight(tribool down, bool previousState)
  234. {
  235. if(down)
  236. return;
  237. if (type < 3)
  238. close();
  239. }
  240. void CCreatureWindow::close()
  241. {
  242. GH.popIntTotally(this);
  243. }
  244. CCreatureWindow::~CCreatureWindow()
  245. {
  246. for(int i=0; i<upgResCost.size();i++)
  247. delete upgResCost[i];
  248. }