Limiter.cpp 8.8 KB

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