mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Add a weighted list pick to the API
Biome, region and child lists are about to count entries by weight. The pick walks such a list with a noise from 0 to 1 and takes an edge rule, so a module that picks from a pack's lists lands on the entry the host does. v3: 96beb86e0
This commit is contained in:
1 parent
f3bf0aea4b
commit
d705dee379
2 files changed
+104
No files matched your search
@@ -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<String>(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.<Weighted<String>>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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user