Reward.cpp 5.3 KB

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