diff --git a/api/api/src/main/java/com/volmit/world/Weighted.java b/api/api/src/main/java/com/volmit/world/Weighted.java new file mode 100644 index 000000000..ce0d70234 --- /dev/null +++ b/api/api/src/main/java/com/volmit/world/Weighted.java @@ -0,0 +1,59 @@ +package com.volmit.world; + +import java.util.List; +import java.util.Objects; + +/** A list entry that counts as {@code weight} copies of itself in a row, as biome, region and child lists write it. + * {@link #pick} walks such a list with a noise from 0 to 1, so a module that picks among a pack's weighted lists + * (a region's biomes, a biome's children) lands on the same entry the host does. + */ +public record Weighted(T value, int weight) { + public static final int MIN_WEIGHT = 1; + public static final int MAX_WEIGHT = 1000; + + /** Which entry gets noise lying exactly on the edge between two. Cellular noise sits on such edges over whole cells, + * so this decides real patches of terrain. A region's biome list gives edges to the earlier entry and children to the + * later one. + */ + public enum Edge { + EARLIER, + LATER + } + + public Weighted { + Objects.requireNonNull(value, "value"); + if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) { + throw new IllegalArgumentException("Weight must be from " + MIN_WEIGHT + " to " + MAX_WEIGHT + ": " + weight); + } + } + + /** Picks along the list as if every entry were written out {@code weight} times: the noise from 0 to 1 is split into + * equal slots, one per copy, in list order. Noise below 0 picks the first entry, noise above 1 the last; an empty list + * picks nothing. + */ + public static T pick(List> entries, double noise, Edge edge) { + if (entries.isEmpty()) { + return null; + } + + double slotPosition = noise * totalWeight(entries); + int slotsUpToHere = 0; + for (Weighted entry : entries) { + slotsUpToHere += entry.weight(); + boolean inThisEntry = edge == Edge.EARLIER ? slotPosition <= slotsUpToHere : slotPosition < slotsUpToHere; + if (inThisEntry) { + return entry.value(); + } + } + + return entries.getLast().value(); + } + + public static int totalWeight(List> entries) { + int total = 0; + for (Weighted entry : entries) { + total += entry.weight(); + } + return total; + } +} diff --git a/api/host/src/test/java/com/volmit/WeightedTest.java b/api/host/src/test/java/com/volmit/WeightedTest.java new file mode 100644 index 000000000..a6f26b172 --- /dev/null +++ b/api/host/src/test/java/com/volmit/WeightedTest.java @@ -0,0 +1,45 @@ +package com.volmit; + +import com.volmit.world.Weighted; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class WeightedTest { + @Test + void weightedEntriesCountAsCopiesOfThemselvesInARow() { + assertThrows(IllegalArgumentException.class, () -> new Weighted<>("plains", 0)); + assertThrows(IllegalArgumentException.class, () -> new Weighted<>("plains", Weighted.MAX_WEIGHT + 1)); + assertThrows(NullPointerException.class, () -> new Weighted(null, 1)); + + var entries = List.of(new Weighted<>("barren", 3), new Weighted<>("forest", 1)); + assertEquals("barren", Weighted.pick(entries, 0.74, Weighted.Edge.EARLIER)); + assertEquals("forest", Weighted.pick(entries, 0.76, Weighted.Edge.EARLIER)); + assertEquals("barren", Weighted.pick(entries, -0.5, Weighted.Edge.EARLIER)); + assertEquals("forest", Weighted.pick(entries, 1.5, Weighted.Edge.LATER)); + assertNull(Weighted.pick(List.>of(), 0.5, Weighted.Edge.EARLIER)); + } + + @Test + void anEdgeGoesWhereTheRuleSays() { + var entries = List.of(new Weighted<>("first", 1), new Weighted<>("second", 1)); + assertEquals("first", Weighted.pick(entries, 0.5, Weighted.Edge.EARLIER)); + assertEquals("second", Weighted.pick(entries, 0.5, Weighted.Edge.LATER)); + assertEquals("second", Weighted.pick(entries, Math.nextUp(0.5), Weighted.Edge.EARLIER)); + } + + @Test + void halfSlotWeightsMatchTheOldRoundedChildPick() { + var oldSlots = List.of("barren", "barren", "barren", "forest", "barren", "barren"); + var converted = List.of(new Weighted<>("barren", 5), new Weighted<>("forest", 2), new Weighted<>("barren", 3)); + for (int step = 0; step <= 1000; step++) { + double noise = step / 1000D; + String oldPick = oldSlots.get((int) Math.round(noise * (oldSlots.size() - 1))); + assertEquals(oldPick, Weighted.pick(converted, noise, Weighted.Edge.LATER), "noise " + noise); + } + } +}