Limiter.cpp 9.2 KB

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