CCreatureWindow.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 "UIFramework/SDL_Extensions.h"
  10. #include "CBitmapHandler.h"
  11. #include "CDefHandler.h"
  12. #include "Graphics.h"
  13. #include "CPlayerInterface.h"
  14. #include "CConfigHandler.h"
  15. #include "CAnimation.h"
  16. #include "../lib/CGameState.h"
  17. #include "../lib/BattleState.h"
  18. #include "../lib/CSpellHandler.h"
  19. #include "../lib/CArtHandler.h"
  20. #include "../lib/NetPacks.h" //ArtifactLocation
  21. #include "UIFramework/CGuiHandler.h"
  22. #include "UIFramework/CIntObjectClasses.h"
  23. using namespace CSDL_Ext;
  24. class CBonusItem;
  25. class CCreatureArtifactInstance;
  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, 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. TResources upgradeCost = ui->cost[0] * st.count;
  73. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  74. {
  75. BLOCK_CAPTURING;
  76. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  77. }
  78. if(LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost))
  79. {
  80. CFunctionList<void()> fs;
  81. fs += Upg;
  82. fs += boost::bind(&CCreatureWindow::close,this);
  83. CFunctionList<void()> cfl;
  84. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], fs, 0, false, boost::ref(upgResCost));
  85. upgrade = new CAdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,385, 148,"IVIEWCR.DEF",SDLK_u);
  86. }
  87. else
  88. {
  89. upgrade = new CAdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),385, 148,"IVIEWCR.DEF");
  90. upgrade->callback.funcs.clear();
  91. upgrade->setOffset(2);
  92. }
  93. }
  94. if(Dsm)
  95. {
  96. CFunctionList<void()> fs[2];
  97. //on dismiss confirmed
  98. fs[0] += Dsm; //dismiss
  99. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  100. CFunctionList<void()> cfl;
  101. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],fs[0],fs[1],false,std::vector<CComponent*>());
  102. dismiss = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,333, 148,"IVIEWCR2.DEF",SDLK_d);
  103. }
  104. }
  105. }
  106. CCreatureWindow::CCreatureWindow (const CCommanderInstance * Commander)
  107. :type (COMMANDER), commander (Commander)
  108. {
  109. OBJ_CONSTRUCTION_CAPTURING_ALL;
  110. init(commander, commander, dynamic_cast<const CGHeroInstance*>(commander->armyObj));
  111. boost::function<void()> Dsm;
  112. CFunctionList<void()> fs[2];
  113. //on dismiss confirmed
  114. fs[0] += Dsm; //dismiss
  115. fs[0] += boost::bind(&CCreatureWindow::close,this);//close this window
  116. CFunctionList<void()> cfl;
  117. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],fs[0],fs[1],false,std::vector<CComponent*>());
  118. dismiss = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, cfl, 333, 148,"IVIEWCR2.DEF", SDLK_d);
  119. }
  120. void CCreatureWindow::init(const CStackInstance *Stack, const CBonusSystemNode *StackNode, const CGHeroInstance *HeroOwner)
  121. {
  122. creatureArtifact = NULL; //may be set later
  123. artifactImage = NULL;
  124. stack = Stack;
  125. c = stack->type;
  126. if(!StackNode)
  127. stackNode = c;
  128. else
  129. stackNode = StackNode;
  130. const CStack *battleStack = dynamic_cast<const CStack*>(stackNode); //only during battle
  131. heroOwner = HeroOwner;
  132. if (battleStack)
  133. count = boost::lexical_cast<std::string>(battleStack->count);
  134. else if (Stack->count)
  135. count = boost::lexical_cast<std::string>(Stack->count);
  136. if (type < COMMANDER)
  137. commander = NULL;
  138. bool creArt = false;
  139. displayedArtifact = ArtifactPosition::CREATURE_SLOT; // 0
  140. //Basic graphics - need to calculate size
  141. int commanderOffset = 0;
  142. if (type >= COMMANDER)
  143. commanderOffset = 74;
  144. BonusList bl, blTemp;
  145. blTemp = (*(stackNode->getBonuses(Selector::durationType(Bonus::PERMANENT))));
  146. while (blTemp.size())
  147. {
  148. Bonus * b = blTemp.front();
  149. bl.push_back (new Bonus(*b));
  150. bl.back()->val = blTemp.valOfBonuses(Selector::typeSubtype(b->type, b->subtype)); //merge multiple bonuses into one
  151. blTemp.remove_if (Selector::typeSubtype(b->type, b->subtype)); //remove used bonuses
  152. }
  153. std::string text;
  154. BOOST_FOREACH(Bonus* b, bl)
  155. {
  156. text = stack->bonusToString(b, false);
  157. if (text.size()) //if it's possible to give any description for this kind of bonus
  158. {
  159. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, stack->bonusToString(b, true), stack->bonusToGraphics(b)));
  160. }
  161. }
  162. int magicResistance = 0; //handle it separately :/
  163. if (battleStack)
  164. {
  165. magicResistance = battleStack->magicResistance(); //include Aura of Resistance
  166. }
  167. else
  168. {
  169. magicResistance = stack->magicResistance(); //include Resiatance hero skill
  170. }
  171. if (magicResistance)
  172. {
  173. std::map<TBonusType, std::pair<std::string, std::string> >::const_iterator it = CGI->creh->stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  174. std::string description;
  175. text = it->second.first;
  176. description = it->second.second;
  177. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(magicResistance));
  178. Bonus b;
  179. b.type = Bonus::MAGIC_RESISTANCE;
  180. bonusItems.push_back (new CBonusItem(genRect(0, 0, 251, 57), text, description, stack->bonusToGraphics(&b)));
  181. }
  182. bonusRows = std::min ((int)((bonusItems.size() + 1) / 2), (screen->h - 230) / 60);
  183. if (type >= COMMANDER)
  184. vstd::amin(bonusRows, 3);
  185. else
  186. vstd::amin(bonusRows, 4);
  187. vstd::amax(bonusRows, 1);
  188. if (type >= COMMANDER)
  189. bitmap = new CPicture("CommWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx");
  190. else
  191. bitmap = new CPicture("CreWin" + boost::lexical_cast<std::string>(bonusRows) + ".pcx"); //1 to 4 rows for now
  192. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  193. pos = bitmap->center();
  194. //Buttons
  195. ok = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CCreatureWindow::close,this), 489, 148, "hsbtns.def", SDLK_RETURN);
  196. ok->assignedKeys.insert(SDLK_ESCAPE);
  197. if (type <= BATTLE) //in battle or info window
  198. {
  199. upgrade = NULL;
  200. dismiss = NULL;
  201. }
  202. anim = new CCreaturePic(22, 48, c);
  203. //Stats
  204. morale = new MoraleLuckBox(true, genRect(42, 42, 335, 100));
  205. morale->set(stack);
  206. luck = new MoraleLuckBox(false, genRect(42, 42, 387, 100));
  207. luck->set(stack);
  208. new CAnimImage("PSKIL42", 4, 0, 387, 51); //exp icon - Print it always?
  209. if (type) //not in fort window
  210. {
  211. if (GameConstants::STACK_EXP && type < COMMANDER)
  212. {
  213. int rank = std::min(stack->getExpRank(), 10); //hopefully nobody adds more
  214. printAtMiddle(boost::lexical_cast<std::string>(stack->experience), 488, 82, FONT_SMALL, Colors::Cornsilk,*bitmap);
  215. printAtMiddle(CGI->generaltexth->zcrexp[rank] + " [" + boost::lexical_cast<std::string>(rank) + "]", 488, 62, FONT_MEDIUM, Colors::Jasmine,*bitmap);
  216. if (type > BATTLE) //we need it only on adv. map
  217. {
  218. int tier = stack->type->level;
  219. if (!vstd::iswithin(tier, 1, 7))
  220. tier = 0;
  221. int number;
  222. std::string expText = CGI->generaltexth->zcrexp[324];
  223. boost::replace_first (expText, "%s", c->namePl);
  224. boost::replace_first (expText, "%s", CGI->generaltexth->zcrexp[rank]);
  225. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(rank));
  226. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->experience));
  227. number = CGI->creh->expRanks[tier][rank] - stack->experience;
  228. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  229. number = CGI->creh->maxExpPerBattle[tier]; //percent
  230. boost::replace_first (expText, "%i%", boost::lexical_cast<std::string>(number));
  231. number *= CGI->creh->expRanks[tier].back() / 100; //actual amount
  232. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number));
  233. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(stack->count)); //Number of Creatures in stack
  234. int expmin = std::max(CGI->creh->expRanks[tier][std::max(rank-1, 0)], (ui32)1);
  235. number = (stack->count * (stack->experience - expmin)) / expmin; //Maximum New Recruits without losing current Rank
  236. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //TODO
  237. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(1)); //TODO Experience Multiplier
  238. number = CGI->creh->expAfterUpgrade;
  239. boost::replace_first (expText, "%.2f", boost::lexical_cast<std::string>(number) + "%"); //Upgrade Multiplier
  240. expmin = CGI->creh->expRanks[tier][9];
  241. int expmax = CGI->creh->expRanks[tier][10];
  242. number = expmax - expmin;
  243. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Experience after Rank 10
  244. number = (stack->count * (expmax - expmin)) / expmin;
  245. boost::replace_first (expText, "%i", boost::lexical_cast<std::string>(number)); //Maximum New Recruits to remain at Rank 10 if at Maximum Experience
  246. expArea = new LRClickableAreaWTextComp(Rect(334, 49, 160, 44),CComponent::experience);
  247. expArea->text = expText;
  248. expArea->bonusValue = 0; //TDO: some specific value or no number at all
  249. }
  250. }
  251. if (GameConstants::STACK_ARTIFACT)
  252. {
  253. creArt = true;
  254. }
  255. }
  256. if (commander) //secondary skills
  257. {
  258. creArt = true;
  259. for (int i = ECommander::ATTACK; i <= ECommander::SPELL_POWER; ++i)
  260. {
  261. if (commander->secondarySkills[i])
  262. {
  263. std::string file = "zvs/Lib1.res/_";
  264. switch (i)
  265. {
  266. case ECommander::ATTACK:
  267. file += "AT";
  268. break;
  269. case ECommander::DEFENSE:
  270. file += "DF";
  271. break;
  272. case ECommander::HEALTH:
  273. file += "HP";
  274. break;
  275. case ECommander::DAMAGE:
  276. file += "DM";
  277. break;
  278. case ECommander::SPEED:
  279. file += "SP";
  280. break;
  281. case ECommander::SPELL_POWER:
  282. file += "MP";
  283. break;
  284. }
  285. std::string sufix = boost::lexical_cast<std::string>((int)(commander->secondarySkills[i] - 1)); //casting ui8 causes ascii char conversion
  286. if (type == COMMANDER_LEVEL_UP)
  287. {
  288. if (commander->secondarySkills[i] < ECommander::MAX_SKILL_LEVEL)
  289. sufix += "="; //level-up highlight
  290. else
  291. sufix = "no"; //not avaliable - no number
  292. }
  293. file += sufix += ".bmp";
  294. auto skillGraphics = new CPicture(file, 37 + i * 84, 224);
  295. }
  296. }
  297. //print commander level
  298. printAtMiddle(boost::lexical_cast<std::string>((ui16)(commander->level)), 488, 62, FONT_MEDIUM, Colors::Jasmine,*bitmap);
  299. printAtMiddle(boost::lexical_cast<std::string>(stack->experience), 488, 82, FONT_SMALL, Colors::Cornsilk,*bitmap);
  300. }
  301. if (creArt) //stack or commander artifacts
  302. {
  303. setArt (stack->getArt(ArtifactPosition::CREATURE_SLOT));
  304. if (type > BATTLE) //artifact buttons inactive in battle
  305. {
  306. //TODO: disable buttons if no artifact is equipped
  307. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, -1), 437, 98, "hsbtns3.def", SDLK_LEFT);
  308. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::scrollArt, this, +1), 516, 98, "hsbtns5.def", SDLK_RIGHT);
  309. if (heroOwner)
  310. passArtToHero = new CAdventureMapButton(std::string(), std::string(), boost::bind (&CCreatureWindow::passArtifactToHero, this), 437, 148, "OVBUTN1.DEF", SDLK_HOME);
  311. }
  312. }
  313. if (battleStack) //only during battle
  314. {
  315. //spell effects
  316. int printed=0; //how many effect pics have been printed
  317. std::vector<si32> spells = battleStack->activeSpells();
  318. BOOST_FOREACH(si32 effect, spells)
  319. {
  320. std::string spellText;
  321. if (effect < graphics->spellEffectsPics->ourImages.size()) //not all effects have graphics (for eg. Acid Breath)
  322. {
  323. spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds."
  324. boost::replace_first (spellText, "%s", CGI->spellh->spells[effect]->name);
  325. int duration = battleStack->getBonus(Selector::source(Bonus::SPELL_EFFECT,effect))->turnsRemain;
  326. boost::replace_first (spellText, "%d", boost::lexical_cast<std::string>(duration));
  327. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 20 + 52 * printed, 184, *bitmap);
  328. spellEffects.push_back(new LRClickableAreaWText(Rect(20 + 52 * printed, 184, 50, 38), spellText, spellText));
  329. if (++printed >= 10) //we can fit only 10 effects
  330. break;
  331. }
  332. }
  333. //print current health
  334. printLine (5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  335. }
  336. if (bonusItems.size() > (bonusRows << 1)) //only after graphics are created
  337. {
  338. slider = new CSlider(528, 231 + commanderOffset, bonusRows*60, boost::bind (&CCreatureWindow::sliderMoved, this, _1),
  339. bonusRows, (bonusItems.size() + 1) >> 1, 0, false, 0);
  340. }
  341. else //slider automatically places bonus Items
  342. recreateSkillList (0);
  343. showAll(screen2);
  344. //AUIDAT.DEF
  345. }
  346. void CCreatureWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  347. {
  348. printAt(text, 162, 48 + nr*19, FONT_SMALL, Colors::Cornsilk, *bitmap);
  349. std::string hlp;
  350. if(range && baseVal != val)
  351. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  352. else if(baseVal != val && val>=0)
  353. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  354. else
  355. hlp = boost::lexical_cast<std::string>(baseVal);
  356. printTo(hlp, 325, 64 + nr*19, FONT_SMALL, Colors::Cornsilk, *bitmap);
  357. }
  358. void CCreatureWindow::recreateSkillList(int Pos)
  359. {
  360. int commanderOffset = 0;
  361. if (type >= COMMANDER)
  362. commanderOffset = 74;
  363. int n = 0, i = 0, j = 0;
  364. int numSkills = std::min ((bonusRows + Pos) << 1, (int)bonusItems.size());
  365. std::string gfxName;
  366. for (n = 0; n < Pos << 1; ++n)
  367. {
  368. bonusItems[n]->visible = false;
  369. }
  370. for (n = Pos << 1; n < numSkills; ++n)
  371. {
  372. int offsetx = 257*j - (bonusRows == 4 ? 1 : 0);
  373. int offsety = 60*i + (bonusRows > 1 ? 1 : 0) + commanderOffset; //lack of precision :/
  374. bonusItems[n]->moveTo (Point(pos.x + offsetx + 10, pos.y + offsety + 230), true);
  375. bonusItems[n]->visible = true;
  376. if (++j > 1) //next line
  377. {
  378. ++i;
  379. j = 0;
  380. }
  381. }
  382. for (n = numSkills; n < bonusItems.size(); ++n)
  383. {
  384. bonusItems[n]->visible = false;
  385. }
  386. }
  387. void CCreatureWindow::showAll(SDL_Surface * to)
  388. {
  389. CIntObject::showAll(to);
  390. printAtMiddle ((type >= COMMANDER? c->nameSing : c->namePl), 180, 30, FONT_SMALL, Colors::Jasmine, *bitmap); //creature name
  391. printLine(0, CGI->generaltexth->primarySkillNames[0], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->Attack());
  392. printLine(1, CGI->generaltexth->primarySkillNames[1], c->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->Defense());
  393. if (stackNode->valOfBonuses(Bonus::SHOTS) && stackNode->hasBonusOfType(Bonus::SHOOTER))
  394. {//only for shooting units - important with wog exp shooters
  395. if (type == BATTLE)
  396. printLine(2, CGI->generaltexth->allTexts[198], dynamic_cast<const CStack*>(stackNode)->shots);
  397. else
  398. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  399. }
  400. if (stackNode->valOfBonuses(Bonus::CASTS))
  401. {
  402. printAtMiddle(CGI->generaltexth->allTexts[399], 356, 62, FONT_SMALL, Colors::Cornsilk,*bitmap);
  403. std::string casts;
  404. if (type == BATTLE)
  405. casts = boost::lexical_cast<std::string>((ui16)dynamic_cast<const CStack*>(stackNode)->casts); //ui8 is converted to char :(
  406. else
  407. casts = boost::lexical_cast<std::string>(stackNode->valOfBonuses(Bonus::CASTS));
  408. printAtMiddle(casts, 356, 82, FONT_SMALL, Colors::Cornsilk,*bitmap);
  409. }
  410. //TODO
  411. int dmgMultiply = 1;
  412. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  413. dmgMultiply += heroOwner->Attack();
  414. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  415. printLine(4, CGI->generaltexth->allTexts[388], c->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  416. printLine(6, CGI->generaltexth->zelp[441].first, c->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  417. BOOST_FOREACH(CBonusItem* b, bonusItems)
  418. b->showAll (to);
  419. }
  420. void CCreatureWindow::show(SDL_Surface * to)
  421. {
  422. if (count.size()) //army stack
  423. printTo(count, pos.x + 114, pos.y + 174,FONT_TIMES, Colors::Cornsilk, to);
  424. }
  425. void CCreatureWindow::sliderMoved(int newpos)
  426. {
  427. recreateSkillList(newpos); //move components
  428. redraw();
  429. }
  430. void CCreatureWindow::setArt(const CArtifactInstance *art)
  431. {
  432. creatureArtifact = art;
  433. if (creatureArtifact)
  434. {
  435. if (artifactImage == NULL)
  436. artifactImage = new CAnimImage("ARTIFACT", creatureArtifact->artType->id, 0, 466, 100);
  437. else
  438. artifactImage->setFrame(creatureArtifact->artType->id);
  439. }
  440. else
  441. artifactImage = NULL;
  442. redraw();
  443. }
  444. void CCreatureWindow::scrollArt(int dir)
  445. {
  446. //TODO: get next artifact
  447. int size = stack->artifactsWorn.size();
  448. displayedArtifact = size ? (displayedArtifact + dir) % size : ArtifactPosition::CREATURE_SLOT;
  449. setArt (stack->getArt(displayedArtifact));
  450. }
  451. void CCreatureWindow::passArtifactToHero()
  452. {
  453. const CGHeroInstance * h = dynamic_cast<const CGHeroInstance *>(stack->armyObj);
  454. if (h && creatureArtifact)
  455. {
  456. LOCPLINT->cb->swapArtifacts (ArtifactLocation (stack, displayedArtifact), ArtifactLocation(h, creatureArtifact->firstBackpackSlot(h)));
  457. }
  458. else
  459. tlog2 << "Pass artifact to hero should be disabled, no hero or no artifact!\n";
  460. //redraw is handled via CArtifactHolder interface
  461. }
  462. void CCreatureWindow::artifactRemoved (const ArtifactLocation &artLoc)
  463. {
  464. //align artifacts to remove holes
  465. BOOST_FOREACH (auto al, stack->artifactsWorn)
  466. {
  467. int freeSlot = al.second.artifact->firstAvailableSlot(stack);
  468. if (freeSlot < al.first)
  469. LOCPLINT->cb->swapArtifacts (ArtifactLocation(stack, al.first), ArtifactLocation(stack, freeSlot));
  470. }
  471. int size = stack->artifactsWorn.size();
  472. displayedArtifact = size ? (displayedArtifact % size) : ArtifactPosition::CREATURE_SLOT; //0
  473. setArt (stack->getArt(displayedArtifact));
  474. }
  475. void CCreatureWindow::artifactMoved (const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  476. {
  477. artifactRemoved (artLoc); //same code
  478. }
  479. void CCreatureWindow::clickRight(tribool down, bool previousState)
  480. {
  481. if(down)
  482. return;
  483. if (type < 3)
  484. close();
  485. }
  486. void CCreatureWindow::close()
  487. {
  488. GH.popIntTotally(this);
  489. }
  490. CCreatureWindow::~CCreatureWindow()
  491. {
  492. for (int i=0; i<upgResCost.size(); ++i)
  493. delete upgResCost[i];
  494. bonusItems.clear();
  495. }
  496. CBonusItem::CBonusItem()
  497. {
  498. }
  499. CBonusItem::CBonusItem(const Rect &Pos, const std::string &Name, const std::string &Description, const std::string &graphicsName)
  500. {
  501. OBJ_CONSTRUCTION;
  502. visible = false;
  503. name = Name;
  504. description = Description;
  505. if (graphicsName.size())
  506. bonusGraphics = new CPicture(graphicsName, 26, 232);
  507. else
  508. bonusGraphics = NULL;
  509. used = 0; //no actions atm
  510. }
  511. void CBonusItem::showAll (SDL_Surface * to)
  512. {
  513. if (visible)
  514. {
  515. printAt(name, pos.x + 72, pos.y + 6, FONT_SMALL, Colors::Jasmine, to);
  516. printAt(description, pos.x + 72, pos.y + 30, FONT_SMALL, Colors::Cornsilk, to);
  517. if (bonusGraphics && bonusGraphics->bg)
  518. blitAtLoc(bonusGraphics->bg, 12, 2, to);
  519. }
  520. }
  521. CBonusItem::~CBonusItem()
  522. {
  523. //delete bonusGraphics; //automatic destruction
  524. }
  525. void CCreInfoWindow::show(SDL_Surface * to)
  526. {
  527. CIntObject::show(to);
  528. creatureCount->showAll(to);
  529. }
  530. CCreInfoWindow::CCreInfoWindow(const CStackInstance &stack, bool LClicked, boost::function<void()> upgradeFunc, boost::function<void()> dismissFunc, UpgradeInfo *upgradeInfo)
  531. {
  532. OBJ_CONSTRUCTION_CAPTURING_ALL;
  533. init(stack.type, &stack, dynamic_cast<const CGHeroInstance*>(stack.armyObj), stack.count, LClicked);
  534. //additional buttons if opened with left click
  535. if(LClicked)
  536. {
  537. boost::function<void()> closeFunc = boost::bind(&CCreInfoWindow::close,this);
  538. if(upgradeFunc && upgradeInfo)
  539. {
  540. TResources upgradeCost = upgradeInfo->cost[0] * stack.count;
  541. for(TResources::nziterator i(upgradeCost); i.valid(); i++)
  542. {
  543. BLOCK_CAPTURING;
  544. upgResCost.push_back(new CComponent(CComponent::resource, i->resType, i->resVal));
  545. }
  546. CFunctionList<void()> onUpgrade;
  547. onUpgrade += upgradeFunc;
  548. onUpgrade += closeFunc;
  549. boost::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  550. LOCPLINT,
  551. CGI->generaltexth->allTexts[207],
  552. onUpgrade, 0, false,
  553. boost::ref(upgResCost));
  554. upgrade = new CAdventureMapButton("", CGI->generaltexth->zelp[446].second, dialog, 76, 237, "IVIEWCR", SDLK_u);
  555. upgrade->block(!LOCPLINT->cb->getResourceAmount().canAfford(upgradeCost));
  556. }
  557. if(dismissFunc)
  558. {
  559. CFunctionList<void()> onDismiss;
  560. onDismiss += dismissFunc;
  561. onDismiss += closeFunc;
  562. boost::function<void()> dialog = boost::bind(&CPlayerInterface::showYesNoDialog,
  563. LOCPLINT,
  564. CGI->generaltexth->allTexts[12],
  565. onDismiss, 0, true, std::vector<CComponent*>());
  566. dismiss = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second, dialog, 21, 237, "IVIEWCR2",SDLK_d);
  567. }
  568. }
  569. }
  570. CCreInfoWindow::CCreInfoWindow(int creatureID, bool LClicked, int creatureCount)
  571. {
  572. OBJ_CONSTRUCTION_CAPTURING_ALL;
  573. const CCreature *creature = CGI->creh->creatures[creatureID];
  574. init(creature, NULL, NULL, creatureCount, LClicked);
  575. }
  576. CCreInfoWindow::CCreInfoWindow(const CStack &stack, bool LClicked)
  577. {
  578. OBJ_CONSTRUCTION_CAPTURING_ALL;
  579. init(stack.getCreature(), &stack, stack.getMyHero(), stack.count, LClicked);
  580. }
  581. CCreInfoWindow::~CCreInfoWindow()
  582. {
  583. BOOST_FOREACH(CComponent* object, upgResCost)
  584. delete object;
  585. }
  586. void CCreInfoWindow::printLine(int position, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  587. {
  588. infoTexts[position].first = new CLabel(155, 48 + position*19, FONT_SMALL, TOPLEFT, Colors::Cornsilk, text);
  589. std::string valueStr;
  590. if(range && baseVal != val)
  591. valueStr = boost::str(boost::format("%d - %d") % baseVal % val);
  592. else if(baseVal != val && val>=0)
  593. valueStr = boost::str(boost::format("%d (%d)") % baseVal % val);
  594. else
  595. valueStr = boost::lexical_cast<std::string>(baseVal);
  596. infoTexts[position].second = new CLabel(276, 63 + position*19, FONT_SMALL, BOTTOMRIGHT, Colors::Cornsilk, valueStr);
  597. }
  598. void CCreInfoWindow::init(const CCreature *creature, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner, int count, bool LClicked)
  599. {
  600. used = 0;
  601. if (!LClicked)
  602. used |= RCLICK;
  603. if(!stackNode)
  604. stackNode = creature;
  605. background = new CPicture("CRSTKPU");
  606. background->colorize(LOCPLINT->playerID);
  607. pos = background->center();
  608. animation = new CCreaturePic(21, 48, creature);
  609. std::string countStr = boost::lexical_cast<std::string>(count);
  610. creatureCount = new CLabel(114, 174, FONT_TIMES, BOTTOMRIGHT, Colors::Cornsilk, countStr);
  611. creatureName = new CLabel(149, 30, FONT_SMALL, CENTER, Colors::Jasmine, creature->namePl);
  612. printLine(0, CGI->generaltexth->primarySkillNames[0], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  613. printLine(1, CGI->generaltexth->primarySkillNames[1], creature->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  614. if(stackNode->valOfBonuses(Bonus::SHOTS))
  615. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  616. //TODO
  617. int dmgMultiply = 1;
  618. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  619. dmgMultiply += heroOwner->Attack();
  620. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  621. printLine(4, CGI->generaltexth->allTexts[388], creature->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  622. printLine(6, CGI->generaltexth->zelp[441].first, creature->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  623. //setting morale
  624. morale = new MoraleLuckBox(true, genRect(42, 42, 22, 186));
  625. morale->set(stackNode);
  626. //setting luck
  627. luck = new MoraleLuckBox(false, genRect(42, 42, 75, 186));
  628. luck->set(stackNode);
  629. if(!LClicked)
  630. {
  631. abilityText = new CLabel(17, 231, FONT_SMALL, TOPLEFT, Colors::Cornsilk, creature->abilityText);
  632. }
  633. else
  634. {
  635. abilityText = NULL;
  636. ok = new CAdventureMapButton("", CGI->generaltexth->zelp[445].second,
  637. boost::bind(&CCreInfoWindow::close,this), 216, 237, "IOKAY.DEF", SDLK_RETURN);
  638. ok->assignedKeys.insert(SDLK_ESCAPE);
  639. }
  640. //if we are displying window fo r stack in battle, there are several more things that we need to display
  641. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  642. {
  643. //print at most 3 spell effects
  644. std::vector<si32> spells = battleStack->activeSpells();
  645. for (size_t i=0; i< std::min(spells.size(), size_t(3)); i++)
  646. effects.push_back(new CAnimImage("SpellInt", spells[i]+1, 0, 127 + 52*i, 186));
  647. //print current health
  648. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  649. }
  650. }
  651. void CCreInfoWindow::close()
  652. {
  653. GH.popIntTotally(this);
  654. }
  655. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  656. {
  657. close();
  658. }
  659. CIntObject * createCreWindow(
  660. const CStack *s, bool lclick/* = false*/)
  661. {
  662. if(settings["general"]["classicCreatureWindow"].Bool())
  663. return new CCreInfoWindow(*s, lclick);
  664. else
  665. return new CCreatureWindow(*s, CCreatureWindow::BATTLE);
  666. }
  667. CIntObject * createCreWindow(int Cid, int Type, int creatureCount)
  668. {
  669. if(settings["general"]["classicCreatureWindow"].Bool())
  670. return new CCreInfoWindow(Cid, Type, creatureCount);
  671. else
  672. return new CCreatureWindow(Cid, Type, creatureCount);
  673. }
  674. CIntObject * createCreWindow(const CStackInstance *s, int type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  675. {
  676. if(settings["general"]["classicCreatureWindow"].Bool())
  677. return new CCreInfoWindow(*s, type==3, Upg, Dsm, ui);
  678. else
  679. return new CCreatureWindow(*s, type, Upg, Dsm, ui);
  680. }