diff --git a/CONTRACTS.md b/CONTRACTS.md index d86722509..52d71e780 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -207,6 +207,8 @@ running Iris is the pack publisher's responsibility. - Serve `PackResources.noise()`: build each style's noise from the host's generators, expressions and image maps, the same values for the same style and seed, cached per pack snapshot; an unknown style name is flat, a missing expression falls back to the base style, and a missing image map fails. + Answer `NoiseSampler.signed(...)` with the generator's own signed read (and its per-thread coordinate memo) when it + has one; the default is `sample * 2 - 1`. - Answer `BlockRegistry.hasTrait` (`VINE` included) and `canPlaceOn` (the support rule decorations need) as the platform does; stages ask per block, so answer from memory once a state was seen. - Resolve `BlockSpec`s in `BlockRegistry` as packs expect (pack block, platform block, backup, air) and turn their diff --git a/DESIGN.md b/DESIGN.md index b4478d4bd..04b1cf241 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -798,6 +798,10 @@ Why these lines: the module's: noise lines up across chunks only when it comes from `worldSeed()`, not a stage's stream. - **`NoiseSampler` moved to `com.volmit.noise`** and gained a two-dimensional `sample(x, z)`: Iris's noise gives different fields in two and three dimensions, and most features sample columns. +- **`NoiseSampler.signed(...)`** is the sample mapped to -1..1 that a host may answer from a per-thread memo of the + coordinates it was just asked: a carver adds several fields and tests the sum around zero, and reads the same points + of one field again and again, so the memo is the difference between caves that cost what they always did and caves that + do not. A default method (`sample * 2 - 1`) keeps every other sampler valid. - **Style names are text with `@Ref(NOISE_STYLE)`**, not an enum: the host owns the list, and an API enum would pin it. An unknown name samples flat, as Iris has always read one. - **`isFlat()` is stricter than Iris's**: a flat base style with an expression or image map is not flat. diff --git a/src/main/java/com/volmit/noise/NoiseSampler.java b/src/main/java/com/volmit/noise/NoiseSampler.java index 463c63cd0..37b3b01da 100644 --- a/src/main/java/com/volmit/noise/NoiseSampler.java +++ b/src/main/java/com/volmit/noise/NoiseSampler.java @@ -9,4 +9,18 @@ public interface NoiseSampler { double sample(double worldX, double worldZ); double sample(double worldX, double y, double worldZ); + + /** {@link #sample(double, double)} mapped from 0..1 to -1..1 ({@code sample * 2 - 1}), for noise that is added up + * and compared around zero. A host may answer a repeated read of the same coordinates from a per-thread memo, so a + * caller that samples the same field at the same points again and again (a carver testing densities) should read + * this instead of mapping {@link #sample(double, double)} itself. + */ + default double signed(double worldX, double worldZ) { + return sample(worldX, worldZ) * 2D - 1D; + } + + /** The three-dimensional counterpart of {@link #signed(double, double)}. */ + default double signed(double worldX, double y, double worldZ) { + return sample(worldX, y, worldZ) * 2D - 1D; + } } diff --git a/src/test/java/com/volmit/noise/NoiseSamplerTest.java b/src/test/java/com/volmit/noise/NoiseSamplerTest.java new file mode 100644 index 000000000..ad71f2b65 --- /dev/null +++ b/src/test/java/com/volmit/noise/NoiseSamplerTest.java @@ -0,0 +1,35 @@ +package com.volmit.noise; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NoiseSamplerTest { + private static final double EXACT = 0D; + + @Test + void signedMapsTheUnitSampleToMinusOneAndOne() { + NoiseSampler noise = new NoiseSampler() { + public double sample(double worldX, double worldZ) { return worldX / 10D; } + public double sample(double worldX, double y, double worldZ) { return y / 10D; } + }; + + assertEquals(-1D, noise.signed(0D, 5D), EXACT); + assertEquals(1D, noise.signed(10D, 5D), EXACT); + assertEquals(0.5D * 2D - 1D, noise.signed(5D, 0D), EXACT); + assertEquals(-1D, noise.signed(3D, 0D, 3D), EXACT); + assertEquals(0.3D * 2D - 1D, noise.signed(3D, 3D, 3D), EXACT); + } + + @Test + void aHostMayAnswerSignedItself() { + NoiseSampler noise = new NoiseSampler() { + public double sample(double worldX, double worldZ) { return 0.25D; } + public double sample(double worldX, double y, double worldZ) { return 0.25D; } + @Override public double signed(double worldX, double worldZ) { return 7D; } + }; + + assertEquals(7D, noise.signed(1D, 1D), EXACT); + assertEquals(0.25D * 2D - 1D, noise.signed(1D, 1D, 1D), EXACT); + } +}