GameRandomizer.h 3.9 KB

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