Limiter.cpp 7.8 KB

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