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

View File

@ -119,14 +119,19 @@ public class LootChestRegion {
// TODO stat to increase chance to get higher tiers?
public ChestTier rollTier() {
double s = 0;
for (ChestTier tier : tiers) {
if (random.nextDouble() < tier.chance / (1 - s))
return tier;
s += tier.chance;
}
// Calculate sum of all chances and then normalize
double norm = 0;
for (ChestTier tier : tiers)
norm += tier.getChance();
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) {