Limiter.cpp 8.2 KB

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