GameRandomizer.h 3.6 KB

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