CCreatureWindow.cpp 31 KB

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