GameRandomizer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * GameRandomizer.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 "GameRandomizer.h"
  12. #include "IGameInfoCallback.h"
  13. #include "../../lib/CRandomGenerator.h"
  14. #include "../../lib/GameLibrary.h"
  15. #include "../../lib/CCreatureHandler.h"
  16. #include "../../lib/CSkillHandler.h"
  17. #include "../../lib/entities/artifact/CArtHandler.h"
  18. #include "../../lib/entities/artifact/EArtifactClass.h"
  19. #include "../../lib/entities/hero/CHeroClass.h"
  20. #include "../../lib/mapObjects/CGHeroInstance.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. bool BiasedRandomizer::roll(vstd::RNG &generator, int successChance, int biasValue)
  23. {
  24. int failChance = 100 - successChance;
  25. int newRoll = generator.nextInt(0,99);
  26. bool success = newRoll + accumulatedBias >= successChance;
  27. if (success)
  28. accumulatedBias -= failChance * biasValue / 100;
  29. else
  30. accumulatedBias += successChance * biasValue / 100;
  31. return success;
  32. }
  33. GameRandomizer::GameRandomizer(const IGameInfoCallback & gameInfo)
  34. : gameInfo(gameInfo)
  35. {
  36. }
  37. GameRandomizer::~GameRandomizer() = default;
  38. //bool GameRandomizer::rollGoodMorale(ObjectInstanceID actor, int moraleValue)
  39. //{
  40. //
  41. //}
  42. //
  43. //bool GameRandomizer::rollBadMorale(ObjectInstanceID actor, int moraleValue)
  44. //{
  45. //
  46. //}
  47. //
  48. //bool GameRandomizer::rollGoodLuck(ObjectInstanceID actor, int luckValue)
  49. //{
  50. //
  51. //}
  52. //
  53. //bool GameRandomizer::rollBadLuck(ObjectInstanceID actor, int luckValue)
  54. //{
  55. //
  56. //}
  57. //
  58. //bool GameRandomizer::rollCombatAbility(ObjectInstanceID actor, int percentageChance)
  59. //{
  60. //
  61. //}
  62. //
  63. //HeroTypeID GameRandomizer::rollHero(PlayerColor player, FactionID faction)
  64. //{
  65. //
  66. //}
  67. CreatureID GameRandomizer::rollCreature()
  68. {
  69. std::vector<CreatureID> allowed;
  70. for(const auto & creatureID : LIBRARY->creh->getDefaultAllowed())
  71. {
  72. const auto * creaturePtr = creatureID.toCreature();
  73. if(!creaturePtr->excludeFromRandomization)
  74. allowed.push_back(creaturePtr->getId());
  75. }
  76. if(allowed.empty())
  77. throw std::runtime_error("Cannot pick a random creature!");
  78. return *RandomGeneratorUtil::nextItem(allowed, getDefault());
  79. }
  80. CreatureID GameRandomizer::rollCreature(int tier)
  81. {
  82. std::vector<CreatureID> allowed;
  83. for(const auto & creatureID : LIBRARY->creh->getDefaultAllowed())
  84. {
  85. const auto * creaturePtr = creatureID.toCreature();
  86. if(creaturePtr->excludeFromRandomization)
  87. continue;
  88. if(creaturePtr->getLevel() == tier)
  89. allowed.push_back(creaturePtr->getId());
  90. }
  91. if(allowed.empty())
  92. throw std::runtime_error("Cannot pick a random creature!");
  93. return *RandomGeneratorUtil::nextItem(allowed, getDefault());
  94. }
  95. ArtifactID GameRandomizer::rollArtifact()
  96. {
  97. std::set<ArtifactID> potentialPicks;
  98. for(const auto & artifactID : LIBRARY->arth->getDefaultAllowed())
  99. {
  100. if(!LIBRARY->arth->legalArtifact(artifactID))
  101. continue;
  102. potentialPicks.insert(artifactID);
  103. }
  104. return rollArtifact(potentialPicks);
  105. }
  106. ArtifactID GameRandomizer::rollArtifact(EArtifactClass type)
  107. {
  108. std::set<ArtifactID> potentialPicks;
  109. for(const auto & artifactID : LIBRARY->arth->getDefaultAllowed())
  110. {
  111. if(!LIBRARY->arth->legalArtifact(artifactID))
  112. continue;
  113. if(!gameInfo.isAllowed(artifactID))
  114. continue;
  115. const auto * artifact = artifactID.toArtifact();
  116. if(type != artifact->aClass)
  117. continue;
  118. potentialPicks.insert(artifactID);
  119. }
  120. return rollArtifact(potentialPicks);
  121. }
  122. ArtifactID GameRandomizer::rollArtifact(std::set<ArtifactID> potentialPicks)
  123. {
  124. // No allowed artifacts at all - give Grail - this can't be banned (hopefully)
  125. // FIXME: investigate how such cases are handled by H3 - some heavily customized user-made maps likely rely on H3 behavior
  126. if(potentialPicks.empty())
  127. {
  128. logGlobal->warn("Failed to find artifact that matches requested parameters!");
  129. return ArtifactID::GRAIL;
  130. }
  131. // Find how many times least used artifacts were picked by randomizer
  132. int leastUsedTimes = std::numeric_limits<int>::max();
  133. for(const auto & artifact : potentialPicks)
  134. if(allocatedArtifacts[artifact] < leastUsedTimes)
  135. leastUsedTimes = allocatedArtifacts[artifact];
  136. // Pick all artifacts that were used least number of times
  137. std::set<ArtifactID> preferredPicks;
  138. for(const auto & artifact : potentialPicks)
  139. if(allocatedArtifacts[artifact] == leastUsedTimes)
  140. preferredPicks.insert(artifact);
  141. assert(!preferredPicks.empty());
  142. ArtifactID artID = *RandomGeneratorUtil::nextItem(preferredPicks, getDefault());
  143. allocatedArtifacts[artID] += 1; // record +1 more usage
  144. return artID;
  145. }
  146. std::vector<ArtifactID> GameRandomizer::rollMarketArtifactSet()
  147. {
  148. std::vector<ArtifactID> out;
  149. for(int j = 0; j < 3; j++)
  150. out.push_back(rollArtifact(EArtifactClass::ART_TREASURE));
  151. for(int j = 0; j < 3; j++)
  152. out.push_back(rollArtifact(EArtifactClass::ART_MINOR));
  153. out.push_back(rollArtifact(EArtifactClass::ART_MAJOR));
  154. return out;
  155. }
  156. vstd::RNG & GameRandomizer::getDefault()
  157. {
  158. return globalRandomNumberGenerator;
  159. }
  160. void GameRandomizer::setSeed(int newSeed)
  161. {
  162. globalRandomNumberGenerator.setSeed(newSeed);
  163. }
  164. PrimarySkill GameRandomizer::rollPrimarySkillForLevelup(const CGHeroInstance * hero)
  165. {
  166. const bool isLowLevelHero = hero->level < GameConstants::HERO_HIGH_LEVEL;
  167. const auto & skillChances = isLowLevelHero ? hero->getHeroClass()->primarySkillLowLevel : hero->getHeroClass()->primarySkillHighLevel;
  168. auto & heroRng = heroSkillSeed.at(hero->getHeroTypeID());
  169. if(hero->isCampaignYog())
  170. {
  171. // Yog can only receive Attack or Defence on level-up
  172. std::vector<int> yogChances = {skillChances[0], skillChances[1]};
  173. return static_cast<PrimarySkill>(RandomGeneratorUtil::nextItemWeighted(yogChances, heroRng.seed));
  174. }
  175. return static_cast<PrimarySkill>(RandomGeneratorUtil::nextItemWeighted(skillChances, heroRng.seed));
  176. }
  177. SecondarySkill GameRandomizer::rollSecondarySkillForLevelup(const CGHeroInstance * hero, const std::set<SecondarySkill> & options)
  178. {
  179. auto & heroRng = heroSkillSeed.at(hero->getHeroTypeID());
  180. auto getObligatorySkills = [](CSkill::Obligatory obl)
  181. {
  182. std::set<SecondarySkill> obligatory;
  183. for(auto i = 0; i < LIBRARY->skillh->size(); i++)
  184. if((*LIBRARY->skillh)[SecondarySkill(i)]->obligatory(obl))
  185. obligatory.insert(i); //Always return all obligatory skills
  186. return obligatory;
  187. };
  188. auto intersect = [](const std::set<SecondarySkill> & left, const std::set<SecondarySkill> & right)
  189. {
  190. std::set<SecondarySkill> intersect;
  191. std::set_intersection(left.begin(), left.end(), right.begin(), right.end(), std::inserter(intersect, intersect.begin()));
  192. return intersect;
  193. };
  194. std::set<SecondarySkill> wisdomList = getObligatorySkills(CSkill::Obligatory::MAJOR);
  195. std::set<SecondarySkill> schoolList = getObligatorySkills(CSkill::Obligatory::MINOR);
  196. bool wantsWisdom = heroRng.wisdomCounter + 1 >= hero->maxlevelsToWisdom();
  197. bool wantsSchool = heroRng.magicSchoolCounter + 1 >= hero->maxlevelsToMagicSchool();
  198. bool selectWisdom = wantsWisdom && !intersect(options, wisdomList).empty();
  199. bool selectSchool = wantsSchool && !intersect(options, schoolList).empty();
  200. std::set<SecondarySkill> actualCandidates;
  201. if(selectWisdom)
  202. actualCandidates = intersect(options, wisdomList);
  203. else if(selectSchool)
  204. actualCandidates = intersect(options, schoolList);
  205. else
  206. actualCandidates = options;
  207. assert(!actualCandidates.empty());
  208. std::vector<int> weights;
  209. std::vector<SecondarySkill> skills;
  210. for(const auto & possible : actualCandidates)
  211. {
  212. skills.push_back(possible);
  213. if(hero->getHeroClass()->secSkillProbability.count(possible) != 0)
  214. {
  215. int weight = hero->getHeroClass()->secSkillProbability.at(possible);
  216. weights.push_back(std::max(1, weight));
  217. }
  218. else
  219. weights.push_back(1); // H3 behavior - banned skills have minimal (1) chance to be picked
  220. }
  221. int selectedIndex = RandomGeneratorUtil::nextItemWeighted(weights, heroRng.seed);
  222. SecondarySkill selectedSkill = skills.at(selectedIndex);
  223. //deterministic secondary skills
  224. ++heroRng.magicSchoolCounter;
  225. ++heroRng.wisdomCounter;
  226. if((*LIBRARY->skillh)[selectedSkill]->obligatory(CSkill::Obligatory::MAJOR))
  227. heroRng.wisdomCounter = 0;
  228. if((*LIBRARY->skillh)[selectedSkill]->obligatory(CSkill::Obligatory::MINOR))
  229. heroRng.magicSchoolCounter = 0;
  230. return selectedSkill;
  231. }
  232. VCMI_LIB_NAMESPACE_END