Fixed chest tier chance rolling

This commit is contained in:
Indyuce 2022-04-25 21:09:01 +02:00
parent 9a315e9431
commit 93377b6027
2 changed files with 17 additions and 9 deletions

View File

@ -10,8 +10,7 @@ public class ChestTier {
private final TierEffect effect; private final TierEffect effect;
private final ScalingFormula capacity; private final ScalingFormula capacity;
private final DropTable table; private final DropTable table;
private final double chance;
public final double chance;
public ChestTier(ConfigurationSection config) { public ChestTier(ConfigurationSection config) {
effect = config.isConfigurationSection("effect") ? new TierEffect(config.getConfigurationSection("effect")) : null; effect = config.isConfigurationSection("effect") ? new TierEffect(config.getConfigurationSection("effect")) : null;
@ -24,6 +23,10 @@ public class ChestTier {
return capacity.calculate(player.getLevel()); return capacity.calculate(player.getLevel());
} }
public double getChance() {
return chance;
}
public DropTable getDropTable() { public DropTable getDropTable() {
return table; return table;
} }

View File

@ -119,14 +119,19 @@ public class LootChestRegion {
// TODO stat to increase chance to get higher tiers? // TODO stat to increase chance to get higher tiers?
public ChestTier rollTier() { public ChestTier rollTier() {
double s = 0; // Calculate sum of all chances and then normalize
for (ChestTier tier : tiers) { double norm = 0;
if (random.nextDouble() < tier.chance / (1 - s)) for (ChestTier tier : tiers)
return tier; norm += tier.getChance();
s += tier.chance;
}
return tiers.stream().findAny().orElse(null); Validate.isTrue(norm > 0, "No tier was found");
double sum = 0;
for (ChestTier tier : tiers)
if (random.nextDouble() < (sum += tier.getChance()) / norm)
return tier;
throw new RuntimeException("Could not roll random chest tier");
} }
public Location getRandomLocation(Location center) { public Location getRandomLocation(Location center) {