diff --git a/CONTRACTS.md b/CONTRACTS.md index 7ee4dd6e3..2c1a4f322 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -127,7 +127,7 @@ running Iris is the pack publisher's responsibility. every chunk within that stage's reach plus the reach of every later stage of a chunk, each chunk once, before the next stage and before that chunk generates, with the mantle widened by the stage's reach. Give chunk stages that chunk's mantle; `ChunkRunner` makes it read-only. Give chunk stages a `TerrainSampler` over the - chunk and the 16 blocks around it. + chunk and the 16 blocks around it, and mantle stages one over the chunk and the larger of their reach and 16 blocks. - Serve `Mantle.blocks()` in every invocation: the blocks placements wrote into the mantle, read-only, none where the dimension keeps no mantle or the chunk generates without it. A block a stage reads and writes back unchanged keeps the host's own identity of it (a custom block of a content plugin stays one). @@ -207,6 +207,13 @@ 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 `NoiseSampler.scaled(factor)` by building the generator again with the factor folded into its scale, leaving the sampler it + is called on untouched (the default multiplies the coordinates, which may round differently). +- Answer `PackWorld.minHeight()` (the platform's Y of the dimension's bottom), `PackResources.idOf(key)` (the ids the host gives + its own resources, characters an id cannot hold replaced) and `TerrainSampler.caveBiome(x, y, z)` (the cave biome of a block, + without the work of the rest of the sample) when the defaults do not fit the host. - 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 f7104797a..e16ab5db3 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -502,6 +502,8 @@ that `column` works out). The sampler also gives the dimension's above it. The mantle's `blocks()` are the blocks placements wrote into the mantle for chunks to take over (objects, structures): a chunk stage copies them into its chunk, as Iris's `iris:matter` does. They are the host's layer, so they need no registration, and stages only read them; placements keep writing blocks through `PlacementAccess.set`. +A mantle stage's `TerrainSampler` covers the same margin (at least 16 blocks, more when its reach is larger): a carver blends +the profiles of the columns around its chunk, so it reads their heights and biomes though it writes only the mantle. `ChunkRunner.generate(inputs, seed, timings)` reports how long each stage that ran took, so the host times modules instead of modules timing themselves. @@ -800,6 +802,25 @@ 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.scaled(factor)`** is a style with an extra coordinate scale that the host folds into its generator: a cave + biome band splits into child bands with noise shrunk by a factor, and a module that multiplied the coordinates itself would + round differently from the generator Iris has always built, moving a cell border here and there. A default method + (multiply the coordinates) keeps every other sampler valid. +- **`PackResources.idOf(packKey)`**: a pack names its biomes, regions and objects by key (`desert/dunes`) in its own settings; the + host's ids for them hold the pack's namespace and replace characters an id cannot hold. A module that turns a key into an id would + have to know that rule, so the resources say it; a default method (namespace and key) serves a host without such characters. +- **`TerrainSampler.caveBiome(x, y, z)`** is `biome(...).caveBiome()` without the region and the surface biome: a cave wall asks for the + cave biome around it five times per block, and the other two lookups were most of the cost. A default method keeps every sampler valid. +- **`ReadOnlyMantle.host()`** hands a host's own extension back the mantle behind a chunk stage's read-only view. A chunk stage may not + write the mantle, but Iris's cave stage stamps markers into the matter that the platform reads, through a built-in facade, so the + facade has to reach past the view. Not for pack modules: writing through it breaks the promise the view makes. +- **`PackWorld.minHeight()`**: all heights are above the dimension's bottom, but packs write some settings as absolute + world Y (cave biome bands, floating island altitudes, structure bands). Those are converted by the module with the + platform's Y of the bottom, a default method (0) so a fixture without negative Y needs nothing. +- **`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/README.md b/README.md index b790929a6..20f937c0e 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,9 @@ X and Z are world coordinates. Y is always the height above the dimension's bott of the dimension, whatever its minimum height is in the platform, so Y is never negative. This holds for every API value that carries a height: terrain and biome samples, `BlockBox` bounds of the terrain sampler, block views, placement edits and the mantle, hunks, block palettes and noise samplers. The host -converts to and from the platform's own Y at its boundary; modules never see it. +converts to and from the platform's own Y at its boundary. The one place a module meets the platform's Y is a +setting that names an absolute world Y, as packs write them (a cave biome band between Y -64 and 320): it converts +with `world.minHeight()`, the platform's Y of the dimension's bottom. ## Random streams @@ -123,7 +125,7 @@ graph.define(APPLY).run(CaveModule::apply); // reads its own chunk's CAVES and A chunk stage writes only its own chunk but may look around it: `ctx.terrain()` covers the chunk and the 16 blocks around it, gives the dimension's `fluidHeight()` and, per column, `upperDimensionY(x, z)` where a dimension stacked above begins (write nothing there). `surfaceY(x, z)` is a column's height alone, cheaper than `column(x, z)` when a stage needs -no slope. `ctx.mantle().blocks()` holds the blocks placements wrote into the mantle for the chunks to +no slope. A mantle stage's `ctx.terrain()` covers the chunk and the larger of its reach and 16 blocks around it. `ctx.mantle().blocks()` holds the blocks placements wrote into the mantle for the chunks to take over, such as objects and structures; a chunk stage copies them in (Iris's `iris:matter`). Stages only read them. ```java @@ -173,7 +175,7 @@ public void register(FeatureRegistry registry) { } // anywhere the pack's resources are at hand, such as a stage or a validator -Keep keep = resources.require(KEEPS.key(new ResourceId(resources.packId().namespace(), "towers/north"))); +Keep keep = resources.require(KEEPS.key(resources.idOf("towers/north"))); List all = resources.ids(Keep.class); // every keep of the pack, in id order ``` @@ -549,6 +551,10 @@ by noise (each block counts as often as its weight, the style's noise at x, y, z one block is that block everywhere, and one without a block to pick (an empty list, or only weights of zero) is an `IllegalArgumentException` when the sampler is asked for. Seed it like noise. +`NoiseSampler.signed(...)` is `sample * 2 - 1`, which a host may answer from a per-thread memo of the coordinates it was just asked (read it +where one field is sampled at the same points again and again, as a carver does). `scaled(factor)` is the same noise with the coordinates +multiplied by `factor`, folded into the host's generator, not a multiplication in front of it. + The types are values the host creates with their no-argument constructors and reads the JSON into, like settings, so an omitted option keeps the default the type declares (a `Range` is 16 to 32 unless the pack or the field's own default says otherwise). They keep the snippet folders packs already use (`style`, `style-range`, `range`, `palette`). diff --git a/src/main/java/com/volmit/generation/MantleGenerationContext.java b/src/main/java/com/volmit/generation/MantleGenerationContext.java index fc27bd6d3..9024981b1 100644 --- a/src/main/java/com/volmit/generation/MantleGenerationContext.java +++ b/src/main/java/com/volmit/generation/MantleGenerationContext.java @@ -12,6 +12,10 @@ public interface MantleGenerationContext extends InvocationContext { /** The chunk's first block along Z, in world coordinates. */ int originZ(); + /** The terrain of the chunk and of the larger of the stage's reach and 16 blocks around it on every side, at full + * height: a mantle stage writes only the mantle, but may look at the terrain around its chunk, such as the heights + * a cave blends over its borders. + */ TerrainSampler terrain(); FeatureSettings settings(); diff --git a/src/main/java/com/volmit/host/ReadOnlyMantle.java b/src/main/java/com/volmit/host/ReadOnlyMantle.java index 577e41c60..191790cc3 100644 --- a/src/main/java/com/volmit/host/ReadOnlyMantle.java +++ b/src/main/java/com/volmit/host/ReadOnlyMantle.java @@ -11,7 +11,7 @@ import com.volmit.mantle.MantleVisitor; /** The mantle as chunk stages see it: reads pass through, every write throws. The mantle pipeline has * finished around the chunk by then, and its neighbors may already be generated. */ -final class ReadOnlyMantle implements Mantle { +public final class ReadOnlyMantle implements Mantle { private static final String READ_ONLY = "Chunk stages read the mantle but do not write it;" + " write it from a mantle stage (Stage.mantle), which runs before the chunks it reaches"; @@ -21,6 +21,13 @@ final class ReadOnlyMantle implements Mantle { this.mantle = mantle; } + /** The host's mantle behind this view: for a host's own extensions of {@link Mantle}, such as a built-in facade that + * reaches the platform's storage. A module that writes through it breaks the promise this view makes to chunk stages. + */ + public Mantle host() { + return mantle; + } + @Override public BlockBox bounds() { return mantle.bounds(); diff --git a/src/main/java/com/volmit/noise/NoiseSampler.java b/src/main/java/com/volmit/noise/NoiseSampler.java index 463c63cd0..25ae9ddce 100644 --- a/src/main/java/com/volmit/noise/NoiseSampler.java +++ b/src/main/java/com/volmit/noise/NoiseSampler.java @@ -9,4 +9,48 @@ 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; + } + + /** This noise with its coordinates multiplied by {@code factor}: a factor above 1 squeezes the pattern, below 1 + * stretches it. A host folds the factor into the generator's own scale, so the result is the very noise the style + * would give with that extra scale (same values and float rounding), and builds it without touching this sampler. + * The default multiplies the coordinates before sampling, which may round differently. + */ + default NoiseSampler scaled(double factor) { + NoiseSampler source = this; + return new NoiseSampler() { + @Override + public double sample(double worldX, double worldZ) { + return source.sample(worldX * factor, worldZ * factor); + } + + @Override + public double sample(double worldX, double y, double worldZ) { + return source.sample(worldX * factor, y * factor, worldZ * factor); + } + + @Override + public double signed(double worldX, double worldZ) { + return source.signed(worldX * factor, worldZ * factor); + } + + @Override + public double signed(double worldX, double y, double worldZ) { + return source.signed(worldX * factor, y * factor, worldZ * factor); + } + }; + } } diff --git a/src/main/java/com/volmit/resource/PackResources.java b/src/main/java/com/volmit/resource/PackResources.java index 5795a3eb6..6d94b5265 100644 --- a/src/main/java/com/volmit/resource/PackResources.java +++ b/src/main/java/com/volmit/resource/PackResources.java @@ -28,6 +28,15 @@ public interface PackResources { /** Builds the noise of the {@link com.volmit.noise.NoiseStyle}s the pack's settings describe. */ NoiseFactory noise(); + /** The id the pack's resources have for the one the pack's own files name by {@code packKey}, such as {@code desert/dunes} + * (a biome's key in a region, a settings field naming a biome): {@code overworld:desert/dunes} in the pack + * {@code overworld}. Characters an id cannot hold are replaced the way the host does for its own ids, so use this and + * not {@code new ResourceId(packId().namespace(), packKey)} for a key that may hold them. + */ + default ResourceId idOf(String packKey) { + return new ResourceId(packId().namespace(), packKey); + } + Optional find(ResourceKey key); /** The ids of every resource of the kind {@code type} selects, in id order. A type the host does not serve diff --git a/src/main/java/com/volmit/world/PackWorld.java b/src/main/java/com/volmit/world/PackWorld.java index a944650c1..93f5431f5 100644 --- a/src/main/java/com/volmit/world/PackWorld.java +++ b/src/main/java/com/volmit/world/PackWorld.java @@ -26,6 +26,14 @@ public interface PackWorld { /** The settings of the world's dimension and of every region and biome it can reach, as mantle reaches see them. */ PackSettings settings(); + /** The platform's Y of the dimension's bottom (-64 in the overworld). Every height the API carries is above that + * bottom; settings that name an absolute world Y, as packs write them for the platform, are converted with it: + * {@code heightAboveBottom = worldY - minHeight()}. Zero unless the host's dimensions begin below Y 0. + */ + default int minHeight() { + return 0; + } + /** * The world's one {@code type} instance, created by {@code create} the first time anyone asks for it. Modules keep * their caches and per-world workers here instead of in fields, since stage bodies and services are shared by every diff --git a/src/main/java/com/volmit/world/TerrainSampler.java b/src/main/java/com/volmit/world/TerrainSampler.java index 65d129fcd..4818853ff 100644 --- a/src/main/java/com/volmit/world/TerrainSampler.java +++ b/src/main/java/com/volmit/world/TerrainSampler.java @@ -1,6 +1,8 @@ package com.volmit.world; import com.volmit.mantle.BlockBox; +import com.volmit.resource.ResourceId; +import java.util.Optional; import java.util.OptionalInt; /** Read-only host sampling for the same pack snapshot as the invocation. @@ -20,6 +22,14 @@ public interface TerrainSampler { } BiomeSample biome(int worldX, int y, int worldZ); + /** The {@link BiomeSample#caveBiome()} alone, for a module that asks it block by block and needs neither the region + * nor the surface biome: the host may answer it without working out the rest of {@link #biome}. Outside the bounds it + * throws like {@link #biome}. + */ + default Optional caveBiome(int worldX, int y, int worldZ) { + return biome(worldX, y, worldZ).caveBiome(); + } + /** The height up to which the host fills the world with fluid wherever the terrain lies lower, the sea level of the * dimension: a column's {@link TerrainSample#fluidY()} is this height when its surface is below it. */ 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..a1eddb856 --- /dev/null +++ b/src/test/java/com/volmit/noise/NoiseSamplerTest.java @@ -0,0 +1,50 @@ +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); + } + + @Test + void scaledSamplesAtTheScaledCoordinates() { + NoiseSampler noise = new NoiseSampler() { + public double sample(double worldX, double worldZ) { return worldX + worldZ * 100D; } + public double sample(double worldX, double y, double worldZ) { return worldX + y * 100D + worldZ * 10000D; } + }; + + NoiseSampler scaled = noise.scaled(2D); + + assertEquals(noise.sample(6D, 4D), scaled.sample(3D, 2D), EXACT); + assertEquals(noise.sample(6D, 4D, 2D), scaled.sample(3D, 2D, 1D), EXACT); + assertEquals(noise.signed(6D, 4D), scaled.signed(3D, 2D), EXACT); + assertEquals(noise.signed(6D, 4D, 2D), scaled.signed(3D, 2D, 1D), EXACT); + } +}