Reward.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. void Rewardable::RewardRevealTiles::serializeJson(JsonSerializeFormat & handler)
  18. {
  19. handler.serializeBool("hide", hide);
  20. handler.serializeInt("scoreSurface", scoreSurface);
  21. handler.serializeInt("scoreSubterra", scoreSubterra);
  22. handler.serializeInt("scoreWater", scoreWater);
  23. handler.serializeInt("scoreRock", scoreRock);
  24. handler.serializeInt("radius", radius);
  25. }
  26. Rewardable::Reward::Reward()
  27. : heroExperience(0)
  28. , heroLevel(0)
  29. , manaDiff(0)
  30. , manaPercentage(-1)
  31. , manaOverflowFactor(0)
  32. , movePoints(0)
  33. , movePercentage(-1)
  34. , primary(4, 0)
  35. , removeObject(false)
  36. , spellCast(SpellID::NONE, MasteryLevel::NONE)
  37. {
  38. }
  39. Rewardable::Reward::~Reward() = default;
  40. si32 Rewardable::Reward::calculateManaPoints(const CGHeroInstance * hero) const
  41. {
  42. si32 manaScaled = hero->mana;
  43. if (manaPercentage >= 0)
  44. manaScaled = hero->manaLimit() * manaPercentage / 100;
  45. si32 manaMissing = std::max(0, hero->manaLimit() - manaScaled);
  46. si32 manaGranted = std::min(manaMissing, manaDiff);
  47. si32 manaOverflow = manaDiff - manaGranted;
  48. si32 manaOverLimit = manaOverflow * manaOverflowFactor / 100;
  49. si32 manaOutput = manaScaled + manaGranted + manaOverLimit;
  50. return manaOutput;
  51. }
  52. Component Rewardable::Reward::getDisplayedComponent(const CGHeroInstance * h) const
  53. {
  54. std::vector<Component> comps;
  55. loadComponents(comps, h);
  56. if (!comps.empty())
  57. return comps.front();
  58. // Rewardable requested component that represent such rewards, to be used as button in UI selection dialog, e.g. Chest with its experience / money pick
  59. // However reward is either completely empty OR has no rewards that target hero can receive OR these rewards have no visible component (e.g. movement)
  60. // Such cases are unreachable in H3, however can be reached by mods
  61. logMod->warn("Failed to find displayed component for reward!");
  62. return Component(ComponentType::NONE, 0);
  63. }
  64. void Rewardable::Reward::loadComponents(std::vector<Component> & comps, const CGHeroInstance * h) const
  65. {
  66. for (auto comp : extraComponents)
  67. comps.push_back(comp);
  68. for (auto & bonus : heroBonuses)
  69. {
  70. if (bonus.type == BonusType::MORALE)
  71. comps.emplace_back(ComponentType::MORALE, bonus.val);
  72. if (bonus.type == BonusType::LUCK)
  73. comps.emplace_back(ComponentType::LUCK, bonus.val);
  74. }
  75. if (heroExperience)
  76. comps.emplace_back(ComponentType::EXPERIENCE, static_cast<si32>(h ? h->calculateXp(heroExperience) : heroExperience));
  77. if (heroLevel)
  78. comps.emplace_back(ComponentType::LEVEL, heroLevel);
  79. if (manaDiff || manaPercentage >= 0)
  80. comps.emplace_back(ComponentType::MANA, h ? (calculateManaPoints(h) - h->mana) : manaDiff);
  81. for (size_t i=0; i<primary.size(); i++)
  82. {
  83. if (primary[i] != 0)
  84. comps.emplace_back(ComponentType::PRIM_SKILL, PrimarySkill(i), primary[i]);
  85. }
  86. for(const auto & entry : secondary)
  87. {
  88. auto skillID = entry.first;
  89. int levelsGained = entry.second;
  90. int currentLevel = h ? h->getSecSkillLevel(skillID) : 0;
  91. int finalLevel = std::min(static_cast<int>(MasteryLevel::EXPERT), currentLevel + levelsGained);
  92. comps.emplace_back(ComponentType::SEC_SKILL, entry.first, finalLevel);
  93. }
  94. for(const auto & entry : grantedArtifacts)
  95. comps.emplace_back(ComponentType::ARTIFACT, entry);
  96. for(const auto & entry : takenArtifacts)
  97. comps.emplace_back(ComponentType::ARTIFACT, entry);
  98. for(const auto & entry : takenArtifactSlots)
  99. {
  100. if (h)
  101. {
  102. const auto & slotContent = h->getSlot(entry);
  103. if (slotContent->artifactID.hasValue())
  104. comps.emplace_back(ComponentType::ARTIFACT, slotContent->getArt()->getTypeId());
  105. }
  106. }
  107. for(const SpellID & spell : grantedScrolls)
  108. comps.emplace_back(ComponentType::SPELL, spell);
  109. for(const SpellID & spell : takenScrolls)
  110. comps.emplace_back(ComponentType::SPELL, spell);
  111. for(const auto & entry : spells)
  112. {
  113. bool learnable = !h || h->canLearnSpell(entry.toEntity(LIBRARY), true);
  114. comps.emplace_back(ComponentType::SPELL, entry, learnable ? 0 : -1);
  115. }
  116. for(const auto & entry : creatures)
  117. comps.emplace_back(ComponentType::CREATURE, entry.getId(), entry.getCount());
  118. for (size_t i=0; i<resources.size(); i++)
  119. {
  120. if (resources[i] !=0)
  121. comps.emplace_back(ComponentType::RESOURCE, GameResID(i), resources[i]);
  122. }
  123. }
  124. void Rewardable::Reward::serializeJson(JsonSerializeFormat & handler)
  125. {
  126. resources.serializeJson(handler, "resources");
  127. handler.serializeBool("removeObject", removeObject);
  128. handler.serializeInt("manaPercentage", manaPercentage);
  129. handler.serializeInt("movePercentage", movePercentage);
  130. handler.serializeInt("heroExperience", heroExperience);
  131. handler.serializeInt("heroLevel", heroLevel);
  132. handler.serializeInt("manaDiff", manaDiff);
  133. handler.serializeInt("manaOverflowFactor", manaOverflowFactor);
  134. handler.serializeInt("movePoints", movePoints);
  135. handler.serializeIdArray("artifacts", grantedArtifacts);
  136. handler.serializeIdArray("takenArtifacts", takenArtifacts);
  137. handler.serializeIdArray("takenArtifactSlots", takenArtifactSlots);
  138. handler.serializeIdArray("scrolls", grantedScrolls);
  139. handler.serializeIdArray("takenScrolls", takenScrolls);
  140. handler.serializeIdArray("spells", spells);
  141. handler.enterArray("creatures").serializeStruct(creatures);
  142. handler.enterArray("primary").serializeArray(primary);
  143. {
  144. auto a = handler.enterArray("secondary");
  145. std::vector<std::pair<SecondarySkill, si32>> fieldValue(secondary.begin(), secondary.end());
  146. a.serializeStruct<std::pair<SecondarySkill, si32>>(fieldValue, [](JsonSerializeFormat & h, std::pair<SecondarySkill, si32> & e)
  147. {
  148. h.serializeId("skill", e.first);
  149. h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);});
  150. });
  151. a.syncSize(fieldValue);
  152. secondary = std::map<SecondarySkill, si32>(fieldValue.begin(), fieldValue.end());
  153. }
  154. {
  155. auto a = handler.enterArray("creaturesChange");
  156. std::vector<std::pair<CreatureID, CreatureID>> fieldValue(creaturesChange.begin(), creaturesChange.end());
  157. a.serializeStruct<std::pair<CreatureID, CreatureID>>(fieldValue, [](JsonSerializeFormat & h, std::pair<CreatureID, CreatureID> & e)
  158. {
  159. h.serializeId("creature", e.first, CreatureID{});
  160. h.serializeId("amount", e.second, CreatureID{});
  161. });
  162. creaturesChange = std::map<CreatureID, CreatureID>(fieldValue.begin(), fieldValue.end());
  163. }
  164. {
  165. auto a = handler.enterStruct("spellCast");
  166. a->serializeId("spell", spellCast.first, SpellID{});
  167. a->serializeInt("level", spellCast.second);
  168. }
  169. }
  170. VCMI_LIB_NAMESPACE_END