Limiter.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "../serializer/JsonSerializeFormat.h"
  16. #include "../constants/StringConstants.h"
  17. #include "../CHeroHandler.h"
  18. #include "../CSkillHandler.h"
  19. #include "../ArtifactUtils.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. , primary(GameConstants::PRIMARY_SKILLS, 0)
  29. {
  30. }
  31. Rewardable::Limiter::~Limiter() = default;
  32. bool Rewardable::Limiter::empty() const
  33. {
  34. if(dayOfWeek != 0
  35. || daysPassed != 0
  36. || heroLevel > 0
  37. || heroExperience > 0
  38. || manaPoints > 0
  39. || manaPercentage > 0
  40. || !secondary.empty()
  41. || !creatures.empty()
  42. || !spells.empty()
  43. || !artifacts.empty()
  44. || !players.empty()
  45. || !heroes.empty()
  46. || !heroClasses.empty()
  47. || resources.nonZero()
  48. || std::find_if(primary.begin(), primary.end(), [](si32 i){return i != 0;}) != primary.end())
  49. return false;
  50. for(const auto & sub : {noneOf, allOf, anyOf})
  51. {
  52. for(const auto & sublimiter : sub)
  53. {
  54. if(!sublimiter->empty())
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. bool Rewardable::Limiter::heroAllowed(const CGHeroInstance * hero) const
  61. {
  62. if(dayOfWeek != 0)
  63. {
  64. if (IObjectInterface::cb->getDate(Date::DAY_OF_WEEK) != dayOfWeek)
  65. return false;
  66. }
  67. if(daysPassed != 0)
  68. {
  69. if (IObjectInterface::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(!IObjectInterface::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(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. {
  110. std::unordered_map<ArtifactID, unsigned, ArtifactID::hash> artifactsRequirements; // artifact ID -> required count
  111. for(const auto & art : artifacts)
  112. ++artifactsRequirements[art];
  113. size_t reqSlots = 0;
  114. for(const auto & elem : artifactsRequirements)
  115. {
  116. // check required amount of artifacts
  117. if(hero->getArtPosCount(elem.first, false, true, true) < elem.second)
  118. return false;
  119. if(!hero->hasArt(elem.first))
  120. reqSlots += hero->getAssemblyByConstituent(elem.first)->getPartsInfo().size() - 2;
  121. }
  122. if(!ArtifactUtils::isBackpackFreeSlots(hero, reqSlots))
  123. return false;
  124. }
  125. if(!players.empty() && !vstd::contains(players, hero->getOwner()))
  126. return false;
  127. if(!heroes.empty() && !vstd::contains(heroes, hero->type->getId()))
  128. return false;
  129. if(!heroClasses.empty() && !vstd::contains(heroClasses, hero->type->heroClass->getId()))
  130. return false;
  131. for(const auto & sublimiter : noneOf)
  132. {
  133. if (sublimiter->heroAllowed(hero))
  134. return false;
  135. }
  136. for(const auto & sublimiter : allOf)
  137. {
  138. if (!sublimiter->heroAllowed(hero))
  139. return false;
  140. }
  141. if(anyOf.empty())
  142. return true;
  143. for(const auto & sublimiter : anyOf)
  144. {
  145. if (sublimiter->heroAllowed(hero))
  146. return true;
  147. }
  148. return false;
  149. }
  150. void Rewardable::Limiter::loadComponents(std::vector<Component> & comps,
  151. const CGHeroInstance * h) const
  152. {
  153. if (heroExperience)
  154. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  155. if (heroLevel)
  156. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  157. if (manaPoints || manaPercentage > 0)
  158. {
  159. int absoluteMana = h->manaLimit() ? (manaPercentage * h->mana / h->manaLimit() / 100) : 0;
  160. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, absoluteMana + manaPoints, 0);
  161. }
  162. for (size_t i=0; i<primary.size(); i++)
  163. {
  164. if (primary[i] != 0)
  165. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  166. }
  167. for(const auto & entry : secondary)
  168. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  169. for(const auto & entry : artifacts)
  170. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  171. for(const auto & entry : spells)
  172. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  173. for(const auto & entry : creatures)
  174. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  175. for(const auto & entry : players)
  176. comps.emplace_back(Component::EComponentType::FLAG, entry, 0, 0);
  177. //FIXME: portrait may not match hero, if custom portrait was set in map editor
  178. for(const auto & entry : heroes)
  179. comps.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroTypes()->getById(entry)->getIconIndex(), 0, 0);
  180. for(const auto & entry : heroClasses)
  181. comps.emplace_back(Component::EComponentType::HERO_PORTRAIT, VLC->heroClasses()->getById(entry)->getIconIndex(), 0, 0);
  182. for (size_t i=0; i<resources.size(); i++)
  183. {
  184. if (resources[i] !=0)
  185. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  186. }
  187. }
  188. void Rewardable::Limiter::serializeJson(JsonSerializeFormat & handler)
  189. {
  190. handler.serializeInt("dayOfWeek", dayOfWeek);
  191. handler.serializeInt("daysPassed", daysPassed);
  192. resources.serializeJson(handler, "resources");
  193. handler.serializeInt("manaPercentage", manaPercentage);
  194. handler.serializeInt("heroExperience", heroExperience);
  195. handler.serializeInt("heroLevel", heroLevel);
  196. handler.serializeIdArray("heroes", heroes);
  197. handler.serializeIdArray("heroClasses", heroClasses);
  198. handler.serializeIdArray("colors", players);
  199. handler.serializeInt("manaPoints", manaPoints);
  200. handler.serializeIdArray("artifacts", artifacts);
  201. handler.enterArray("creatures").serializeStruct(creatures);
  202. handler.enterArray("primary").serializeArray(primary);
  203. {
  204. auto a = handler.enterArray("secondary");
  205. std::vector<std::pair<SecondarySkill, si32>> fieldValue(secondary.begin(), secondary.end());
  206. a.serializeStruct<std::pair<SecondarySkill, si32>>(fieldValue, [](JsonSerializeFormat & h, std::pair<SecondarySkill, si32> & e)
  207. {
  208. h.serializeId("skill", e.first, SecondarySkill{}, VLC->skillh->decodeSkill, VLC->skillh->encodeSkill);
  209. h.serializeId("level", e.second, 0, [](const std::string & i){return vstd::find_pos(NSecondarySkill::levels, i);}, [](si32 i){return NSecondarySkill::levels.at(i);});
  210. });
  211. a.syncSize(fieldValue);
  212. secondary = std::map<SecondarySkill, si32>(fieldValue.begin(), fieldValue.end());
  213. }
  214. //sublimiters
  215. auto serializeSublimitersList = [&handler](const std::string & field, LimitersList & container)
  216. {
  217. auto a = handler.enterArray(field);
  218. a.syncSize(container);
  219. for(int i = 0; i < container.size(); ++i)
  220. {
  221. if(!handler.saving)
  222. container[i] = std::make_shared<Rewardable::Limiter>();
  223. auto e = a.enterStruct(i);
  224. container[i]->serializeJson(handler);
  225. }
  226. };
  227. serializeSublimitersList("allOf", allOf);
  228. serializeSublimitersList("anyOf", anyOf);
  229. serializeSublimitersList("noneOf", noneOf);
  230. }
  231. VCMI_LIB_NAMESPACE_END