Reward.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "../mapObjects/CGHeroInstance.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. Rewardable::Reward::Reward()
  15. : heroExperience(0)
  16. , heroLevel(0)
  17. , manaDiff(0)
  18. , manaPercentage(-1)
  19. , movePoints(0)
  20. , movePercentage(-1)
  21. , primary(4, 0)
  22. , removeObject(false)
  23. , spellCast(SpellID::NONE, SecSkillLevel::NONE)
  24. {
  25. }
  26. Rewardable::Reward::~Reward() = default;
  27. si32 Rewardable::Reward::calculateManaPoints(const CGHeroInstance * hero) const
  28. {
  29. si32 manaScaled = hero->mana;
  30. if (manaPercentage >= 0)
  31. manaScaled = hero->manaLimit() * manaPercentage / 100;
  32. si32 manaMissing = std::max(0, hero->manaLimit() - manaScaled);
  33. si32 manaGranted = std::min(manaMissing, manaDiff);
  34. si32 manaOverflow = manaDiff - manaGranted;
  35. si32 manaOverLimit = manaOverflow * manaOverflowFactor / 100;
  36. si32 manaOutput = manaScaled + manaGranted + manaOverLimit;
  37. return manaOutput;
  38. }
  39. Component Rewardable::Reward::getDisplayedComponent(const CGHeroInstance * h) const
  40. {
  41. std::vector<Component> comps;
  42. loadComponents(comps, h);
  43. assert(!comps.empty());
  44. return comps.front();
  45. }
  46. void Rewardable::Reward::loadComponents(std::vector<Component> & comps,
  47. const CGHeroInstance * h) const
  48. {
  49. for (auto comp : extraComponents)
  50. comps.push_back(comp);
  51. if (heroExperience)
  52. {
  53. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  54. }
  55. if (heroLevel)
  56. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  57. if (manaDiff || manaPercentage >= 0)
  58. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, calculateManaPoints(h) - h->mana, 0);
  59. for (size_t i=0; i<primary.size(); i++)
  60. {
  61. if (primary[i] != 0)
  62. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  63. }
  64. for(const auto & entry : secondary)
  65. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  66. for(const auto & entry : artifacts)
  67. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  68. for(const auto & entry : spells)
  69. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  70. for(const auto & entry : creatures)
  71. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  72. for (size_t i=0; i<resources.size(); i++)
  73. {
  74. if (resources[i] !=0)
  75. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  76. }
  77. }
  78. VCMI_LIB_NAMESPACE_END