Reward.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Reward.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Reward.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. si32 Rewardable::Reward::calculateManaPoints(const CGHeroInstance * hero) const
  14. {
  15. si32 manaScaled = hero->mana;
  16. if (manaPercentage >= 0)
  17. manaScaled = hero->manaLimit() * manaPercentage / 100;
  18. si32 manaMissing = std::max(0, hero->manaLimit() - manaScaled);
  19. si32 manaGranted = std::min(manaMissing, manaDiff);
  20. si32 manaOverflow = manaDiff - manaGranted;
  21. si32 manaOverLimit = manaOverflow * manaOverflowFactor / 100;
  22. si32 manaOutput = manaScaled + manaGranted + manaOverLimit;
  23. return manaOutput;
  24. }
  25. Component Rewardable::Reward::getDisplayedComponent(const CGHeroInstance * h) const
  26. {
  27. std::vector<Component> comps;
  28. loadComponents(comps, h);
  29. assert(!comps.empty());
  30. return comps.front();
  31. }
  32. void Rewardable::Reward::loadComponents(std::vector<Component> & comps,
  33. const CGHeroInstance * h) const
  34. {
  35. for (auto comp : extraComponents)
  36. comps.push_back(comp);
  37. if (heroExperience)
  38. {
  39. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  40. }
  41. if (heroLevel)
  42. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  43. if (manaDiff || manaPercentage >= 0)
  44. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, calculateManaPoints(h) - h->mana, 0);
  45. for (size_t i=0; i<primary.size(); i++)
  46. {
  47. if (primary[i] != 0)
  48. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  49. }
  50. for(const auto & entry : secondary)
  51. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  52. for(const auto & entry : artifacts)
  53. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  54. for(const auto & entry : spells)
  55. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  56. for(const auto & entry : creatures)
  57. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  58. for (size_t i=0; i<resources.size(); i++)
  59. {
  60. if (resources[i] !=0)
  61. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  62. }
  63. }
  64. VCMI_LIB_NAMESPACE_END