1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

Fix formatting, remove unused code

This commit is contained in:
Ivan Savenko
2025-05-19 17:43:29 +03:00
parent 157b4fea74
commit 38f7c04471
4 changed files with 55 additions and 44 deletions

View File

@@ -24,18 +24,13 @@
VCMI_LIB_NAMESPACE_BEGIN
BiasedRandomizer::BiasedRandomizer(int seed)
: seed(seed)
{
}
bool BiasedRandomizer::roll(int successChance, int totalWeight, int biasValue)
bool RandomizationBias::roll(vstd::RNG & generator, int successChance, int totalWeight, int biasValue)
{
assert(successChance > 0);
assert(totalWeight >= successChance);
int failChance = totalWeight - successChance;
int newRoll = seed.nextInt(1,totalWeight);
int newRoll = generator.nextInt(1, totalWeight);
// accumulated bias is stored as premultiplied to avoid precision loss on division
// so multiply everything else in equation to compensate
// precision loss is small, and generally insignificant, but better to play it safe
@@ -48,6 +43,16 @@ bool BiasedRandomizer::roll(int successChance, int totalWeight, int biasValue)
return success;
}
RandomGeneratorWithBias::RandomGeneratorWithBias(int seed)
: generator(seed)
{
}
bool RandomGeneratorWithBias::roll(int successChance, int totalWeight, int biasValue)
{
return bias.roll(generator, successChance, totalWeight, biasValue);
}
GameRandomizer::GameRandomizer(const IGameInfoCallback & gameInfo)
: gameInfo(gameInfo)
{
@@ -56,7 +61,7 @@ GameRandomizer::GameRandomizer(const IGameInfoCallback & gameInfo)
GameRandomizer::~GameRandomizer() = default;
bool GameRandomizer::rollMoraleLuck(std::map<ObjectInstanceID, BiasedRandomizer> & seeds, ObjectInstanceID actor, int moraleLuckValue, EGameSettings diceSize, EGameSettings diceWeights)
bool GameRandomizer::rollMoraleLuck(std::map<ObjectInstanceID, RandomGeneratorWithBias> & seeds, ObjectInstanceID actor, int moraleLuckValue, EGameSettings diceSize, EGameSettings diceWeights)
{
assert(moraleLuckValue > 0);
auto goodLuckChanceVector = gameInfo.getSettings().getVector(diceWeights);
@@ -106,11 +111,6 @@ bool GameRandomizer::rollCombatAbility(ObjectInstanceID actor, int percentageCha
return combatAbilitySeed.at(actor).roll(percentageChance, 100, biasValueAbility);
}
//HeroTypeID GameRandomizer::rollHero(PlayerColor player, FactionID faction)
//{
//
//}
CreatureID GameRandomizer::rollCreature()
{
std::vector<CreatureID> allowed;
@@ -215,14 +215,15 @@ ArtifactID GameRandomizer::rollArtifact(std::set<ArtifactID> potentialPicks)
std::vector<ArtifactID> GameRandomizer::rollMarketArtifactSet()
{
std::vector<ArtifactID> out;
for(int j = 0; j < 3; j++)
out.push_back(rollArtifact(EArtifactClass::ART_TREASURE));
for(int j = 0; j < 3; j++)
out.push_back(rollArtifact(EArtifactClass::ART_MINOR));
out.push_back(rollArtifact(EArtifactClass::ART_MAJOR));
return out;
return {
rollArtifact(EArtifactClass::ART_TREASURE),
rollArtifact(EArtifactClass::ART_TREASURE),
rollArtifact(EArtifactClass::ART_TREASURE),
rollArtifact(EArtifactClass::ART_MINOR),
rollArtifact(EArtifactClass::ART_MINOR),
rollArtifact(EArtifactClass::ART_MINOR),
rollArtifact(EArtifactClass::ART_MAJOR)
};
}
vstd::RNG & GameRandomizer::getDefault()
@@ -314,7 +315,6 @@ SecondarySkill GameRandomizer::rollSecondarySkillForLevelup(const CGHeroInstance
int selectedIndex = RandomGeneratorUtil::nextItemWeighted(weights, heroRng.seed);
SecondarySkill selectedSkill = skills.at(selectedIndex);
//deterministic secondary skills
++heroRng.magicSchoolCounter;
++heroRng.wisdomCounter;

View File

@@ -18,18 +18,29 @@ enum class EGameSettings;
class CGHeroInstance;
class RandomizationBias
{
int32_t accumulatedBias = 0;
public:
/// Performs coin flip with specified success chance
/// Returns true with probability successChance percents, and false with probability totalWeight-successChance percents
bool roll(vstd::RNG & generator, int successChance, int totalWeight, int biasValue);
};
/// Biased randomizer that has following properties:
/// - at bias value of 0 it acts as statistical random generator, just like vstd::RNG
/// - at bias value of 100 it guarantees that it will take at most 100/chance rolls till succesfull roll
/// - at bias value between 1..99 similar guarantee is also provided, but with larger number of rolls
/// No matter what bias is, statistical probability on large number of rolls remains the same
/// Its goal is to simulate human expectations of random distributions and reduce frustration from "bad" rolls
class BiasedRandomizer
class RandomGeneratorWithBias
{
CRandomGenerator seed;
int32_t accumulatedBias = 0;
CRandomGenerator generator;
RandomizationBias bias;
public:
explicit BiasedRandomizer(int seed);
explicit RandomGeneratorWithBias(int seed);
/// Performs coin flip with specified success chance
/// Returns true with probability successChance percents, and false with probability 100-successChance percents
bool roll(int successChance, int totalWeight, int biasValue);
@@ -42,7 +53,7 @@ class DLL_LINKAGE GameRandomizer final : public IGameRandomizer
struct HeroSkillRandomizer
{
HeroSkillRandomizer(int seed)
explicit HeroSkillRandomizer(int seed)
: seed(seed)
{}
@@ -62,13 +73,14 @@ class DLL_LINKAGE GameRandomizer final : public IGameRandomizer
std::map<HeroTypeID, HeroSkillRandomizer> heroSkillSeed;
std::map<PlayerColor, CRandomGenerator> playerTavern;
std::map<ObjectInstanceID, BiasedRandomizer> goodMoraleSeed;
std::map<ObjectInstanceID, BiasedRandomizer> badMoraleSeed;
std::map<ObjectInstanceID, BiasedRandomizer> goodLuckSeed;
std::map<ObjectInstanceID, BiasedRandomizer> badLuckSeed;
std::map<ObjectInstanceID, BiasedRandomizer> combatAbilitySeed;
std::map<ObjectInstanceID, RandomGeneratorWithBias> goodMoraleSeed;
std::map<ObjectInstanceID, RandomGeneratorWithBias> badMoraleSeed;
std::map<ObjectInstanceID, RandomGeneratorWithBias> goodLuckSeed;
std::map<ObjectInstanceID, RandomGeneratorWithBias> badLuckSeed;
std::map<ObjectInstanceID, RandomGeneratorWithBias> combatAbilitySeed;
bool rollMoraleLuck(std::map<ObjectInstanceID, RandomGeneratorWithBias> & seeds, ObjectInstanceID actor, int moraleLuckValue, EGameSettings diceSize, EGameSettings diceWeights);
bool rollMoraleLuck(std::map<ObjectInstanceID, BiasedRandomizer> & seeds, ObjectInstanceID actor, int moraleLuckValue, EGameSettings diceSize, EGameSettings diceWeights);
public:
explicit GameRandomizer(const IGameInfoCallback & gameInfo);
~GameRandomizer();
@@ -83,8 +95,6 @@ public:
bool rollCombatAbility(ObjectInstanceID actor, int percentageChance);
// HeroTypeID rollHero(PlayerColor player, FactionID faction) override;
CreatureID rollCreature() override;
CreatureID rollCreature(int tier) override;

View File

@@ -117,6 +117,7 @@ public:
virtual bool isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero) = 0;
/// Returns global random generator. TODO: remove, replace with IGameRanndomizer as separate parameter to such methods
virtual vstd::RNG & getRandomGenerator() = 0;
};