Add PaletteSpec, PaletteSampler and BlockRegistry.palette

This commit is contained in:
piexel committed 2026-10-09 20:28:54 +02:00
1 parent 0f05676b7a
commit 435d3798fd
8 files changed
+167 -8

No files matched your search

@@ -24,6 +24,16 @@ public interface BlockRegistry {
*/
Optional<BlockEntityData> blockEntity(BlockSpec spec);
/** The picker for {@code spec}: every block counts as often as its weight, and the entry at a position is chosen by
* the style's noise at (x, y, z) divided by the zoom, so the palette changes smoothly over the world; a palette of
* one block always gives it. Blocks resolve as {@link #resolve(BlockSpec)}. Seed the noise like
* {@link com.volmit.noise.NoiseFactory#sampler}: from the world seed when the palette must line up across chunks.
* The host keeps what it builds for the pack snapshot, so asking again for the same palette and seed is cheap.
*
* @throws IllegalArgumentException when no block of the palette has a weight above zero, which includes an empty list
*/
PaletteSampler palette(PaletteSpec spec, long seed);
boolean hasTrait(BlockState state, BlockTrait trait);
boolean inTag(BlockState state, ResourceId tag);
/** Whether state can attach to the specified face of support, ignoring other neighbors. */
@@ -0,0 +1,11 @@
package com.volmit.block;
/** The block a {@link PaletteSpec} picks at a position, built by the host ({@link BlockRegistry#palette}). The
* coordinates are the noise's input, so they are the same kind the module's other noise uses: world X and Z, and Y as the
* module counts it, which is the height above the dimension's bottom unless the module documents otherwise. The same
* position always gives the same block. Safe for concurrent calls.
*/
@FunctionalInterface
public interface PaletteSampler {
BlockState at(double x, double y, double z);
}
@@ -0,0 +1,94 @@
package com.volmit.block;
import com.volmit.config.Desc;
import com.volmit.config.MinItems;
import com.volmit.config.MinNumber;
import com.volmit.config.Required;
import com.volmit.config.Snippet;
import com.volmit.noise.NoiseStyle;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/** A palette of blocks as packs write it in settings, such as
* {@code {"style": "SIMPLEX", "zoom": 8, "palette": [{"block": "stone"}, {"block": "andesite", "weight": 2}]}}, or a
* snippet under {@code snippet/palette/}. The blocks are picked by noise: the host picks the entry the style's noise
* at the position over {@code zoom} selects, each block counting {@link BlockSpec#weight() weight} times.
*
* <p>A palette is a description, not a picker: the host builds the picker
* ({@link BlockRegistry#palette(PaletteSpec, long)}). It is a value the host creates with the no-argument
* constructor and reads the JSON into; nothing changes it afterwards.
*/
@Snippet("palette")
@Desc("A palette of materials")
public final class PaletteSpec {
private static final String DEFAULT_STYLE = "STATIC";
private static final double DEFAULT_ZOOM = 5;
private static final String DEFAULT_BLOCK = "stone";
@Desc("The style of noise")
private NoiseStyle style = NoiseStyle.of(DEFAULT_STYLE);
@MinNumber(0.0001)
@Desc("The terrain zoom mostly for zooming in on a wispy palette")
private double zoom = DEFAULT_ZOOM;
@Required
@MinItems(1)
@Desc("The palette of blocks to be used in this layer")
private List<BlockSpec> palette = List.of(BlockSpec.of(DEFAULT_BLOCK));
private PaletteSpec() {
}
private PaletteSpec(NoiseStyle style, double zoom, List<BlockSpec> palette) {
this.style = Objects.requireNonNull(style, "style");
this.zoom = zoom;
this.palette = List.copyOf(palette);
}
/** A palette of {@code blocks} with the default style and zoom. */
public static PaletteSpec of(List<BlockSpec> blocks) {
return new PaletteSpec(NoiseStyle.of(DEFAULT_STYLE), DEFAULT_ZOOM, blocks);
}
public PaletteSpec withStyle(NoiseStyle style) {
return new PaletteSpec(style, zoom, palette());
}
/** This palette with zoom {@code zoom}: above 1 stretches the noise that picks the blocks, below 1 squeezes it. */
public PaletteSpec withZoom(double zoom) {
return new PaletteSpec(style(), zoom, palette());
}
/** The noise that picks the blocks; the default {@code STATIC} style where the pack wrote none. */
public NoiseStyle style() {
return style == null ? NoiseStyle.of(DEFAULT_STYLE) : style;
}
/** The factor the position is divided by before it is looked up in the noise. */
public double zoom() {
return zoom;
}
/** The blocks to pick from, as the pack wrote them. */
public List<BlockSpec> palette() {
return palette == null ? List.of() : Collections.unmodifiableList(palette);
}
@Override
public boolean equals(Object other) {
return other instanceof PaletteSpec spec && style().equals(spec.style())
&& Double.compare(zoom, spec.zoom) == 0 && palette().equals(spec.palette());
}
@Override
public int hashCode() {
return Objects.hash(style(), zoom, palette());
}
@Override
public String toString() {
return "PaletteSpec[" + palette() + ", " + style() + ", zoom " + zoom + "]";
}
}
@@ -1,6 +1,8 @@
package com.volmit;
import com.volmit.block.BlockSpec;
import com.volmit.block.PaletteSpec;
import com.volmit.config.OptionsMetadata;
import com.volmit.config.Range;
import com.volmit.noise.NoiseFactory;
import com.volmit.noise.NoiseSampler;
@@ -53,6 +55,23 @@ class SettingsValuesTest {
assertEquals(Optional.empty(), simplex.fracture());
}
@Test
void palettesAreValuesWhoseDefaultsAreThoseOfIrisMaterialPalettes() {
var options = OptionsMetadata.inspect(PaletteSpec.class);
var palette = PaletteSpec.of(List.of(BlockSpec.of("stone"), BlockSpec.of("andesite").withWeight(2)));
assertEquals(List.of("style", "zoom", "palette"), options.options().stream().map(OptionsMetadata.Option::name).toList());
assertEquals("palette", options.snippet());
assertEquals(1, options.options().get(2).minItems());
assertEquals(0.0001, options.options().get(1).minimum());
assertEquals(NoiseStyle.of("STATIC"), palette.style());
assertEquals(5, palette.zoom());
assertEquals(palette.withZoom(8).withStyle(NoiseStyle.of("SIMPLEX")), palette.withStyle(NoiseStyle.of("SIMPLEX")).withZoom(8));
assertNotEquals(palette, palette.withZoom(8));
assertEquals(palette.hashCode(), PaletteSpec.of(palette.palette()).hashCode());
assertThrows(UnsupportedOperationException.class, () -> palette.palette().clear());
}
@Test
void styledRangesPickBetweenTheirBoundsWithTheStylesNoise() {
var asked = new ArrayList<String>();
@@ -421,6 +421,7 @@ class PackSchemaTest {
import com.volmit.FeatureAttachment.Target;
import com.volmit.IrisFeature;
import com.volmit.block.BlockSpec;
import com.volmit.block.PaletteSpec;
import com.volmit.config.Feature;
import com.volmit.config.Range;
import com.volmit.noise.NoiseStyle;
@@ -432,6 +433,7 @@ class PackSchemaTest {
public final class FloraSettings implements IrisFeature {
private List<BlockSpec> palette = List.of(BlockSpec.of("poppy"));
private NoiseStyle style = NoiseStyle.of("SIMPLEX").withZoom(4);
private PaletteSpec ground = PaletteSpec.of(List.of(BlockSpec.of("grass_block")));
private StyledRange height = StyledRange.of(2, 5, NoiseStyle.of("SIMPLEX"));
private Range light = Range.of(0, 15);
private BlockPosition offset = new BlockPosition(1, 2, 3);
@@ -446,6 +448,7 @@ class PackSchemaTest {
assertEquals(Optional.of(List.of(Map.of("block", "poppy", "data", Map.of(), "tileData", Map.of(), "weight", 1L))),
option(settings, "palette").defaultValue());
assertEquals(new ObjectKind("com.volmit.noise.NoiseStyle"), option(settings, "style").kind());
assertEquals(new ObjectKind("com.volmit.block.PaletteSpec"), option(settings, "ground").kind());
assertEquals(new ObjectKind("com.volmit.noise.StyledRange"), option(settings, "height").kind());
assertEquals(new ObjectKind("com.volmit.config.Range"), option(settings, "light").kind());
assertEquals(Optional.of(Map.of("max", 15.0, "min", 0.0)), option(settings, "light").defaultValue());
@@ -461,13 +464,22 @@ class PackSchemaTest {
assertEquals(new ObjectKind("com.volmit.block.BlockSpec"), option(block, "backup").kind());
assertEquals(Optional.of("air"), option(block, "block").defaultValue());
var ground = schema.type("com.volmit.block.PaletteSpec");
assertEquals(Optional.of("palette"), ground.snippet());
assertEquals(List.of("style", "zoom", "palette"), ground.options().stream().map(SchemaOption::name).toList());
assertEquals(new ListKind(new ObjectKind("com.volmit.block.BlockSpec"), false), option(ground, "palette").kind());
assertTrue(option(ground, "palette").required());
assertEquals(OptionalInt.of(1), option(ground, "palette").minItems());
assertEquals(OptionalDouble.of(0.0001), option(ground, "zoom").minimum());
assertEquals(Optional.of(5.0), option(ground, "zoom").defaultValue());
var style = schema.type("com.volmit.noise.NoiseStyle");
assertEquals(Optional.of("style"), style.snippet(), "API classes keep the snippet folders packs already use");
assertEquals(new ReferenceKind(Reference.NOISE_STYLE, TextFormat.PLAIN), option(style, "style").kind());
assertEquals(new ReferenceKind(Reference.PACK_EXPRESSION, TextFormat.PLAIN), option(style, "expression").kind());
assertEquals(new ReferenceKind(Reference.PACK_IMAGE_MAP, TextFormat.PLAIN), option(style, "imageMap").kind());
assertEquals(Optional.of("FLAT"), option(style, "style").defaultValue());
assertEquals(List.of("range", "style", "style-range"), schema.snippets().stream().map(type -> type.snippet().orElseThrow()).toList());
assertEquals(List.of("palette", "range", "style", "style-range"), schema.snippets().stream().map(type -> type.snippet().orElseThrow()).toList());
}
@Test