CCreatureWindow.cpp 32 KB

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