Limiter.cpp 7.6 KB

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