From d65da9862499f5b802fea259365e0802b5335316 Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 5 Nov 2026 21:14:53 +0100 Subject: [PATCH] Space surface objects apart and vary their chance with noise Ported from v3e (4be4f9774, 92a1fa645). An object placement can set a separation in blocks: each chunk rolls one candidate spot, and it stands only when no lower ranked candidate of a placement with the same objects lies closer. Spots and ranks come from the world seed, the chunk and the objects, so spacing holds across chunk, biome and region borders in any generation order. A chanceStyle multiplies the chance by noise. --- .../arcane/iris/builtin/IrisSeedPurposes.java | 2 + .../iris/builtin/objects/ObjectPlacement.java | 23 ++- .../mantle/components/ChunkObjectPlacing.java | 86 +++++++---- .../mantle/components/ObjectPlacing.java | 26 +++- .../modules/objects/ObjectSeparation.java | 140 ++++++++++++++++++ 5 files changed, 244 insertions(+), 33 deletions(-) create mode 100644 src/main/java/art/arcane/iris/modules/objects/ObjectSeparation.java diff --git a/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java b/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java index c292be806..11ec40496 100644 --- a/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java +++ b/src/main/java/art/arcane/iris/builtin/IrisSeedPurposes.java @@ -38,6 +38,8 @@ public final class IrisSeedPurposes { public static final ResourceId MANTLE = purpose("mantle"); /** 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. */ + public static final ResourceId OBJECT_SEPARATION = purpose("object-separation"); public static final ResourceId POST = purpose("post"); public static final ResourceId TERRAIN = purpose("terrain"); 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 9c6f0db63..715751a4b 100644 --- a/src/main/java/art/arcane/iris/builtin/objects/ObjectPlacement.java +++ b/src/main/java/art/arcane/iris/builtin/objects/ObjectPlacement.java @@ -98,8 +98,13 @@ public final class ObjectPlacement implements PlacementSource { @MaxNumber(1) @Desc("The chance for this to place in a chunk. If you need multiple per chunk, set this to 1 and use density.") private double chance = 1; + @Desc("If set, chance is multiplied by this noise (0 to 1) at the chunk, so objects gather in some areas and thin out in others.") + private NoiseStyle chanceStyle = null; + @MinNumber(0) + @Desc("Keeps objects of this placement at least this many blocks apart, also across chunks and biomes that place the same objects. Each chunk then places at most one. 0 turns it off. Surface placements only.") + private int separation = 0; @MinNumber(1) - @Desc("If the chance check passes, place this many in a single chunk") + @Desc("If the chance check passes, place this many in a single chunk. Ignored when separation is set.") private int density = 1; @Desc("If the chance check passes, and you specify this, it picks a number in the range based on noise, and 'density' is ignored.") private StyledRange densityStyle = null; @@ -199,6 +204,8 @@ public final class ObjectPlacement implements PlacementSource { slopeCondition = source.slopeCondition; rotateTowardsSlope = source.rotateTowardsSlope; chance = source.chance; + chanceStyle = source.chanceStyle; + separation = source.separation; density = source.density; densityStyle = source.densityStyle; stiltSettings = source.stiltSettings; @@ -284,6 +291,20 @@ public final class ObjectPlacement implements PlacementSource { return chance; } + /** The noise, from 0 to 1, that {@link #chance()} is multiplied by at each chunk; empty when the chance is the same everywhere. */ + public Optional chanceStyle() { + return Optional.ofNullable(chanceStyle); + } + + /** The least distance in blocks between two objects of placements with the same objects; 0 when they are not kept apart. */ + public int separation() { + return separation; + } + + public boolean isSeparated() { + return separation > 0; + } + /** The count of placements when the chance check passes, unless {@link #densityStyle()} picks it. */ public int density() { return density; 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 85505f258..809f5273a 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 @@ -35,6 +35,7 @@ import art.arcane.iris.modules.objects.CavePlacement; import art.arcane.iris.modules.objects.TerrainTransaction; import art.arcane.iris.modules.objects.EffectiveSettings; import art.arcane.iris.modules.objects.ObjectPool; +import art.arcane.iris.modules.objects.ObjectSeparation; import art.arcane.iris.modules.objects.PlacementChanges; import art.arcane.iris.modules.objects.PlacementTags; import art.arcane.iris.modules.objects.ProceduralPlacement; @@ -73,6 +74,7 @@ final class ChunkObjectPlacing { private static final String REGEN_THREAD_PREFIX = "Iris-Regen-"; private final ObjectPool objects; + private final ObjectSeparation separation; private final ModuleLog log; private final MantleStageContext context; private final ObjectChunk chunk; @@ -91,6 +93,7 @@ final class ChunkObjectPlacing { ChunkObjectPlacing(ObjectPool objects, EffectiveSettings settings, ModuleLog log, MantleStageContext context) { this.objects = objects; + this.separation = ObjectSeparation.of(context.world()); this.log = log; this.context = context; this.chunk = new ObjectChunk(context, settings, log); @@ -241,13 +244,17 @@ final class ChunkObjectPlacing { private void placeSurfaceScope(Tally tally, String scope, List placements, String ownerName) { for (ObjectPlacement placement : placements) { tally.checked(scope); - boolean passes = rng.chance(placement.chance() + rng.d(-CHANCE_JITTER, CHANCE_JITTER)); - traceChance(scope, passes, placement); - if (!passes) { - continue; - } - tally.triggered(scope); try { + if (placement.isSeparated()) { + placeSeparatedObject(tally, placement, scope); + continue; + } + boolean passes = rng.chance(separation.chanceAt(placement, centerX, centerZ) + rng.d(-CHANCE_JITTER, CHANCE_JITTER)); + traceChance(scope, passes, placement); + if (!passes) { + continue; + } + tally.triggered(scope); placeObject(tally, placement, scope); } catch (Throwable failure) { tally.errors++; @@ -256,6 +263,25 @@ final class ChunkObjectPlacing { } } + /** Places the one object the chunk's candidate stands for, when no closer candidate of a lower rank outranks it. */ + private void placeSeparatedObject(Tally tally, ObjectPlacement placement, String scope) { + Optional kept = separation.keptCandidate(chunkX, chunkZ, placement, context.settings()); + traceChance(scope, kept.isPresent(), placement); + if (kept.isEmpty()) { + return; + } + tally.triggered(scope); + ObjectSeparation.Candidate candidate = kept.get(); + tally.attempts++; + Optional picked = objects.pickScaled(placement, candidate.rng()); + if (picked.isEmpty()) { + tally.nullObjects++; + traceNullObject(scope, 0, 1, placement); + return; + } + placeOnSurfaceAt(tally, placement, scope, picked.get(), candidate.x(), candidate.z(), candidate.rng(), 0, 1); + } + private void placeCaveScope(Tally tally, String scope, List placements, CaveProfile profile, ResourceId expectedCaveBiome, String ownerName, CaveAnchors anchors) { for (ObjectPlacement placement : placements) { @@ -297,31 +323,35 @@ final class ChunkObjectPlacing { traceNullObject(scope, i, density, placement); continue; } - PlaceableObject object = picked.get(); int x = rng.i(blockX, blockX + CHUNK_WIDTH_BLOCKS); int z = rng.i(blockZ, blockZ + CHUNK_WIDTH_BLOCKS); - PlacementChanges changes = effective.forSurface(placement, object); - boolean treePlacement = isTreePlacement(object, placement); - int id = rng.i(0, Integer.MAX_VALUE); - String goldenTag = golden ? scope + "/" + object.name() : null; - if (golden) { - log.debug("Goldendebug object attempt: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name() - + " densityIndex=" + i + " xx=" + x + " zz=" + z + " mode=" + placement.mode()); - } - try { - PlacementTags tags = tags(object, id, "surface", treePlacement, placement); - int result = chunk.placeOnSurface(object, x, z, placement, changes, tags, goldenTag); - if (result >= 0) { - tally.placed++; - } else { - tally.rejected++; - } - traceObjectResult(scope, object, result, x, z, i, density); - } catch (Throwable failure) { - tally.errors++; - log.error("Regen object placement exception: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name() - + " densityIndex=" + i + " density=" + density + " error=" + failure.getClass().getSimpleName() + ":" + failure.getMessage(), failure); + placeOnSurfaceAt(tally, placement, scope, picked.get(), x, z, rng, i, density); + } + } + + private void placeOnSurfaceAt(Tally tally, ObjectPlacement placement, String scope, PlaceableObject object, int x, int z, + RNG placingRng, int densityIndex, int density) { + PlacementChanges changes = effective.forSurface(placement, object); + boolean treePlacement = isTreePlacement(object, placement); + int id = placingRng.i(0, Integer.MAX_VALUE); + String goldenTag = golden ? scope + "/" + object.name() : null; + if (golden) { + log.debug("Goldendebug object attempt: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name() + + " densityIndex=" + densityIndex + " xx=" + x + " zz=" + z + " mode=" + placement.mode()); + } + try { + PlacementTags tags = tags(object, id, "surface", treePlacement, placement); + int result = chunk.placeOnSurface(object, x, z, placement, changes, tags, goldenTag); + if (result >= 0) { + tally.placed++; + } else { + tally.rejected++; } + traceObjectResult(scope, object, result, x, z, densityIndex, density); + } catch (Throwable failure) { + tally.errors++; + log.error("Regen object placement exception: chunk=" + chunkX + "," + chunkZ + " scope=" + scope + " object=" + object.name() + + " densityIndex=" + densityIndex + " density=" + density + " error=" + failure.getClass().getSimpleName() + ":" + failure.getMessage(), failure); } } 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 3be711ca4..7cf5256a9 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 @@ -22,8 +22,10 @@ import art.arcane.iris.builtin.objects.ObjectPlacement; import art.arcane.iris.builtin.objects.ObjectSettings; import art.arcane.iris.modules.objects.EffectiveSettings; import art.arcane.iris.modules.objects.ObjectPool; +import art.arcane.iris.modules.objects.ObjectSeparation; import art.arcane.volmlib.util.math.RNG; import com.volmit.ModuleLog; +import com.volmit.config.FeatureSettings; import com.volmit.config.Level; import com.volmit.config.SettingsStack; import com.volmit.object.PlaceableObject; @@ -47,12 +49,14 @@ public final class ObjectPlacing { private final PackWorld world; private final ObjectPool objects; + private final ObjectSeparation separation; private final EffectiveSettings settings = new EffectiveSettings(); private final ModuleLog log; private ObjectPlacing(PackWorld world) { this.world = world; this.objects = ObjectPool.of(world); + this.separation = ObjectSeparation.of(world); this.log = world.log(); } @@ -71,22 +75,36 @@ public final class ObjectPlacing { RNG rng = objects.chunkRandom(chunkX, chunkZ); int centerX = (chunkX << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS; int centerZ = (chunkZ << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS; - SettingsStack declared = world.running().orElseThrow().settings().at(ObjectSettings.class, centerX, centerZ); + FeatureSettings worldSettings = world.running().orElseThrow().settings(); + SettingsStack declared = worldSettings.at(ObjectSettings.class, centerX, centerZ); Set keys = new HashSet<>(); for (Level level : GUESSED_LEVELS) { - declared.level(level).ifPresent(settings -> guessFrom(settings, rng, chunkX, chunkZ, keys)); + declared.level(level).ifPresent(settings -> guessFrom(settings, worldSettings, rng, chunkX, chunkZ, keys)); } return keys; } - private void guessFrom(ObjectSettings settings, RNG rng, int chunkX, int chunkZ, Set keys) { + private void guessFrom(ObjectSettings settings, FeatureSettings worldSettings, RNG rng, int chunkX, int chunkZ, Set keys) { + int centerX = (chunkX << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS; + int centerZ = (chunkZ << CHUNK_SHIFT) + CHUNK_CENTER_OFFSET_BLOCKS; for (ObjectPlacement placement : settings.objects()) { - if (placement.carvingSupport().supportsSurface() && rng.chance(placement.chance() + rng.d(-CHANCE_JITTER, CHANCE_JITTER))) { + if (!placement.carvingSupport().supportsSurface()) { + continue; + } + if (placement.isSeparated()) { + guessSeparatedKey(chunkX, chunkZ, placement, worldSettings).ifPresent(keys::add); + } else if (rng.chance(separation.chanceAt(placement, centerX, centerZ) + rng.d(-CHANCE_JITTER, CHANCE_JITTER))) { keys.addAll(guessPlacedKeys(rng, chunkX, chunkZ, placement)); } } } + private Optional guessSeparatedKey(int chunkX, int chunkZ, ObjectPlacement placement, FeatureSettings worldSettings) { + return separation.keptCandidate(chunkX, chunkZ, placement, worldSettings) + .flatMap(candidate -> objects.pickScaled(placement, candidate.rng())) + .map(PlaceableObject::name); + } + 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++) { diff --git a/src/main/java/art/arcane/iris/modules/objects/ObjectSeparation.java b/src/main/java/art/arcane/iris/modules/objects/ObjectSeparation.java new file mode 100644 index 000000000..b3c130ea0 --- /dev/null +++ b/src/main/java/art/arcane/iris/modules/objects/ObjectSeparation.java @@ -0,0 +1,140 @@ +package art.arcane.iris.modules.objects; + +import art.arcane.iris.builtin.IrisSeedPurposes; +import art.arcane.iris.builtin.objects.ObjectPlacement; +import art.arcane.iris.builtin.objects.ObjectSettings; +import art.arcane.volmlib.util.math.RNG; +import com.volmit.config.FeatureSettings; +import com.volmit.config.Level; +import com.volmit.config.SettingsStack; +import com.volmit.noise.NoiseSampler; +import com.volmit.noise.NoiseStyle; +import com.volmit.world.PackWorld; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +/** + * The chance and spacing of surface object placements in a world. + * + *

A separated placement rolls one candidate spot per chunk. The candidate stands only when no candidate of a lower rank, + * from a placement with the same objects, lies closer than the separation. Spots and ranks come from the world seed, the + * chunk and the objects alone, so every chunk agrees on them across chunk, biome and region borders, and objects never + * stand closer than the separation, whatever order chunks generate in. + */ +public final class ObjectSeparation { + private static final int CHUNK_WIDTH_BLOCKS = 16; + private static final int CHUNK_SHIFT = 4; + /** Placements read a chunk's settings at local 8, 8, as Iris always did. */ + private static final int SETTINGS_COLUMN_OFFSET_BLOCKS = 8; + /** A chunk places the biome's objects before the region's, so the biome's placement speaks for the chunk. */ + private static final List PLACING_LEVELS = List.of(Level.BIOME, Level.REGION); + + private final PackWorld world; + private final long seed; + private final Map chanceNoises = new ConcurrentHashMap<>(); + + private ObjectSeparation(PackWorld world) { + this.world = world; + this.seed = world.seed(IrisSeedPurposes.OBJECT_SEPARATION); + } + + /** The separation of {@code world}. */ + public static ObjectSeparation of(PackWorld world) { + return world.state(ObjectSeparation.class, ObjectSeparation::new); + } + + /** The chance to place at the block column: the placement's chance, multiplied by its chance noise when it has one. */ + public double chanceAt(ObjectPlacement placement, double x, double z) { + Optional style = placement.chanceStyle(); + if (style.isEmpty()) { + return placement.chance(); + } + double noise = chanceNoises.computeIfAbsent(style.get(), declared -> world.resources().noise().sampler(declared, seed)).sample(x, z); + return placement.chance() * Math.max(0D, Math.min(1D, noise)); + } + + /** + * The candidate the chunk places for the separated placement; empty when it rolled none or a lower ranked one is too + * close. {@code settings} tells which placements the neighbouring chunks declare. + */ + public Optional keptCandidate(int chunkX, int chunkZ, ObjectPlacement placement, FeatureSettings settings) { + Optional own = candidateOf(chunkX, chunkZ, placement); + if (own.isEmpty() || isOutranked(own.get(), chunkX, chunkZ, placement, settings)) { + return Optional.empty(); + } + return own; + } + + private boolean isOutranked(Candidate own, int chunkX, int chunkZ, ObjectPlacement placement, FeatureSettings settings) { + int reach = placement.separation(); + for (int x = Math.floorDiv(own.x() - reach, CHUNK_WIDTH_BLOCKS); x <= Math.floorDiv(own.x() + reach, CHUNK_WIDTH_BLOCKS); x++) { + for (int z = Math.floorDiv(own.z() - reach, CHUNK_WIDTH_BLOCKS); z <= Math.floorDiv(own.z() + reach, CHUNK_WIDTH_BLOCKS); z++) { + if (x == chunkX && z == chunkZ) { + continue; + } + int rivalX = x; + int rivalZ = z; + Optional rival = placementOfSameObjectsAt(rivalX, rivalZ, placement.place(), settings) + .flatMap(other -> candidateOf(rivalX, rivalZ, other)); + if (rival.isPresent() && rival.get().rank() < own.rank() && rival.get().isCloserThan(own, reach)) { + return true; + } + } + } + return false; + } + + private static Optional placementOfSameObjectsAt(int chunkX, int chunkZ, List objects, FeatureSettings settings) { + int x = (chunkX << CHUNK_SHIFT) + SETTINGS_COLUMN_OFFSET_BLOCKS; + int z = (chunkZ << CHUNK_SHIFT) + SETTINGS_COLUMN_OFFSET_BLOCKS; + SettingsStack declared = settings.at(ObjectSettings.class, x, z); + for (Level level : PLACING_LEVELS) { + Optional placing = declared.level(level).flatMap(placements -> separatedPlacing(placements, objects)); + if (placing.isPresent()) { + return placing; + } + } + return Optional.empty(); + } + + private static Optional separatedPlacing(ObjectSettings settings, List objects) { + return settings.objects().stream() + .filter(placement -> placement.isSeparated() && placement.carvingSupport().supportsSurface() && placement.place().equals(objects)) + .findFirst(); + } + + private Optional candidateOf(int chunkX, int chunkZ, ObjectPlacement placement) { + RNG rng = new RNG(chunkSeed(chunkX, chunkZ, placement.place().hashCode())); + int x = (chunkX << CHUNK_SHIFT) + rng.nextInt(CHUNK_WIDTH_BLOCKS); + int z = (chunkZ << CHUNK_SHIFT) + rng.nextInt(CHUNK_WIDTH_BLOCKS); + if (!rng.chance(chanceAt(placement, x, z))) { + return Optional.empty(); + } + return Optional.of(new Candidate(x, z, rng.nextLong(), rng)); + } + + // Each input is mixed in after the previous one: XORing scaled coordinates gave chunk x,z and -x,-z the same seed + // whenever both were odd. + private long chunkSeed(int chunkX, int chunkZ, int objectsHash) { + return splitMix(splitMix(splitMix(seed + objectsHash) + chunkX) + chunkZ); + } + + private static long splitMix(long value) { + long mixed = value + 0x9E3779B97F4A7C15L; + mixed = (mixed ^ (mixed >>> 30)) * 0xBF58476D1CE4E5B9L; + mixed = (mixed ^ (mixed >>> 27)) * 0x94D049BB133111EBL; + return mixed ^ (mixed >>> 31); + } + + /** Where a chunk would place, in block coordinates, with the random that picks and places the object there. */ + public record Candidate(int x, int z, long rank, RNG rng) { + boolean isCloserThan(Candidate other, int blocks) { + long dx = x - other.x; + long dz = z - other.z; + return dx * dx + dz * dz < (long) blocks * blocks; + } + } +}