Limiter.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 (canLearnSkills && !hero->canLearnSkill())
  90. return false;
  91. if(manaPercentage > 100 * hero->mana / hero->manaLimit())
  92. return false;
  93. for(size_t i=0; i<primary.size(); i++)
  94. {
  95. if(primary[i] > hero->getPrimSkillLevel(static_cast<PrimarySkill>(i)))
  96. return false;
  97. }
  98. for(const auto & skill : secondary)
  99. {
  100. if (skill.second > hero->getSecSkillLevel(skill.first))
  101. return false;
  102. }
  103. for(const auto & spell : spells)
  104. {
  105. if (!hero->spellbookContainsSpell(spell))
  106. return false;
  107. }
  108. {
  109. std::unordered_map<ArtifactID, unsigned int, ArtifactID::hash> artifactsRequirements; // artifact ID -> required count
  110. for(const auto & art : artifacts)
  111. ++artifactsRequirements[art];
  112. size_t reqSlots = 0;
  113. for(const auto & elem : artifactsRequirements)
  114. {
  115. // check required amount of artifacts
  116. if(hero->getArtPosCount(elem.first, false, true, true) < elem.second)
  117. return false;
  118. if(!hero->hasArt(elem.first))
  119. reqSlots += hero->getAssemblyByConstituent(elem.first)->getPartsInfo().size() - 2;
  120. }
  121. if(!ArtifactUtils::isBackpackFreeSlots(hero, reqSlots))
  122. return false;
  123. }
  124. if(!players.empty() && !vstd::contains(players, hero->getOwner()))
  125. return false;
  126. if(!heroes.empty() && !vstd::contains(heroes, hero->type->getId()))
  127. return false;
  128. if(!heroClasses.empty() && !vstd::contains(heroClasses, hero->type->heroClass->getId()))
  129. return false;
  130. for(const auto & sublimiter : noneOf)
  131. {
  132. if (sublimiter->heroAllowed(hero))
  133. return false;
  134. }
  135. for(const auto & sublimiter : allOf)
  136. {
  137. if (!sublimiter->heroAllowed(hero))
  138. return false;
  139. }
  140. if(anyOf.empty())
  141. return true;
  142. for(const auto & sublimiter : anyOf)
  143. {
  144. if (sublimiter->heroAllowed(hero))
  145. return true;
  146. }
  147. return false;
  148. }
  149. void Rewardable::Limiter::loadComponents(std::vector<Component> & comps,
  150. const CGHeroInstance * h) const
  151. {
  152. if (heroExperience)
  153. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  154. if (heroLevel > 0)
  155. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  156. if (manaPoints || manaPercentage > 0)
  157. {
  158. int absoluteMana = h->manaLimit() ? (manaPercentage * h->mana / h->manaLimit() / 100) : 0;
  159. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, absoluteMana + manaPoints, 0);
  160. }
  161. for (size_t i=0; i<primary.size(); i++)
  162. {
  163. if (primary[i] != 0)
  164. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  165. }
  166. for(const auto & entry : secondary)
  167. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  168. for(const auto & entry : artifacts)
  169. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  170. for(const auto & entry : spells)
  171. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  172. for(const auto & entry : creatures)
  173. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  174. for(const auto & entry : players)
  175. comps.emplace_back(Component::EComponentType::FLAG, entry, 0, 0);
  176. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  177. for(const auto & entry : heroes)
  178. comps.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroTypes()->getById(entry)->getIconIndex(), 0, 0);
  179. for(const auto & entry : heroClasses)
  180. comps.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroClasses()->getById(entry)->getIconIndex(), 0, 0);
  181. for (size_t i=0; i<resources.size(); i++)
  182. {
  183. if (resources[i] !=0)
  184. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  185. }
  186. }
  187. void Rewardable::Limiter::serializeJson(JsonSerializeFormat & handler)
  188. {
  189. handler.serializeInt("dayOfWeek", dayOfWeek);
  190. handler.serializeInt("daysPassed", daysPassed);
  191. resources.serializeJson(handler, "resources");
  192. handler.serializeInt("manaPercentage", manaPercentage);
  193. handler.serializeInt("heroExperience", heroExperience);
  194. handler.serializeInt("heroLevel", heroLevel);
  195. handler.serializeIdArray("heroes", heroes);
  196. handler.serializeIdArray("heroClasses", heroClasses);
  197. handler.serializeIdArray("colors", players);
  198. handler.serializeInt("manaPoints", manaPoints);
  199. handler.serializeIdArray("artifacts", artifacts);
  200. handler.enterArray("creatures").serializeStruct(creatures);
  201. handler.enterArray("primary").serializeArray(primary);
  202. {
  203. auto a = handler.enterArray("secondary");
  204. std::vector<std::pair<SecondarySkill, si32>> fieldValue(secondary.begin(), secondary.end());
  205. a.serializeStruct<std::pair<SecondarySkill, si32>>(fieldValue, [](JsonSerializeFormat & h, std::pair<SecondarySkill, si32> & e)
  206. {
  207. h.serializeId("skill", e.first, SecondarySkill{}, VLC->skillh->decodeSkill, VLC->skillh->encodeSkill);
  208. h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);});
  209. });
  210. a.syncSize(fieldValue);
  211. secondary = std::map<SecondarySkill, si32>(fieldValue.begin(), fieldValue.end());
  212. }
  213. //sublimiters
  214. auto serializeSublimitersList = [&handler](const std::string & field, LimitersList & container)
  215. {
  216. auto a = handler.enterArray(field);
  217. a.syncSize(container);
  218. for(int i = 0; i < container.size(); ++i)
  219. {
  220. if(!handler.saving)
  221. container[i] = std::make_shared<Rewardable::Limiter>();
  222. auto e = a.enterStruct(i);
  223. container[i]->serializeJson(handler);
  224. }
  225. };
  226. serializeSublimitersList("allOf", allOf);
  227. serializeSublimitersList("anyOf", anyOf);
  228. serializeSublimitersList("noneOf", noneOf);
  229. }
  230. VCMI_LIB_NAMESPACE_END