Limiter.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Limiter.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 "Limiter.h"
  12. #include "../IGameCallback.h"
  13. #include "../CPlayerState.h"
  14. #include "../mapObjects/CGHeroInstance.h"
  15. #include "../serializer/JsonSerializeFormat.h"
  16. #include "../constants/StringConstants.h"
  17. #include "../CHeroHandler.h"
  18. #include "../CSkillHandler.h"
  19. #include "../ArtifactUtils.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. Rewardable::Limiter::Limiter()
  22. : dayOfWeek(0)
  23. , daysPassed(0)
  24. , heroExperience(0)
  25. , heroLevel(-1)
  26. , manaPercentage(0)
  27. , manaPoints(0)
  28. , primary(GameConstants::PRIMARY_SKILLS, 0)
  29. {
  30. }
  31. Rewardable::Limiter::~Limiter() = default;
  32. bool Rewardable::Limiter::heroAllowed(const CGHeroInstance * hero) const
  33. {
  34. if(dayOfWeek != 0)
  35. {
  36. if (IObjectInterface::cb->getDate(Date::DAY_OF_WEEK) != dayOfWeek)
  37. return false;
  38. }
  39. if(daysPassed != 0)
  40. {
  41. if (IObjectInterface::cb->getDate(Date::DAY) < daysPassed)
  42. return false;
  43. }
  44. for(const auto & reqStack : creatures)
  45. {
  46. size_t count = 0;
  47. for(const auto & slot : hero->Slots())
  48. {
  49. const CStackInstance * heroStack = slot.second;
  50. if (heroStack->type == reqStack.type)
  51. count += heroStack->count;
  52. }
  53. if (count < reqStack.count) //not enough creatures of this kind
  54. return false;
  55. }
  56. if(!IObjectInterface::cb->getPlayerState(hero->tempOwner)->resources.canAfford(resources))
  57. return false;
  58. if(heroLevel > static_cast<si32>(hero->level))
  59. return false;
  60. if(static_cast<TExpType>(heroExperience) > hero->exp)
  61. return false;
  62. if(manaPoints > hero->mana)
  63. return false;
  64. if(manaPercentage > 100 * hero->mana / hero->manaLimit())
  65. return false;
  66. for(size_t i=0; i<primary.size(); i++)
  67. {
  68. if(primary[i] > hero->getPrimSkillLevel(static_cast<PrimarySkill>(i)))
  69. return false;
  70. }
  71. for(const auto & skill : secondary)
  72. {
  73. if (skill.second > hero->getSecSkillLevel(skill.first))
  74. return false;
  75. }
  76. for(const auto & spell : spells)
  77. {
  78. if (!hero->spellbookContainsSpell(spell))
  79. return false;
  80. }
  81. {
  82. std::unordered_map<ArtifactID, unsigned, ArtifactID::hash> artifactsRequirements; // artifact ID -> required count
  83. for(const auto & art : artifacts)
  84. ++artifactsRequirements[art];
  85. size_t reqSlots = 0;
  86. for(const auto & elem : artifactsRequirements)
  87. {
  88. // check required amount of artifacts
  89. if(hero->getArtPosCount(elem.first, false, true, true) < elem.second)
  90. return false;
  91. if(!hero->hasArt(elem.first))
  92. reqSlots += hero->getAssemblyByConstituent(elem.first)->getPartsInfo().size() - 2;
  93. }
  94. if(!ArtifactUtils::isBackpackFreeSlots(hero, reqSlots))
  95. return false;
  96. }
  97. if(!players.empty() && !vstd::contains(players, hero->getOwner()))
  98. return false;
  99. if(!heroes.empty() && !vstd::contains(heroes, hero->type->getId()))
  100. return false;
  101. if(!heroClasses.empty() && !vstd::contains(heroClasses, hero->type->heroClass->getId()))
  102. return false;
  103. for(const auto & sublimiter : noneOf)
  104. {
  105. if (sublimiter->heroAllowed(hero))
  106. return false;
  107. }
  108. for(const auto & sublimiter : allOf)
  109. {
  110. if (!sublimiter->heroAllowed(hero))
  111. return false;
  112. }
  113. if(anyOf.empty())
  114. return true;
  115. for(const auto & sublimiter : anyOf)
  116. {
  117. if (sublimiter->heroAllowed(hero))
  118. return true;
  119. }
  120. return false;
  121. }
  122. void Rewardable::Limiter::loadComponents(std::vector<Component> & comps,
  123. const CGHeroInstance * h) const
  124. {
  125. if (heroExperience)
  126. {
  127. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  128. }
  129. if (heroLevel)
  130. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  131. if (manaPoints || manaPercentage > 0)
  132. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, 0, 0);
  133. for (size_t i=0; i<primary.size(); i++)
  134. {
  135. if (primary[i] != 0)
  136. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  137. }
  138. for(const auto & entry : secondary)
  139. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  140. for(const auto & entry : artifacts)
  141. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  142. for(const auto & entry : spells)
  143. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  144. for(const auto & entry : creatures)
  145. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  146. for(const auto & entry : players)
  147. comps.emplace_back(Component::EComponentType::FLAG, entry, 0, 0);
  148. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  149. for(const auto & entry : heroes)
  150. comps.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroTypes()->getById(entry)->getIconIndex(), 0, 0);
  151. for(const auto & entry : heroClasses)
  152. comps.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroClasses()->getById(entry)->getIconIndex(), 0, 0);
  153. for (size_t i=0; i<resources.size(); i++)
  154. {
  155. if (resources[i] !=0)
  156. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  157. }
  158. }
  159. void Rewardable::Limiter::serializeJson(JsonSerializeFormat & handler)
  160. {
  161. handler.serializeInt("dayOfWeek", dayOfWeek);
  162. handler.serializeInt("daysPassed", daysPassed);
  163. resources.serializeJson(handler, "resources");
  164. handler.serializeInt("manaPercentage", manaPercentage);
  165. handler.serializeInt("heroExperience", heroExperience);
  166. handler.serializeInt("heroLevel", heroLevel);
  167. handler.serializeIdArray("heroes", heroes);
  168. handler.serializeIdArray("heroClasses", heroClasses);
  169. handler.serializeIdArray("colors", players);
  170. handler.serializeInt("manaPoints", manaPoints);
  171. handler.serializeIdArray("artifacts", artifacts);
  172. handler.enterArray("creatures").serializeStruct(creatures);
  173. handler.enterArray("primary").serializeArray(primary);
  174. {
  175. auto a = handler.enterArray("secondary");
  176. std::vector<std::pair<SecondarySkill, si32>> fieldValue(secondary.begin(), secondary.end());
  177. a.serializeStruct<std::pair<SecondarySkill, si32>>(fieldValue, [](JsonSerializeFormat & h, std::pair<SecondarySkill, si32> & e)
  178. {
  179. h.serializeId("skill", e.first, SecondarySkill{}, VLC->skillh->decodeSkill, VLC->skillh->encodeSkill);
  180. h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);});
  181. });
  182. a.syncSize(fieldValue);
  183. secondary = std::map<SecondarySkill, si32>(fieldValue.begin(), fieldValue.end());
  184. }
  185. //sublimiters
  186. auto serializeSublimitersList = [&handler](const std::string & field, LimitersList & container)
  187. {
  188. auto a = handler.enterArray(field);
  189. a.syncSize(container);
  190. for(int i = 0; i < container.size(); ++i)
  191. {
  192. if(!handler.saving)
  193. container[i] = std::make_shared<Rewardable::Limiter>();
  194. auto e = a.enterStruct(i);
  195. container[i]->serializeJson(handler);
  196. }
  197. };
  198. serializeSublimitersList("allOf", allOf);
  199. serializeSublimitersList("anyOf", anyOf);
  200. serializeSublimitersList("noneOf", noneOf);
  201. }
  202. VCMI_LIB_NAMESPACE_END