GameRandomizer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * GameRandomizer.h, 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. #pragma once
  11. #include "callback/IGameRandomizer.h"
  12. #include "CRandomGenerator.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. enum class EGameSettings;
  15. class CGHeroInstance;
  16. class DLL_LINKAGE RandomizationBias
  17. {
  18. int32_t accumulatedBias = 0;
  19. public:
  20. /// Performs coin flip with specified success chance
  21. /// Returns true with probability successChance percents, and false with probability totalWeight-successChance percents
  22. bool roll(vstd::RNG & generator, int successChance, int totalWeight, int biasValue);
  23. template<typename Handler>
  24. void serialize(Handler & h)
  25. {
  26. h & accumulatedBias;
  27. }
  28. };
  29. /// Biased randomizer that has following properties:
  30. /// - at bias value of 0 it acts as statistical random generator, just like vstd::RNG
  31. /// - at bias value of 100 it guarantees that it will take at most 100/chance rolls till succesfull roll
  32. /// - at bias value between 1..99 similar guarantee is also provided, but with larger number of rolls
  33. /// No matter what bias is, statistical probability on large number of rolls remains the same
  34. /// Its goal is to simulate human expectations of random distributions and reduce frustration from "bad" rolls
  35. class DLL_LINKAGE RandomGeneratorWithBias
  36. {
  37. CRandomGenerator generator;
  38. RandomizationBias bias;
  39. public:
  40. explicit RandomGeneratorWithBias(int seed = 0);
  41. /// Performs coin flip with specified success chance
  42. /// Returns true with probability successChance percents, and false with probability 100-successChance percents
  43. bool roll(int successChance, int totalWeight, int biasValue);
  44. template<typename Handler>
  45. void serialize(Handler & h)
  46. {
  47. h & generator;
  48. h & bias;
  49. }
  50. };
  51. class DLL_LINKAGE GameRandomizer final : public IGameRandomizer
  52. {
  53. struct HeroSkillRandomizer
  54. {
  55. explicit HeroSkillRandomizer(int seed = 0)
  56. : seed(seed)
  57. {}
  58. CRandomGenerator seed;
  59. int8_t magicSchoolCounter = 1;
  60. int8_t wisdomCounter = 1;
  61. template<typename Handler>
  62. void serialize(Handler & h)
  63. {
  64. h & seed;
  65. h & magicSchoolCounter;
  66. h & wisdomCounter;
  67. }
  68. };
  69. const IGameInfoCallback & gameInfo;
  70. /// Global RNG, for use when there is no specialized instance
  71. CRandomGenerator globalRandomNumberGenerator;
  72. /// Stores number of times each artifact was placed on map via randomization
  73. std::map<ArtifactID, int> allocatedArtifacts;
  74. std::map<HeroTypeID, HeroSkillRandomizer> heroSkillSeed;
  75. std::map<ObjectInstanceID, RandomGeneratorWithBias> goodMoraleSeed;
  76. std::map<ObjectInstanceID, RandomGeneratorWithBias> badMoraleSeed;
  77. std::map<ObjectInstanceID, RandomGeneratorWithBias> goodLuckSeed;
  78. std::map<ObjectInstanceID, RandomGeneratorWithBias> badLuckSeed;
  79. std::map<ObjectInstanceID, RandomGeneratorWithBias> combatAbilitySeed;
  80. bool rollMoraleLuck(std::map<ObjectInstanceID, RandomGeneratorWithBias> & seeds, ObjectInstanceID actor, int moraleLuckValue, EGameSettings biasValue, EGameSettings diceSize, EGameSettings diceWeights);
  81. public:
  82. explicit GameRandomizer(const IGameInfoCallback & gameInfo);
  83. ~GameRandomizer();
  84. PrimarySkill rollPrimarySkillForLevelup(const CGHeroInstance * hero) override;
  85. SecondarySkill rollSecondarySkillForLevelup(const CGHeroInstance * hero, const std::set<SecondarySkill> & candidates) override;
  86. bool rollGoodMorale(ObjectInstanceID actor, int moraleValue);
  87. bool rollBadMorale(ObjectInstanceID actor, int moraleValue);
  88. bool rollGoodLuck(ObjectInstanceID actor, int luckValue);
  89. bool rollBadLuck(ObjectInstanceID actor, int luckValue);
  90. bool rollCombatAbility(ObjectInstanceID actor, int percentageChance);
  91. CreatureID rollCreature() override;
  92. CreatureID rollCreature(int tier) override;
  93. ArtifactID rollArtifact() override;
  94. ArtifactID rollArtifact(EArtifactClass type) override;
  95. ArtifactID rollArtifact(std::set<ArtifactID> filtered) override;
  96. std::vector<ArtifactID> rollMarketArtifactSet() override;
  97. vstd::RNG & getDefault() override;
  98. void setSeed(int newSeed);
  99. template<typename Handler>
  100. void serialize(Handler & h)
  101. {
  102. h & globalRandomNumberGenerator;
  103. if (h.hasFeature(Handler::Version::RANDOMIZATION_REWORK))
  104. {
  105. h & allocatedArtifacts;
  106. h & heroSkillSeed;
  107. h & goodMoraleSeed;
  108. h & badMoraleSeed;
  109. h & goodLuckSeed;
  110. h & badLuckSeed;
  111. h & combatAbilitySeed;
  112. }
  113. }
  114. };
  115. VCMI_LIB_NAMESPACE_END