CCreatureWindow.cpp 27 KB

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