Reward.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include "../serializer/JsonSerializeFormat.h"
  14. #include "../constants/StringConstants.h"
  15. #include "../CSkillHandler.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. Rewardable::Reward::Reward()
  18. : heroExperience(0)
  19. , heroLevel(0)
  20. , manaDiff(0)
  21. , manaPercentage(-1)
  22. , movePoints(0)
  23. , movePercentage(-1)
  24. , primary(4, 0)
  25. , removeObject(false)
  26. , spellCast(SpellID::NONE, SecSkillLevel::NONE)
  27. {
  28. }
  29. Rewardable::Reward::~Reward() = default;
  30. si32 Rewardable::Reward::calculateManaPoints(const CGHeroInstance * hero) const
  31. {
  32. si32 manaScaled = hero->mana;
  33. if (manaPercentage >= 0)
  34. manaScaled = hero->manaLimit() * manaPercentage / 100;
  35. si32 manaMissing = std::max(0, hero->manaLimit() - manaScaled);
  36. si32 manaGranted = std::min(manaMissing, manaDiff);
  37. si32 manaOverflow = manaDiff - manaGranted;
  38. si32 manaOverLimit = manaOverflow * manaOverflowFactor / 100;
  39. si32 manaOutput = manaScaled + manaGranted + manaOverLimit;
  40. return manaOutput;
  41. }
  42. Component Rewardable::Reward::getDisplayedComponent(const CGHeroInstance * h) const
  43. {
  44. std::vector<Component> comps;
  45. loadComponents(comps, h);
  46. assert(!comps.empty());
  47. return comps.front();
  48. }
  49. void Rewardable::Reward::loadComponents(std::vector<Component> & comps,
  50. const CGHeroInstance * h) const
  51. {
  52. for (auto comp : extraComponents)
  53. comps.push_back(comp);
  54. for (auto & bonus : bonuses)
  55. {
  56. if (bonus.type == BonusType::MORALE)
  57. comps.emplace_back(Component::EComponentType::MORALE, 0, bonus.val, 0);
  58. if (bonus.type == BonusType::LUCK)
  59. comps.emplace_back(Component::EComponentType::LUCK, 0, bonus.val, 0);
  60. }
  61. if (heroExperience)
  62. {
  63. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  64. }
  65. if (heroLevel)
  66. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  67. if (manaDiff || manaPercentage >= 0)
  68. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, calculateManaPoints(h) - h->mana, 0);
  69. for (size_t i=0; i<primary.size(); i++)
  70. {
  71. if (primary[i] != 0)
  72. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  73. }
  74. for(const auto & entry : secondary)
  75. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  76. for(const auto & entry : artifacts)
  77. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  78. for(const auto & entry : spells)
  79. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  80. for(const auto & entry : creatures)
  81. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  82. for (size_t i=0; i<resources.size(); i++)
  83. {
  84. if (resources[i] !=0)
  85. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  86. }
  87. }
  88. void Rewardable::Reward::serializeJson(JsonSerializeFormat & handler)
  89. {
  90. resources.serializeJson(handler, "resources");
  91. handler.serializeBool("removeObject", removeObject);
  92. handler.serializeInt("manaPercentage", manaPercentage);
  93. handler.serializeInt("movePercentage", movePercentage);
  94. handler.serializeInt("heroExperience", heroExperience);
  95. handler.serializeInt("heroLevel", heroLevel);
  96. handler.serializeInt("manaDiff", manaDiff);
  97. handler.serializeInt("manaOverflowFactor", manaOverflowFactor);
  98. handler.serializeInt("movePoints", movePoints);
  99. handler.serializeIdArray("artifacts", artifacts);
  100. handler.serializeIdArray("spells", spells);
  101. handler.enterArray("creatures").serializeStruct(creatures);
  102. handler.enterArray("primary").serializeArray(primary);
  103. {
  104. auto a = handler.enterArray("secondary");
  105. std::vector<std::pair<SecondarySkill, si32>> fieldValue(secondary.begin(), secondary.end());
  106. a.serializeStruct<std::pair<SecondarySkill, si32>>(fieldValue, [](JsonSerializeFormat & h, std::pair<SecondarySkill, si32> & e)
  107. {
  108. h.serializeId("skill", e.first, SecondarySkill{}, VLC->skillh->decodeSkill, VLC->skillh->encodeSkill);
  109. h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);});
  110. });
  111. a.syncSize(fieldValue);
  112. secondary = std::map<SecondarySkill, si32>(fieldValue.begin(), fieldValue.end());
  113. }
  114. {
  115. auto a = handler.enterArray("creaturesChange");
  116. std::vector<std::pair<CreatureID, CreatureID>> fieldValue(creaturesChange.begin(), creaturesChange.end());
  117. a.serializeStruct<std::pair<CreatureID, CreatureID>>(fieldValue, [](JsonSerializeFormat & h, std::pair<CreatureID, CreatureID> & e)
  118. {
  119. h.serializeId("creature", e.first, CreatureID{});
  120. h.serializeId("amount", e.second, CreatureID{});
  121. });
  122. creaturesChange = std::map<CreatureID, CreatureID>(fieldValue.begin(), fieldValue.end());
  123. }
  124. {
  125. auto a = handler.enterStruct("spellCast");
  126. a->serializeId("spell", spellCast.first, SpellID{});
  127. a->serializeInt("level", spellCast.second);
  128. }
  129. }
  130. VCMI_LIB_NAMESPACE_END