From fa7c3a0abfdce2ea131697264fc9917b4961c6c4 Mon Sep 17 00:00:00 2001 From: piexel Date: Fri, 6 Nov 2026 11:39:35 +0100 Subject: [PATCH] Draw object chunk random, density and heightmap from API types The chunk random is seeded from a purpose seed and the chunk, the density from StyledRange over the API noise factory, and VirtualHeightmap builds the virtual heightmap from NoiseSampler. LegacyStreams and ObjectPlacement.hostForm, which only served Iris's placement form, are removed. The chunk random and density noise now differ from the old streams; heightmaps are unchanged. --- README.md | 17 ++- .../arcane/iris/builtin/IrisSeedPurposes.java | 4 + .../arcane/iris/builtin/LegacyStreams.java | 38 ------ .../iris/builtin/objects/ObjectPlacement.java | 17 --- .../art/arcane/iris/builtin/package-info.java | 5 +- .../mantle/components/ChunkObjectPlacing.java | 6 +- .../components/FloatingObjectPlacing.java | 6 +- .../mantle/components/ObjectPlacing.java | 2 +- .../mantle/components/ObjectWarmUp.java | 12 +- .../iris/modules/objects/ObjectPool.java | 36 ++++-- .../modules/placement/RunningPlacement.java | 10 +- .../modules/placement/VirtualHeightmap.java | 111 ++++++++++++++++++ 12 files changed, 163 insertions(+), 101 deletions(-) delete mode 100644 src/main/java/art/arcane/iris/builtin/LegacyStreams.java create mode 100644 src/main/java/art/arcane/iris/modules/placement/VirtualHeightmap.java diff --git a/README.md b/README.md index b97b0d1e2..7cb99f033 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,8 @@ An object placement is `iris:objects` settings (`objects.ObjectPlacement`, under settings inside it, such as its clamp, stilt, vacuum and edit settings, its slope condition or a procedural coral, are module-owned classes too (`art.arcane.iris.builtin.validate`, `.stilt`, `.vacuum`, `.blocks`, `.prepare`, `.carve`, `.floating`, `.objects.*`), so studio describes them from the module's own annotations. Iris reads a placement's module -settings (the keys pack modules add at `Level.PLACEMENT`) from the JSON the placement was written as (`json()`), and keeps -what it makes of the placement in `hostForm()`; a placement made in code uses `ObjectPlacement.builder()` or -`toBuilder()`. Packs write the same JSON as before. Behaviour +settings (the keys pack modules add at `Level.PLACEMENT`) from the JSON the placement was written as (`json()`); a +placement made in code uses `ObjectPlacement.builder()` or `toBuilder()`. Packs write the same JSON as before. Behaviour that needs Iris (noise built from the styles, blocks resolved through the pack, baked object variants) lives in `Resolved*` classes beside the worker (`ResolvedReplace`, `ResolvedCaveProfile`, `ResolvedFloatingChild`, `ResolvedProceduralPlacement`). @@ -202,14 +201,14 @@ none of the jar's modules (`IrisModuleHost.JAR_MODULE_IDS`, which a test keeps e `art.arcane.iris.builtin` also holds what the compiled-in built-ins and the jar must share, because each sees its own copy of every other class. The facades among them are interfaces Iris implements and offers through IrisApi's extension point, never -through the thread or a cast: a module asks `world.extension(X.class)` for what belongs to a world (`LegacyStreams`, -`StructureHost`, `spawn.SpawnCooldowns`), `context.extension(X.class)` for what belongs to one invocation (`MatterMantle` +through the thread or a cast: a module asks `world.extension(X.class)` for what belongs to a world (`StructureHost`), +`context.extension(X.class)` for what belongs to one invocation (`MatterMantle` in a chunk or mantle stage) and `settings.extension(X.class)` for a reach worked out before any world exists, each with `.orElseThrow()`, since a built-in runs only where Iris offers them. Iris's per-feature world seeds are -IrisApi purpose seeds: `world.seed(IrisSeedPurposes.CARVE)` (`iris:seed/carve`), by the API's rule like any module's; -`spawn.SpawnCooldowns` (the spawner cooldowns -Iris saves with the world's data, so they survive a restart), -`loot.LootResolver` (the random streams loot is drawn from, shared with the hosts that fill containers) and the loot and +IrisApi purpose seeds: `world.seed(IrisSeedPurposes.CARVE)` (`iris:seed/carve`), by the API's rule like any module's. What the +built-ins keep with a world across restarts, such as the spawner cooldowns (`SpawnCooldownBook`), they save through +`RunningWorld.saved`; object placing seeds its chunk random, density noise and virtual heightmaps from purpose seeds and IrisApi noise. +The package also holds `loot.LootResolver` (the random streams loot is drawn from, shared with the hosts that fill containers) and the loot and marker lists of an object placement (`loot.ObjectLoot`, `loot.ObjectVanillaLoot`, `spawn.PlacementMarker`). The placement stages need more of the object being placed than IrisApi describes, so `placement.ObjectPlacement` is the facade Iris implements for each placement and offers with its invocation (`context.extension(ObjectPlacement.class)`): the object's size (`ObjectShape`), the placement's settings (`PlacementSettings`, read from diff --git a/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java b/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java index 22f61d389..f05347f18 100644 --- a/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java +++ b/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java @@ -34,6 +34,10 @@ public final class IrisSeedPurposes { public static final ResourceId ENTITY = purpose("entity"); public static final ResourceId LOOT = purpose("loot"); public static final ResourceId MANTLE = purpose("mantle"); + /** The random each chunk's objects are placed with, before the chunk's coordinates are mixed in. */ + public static final ResourceId OBJECT_CHUNK = purpose("object-chunk"); + /** The noise that picks the count of a placement with a density style. */ + public static final ResourceId OBJECT_DENSITY = purpose("object-density"); /** The noise that warps the columns of object placements with a surface warp. */ public static final ResourceId OBJECT_WARP = purpose("object-warp"); /** The candidate spots, ranks and chance noise of separated object placements. */ diff --git a/src/main/java/art/arcane/iris/builtin/LegacyStreams.java b/src/main/java/art/arcane/iris/builtin/LegacyStreams.java deleted file mode 100644 index e00716157..000000000 --- a/src/main/java/art/arcane/iris/builtin/LegacyStreams.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Iris is a World Generator for Minecraft Servers - * Copyright (c) 2026 Arcane Arts (Volmit Software) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package art.arcane.iris.builtin; - -import art.arcane.iris.builtin.objects.ObjectPlacement; -import art.arcane.volmlib.util.math.RNG; - -/** - * The random streams and noise of Iris's own generators that decide where existing worlds have their objects, which no - * API noise reproduces: Iris's chunk random, and the noise of a placement's density style and virtual heightmap. Iris - * offers it as an extension of its worlds: {@code world.extension(LegacyStreams.class)}. - */ -public interface LegacyStreams { - /** The random a chunk's objects are placed with: the same for the same chunk of the same world. */ - RNG objectChunkRandom(int chunkX, int chunkZ); - - /** How many objects the placement places at the column: its fixed density, or what its density style gives there for {@code rng}. */ - int density(ObjectPlacement placement, RNG rng, double x, double z); - - /** The height the placement's virtual heightmap gives the column, its noise seeded with {@code seed}; for a placement with a heightmap. */ - int heightmapSurface(ObjectPlacement placement, long seed, int x, int z); -} diff --git a/src/main/java/art/arcane/iris/builtin/objects/ObjectPlacement.java b/src/main/java/art/arcane/iris/builtin/objects/ObjectPlacement.java index 44ac1c362..4ffb1f5cc 100644 --- a/src/main/java/art/arcane/iris/builtin/objects/ObjectPlacement.java +++ b/src/main/java/art/arcane/iris/builtin/objects/ObjectPlacement.java @@ -190,7 +190,6 @@ public final class ObjectPlacement { private boolean forcePlace = false; private transient volatile String json; - private transient volatile Object hostForm; public ObjectPlacement() { } @@ -240,7 +239,6 @@ public final class ObjectPlacement { forbiddenCollisions = source.forbiddenCollisions; forcePlace = source.forcePlace; json = source.json; - hostForm = source.hostForm; } /** A builder of a placement made in code, starting from the defaults a pack's placement starts from. */ @@ -478,18 +476,6 @@ public final class ObjectPlacement { return names; } - /** - * What Iris made of this placement the last time it asked, kept here so it asks once. Only Iris sets or reads it, - * and it is its own type. - */ - public Object hostForm() { - return hostForm; - } - - public void hostForm(Object form) { - this.hostForm = form; - } - private static List listOf(List values) { return values == null ? List.of() : Collections.unmodifiableList(values); } @@ -544,7 +530,6 @@ public final class ObjectPlacement { public Builder densityStyle(StyledRange densityStyle) { placement.densityStyle = densityStyle; - placement.hostForm = null; return this; } @@ -605,7 +590,6 @@ public final class ObjectPlacement { public Builder heightmap(HeightmapNoise heightmap) { placement.heightmap = heightmap; - placement.hostForm = null; return this; } @@ -646,7 +630,6 @@ public final class ObjectPlacement { public Builder warp(NoiseStyle warp) { placement.warp = warp; - placement.hostForm = null; return this; } diff --git a/src/main/java/art/arcane/iris/builtin/package-info.java b/src/main/java/art/arcane/iris/builtin/package-info.java index 5a6a97740..8d084d393 100644 --- a/src/main/java/art/arcane/iris/builtin/package-info.java +++ b/src/main/java/art/arcane/iris/builtin/package-info.java @@ -30,9 +30,8 @@ *

* Besides those, it holds what Iris and the built-ins jar must share, since each sees its own copy * of every other class: narrow facades only Iris's own built-ins use, interfaces Iris's worlds implement - * ({@link art.arcane.iris.builtin.spawn.SpawnCooldowns}, {@link art.arcane.iris.builtin.LegacyStreams}), and the seed purposes Iris - * answers with its legacy seeds ({@link art.arcane.iris.builtin.IrisSeedPurposes}). Keep them few: each is - * a promise. + * ({@link art.arcane.iris.builtin.structure.StructureHost}), and the seed purposes of the built-in features + * ({@link art.arcane.iris.builtin.IrisSeedPurposes}). Keep them few: each is a promise. *

* The placement stages ({@code iris:prepare} to {@code iris:snow}) run in the pack's placement graph, which * {@code PackWorld.placements()} runs. They read the object and its settings from the run's input, diff --git a/src/main/java/art/arcane/iris/engine/mantle/components/ChunkObjectPlacing.java b/src/main/java/art/arcane/iris/engine/mantle/components/ChunkObjectPlacing.java index fae8e63b0..d54dcc9aa 100644 --- a/src/main/java/art/arcane/iris/engine/mantle/components/ChunkObjectPlacing.java +++ b/src/main/java/art/arcane/iris/engine/mantle/components/ChunkObjectPlacing.java @@ -302,7 +302,7 @@ final class ChunkObjectPlacing { private void placeObject(Tally tally, ObjectPlacement placement, String scope) { int blockX = context.originX(); int blockZ = context.originZ(); - int density = objects.density(placement, rng, blockX, blockZ); + int density = objects.density(placement, blockX, blockZ); for (int i = 0; i < density; i++) { tally.attempts++; Optional picked = objects.pickScaled(placement, rng); @@ -347,7 +347,7 @@ final class ChunkObjectPlacing { ResourceId expectedCaveBiome, CaveAnchors anchors) { int minX = context.originX(); int minZ = context.originZ(); - int density = objects.density(placement, rng, minX, minZ); + int density = objects.density(placement, minX, minZ); CaveAnchors.Search search = caves.searchFor(placement, profile); for (int i = 0; i < density; i++) { tally.attempts++; @@ -439,7 +439,7 @@ final class ChunkObjectPlacing { private void placeUpperObject(ObjectPlacement placement, boolean forcePlace, String scope) { int minX = context.originX(); int minZ = context.originZ(); - int density = objects.density(placement, rng, minX, minZ); + int density = objects.density(placement, minX, minZ); for (int i = 0; i < density; i++) { Optional picked = objects.pickScaled(placement, rng); if (picked.isEmpty()) { diff --git a/src/main/java/art/arcane/iris/engine/mantle/components/FloatingObjectPlacing.java b/src/main/java/art/arcane/iris/engine/mantle/components/FloatingObjectPlacing.java index 0d7964d2e..648ebe491 100644 --- a/src/main/java/art/arcane/iris/engine/mantle/components/FloatingObjectPlacing.java +++ b/src/main/java/art/arcane/iris/engine/mantle/components/FloatingObjectPlacing.java @@ -200,7 +200,7 @@ public final class FloatingObjectPlacing { private void placeFloating(Chunk chunk, ObjectPlacement placement, KList columns, ResolvedFloatingChild entry) { RNG rng = chunk.rng; - int density = objects.density(placement, rng, chunk.minX, chunk.minZ); + int density = objects.density(placement, chunk.minX, chunk.minZ); for (int i = 0; i < density; i++) { Optional picked = pick(placement, entry, rng); if (picked.isEmpty()) { @@ -214,7 +214,7 @@ public final class FloatingObjectPlacing { private void placeAnchored(Chunk chunk, ObjectPlacement placement, KList columns, KList interior, ResolvedFloatingChild entry) { RNG rng = chunk.rng; - int density = objects.density(placement, rng, chunk.minX, chunk.minZ); + int density = objects.density(placement, chunk.minX, chunk.minZ); for (int i = 0; i < density; i++) { Optional picked = pick(placement, entry, rng); if (picked.isEmpty()) { @@ -249,7 +249,7 @@ public final class FloatingObjectPlacing { private void placeInverted(Chunk chunk, ObjectPlacement placement, KList columns, KList interior, ResolvedFloatingChild entry) { RNG rng = chunk.rng; - int density = objects.density(placement, rng, chunk.minX, chunk.minZ); + int density = objects.density(placement, chunk.minX, chunk.minZ); for (int i = 0; i < density; i++) { Optional picked = pick(placement, entry, rng); if (picked.isEmpty()) { diff --git a/src/main/java/art/arcane/iris/engine/mantle/components/ObjectPlacing.java b/src/main/java/art/arcane/iris/engine/mantle/components/ObjectPlacing.java index 7a2770d64..6ad7a402a 100644 --- a/src/main/java/art/arcane/iris/engine/mantle/components/ObjectPlacing.java +++ b/src/main/java/art/arcane/iris/engine/mantle/components/ObjectPlacing.java @@ -105,7 +105,7 @@ public final class ObjectPlacing { private Set guessPlacedKeys(RNG rng, int x, int z, ObjectPlacement placement) { Set keys = new HashSet<>(); - for (int i = 0; i < objects.density(placement, rng, x, z); i++) { + for (int i = 0; i < objects.density(placement, x, z); i++) { Optional picked = objects.pickScaled(placement, rng); picked.ifPresent(object -> keys.add(object.name())); } diff --git a/src/main/java/art/arcane/iris/engine/mantle/components/ObjectWarmUp.java b/src/main/java/art/arcane/iris/engine/mantle/components/ObjectWarmUp.java index f48ddb42c..8e60aeaa1 100644 --- a/src/main/java/art/arcane/iris/engine/mantle/components/ObjectWarmUp.java +++ b/src/main/java/art/arcane/iris/engine/mantle/components/ObjectWarmUp.java @@ -18,12 +18,10 @@ package art.arcane.iris.engine.mantle.components; -import art.arcane.iris.builtin.IrisSeedPurposes; import art.arcane.iris.builtin.objects.ObjectPlacement; import art.arcane.iris.builtin.objects.ObjectSettings; import art.arcane.iris.modules.objects.Declaration; import art.arcane.iris.modules.objects.ObjectPool; -import art.arcane.volmlib.util.math.RNG; import com.volmit.config.Level; import com.volmit.world.PackWorld; @@ -32,22 +30,18 @@ import java.util.List; /** * Builds what object placing would build on first use, for every object placement and procedural object the world's - * biomes and regions declare: the placement's density noise, and a procedural object's variants. None of it depends on - * the random it is warmed with; the density noise is cached per seed and the variants are baked whatever picks one. + * biomes and regions declare: the placement's density noise, and a procedural object's variants. The density noise is + * cached per seed and the variants are baked whatever picks one. */ final class ObjectWarmUp { - private static final long ROOT_SEED_SALT = 7777L; private static final Level[] WARMED_LEVELS = {Level.BIOME, Level.REGION}; private final PackWorld world; private final ObjectPool objects; - private final RNG root; - private int warmed; ObjectWarmUp(PackWorld world) { this.world = world; this.objects = ObjectPool.of(world); - this.root = new RNG(world.seed(IrisSeedPurposes.COMPONENT) + ROOT_SEED_SALT); } void warm() { @@ -68,7 +62,7 @@ final class ObjectWarmUp { private void warmPlacements(List placements) { for (ObjectPlacement placement : placements) { if (placement != null) { - objects.warm(placement, root.nextParallelRNG(warmed++)); + objects.warm(placement); } } } diff --git a/src/main/java/art/arcane/iris/modules/objects/ObjectPool.java b/src/main/java/art/arcane/iris/modules/objects/ObjectPool.java index 014b5ef01..36cd79883 100644 --- a/src/main/java/art/arcane/iris/modules/objects/ObjectPool.java +++ b/src/main/java/art/arcane/iris/modules/objects/ObjectPool.java @@ -18,15 +18,18 @@ package art.arcane.iris.modules.objects; -import art.arcane.iris.builtin.LegacyStreams; +import art.arcane.iris.builtin.IrisSeedPurposes; import art.arcane.iris.builtin.objects.ObjectPlacement; import art.arcane.iris.builtin.objects.ObjectScale; import art.arcane.iris.builtin.objects.ScaleInterpolation; import art.arcane.iris.engine.procedural.ProceduralObjectCatalog; +import art.arcane.volmlib.util.cache.CacheKey; import art.arcane.volmlib.util.math.RNG; +import com.volmit.noise.StyledRange; import com.volmit.object.PackObjects; import com.volmit.object.PlaceableObject; import com.volmit.object.ScaleMethod; +import com.volmit.random.WorldRandom; import com.volmit.resource.PackResources; import com.volmit.world.PackWorld; @@ -35,30 +38,33 @@ import java.util.Optional; /** * The pack's objects as object placements pick them: by key, scaled the way a placement scales them, and as many at a - * column as its density says, drawn from Iris's own streams so existing worlds keep their objects. + * column as its density says, drawn from random streams and noise seeded by the world. */ public final class ObjectPool { private static final double NO_SCALE = 1D; private static final int MAX_SCALE_VARIANTS = 32; private final PackResources resources; - private final LegacyStreams streams; + private final long chunkSeed; + private final long densitySeed; private final ProceduralObjectCatalog procedural; - private ObjectPool(PackResources resources, LegacyStreams streams, ProceduralObjectCatalog procedural) { + private ObjectPool(PackResources resources, long chunkSeed, long densitySeed, ProceduralObjectCatalog procedural) { this.resources = resources; - this.streams = streams; + this.chunkSeed = chunkSeed; + this.densitySeed = densitySeed; this.procedural = procedural; } /** The objects of {@code world}'s pack, its procedural objects among them. */ public static ObjectPool of(PackWorld world) { - return new ObjectPool(world.resources(), world.extension(LegacyStreams.class).orElseThrow(), ProceduralObjectCatalog.of(world)); + return new ObjectPool(world.resources(), world.seed(IrisSeedPurposes.OBJECT_CHUNK), + world.seed(IrisSeedPurposes.OBJECT_DENSITY), ProceduralObjectCatalog.of(world)); } - /** The random a chunk's objects are placed with. */ + /** The random a chunk's objects are placed with: the same for the same chunk of the same world. */ public RNG chunkRandom(int chunkX, int chunkZ) { - return streams.objectChunkRandom(chunkX, chunkZ); + return new RNG(new WorldRandom(chunkSeed + CacheKey.key(chunkX, chunkZ)).nextLong()); } /** The pack object {@code key} names; for a procedural pool, the variant {@code rng} picks. */ @@ -83,14 +89,18 @@ public final class ObjectPool { return factor == NO_SCALE ? origin : objects().scaled(origin, factor, ScaleMethod.NONE); } - /** How many objects the placement places at the column, when its chance check passes. */ - public int density(ObjectPlacement placement, RNG rng, double x, double z) { - return streams.density(placement, rng, x, z); + /** How many objects the placement places at the column, when its chance check passes: its fixed density, or what its density style gives there. */ + public int density(ObjectPlacement placement, double x, double z) { + Optional densityStyle = placement.densityStyle(); + if (densityStyle.isEmpty()) { + return placement.density(); + } + return (int) Math.round(densityStyle.get().at(resources.noise(), densitySeed, x, z)); } /** Builds the noise the placement draws its density from and bakes its procedural objects, so the first chunk to place it does not wait. */ - public void warm(ObjectPlacement placement, RNG rng) { - streams.density(placement, rng, 0, 0); + public void warm(ObjectPlacement placement) { + density(placement, 0, 0); placement.place().stream().filter(ProceduralObjectCatalog::isProceduralKey).forEach(procedural::placeable); } diff --git a/src/main/java/art/arcane/iris/modules/placement/RunningPlacement.java b/src/main/java/art/arcane/iris/modules/placement/RunningPlacement.java index 6f14cbc36..d37650b2a 100644 --- a/src/main/java/art/arcane/iris/modules/placement/RunningPlacement.java +++ b/src/main/java/art/arcane/iris/modules/placement/RunningPlacement.java @@ -19,7 +19,7 @@ package art.arcane.iris.modules.placement; import art.arcane.iris.builtin.IrisSeedPurposes; -import art.arcane.iris.builtin.LegacyStreams; +import art.arcane.iris.builtin.objects.HeightmapNoise; import art.arcane.iris.builtin.objects.ObjectPlacement; import art.arcane.iris.builtin.objects.ObjectRun; import art.arcane.iris.builtin.prepare.ObjectRotation; @@ -103,13 +103,13 @@ public final class RunningPlacement { * terrain with its heights from the heightmap's noise, seeded from {@code rng}. */ public StageTerrain terrainFor(RNG rng) { - ObjectPlacement config = run.settings(); - if (config.heightmap().isEmpty()) { + Optional heightmapSettings = run.settings().heightmap(); + if (heightmapSettings.isEmpty()) { return terrain; } long heightmapSeed = rng.nextLong() + run.requestedY() + run.z() - run.x(); - LegacyStreams streams = world().extension(LegacyStreams.class).orElseThrow(); - return terrain.withHeights((x, z) -> streams.heightmapSurface(config, heightmapSeed, x, z)); + VirtualHeightmap heightmap = VirtualHeightmap.of(heightmapSettings.get(), world().resources().noise(), heightmapSeed); + return terrain.withHeights(heightmap::surface); } /** diff --git a/src/main/java/art/arcane/iris/modules/placement/VirtualHeightmap.java b/src/main/java/art/arcane/iris/modules/placement/VirtualHeightmap.java new file mode 100644 index 000000000..8a240f510 --- /dev/null +++ b/src/main/java/art/arcane/iris/modules/placement/VirtualHeightmap.java @@ -0,0 +1,111 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.modules.placement; + +import art.arcane.iris.builtin.objects.HeightmapNoise; +import com.volmit.noise.NoiseFactory; +import com.volmit.noise.NoiseSampler; + +import java.util.ArrayList; +import java.util.List; + +/** + * The heights of an object placement's virtual heightmap: the noise of a {@link HeightmapNoise} and of the + * generators that fracture its coordinates, built once from API noise and seeded from one number. The setting + * {@code octaves} never changed Iris's values, so it is not read. + */ +public final class VirtualHeightmap { + private static final long GENERATOR_SEED_SALT = 33_955_677L; + private static final long FIRST_FRACTURE_SEED_OFFSET = 33L; + private static final long FRACTURE_SEED_STEP = 819L; + + private final HeightmapNoise settings; + private final NoiseSampler sampler; + private final List fractures; + + private VirtualHeightmap(HeightmapNoise settings, NoiseFactory noise, long seed) { + this.settings = settings; + this.sampler = settings.enabled() ? noise.sampler(settings.style(), seed + GENERATOR_SEED_SALT - settings.seed()) : null; + this.fractures = fracturesOf(settings, noise, seed); + } + + /** The heightmap {@code settings} describe, its noise seeded with {@code seed}. */ + public static VirtualHeightmap of(HeightmapNoise settings, NoiseFactory noise, long seed) { + return new VirtualHeightmap(settings, noise, seed); + } + + /** The height at the column, rounded to a whole block. */ + public int surface(int x, int z) { + return (int) Math.round(valueAt(x, z)); + } + + private static List fracturesOf(HeightmapNoise settings, NoiseFactory noise, long seed) { + List built = new ArrayList<>(); + long fractureOffset = FIRST_FRACTURE_SEED_OFFSET; + for (HeightmapNoise fracture : settings.fracture()) { + if (fracture.enabled()) { + built.add(new VirtualHeightmap(fracture, noise, seed + settings.seed() + fractureOffset)); + } + fractureOffset += FRACTURE_SEED_STEP; + } + return List.copyOf(built); + } + + private double valueAt(double columnX, double columnZ) { + if (!settings.enabled()) { + return settings.offsetY(); + } + double x = columnX; + double z = columnZ; + for (VirtualHeightmap fracture : fractures) { + double centre = fracture.settings.opacity() / 2D; + x += fracture.valueAt(columnX, columnZ) - centre; + z += fracture.valueAt(columnZ, columnX) - centre; + } + double sampleX = (x / settings.zoom()) + settings.offsetX(); + double sampleZ = (z / settings.zoom()) + settings.offsetZ(); + return shaped(sampler.sample(sampleX, sampleZ) * settings.opacity()); + } + + private double shaped(double sampled) { + double value = settings.negative() ? (-sampled + settings.opacity()) : sampled; + value = raised(value) + settings.offsetY(); + if (settings.parametric()) { + value = parametric(value); + } + if (settings.bezier()) { + value = value * value * (3D - 2D * value); + } + return settings.sinCentered() ? Math.sin(value * Math.PI) : value; + } + + private double raised(double value) { + double exponent = settings.exponent(); + if (exponent == 1D) { + return value; + } + return value < 0 ? -Math.pow(-value, exponent) : Math.pow(value, exponent); + } + + /** The parametric curve of alpha 1, which is what the setting has always applied. */ + private static double parametric(double value) { + double raised = Math.pow(value, 1D); + return raised / ((raised - Math.pow(value, 0D)) + 1D); + } +}