Limiter.cpp 7.6 KB

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