RandomizationProcessor.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * RandomizationProcessor.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 "../lib/callback/IGameRandomizer.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class CRandomGenerator;
  14. class CGHeroInstance;
  15. VCMI_LIB_NAMESPACE_END
  16. /// Biased randomizer that has following properties:
  17. /// - at bias value of 0 it acts as statistical random generator
  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. int accumulatedBias;
  25. public:
  26. /// Performs coin flip with specified success chance
  27. /// Returns true with probability successChance percents, and false with probability 100-successChance percents
  28. bool roll(vstd::RNG & generator, int successChance, int biasValue);
  29. };
  30. class RandomizationProcessor final : public IGameRandomizer
  31. {
  32. std::unique_ptr<CRandomGenerator> globalRandomNumberGenerator;
  33. std::map<HeroTypeID, std::unique_ptr<CRandomGenerator>> heroSeed;
  34. std::map<PlayerColor, std::unique_ptr<CRandomGenerator>> playerTavern;
  35. std::map<ObjectInstanceID, BiasedRandomizer> goodMoraleSeed;
  36. std::map<ObjectInstanceID, BiasedRandomizer> badMoraleSeed;
  37. std::map<ObjectInstanceID, BiasedRandomizer> goodLuckSeed;
  38. std::map<ObjectInstanceID, BiasedRandomizer> badLuckSeed;
  39. std::map<ObjectInstanceID, BiasedRandomizer> combatAbilitySeed;
  40. public:
  41. RandomizationProcessor();
  42. PrimarySkill rollPrimarySkillForLevelup(const CGHeroInstance * hero);
  43. SecondarySkill rollSecondarySkillForLevelup(const CGHeroInstance * hero, const std::vector<SecondarySkill> & candidates);
  44. bool rollGoodMorale(ObjectInstanceID actor, int moraleValue);
  45. bool rollBadMorale(ObjectInstanceID actor, int moraleValue);
  46. bool rollGoodLuck(ObjectInstanceID actor, int luckValue);
  47. bool rollBadLuck(ObjectInstanceID actor, int luckValue);
  48. bool rollCombatAbility(ObjectInstanceID actor, int percentageChance);
  49. HeroTypeID rollHero(PlayerColor player, FactionID faction) override;
  50. CreatureID rollCreature() override;
  51. CreatureID rollCreature(int tier) override;
  52. ArtifactID rollArtifact() override;
  53. ArtifactID rollArtifact(EArtifactClass type) override;
  54. ArtifactID rollArtifact(std::set<ArtifactID> filtered) override;
  55. std::vector<ArtifactID> rollMarketArtifactSet() override;
  56. std::string rollTownName(FactionID faction) override;
  57. vstd::RNG & getDefault() override;
  58. void setSeed(int newSeed);
  59. template<typename Handler>
  60. void serialize(Handler & h)
  61. {
  62. h & *globalRandomNumberGenerator;
  63. }
  64. };