diff --git a/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java b/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java index ac53f6af3..1f0628185 100644 --- a/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java +++ b/core/src/main/java/art/arcane/iris/generation/mantle/MantleObjectComponent.java @@ -69,8 +69,11 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import art.arcane.volmlib.nativelib.terrain.NativeBlockState; import art.arcane.iris.generation.block.DataProvider; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.EnumSet; import java.util.HashMap; @@ -84,6 +87,7 @@ import java.util.function.BiConsumer; public class MantleObjectComponent extends IrisMantleComponent { private static final long CAVE_REJECT_LOG_THROTTLE_MS = 5000L; private static final int BEDROCK_CLEARANCE = 6; + private static final long OBJECT_SEPARATION_SEED_SALT = 0x5E9A2A7E0B1EC75L; static final byte LIQUID_FORCED_AIR = 3; private static final Map CAVE_REJECT_LOG_STATE = new ConcurrentHashMap<>(); private static final Set MISSING_LOAD_KEY_WARNED = ConcurrentHashMap.newKeySet(); @@ -479,29 +483,17 @@ public class MantleObjectComponent extends IrisMantleComponent { for (IrisObjectPlacement i : surfaceBiome.getSurfaceObjects()) { biomeSurfaceChecked++; - boolean chance = rng.chance(i.getChance() + rng.d(-0.005, 0.005)) && !i.isCompatExcluded(getData()); - if (traceRegen) { - IrisLogging.debug("Regen object placer chance: chunk=" + x + "," + z - + " scope=biome-surface" - + " chanceResult=" + chance - + " chanceBase=" + i.getChance() - + " densityMid=" + i.getDensity() - + " objects=" + i.getPlace().size()); - } - if (chance) { - biomeSurfaceTriggered++; - try { - ObjectPlacementResult result = placeObject(writer, rng, x << 4, z << 4, i, complex, traceRegen, x, z, "biome-surface"); - attempts += result.attempts(); - placed += result.placed(); - rejected += result.rejected(); - nullObjects += result.nullObjects(); - errors += result.errors(); - } catch (Throwable e) { - GenerationFailures.skipPlacement(e, "object", i.getPlace().toString(", "), x << 4, -1, z << 4, surfaceBiome.getLoadKey()); - errors++; - } + Optional triggered = placeSurfacePlacement(writer, rng, x, z, i, complex, traceRegen, "biome-surface", surfaceBiome.getLoadKey()); + if (triggered.isEmpty()) { + continue; } + biomeSurfaceTriggered++; + ObjectPlacementResult result = triggered.get(); + attempts += result.attempts(); + placed += result.placed(); + rejected += result.rejected(); + nullObjects += result.nullObjects(); + errors += result.errors(); } for (IrisObjectPlacement i : caveBiome.getCarvingObjects()) { @@ -536,29 +528,17 @@ public class MantleObjectComponent extends IrisMantleComponent { for (IrisObjectPlacement i : region.getSurfaceObjects()) { regionSurfaceChecked++; - boolean chance = rng.chance(i.getChance() + rng.d(-0.005, 0.005)) && !i.isCompatExcluded(getData()); - if (traceRegen) { - IrisLogging.debug("Regen object placer chance: chunk=" + x + "," + z - + " scope=region-surface" - + " chanceResult=" + chance - + " chanceBase=" + i.getChance() - + " densityMid=" + i.getDensity() - + " objects=" + i.getPlace().size()); - } - if (chance) { - regionSurfaceTriggered++; - try { - ObjectPlacementResult result = placeObject(writer, rng, x << 4, z << 4, i, complex, traceRegen, x, z, "region-surface"); - attempts += result.attempts(); - placed += result.placed(); - rejected += result.rejected(); - nullObjects += result.nullObjects(); - errors += result.errors(); - } catch (Throwable e) { - GenerationFailures.skipPlacement(e, "object", i.getPlace().toString(", "), x << 4, -1, z << 4, region.getLoadKey()); - errors++; - } + Optional triggered = placeSurfacePlacement(writer, rng, x, z, i, complex, traceRegen, "region-surface", region.getLoadKey()); + if (triggered.isEmpty()) { + continue; } + regionSurfaceTriggered++; + ObjectPlacementResult result = triggered.get(); + attempts += result.attempts(); + placed += result.placed(); + rejected += result.rejected(); + nullObjects += result.nullObjects(); + errors += result.errors(); } for (IrisObjectPlacement i : region.getCarvingObjects()) { @@ -960,93 +940,200 @@ public class MantleObjectComponent extends IrisMantleComponent { int nullObjects = 0; int errors = 0; int density = objectPlacement.getDensity(rng, x, z, getData()); - boolean golden = GoldenDebugObjectPlacer.isGoldenDebugChunk(chunkX, chunkZ); for (int i = 0; i < density; i++) { attempts++; IrisObject v = objectPlacement.scaleObject(rng, objectPlacement.getObject(complex, rng), getDimension()); if (v == null) { nullObjects++; - if (traceRegen) { - IrisLogging.debug("Regen object placement null object: chunk=" + chunkX + "," + chunkZ - + " scope=" + scope - + " densityIndex=" + i - + " density=" + density - + " placementKeys=" + objectPlacement.getPlace().toString(",")); - } + traceNullObject(traceRegen, chunkX, chunkZ, scope, i, density, objectPlacement); continue; } int xx = rng.i(x, x + 16); int zz = rng.i(z, z + 16); - IrisObjectPlacement effectivePlacement = resolveEffectivePlacement(objectPlacement, v); - if (!allowsObjectPlacement(complex, v, effectivePlacement, xx, zz)) { - rejected++; - continue; - } - boolean treePlacement = isTreePlacement(v, effectivePlacement); - int id = rng.i(0, Integer.MAX_VALUE); - IObjectPlacer placePlacer = golden ? new GoldenDebugObjectPlacer(writer, scope + "/" + v.getLoadKey()) : writer; - if (golden) { - IrisLogging.debug("Goldendebug object attempt: chunk=" + chunkX + "," + chunkZ - + " scope=" + scope - + " object=" + v.getLoadKey() - + " densityIndex=" + i - + " xx=" + xx - + " zz=" + zz - + " mode=" + effectivePlacement.getMode()); - } - try { - String marker = placementMarker(v, id, "surface"); - int continuation = writer.beginObjectPlacement(effectivePlacement.waterRule()); - int result = v.place(xx, zz, placePlacer, effectivePlacement, rng, (b, data) -> { - if (marker != null) { - writer.setData(b.getX(), b.getY(), b.getZ(), marker); - } - if (treePlacement && marker != null) { - writeTreeMaterial(writer, b.getX(), b.getY(), b.getZ(), data); - } - if (effectivePlacement.isDolphinTarget() && effectivePlacement.isUnderwater() && B.isStorageChest(data)) { - writer.setData(b.getX(), b.getY(), b.getZ(), MatterStructurePOI.BURIED_TREASURE); - } - }, null, getData()); - - if (result >= 0) { - writer.recordObjectPlacement(continuation); - placed++; - } else { - rejected++; - } - - if (golden) { - IrisLogging.debug("Goldendebug object result: chunk=" + chunkX + "," + chunkZ - + " scope=" + scope - + " object=" + v.getLoadKey() - + " resultY=" + result - + " fallback=surface"); - } - - if (traceRegen) { - IrisLogging.debug("Regen object placement result: chunk=" + chunkX + "," + chunkZ - + " scope=" + scope - + " object=" + v.getLoadKey() - + " resultY=" + result - + " px=" + xx - + " pz=" + zz - + " fallback=surface" - + " densityIndex=" + i - + " density=" + density); - } - } catch (Throwable e) { - GenerationFailures.skipPlacement(e, "object", v.getLoadKey(), chunkX << 4, -1, chunkZ << 4, scope); - errors++; - } finally { - writer.endObjectPlacement(); - } + ObjectPlacementResult result = placeObjectAt(writer, rng, v, xx, zz, objectPlacement, complex, traceRegen, chunkX, chunkZ, scope, i, density); + placed += result.placed(); + rejected += result.rejected(); + errors += result.errors(); } return new ObjectPlacementResult(attempts, placed, rejected, nullObjects, errors); } + private void traceNullObject(boolean traceRegen, int chunkX, int chunkZ, String scope, int densityIndex, int density, IrisObjectPlacement objectPlacement) { + if (!traceRegen) { + return; + } + IrisLogging.debug("Regen object placement null object: chunk=" + chunkX + "," + chunkZ + + " scope=" + scope + + " densityIndex=" + densityIndex + + " density=" + density + + " placementKeys=" + objectPlacement.getPlace().toString(",")); + } + + @BlockCoordinates + private ObjectPlacementResult placeObjectAt( + ObjectPassPlacer writer, + RNG rng, + IrisObject v, + int xx, + int zz, + IrisObjectPlacement objectPlacement, + IrisComplex complex, + boolean traceRegen, + int chunkX, + int chunkZ, + String scope, + int densityIndex, + int density + ) { + IrisObjectPlacement effectivePlacement = resolveEffectivePlacement(objectPlacement, v); + if (!allowsObjectPlacement(complex, v, effectivePlacement, xx, zz)) { + return new ObjectPlacementResult(0, 0, 1, 0, 0); + } + boolean golden = GoldenDebugObjectPlacer.isGoldenDebugChunk(chunkX, chunkZ); + boolean treePlacement = isTreePlacement(v, effectivePlacement); + int id = rng.i(0, Integer.MAX_VALUE); + IObjectPlacer placePlacer = golden ? new GoldenDebugObjectPlacer(writer, scope + "/" + v.getLoadKey()) : writer; + if (golden) { + IrisLogging.debug("Goldendebug object attempt: chunk=" + chunkX + "," + chunkZ + + " scope=" + scope + + " object=" + v.getLoadKey() + + " densityIndex=" + densityIndex + + " xx=" + xx + + " zz=" + zz + + " mode=" + effectivePlacement.getMode()); + } + try { + String marker = placementMarker(v, id, "surface"); + int continuation = writer.beginObjectPlacement(effectivePlacement.waterRule()); + int result = v.place(xx, zz, placePlacer, effectivePlacement, rng, (b, data) -> { + if (marker != null) { + writer.setData(b.getX(), b.getY(), b.getZ(), marker); + } + if (treePlacement && marker != null) { + writeTreeMaterial(writer, b.getX(), b.getY(), b.getZ(), data); + } + if (effectivePlacement.isDolphinTarget() && effectivePlacement.isUnderwater() && B.isStorageChest(data)) { + writer.setData(b.getX(), b.getY(), b.getZ(), MatterStructurePOI.BURIED_TREASURE); + } + }, null, getData()); + + if (result >= 0) { + writer.recordObjectPlacement(continuation); + } + + if (golden) { + IrisLogging.debug("Goldendebug object result: chunk=" + chunkX + "," + chunkZ + + " scope=" + scope + + " object=" + v.getLoadKey() + + " resultY=" + result + + " fallback=surface"); + } + + if (traceRegen) { + IrisLogging.debug("Regen object placement result: chunk=" + chunkX + "," + chunkZ + + " scope=" + scope + + " object=" + v.getLoadKey() + + " resultY=" + result + + " px=" + xx + + " pz=" + zz + + " fallback=surface" + + " densityIndex=" + densityIndex + + " density=" + density); + } + return result >= 0 ? new ObjectPlacementResult(0, 1, 0, 0, 0) : new ObjectPlacementResult(0, 0, 1, 0, 0); + } catch (Throwable e) { + GenerationFailures.skipPlacement(e, "object", v.getLoadKey(), chunkX << 4, -1, chunkZ << 4, scope); + return new ObjectPlacementResult(0, 0, 0, 0, 1); + } finally { + writer.endObjectPlacement(); + } + } + + private Optional placeSurfacePlacement( + ObjectPassPlacer writer, + RNG rng, + int chunkX, + int chunkZ, + IrisObjectPlacement placement, + IrisComplex complex, + boolean traceRegen, + String scope, + String ownerKey + ) { + try { + if (placement.isSeparated()) { + return placeSeparatedObject(writer, chunkX, chunkZ, placement, complex, traceRegen, scope); + } + double chance = objectSeparation().chanceAt(placement, (chunkX << 4) + 8, (chunkZ << 4) + 8); + boolean passes = rng.chance(chance + rng.d(-0.005, 0.005)) && !placement.isCompatExcluded(getData()); + traceSurfaceChance(traceRegen, chunkX, chunkZ, scope, passes, placement); + if (!passes) { + return Optional.empty(); + } + return Optional.of(placeObject(writer, rng, chunkX << 4, chunkZ << 4, placement, complex, traceRegen, chunkX, chunkZ, scope)); + } catch (Throwable e) { + GenerationFailures.skipPlacement(e, "object", placement.getPlace().toString(", "), chunkX << 4, -1, chunkZ << 4, ownerKey); + return Optional.of(new ObjectPlacementResult(0, 0, 0, 0, 1)); + } + } + + private void traceSurfaceChance(boolean traceRegen, int chunkX, int chunkZ, String scope, boolean passes, IrisObjectPlacement placement) { + if (!traceRegen) { + return; + } + IrisLogging.debug("Regen object placer chance: chunk=" + chunkX + "," + chunkZ + + " scope=" + scope + + " chanceResult=" + passes + + " chanceBase=" + placement.getChance() + + " densityMid=" + placement.getDensity() + + " objects=" + placement.getPlace().size()); + } + + private Optional placeSeparatedObject( + ObjectPassPlacer writer, + int chunkX, + int chunkZ, + IrisObjectPlacement placement, + IrisComplex complex, + boolean traceRegen, + String scope + ) { + Optional kept = placement.isCompatExcluded(getData()) + ? Optional.empty() + : objectSeparation().keptCandidate(chunkX, chunkZ, placement); + traceSurfaceChance(traceRegen, chunkX, chunkZ, scope, kept.isPresent(), placement); + if (kept.isEmpty()) { + return Optional.empty(); + } + ObjectSeparation.Candidate candidate = kept.get(); + IrisObject object = placement.scaleObject(candidate.rng(), placement.getObject(complex, candidate.rng()), getDimension()); + if (object == null) { + traceNullObject(traceRegen, chunkX, chunkZ, scope, 0, 1, placement); + return Optional.of(new ObjectPlacementResult(1, 0, 0, 1, 0)); + } + ObjectPlacementResult result = placeObjectAt(writer, candidate.rng(), object, candidate.x(), candidate.z(), placement, complex, traceRegen, chunkX, chunkZ, scope, 0, 1); + return Optional.of(new ObjectPlacementResult(1, result.placed(), result.rejected(), 0, result.errors())); + } + + private ObjectSeparation objectSeparation() { + return new ObjectSeparation(seed() + OBJECT_SEPARATION_SEED_SALT, getData(), this::surfacePlacementsAt); + } + + private List surfacePlacementsAt(int chunkX, int chunkZ) { + IrisComplex complex = getComplex(); + if (!complex.allowsNewGenerationChunk(chunkX, chunkZ)) { + return List.of(); + } + int blockX = (chunkX << 4) + 8; + int blockZ = (chunkZ << 4) + 8; + List placements = new ArrayList<>(complex.getTrueBiomeStream().get(blockX, blockZ).getSurfaceObjects()); + placements.addAll(complex.getRegionStream().get(blockX, blockZ).getSurfaceObjects()); + placements.removeIf(placement -> placement.isCompatExcluded(getData())); + return placements; + } + @ChunkCoordinates private boolean caveAnchorBiomeConflicts(int x, int y, int z, String expectedCaveBiomeKey) { if (expectedCaveBiomeKey == null) { @@ -1754,21 +1841,34 @@ public class MantleObjectComponent extends IrisMantleComponent { IrisBiome biome = getComplex().getTrueBiomeStream().get(blockX, blockZ); IrisRegion region = getComplex().getRegionStream().get(blockX, blockZ); Set v = new KSet<>(); - for (IrisObjectPlacement i : biome.getSurfaceObjects()) { - if (rng.chance(i.getChance() + rng.d(-0.005, 0.005)) && !i.isCompatExcluded(getData())) { - v.addAll(guessPlacedKeys(rng, x, z, i)); - } - } - - for (IrisObjectPlacement i : region.getSurfaceObjects()) { - if (rng.chance(i.getChance() + rng.d(-0.005, 0.005)) && !i.isCompatExcluded(getData())) { - v.addAll(guessPlacedKeys(rng, x, z, i)); - } - } - + guessSurfaceKeys(rng, x, z, biome.getSurfaceObjects(), v); + guessSurfaceKeys(rng, x, z, region.getSurfaceObjects(), v); return v; } + private void guessSurfaceKeys(RNG rng, int x, int z, List placements, Set keys) { + ObjectSeparation separation = objectSeparation(); + for (IrisObjectPlacement i : placements) { + if (i.isSeparated()) { + guessSeparatedKey(separation, x, z, i).ifPresent(keys::add); + continue; + } + double chance = separation.chanceAt(i, (x << 4) + 8, (z << 4) + 8); + if (rng.chance(chance + rng.d(-0.005, 0.005)) && !i.isCompatExcluded(getData())) { + keys.addAll(guessPlacedKeys(rng, x, z, i)); + } + } + } + + private Optional guessSeparatedKey(ObjectSeparation separation, int x, int z, IrisObjectPlacement placement) { + if (placement.isCompatExcluded(getData())) { + return Optional.empty(); + } + return separation.keptCandidate(x, z, placement) + .map(candidate -> placement.scaleObject(candidate.rng(), placement.getObject(getComplex(), candidate.rng()), getDimension())) + .map(IrisObject::getLoadKey); + } + protected int computeRadius() { KSet warnedLargeObjects = new KSet<>(); PlacementReach reach = new ReachScan(getDimension(), this::getData, warnedLargeObjects).dimension(); diff --git a/core/src/main/java/art/arcane/iris/generation/mantle/ObjectSeparation.java b/core/src/main/java/art/arcane/iris/generation/mantle/ObjectSeparation.java new file mode 100644 index 000000000..9e5df20a3 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/generation/mantle/ObjectSeparation.java @@ -0,0 +1,137 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2022 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.generation.mantle; + +import art.arcane.iris.generation.noise.IrisGeneratorStyle; +import art.arcane.iris.pack.loading.IrisData; +import art.arcane.iris.structure.object.IrisObjectPlacement; +import art.arcane.volmlib.util.math.RNG; + +import java.util.List; +import java.util.Optional; + +/** + * The chance and spacing of surface object placements. + * + *

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 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. + */ +final class ObjectSeparation { + private static final int CHUNK_WIDTH_BLOCKS = 16; + private static final int CHUNK_SHIFT = 4; + + private final long seed; + private final IrisData data; + private final SurfacePlacements surfacePlacements; + + ObjectSeparation(long seed, IrisData data, SurfacePlacements surfacePlacements) { + this.seed = seed; + this.data = data; + this.surfacePlacements = surfacePlacements; + } + + /** The placements a chunk places on the surface, in placing order. A chunk places the biome's before the region's. */ + @FunctionalInterface + interface SurfacePlacements { + List at(int chunkX, int chunkZ); + } + + /** Where a chunk would place, in block coordinates, with the random that picks and places the object there. */ + 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; + } + } + + /** The chance to place at the block column: the placement's chance, multiplied by its chance noise when it has one. */ + double chanceAt(IrisObjectPlacement placement, double x, double z) { + IrisGeneratorStyle style = placement.getChanceStyle(); + if (style == null) { + return placement.getChance(); + } + return placement.getChance() * style.create(seed, data).fitDouble(0D, 1D, x, z); + } + + /** + * The candidate the chunk places for the separated placement; empty when it rolled none or a lower ranked one is too + * close. + */ + Optional keptCandidate(int chunkX, int chunkZ, IrisObjectPlacement placement) { + Optional own = candidateOf(chunkX, chunkZ, placement); + if (own.isEmpty() || isOutranked(own.get(), chunkX, chunkZ, placement)) { + return Optional.empty(); + } + return own; + } + + private boolean isOutranked(Candidate own, int chunkX, int chunkZ, IrisObjectPlacement placement) { + int reach = placement.getSeparation(); + int minimumChunkX = Math.floorDiv(own.x() - reach, CHUNK_WIDTH_BLOCKS); + int maximumChunkX = Math.floorDiv(own.x() + reach, CHUNK_WIDTH_BLOCKS); + int minimumChunkZ = Math.floorDiv(own.z() - reach, CHUNK_WIDTH_BLOCKS); + int maximumChunkZ = Math.floorDiv(own.z() + reach, CHUNK_WIDTH_BLOCKS); + for (int x = minimumChunkX; x <= maximumChunkX; x++) { + for (int z = minimumChunkZ; z <= maximumChunkZ; z++) { + if ((x != chunkX || z != chunkZ) && isOutrankedBy(x, z, own, placement)) { + return true; + } + } + } + return false; + } + + private boolean isOutrankedBy(int rivalChunkX, int rivalChunkZ, Candidate own, IrisObjectPlacement placement) { + Optional rival = placementOfSameObjectsAt(rivalChunkX, rivalChunkZ, placement) + .flatMap(other -> candidateOf(rivalChunkX, rivalChunkZ, other)); + return rival.isPresent() && rival.get().rank() < own.rank() && rival.get().isCloserThan(own, placement.getSeparation()); + } + + private Optional placementOfSameObjectsAt(int chunkX, int chunkZ, IrisObjectPlacement placement) { + return surfacePlacements.at(chunkX, chunkZ).stream() + .filter(other -> other.isSeparated() && other.getPlace().equals(placement.getPlace())) + .findFirst(); + } + + private Optional candidateOf(int chunkX, int chunkZ, IrisObjectPlacement placement) { + RNG rng = new RNG(chunkSeed(chunkX, chunkZ, placement.getPlace().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); + } +} diff --git a/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java b/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java index 60d98d731..fbde0c113 100644 --- a/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java +++ b/core/src/main/java/art/arcane/iris/structure/object/IrisObjectPlacement.java @@ -116,8 +116,13 @@ public class IrisObjectPlacement { @MaxNumber(1) @Description("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; + @Description("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 IrisGeneratorStyle chanceStyle = null; + @MinNumber(0) + @Description("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) - @Description("If the chance check passes, place this many in a single chunk") + @Description("If the chance check passes, place this many in a single chunk. Ignored when separation is set.") private int density = 1; @Description("If the chance check passes, and you specify this, it picks a number in the range based on noise, and 'density' is ignored.") private IrisStyledRange densityStyle = null; @@ -342,6 +347,8 @@ public class IrisObjectPlacement { p.setDensity(density); p.setDensityStyle(densityStyle); p.setChance(chance); + p.setChanceStyle(chanceStyle); + p.setSeparation(separation); p.setSnow(snow); p.setClamp(clamp); p.setRotation(rotation); @@ -407,6 +414,10 @@ public class IrisObjectPlacement { return false; } + public boolean isSeparated() { + return separation > 0; + } + public int getDensity() { if (densityStyle == null) { return density; diff --git a/core/src/test/java/art/arcane/iris/generation/mantle/ObjectSeparationTest.java b/core/src/test/java/art/arcane/iris/generation/mantle/ObjectSeparationTest.java new file mode 100644 index 000000000..1bdd91d8d --- /dev/null +++ b/core/src/test/java/art/arcane/iris/generation/mantle/ObjectSeparationTest.java @@ -0,0 +1,140 @@ +package art.arcane.iris.generation.mantle; + +import art.arcane.iris.generation.noise.IrisGeneratorStyle; +import art.arcane.iris.generation.noise.NoiseStyle; +import art.arcane.iris.structure.object.IrisObjectPlacement; +import art.arcane.volmlib.util.collection.KList; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ObjectSeparationTest { + private static final long SEED = 1337L; + private static final int SEPARATION_BLOCKS = 40; + private static final int GRID_HALF_WIDTH_CHUNKS = 24; + + @Test + public void keptObjectsStayApartAcrossBiomeBorders() { + IrisObjectPlacement westBiome = separated(1, SEPARATION_BLOCKS, "tree"); + IrisObjectPlacement eastBiome = separated(1, SEPARATION_BLOCKS, "tree"); + ObjectSeparation separation = separation( + (chunkX, chunkZ) -> List.of(chunkX < 0 ? westBiome : eastBiome)); + + List kept = keptOnGrid(separation, (chunkX, chunkZ) -> chunkX < 0 ? westBiome : eastBiome); + + assertTrue(kept.size() > 10); + for (int first = 0; first < kept.size(); first++) { + for (int second = first + 1; second < kept.size(); second++) { + assertFalse(kept.get(first).isCloserThan(kept.get(second), SEPARATION_BLOCKS)); + } + } + } + + @Test + public void keptObjectsDoNotDependOnQueryOrder() { + IrisObjectPlacement placement = separated(1, SEPARATION_BLOCKS, "tree"); + ObjectSeparation separation = separation((chunkX, chunkZ) -> List.of(placement)); + + List forward = keptOnGrid(separation, (chunkX, chunkZ) -> placement); + List reversedRanks = new ArrayList<>(); + for (int chunkX = GRID_HALF_WIDTH_CHUNKS; chunkX >= -GRID_HALF_WIDTH_CHUNKS; chunkX--) { + for (int chunkZ = GRID_HALF_WIDTH_CHUNKS; chunkZ >= -GRID_HALF_WIDTH_CHUNKS; chunkZ--) { + separation.keptCandidate(chunkX, chunkZ, placement).ifPresent(candidate -> reversedRanks.add(candidate.rank())); + } + } + + List forwardRanks = forward.stream().map(ObjectSeparation.Candidate::rank).sorted().toList(); + assertEquals(forwardRanks, reversedRanks.stream().sorted().toList()); + } + + @Test + public void placementsWithOtherObjectsDoNotKeepEachOtherApart() { + IrisObjectPlacement trees = separated(1, SEPARATION_BLOCKS, "tree"); + IrisObjectPlacement rocks = separated(1, SEPARATION_BLOCKS, "rock"); + ObjectSeparation treesOnly = separation((chunkX, chunkZ) -> List.of(trees)); + ObjectSeparation both = separation((chunkX, chunkZ) -> List.of(trees, rocks)); + + assertEquals( + keptOnGrid(treesOnly, (chunkX, chunkZ) -> trees).size(), + keptOnGrid(both, (chunkX, chunkZ) -> trees).size()); + } + + @Test + public void zeroChanceKeepsNothing() { + IrisObjectPlacement placement = separated(0, SEPARATION_BLOCKS, "tree"); + ObjectSeparation separation = separation((chunkX, chunkZ) -> List.of(placement)); + + assertTrue(keptOnGrid(separation, (chunkX, chunkZ) -> placement).isEmpty()); + } + + @Test + public void wideSeparationThinsObjectsOut() { + IrisObjectPlacement narrow = separated(1, 8, "tree"); + IrisObjectPlacement wide = separated(1, 96, "tree"); + + int narrowCount = keptOnGrid(separation((chunkX, chunkZ) -> List.of(narrow)), (chunkX, chunkZ) -> narrow).size(); + int wideCount = keptOnGrid(separation((chunkX, chunkZ) -> List.of(wide)), (chunkX, chunkZ) -> wide).size(); + + assertTrue(narrowCount > wideCount * 2); + } + + @Test + public void placementsStayUnchangedByDefault() { + IrisObjectPlacement placement = new IrisObjectPlacement(); + placement.setChance(0.35); + + assertFalse(placement.isSeparated()); + assertEquals(0.35, separation((chunkX, chunkZ) -> List.of()).chanceAt(placement, 12, -40), 0D); + } + + @Test + public void chanceStyleScalesChanceWithoutRaisingIt() { + IrisObjectPlacement placement = new IrisObjectPlacement(); + placement.setChance(0.5); + placement.setChanceStyle(new IrisGeneratorStyle(NoiseStyle.STATIC)); + ObjectSeparation separation = separation((chunkX, chunkZ) -> List.of()); + + Optional lowest = Optional.empty(); + Optional highest = Optional.empty(); + for (int x = 0; x < 200; x += 7) { + double chance = separation.chanceAt(placement, x, x * 3); + assertTrue(chance >= 0D && chance <= 0.5); + lowest = Optional.of(Math.min(chance, lowest.orElse(chance))); + highest = Optional.of(Math.max(chance, highest.orElse(chance))); + } + assertTrue(highest.get() - lowest.get() > 0.1); + } + + private static List keptOnGrid(ObjectSeparation separation, PlacementAt placementAt) { + List kept = new ArrayList<>(); + for (int chunkX = -GRID_HALF_WIDTH_CHUNKS; chunkX <= GRID_HALF_WIDTH_CHUNKS; chunkX++) { + for (int chunkZ = -GRID_HALF_WIDTH_CHUNKS; chunkZ <= GRID_HALF_WIDTH_CHUNKS; chunkZ++) { + separation.keptCandidate(chunkX, chunkZ, placementAt.at(chunkX, chunkZ)).ifPresent(kept::add); + } + } + return kept; + } + + private static ObjectSeparation separation(ObjectSeparation.SurfacePlacements surfacePlacements) { + return new ObjectSeparation(SEED, null, surfacePlacements); + } + + private static IrisObjectPlacement separated(double chance, int separationBlocks, String... objects) { + IrisObjectPlacement placement = new IrisObjectPlacement(); + placement.setPlace(new KList<>(objects)); + placement.setChance(chance); + placement.setSeparation(separationBlocks); + return placement; + } + + @FunctionalInterface + private interface PlacementAt { + IrisObjectPlacement at(int chunkX, int chunkZ); + } +}