2
0

GameRandomizer.cpp 10 KB

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