diff --git a/gameserver/src/main/java/brainwine/gameserver/util/WeightedMap.java b/gameserver/src/main/java/brainwine/gameserver/util/WeightedMap.java index 4711410..e55655c 100644 --- a/gameserver/src/main/java/brainwine/gameserver/util/WeightedMap.java +++ b/gameserver/src/main/java/brainwine/gameserver/util/WeightedMap.java @@ -14,8 +14,8 @@ import io.netty.util.internal.ThreadLocalRandom; public class WeightedMap { @JsonValue - private final Map map = new HashMap<>(); - private int totalWeight; + private final Map map = new HashMap<>(); + private double totalWeight; public WeightedMap() {} @@ -27,7 +27,7 @@ public class WeightedMap { } @JsonCreator - public WeightedMap(Map map) { + public WeightedMap(Map map) { map.forEach((entry, weight) -> { addEntry(entry, weight); }); @@ -37,7 +37,7 @@ public class WeightedMap { return addEntry(entry, 1); } - public WeightedMap addEntry(T entry, int weight) { + public WeightedMap addEntry(T entry, double weight) { if(weight > 0 && entry != null) { map.put(entry, weight); totalWeight += weight; @@ -64,10 +64,10 @@ public class WeightedMap { public T next(Random random, T def) { if(!map.isEmpty()) { - int rolled = random.nextInt(totalWeight); + double rolled = random.nextDouble() * totalWeight; - for(Entry entry : map.entrySet()) { - int weight = entry.getValue(); + for(Entry entry : map.entrySet()) { + double weight = entry.getValue(); if(rolled < weight) { return entry.getKey();