From b59538ca6f0af4e3b2f02823f0c6bed2436cf667 Mon Sep 17 00:00:00 2001 From: piexel Date: Mon, 12 Oct 2026 02:01:13 +0200 Subject: [PATCH] iris:deposit ported onto IrisApi and listed in the built-ins jar --- builtin-modules.txt | 4 + .../art/arcane/iris/builtin/IrisSeed.java | 2 +- .../iris/engine/modifier/DepositClump.java | 70 +++ .../iris/engine/modifier/DepositModule.java | 8 +- .../iris/engine/modifier/DepositPlacer.java | 488 ++++++++++-------- .../iris/engine/modifier/ResolvedDeposit.java | 136 +++-- .../modifier/ResolvedDepositVariant.java | 45 +- 7 files changed, 453 insertions(+), 300 deletions(-) create mode 100644 src/main/java/art/arcane/iris/engine/modifier/DepositClump.java diff --git a/builtin-modules.txt b/builtin-modules.txt index 824782905..7691fee17 100644 --- a/builtin-modules.txt +++ b/builtin-modules.txt @@ -18,3 +18,7 @@ module art.arcane.iris.engine.modifier.PostModule include art/arcane/iris/engine/modifier/SurfaceSmoothing*.class module art.arcane.iris.engine.modifier.PerfectionModule module art.arcane.iris.engine.modifier.MatterModule +module art.arcane.iris.engine.modifier.DepositModule +include art/arcane/iris/engine/modifier/DepositPlacer*.class +include art/arcane/iris/engine/modifier/DepositClump*.class +include art/arcane/iris/engine/modifier/ResolvedDeposit*.class diff --git a/src/main/java/art/arcane/iris/builtin/IrisSeed.java b/src/main/java/art/arcane/iris/builtin/IrisSeed.java index 9e589b67e..9f141f2d8 100644 --- a/src/main/java/art/arcane/iris/builtin/IrisSeed.java +++ b/src/main/java/art/arcane/iris/builtin/IrisSeed.java @@ -27,7 +27,7 @@ import com.volmit.world.PackWorld; * from {@link PackWorld#seed()}. */ public enum IrisSeed { - BIOME, CARVE, COMPONENT, DECORATOR, DEPOSIT, ENTITY, LOOT, MANTLE, POST, TERRAIN; + BIOME, CARVE, COMPLEX, COMPONENT, DECORATOR, DEPOSIT, ENTITY, LOOT, MANTLE, POST, TERRAIN; /** This seed of {@code world}, which must be a world Iris generates. */ public long of(PackWorld world) { diff --git a/src/main/java/art/arcane/iris/engine/modifier/DepositClump.java b/src/main/java/art/arcane/iris/engine/modifier/DepositClump.java new file mode 100644 index 000000000..4f72c2a5a --- /dev/null +++ b/src/main/java/art/arcane/iris/engine/modifier/DepositClump.java @@ -0,0 +1,70 @@ +/* + * 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.engine.modifier; + +import art.arcane.volmlib.util.data.VectorMap; +import art.arcane.volmlib.util.math.IrisBlockVector; +import art.arcane.volmlib.util.math.Vector3i; +import com.volmit.block.BlockState; + +/** + * The blocks of one deposit clump, around its centre. Blocks are kept in a {@link VectorMap} of positions relative to the centre, + * which is how Iris always held a clump, so a clump's blocks come in the same order and a chunk's deposits draw the same random + * numbers. + */ +final class DepositClump { + private final int width; + private final int height; + private final int depth; + private final Vector3i center; + private final VectorMap blocks = new VectorMap<>(); + + DepositClump(int width, int height, int depth) { + this.width = width; + this.height = height; + this.depth = depth; + this.center = new Vector3i(width / 2, height / 2, depth / 2); + } + + /** How many blocks wide the clump's box is, along X. */ + int width() { + return width; + } + + Vector3i center() { + return center; + } + + /** The clump's blocks by their position relative to its centre. */ + VectorMap blocks() { + return blocks; + } + + /** Puts {@code block} at {@code x}, {@code y}, {@code z} counted from the corner of the clump's box. */ + void setFromCorner(int x, int y, int z, BlockState block) { + if (x >= width || y >= height || z >= depth) { + throw new IllegalArgumentException(x + " " + y + " " + z + " exceeds limit of " + width + " " + height + " " + depth); + } + blocks.put(new IrisBlockVector(x - center.getX(), y - center.getY(), z - center.getZ()), block); + } + + /** Puts {@code block} on the centre, which is where a one block clump has its block. */ + void setCentre(BlockState block) { + blocks.put(new IrisBlockVector(center.getX(), center.getY(), center.getZ()), block); + } +} diff --git a/src/main/java/art/arcane/iris/engine/modifier/DepositModule.java b/src/main/java/art/arcane/iris/engine/modifier/DepositModule.java index 668755e37..64b521cf8 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/DepositModule.java +++ b/src/main/java/art/arcane/iris/engine/modifier/DepositModule.java @@ -24,7 +24,6 @@ import art.arcane.iris.builtin.deposit.DepositSettings; import art.arcane.iris.builtin.deposit.DepositVariantSettings; import art.arcane.iris.builtin.deposit.OreFrequencySettings; import art.arcane.iris.builtin.deposit.OreSizeSettings; -import art.arcane.iris.core.modules.IrisChunk; import com.volmit.FeatureRegistry; import com.volmit.IrisModule; import com.volmit.ModuleInfo; @@ -45,7 +44,6 @@ import java.util.List; public final class DepositModule implements IrisModule { private static final String VERSION = "built-in"; private static final int CHUNK_CENTRE = 7; - private static final int CHUNK_SHIFT = 4; @Override public ModuleInfo info() { @@ -69,13 +67,11 @@ public final class DepositModule implements IrisModule { } private static void place(ChunkStageContext context, List> inChunk) { - IrisChunk chunk = IrisChunk.current(); List generators = generatorsAtCentre(context.settings(), context.originX(), context.originZ()); - if (generators.isEmpty() || chunk.bypassesMantleStages()) { + if (generators.isEmpty()) { return; } - chunk.state(DepositPlacer.class, DepositPlacer::new).generate(generators, chunk.blocks(), - chunk.originX() >> CHUNK_SHIFT, chunk.originZ() >> CHUNK_SHIFT, chunk.multicore(), chunk.context()); + DepositPlacer.of(context.world()).generate(context, generators); } /** Every level's generators at the centre column of the chunk whose first block is {@code originX}, {@code originZ}, in level order. */ diff --git a/src/main/java/art/arcane/iris/engine/modifier/DepositPlacer.java b/src/main/java/art/arcane/iris/engine/modifier/DepositPlacer.java index b5f38119f..f611b34f8 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/DepositPlacer.java +++ b/src/main/java/art/arcane/iris/engine/modifier/DepositPlacer.java @@ -1,6 +1,6 @@ /* - * Iris is a World Generator for Minecraft Bukkit Servers - * Copyright (c) 2022 Arcane Arts (Volmit Software) + * 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 @@ -15,9 +15,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package art.arcane.iris.engine.modifier; +import art.arcane.iris.builtin.IrisSeed; import art.arcane.iris.builtin.deposit.DepositGenerator; import art.arcane.iris.builtin.deposit.DepositHeightDistribution; import art.arcane.iris.builtin.deposit.DepositPlacementScope; @@ -25,213 +25,341 @@ import art.arcane.iris.builtin.deposit.DepositVariant; import art.arcane.iris.builtin.deposit.DepositVariantSettings; import art.arcane.iris.builtin.deposit.OreFrequencySettings; import art.arcane.iris.builtin.deposit.OreSizeSettings; -import art.arcane.iris.core.loader.IrisRegistrant; -import art.arcane.iris.engine.framework.Engine; -import art.arcane.iris.engine.framework.EngineAssignedComponent; -import art.arcane.iris.engine.object.world.biome.IrisBiome; -import art.arcane.iris.engine.object.world.dimension.IrisDimension; -import art.arcane.iris.engine.object.prefab.IrisObject; -import art.arcane.iris.engine.object.procedural.IrisProceduralBlocks; -import art.arcane.iris.engine.object.world.region.IrisRegion; -import art.arcane.iris.util.project.context.ChunkContext; -import art.arcane.volmlib.util.data.HeightMap; -import art.arcane.iris.util.project.hunk.Hunk; -import art.arcane.volmlib.util.mantle.runtime.MantleChunk; +import art.arcane.iris.engine.modifier.ResolvedDeposit.BiomeIdentity; +import art.arcane.volmlib.util.math.IrisBlockVector; import art.arcane.volmlib.util.math.RNG; -import art.arcane.volmlib.util.matter.MatterCavern; -import art.arcane.iris.util.common.parallel.BurstExecutor; -import art.arcane.volmlib.util.scheduling.PrecisionStopwatch; -import art.arcane.iris.spi.PlatformBlockState; -import art.arcane.iris.util.common.data.B; +import com.volmit.FeatureAttachment.Target; +import com.volmit.ModuleLog; +import com.volmit.block.BlockRegistry; +import com.volmit.block.BlockState; +import com.volmit.block.BlockTrait; +import com.volmit.config.FeatureSettings; +import com.volmit.config.FeatureSettings.Entry; +import com.volmit.hunk.Hunk; +import com.volmit.mantle.MantleLayer; +import com.volmit.resource.PackResources; +import com.volmit.resource.ResourceId; +import com.volmit.resource.ResourceKey; +import com.volmit.stage.ChunkStageContext; +import com.volmit.world.BiomeSample; +import com.volmit.world.PackBiome; +import com.volmit.world.PackWorld; +import com.volmit.world.TerrainSampler; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Predicate; +import java.util.function.Supplier; /** - * Places deposit clumps into one chunk for {@link DepositModule}: one per engine runtime, holding the deposit - * stream's random and each generator made ready for this engine ({@link ResolvedDeposit}: resolved blocks and clump - * shapes). Each generator gets its own random, derived from the chunk and its position in the list. + * Places deposit clumps into one chunk for {@link DepositModule}: one per world, holding the deposit stream's random and each + * generator made ready for this world ({@link ResolvedDeposit}: resolved blocks and clump shapes). Each generator gets its own + * random, derived from the chunk and its position in the list, and the generators of a chunk run one after the other. */ -public class DepositPlacer extends EngineAssignedComponent { - private final RNG rng; +final class DepositPlacer { private static final double UNCHANGED_ORE_MULTIPLIER = 1D; + private static final int CHUNK_SHIFT = 4; + private static final int CHUNK_WIDTH = 16; + private static final long CHUNK_X_SEED_FACTOR = 341873128712L; + private static final long CHUNK_Z_SEED_FACTOR = 132897987541L; + private static final int DEFAULT_SURFACE_CLEARANCE = 7; + private static final BlockState BEDROCK = new BlockState("minecraft:bedrock"); + private final RNG rng; + private final BlockRegistry registry; + private final PackResources resources; + private final ModuleLog log; + private final boolean tunesOres; + private final boolean remapsOres; private final Map resolved = new ConcurrentHashMap<>(); private final Map variants = new ConcurrentHashMap<>(); + private final Map> biomeIdentities = new ConcurrentHashMap<>(); - public DepositPlacer(Engine engine) { - super(engine, "Deposit"); - rng = new RNG(getEngine().getSeedManager().getDeposit()); + DepositPlacer(PackWorld world) { + this.rng = new RNG(IrisSeed.DEPOSIT.of(world)); + this.resources = world.resources(); + this.registry = resources.blocks(); + this.log = world.log(); + this.tunesOres = declares(world, OreFrequencySettings.class) || declares(world, OreSizeSettings.class); + this.remapsOres = declares(world, DepositVariantSettings.class); } - /** Runs {@code generators}, in order, on the chunk at chunk coordinates {@code x}, {@code z}. */ - public void generate(List generators, Hunk terrain, int x, int z, boolean multicore, - ChunkContext context) { - PrecisionStopwatch p = PrecisionStopwatch.start(); - generateDeposits(generators, terrain, x, z, multicore, context); - getEngine().getMetrics().getDeposit().put(p.getMilliseconds()); + /** The world's deposit placer, kept in its state store. */ + static DepositPlacer of(PackWorld world) { + return world.state(DepositPlacer.class, DepositPlacer::new); } - private void generateDeposits(List generators, Hunk terrain, int x, int z, - boolean multicore, ChunkContext context) { - BurstExecutor burst = burst().burst(multicore); + /** Whether any level of the pack declares {@code settings}; a world that declares none never asks a biome for them. */ + private static boolean declares(PackWorld world, Class settings) { + return !world.settings().declared(settings).isEmpty(); + } - long seed = x * 341873128712L + z * 132897987541L; + /** Runs {@code generators}, in order, on the chunk of {@code context}. */ + void generate(ChunkStageContext context, List generators) { + Placing placing = new Placing(context); + int chunkX = context.originX() >> CHUNK_SHIFT; + int chunkZ = context.originZ() >> CHUNK_SHIFT; + long seed = chunkX * CHUNK_X_SEED_FACTOR + chunkZ * CHUNK_Z_SEED_FACTOR; long mask = 0; - MantleChunk chunk = getEngine().getMantle().getMantle().getChunk(x, z).use(); - try { - for (DepositGenerator k : generators) { - long finalSeed = seed * ++mask; - burst.queue(() -> generate(k, chunk, terrain, rng.nextParallelRNG(finalSeed), x, z, false, context)); - } - } finally { - // complete() must run before release() even when queueing throws — already - // submitted burst tasks must never write into a released chunk. - try { - burst.complete(); - } finally { - chunk.release(); - } + for (DepositGenerator generator : generators) { + long finalSeed = seed * ++mask; + placing.generate(generator, rng.nextParallelRNG(finalSeed)); } } - public void generate(DepositGenerator k, MantleChunk chunk, Hunk data, RNG rng, int cx, int cz, boolean safe, ChunkContext context) { - generate(k, chunk, data, rng, cx, cz, safe, null, context); + private ResolvedDeposit resolved(DepositGenerator generator) { + return resolved.computeIfAbsent(generator, ignored -> new ResolvedDeposit(generator, rng.getSeed(), registry)); } - public void generate(DepositGenerator k, MantleChunk chunk, Hunk data, RNG rng, int cx, int cz, boolean safe, HeightMap he, ChunkContext context) { - ResolvedDeposit deposit = resolved(k); - if (k.spawnChance() < rng.d()) - return; + private Optional identityOf(ResourceId biome) { + return biomeIdentities.computeIfAbsent(biome, id -> resources.find(new ResourceKey<>(id, PackBiome.class)) + .map(found -> new BiomeIdentity(id.path(), found.platformBiome().toString(), found.vanillaBiome().toString()))); + } - for (int l = 0; l < rng.i(k.minPerChunk(), k.maxPerChunk() + 1); l++) { - if (k.perClumpSpawnChance() < rng.d()) - continue; + /** Places deposits into one chunk, with the views of its invocation. */ + private final class Placing { + private final Hunk blocks; + private final TerrainSampler terrain; + private final FeatureSettings settings; + private final MantleLayer caverns; + private final int originX; + private final int originZ; + private final int worldHeight; + private final int minWorldY; + private final int[] surfaceHeights = new int[CHUNK_WIDTH * CHUNK_WIDTH]; + private boolean surfaceKnown; + private final Map airStates = new IdentityHashMap<>(); + private final Map replaceableStates = new IdentityHashMap<>(); + private final Map> suitedOres = new IdentityHashMap<>(); - IrisObject clump = deposit.clump(rng); + Placing(ChunkStageContext context) { + this.blocks = context.hunk(BlockState.class); + this.terrain = context.terrain(); + this.settings = context.settings(); + this.caverns = context.mantle().caverns(); + this.originX = context.originX(); + this.originZ = context.originZ(); + this.worldHeight = blocks.getHeight(); + this.minWorldY = terrain.minWorldY(); + } - int dim = clump.getW(); - int min = dim / 2; - int max = (int) (16D - dim / 2D); + void generate(DepositGenerator k, RNG rng) { + ResolvedDeposit deposit = resolved(k); + if (k.spawnChance() < rng.d()) + return; - if (min > max || min < 0 || max > 15) { - min = 6; - max = 9; - } + for (int l = 0; l < rng.i(k.minPerChunk(), k.maxPerChunk() + 1); l++) { + if (k.perClumpSpawnChance() < rng.d()) + continue; - int x = rng.i(min, max + 1); - int z = rng.i(min, max + 1); - int terrainSurface = getDepositTerrainSurface(cx, cz, x, z, he, context); - int height = k.placementScope() == DepositPlacementScope.TERRAIN - ? depositSurfaceLimit(terrainSurface, k.surfaceClearance()) - : getEngine().getHeight() - 1; + DepositClump clump = deposit.clump(rng); - if (height < 0) { - continue; - } + int dim = clump.width(); + int min = dim / 2; + int max = (int) (16D - dim / 2D); - int y = sampleHeight( - k.heightDistribution(), rng, k.minHeight(), k.maxHeight(), - Math.min(height, getEngine().getHeight() - 1)); - if (y == Integer.MIN_VALUE) { - continue; - } + if (min > max || min < 0 || max > 15) { + min = 6; + max = 9; + } - boolean clippedHeight = k.heightDistribution() == DepositHeightDistribution.CLIPPED_UNIFORM; - if (clippedHeight && y > height - 2) - continue; + int x = rng.i(min, max + 1); + int z = rng.i(min, max + 1); + int terrainSurface = terrainSurface(x, z); + int height = k.placementScope() == DepositPlacementScope.TERRAIN + ? depositSurfaceLimit(terrainSurface, k.surfaceClearance()) + : worldHeight - 1; - int biomeY = Math.max(0, Math.min(getEngine().getHeight() - 1, y)); - boolean oreDeposit = deposit.isOre(); - IrisBiome surfaceBiome = context.getBiome().get(x, z); - IrisBiome depositBiome = oreDeposit || k.usesCaveBiomeFilter() - ? getEngine().getCaveBiome((cx << 4) + x, biomeY, (cz << 4) + z) - : null; - if (!deposit.matchesBiome(surfaceBiome, depositBiome)) { - continue; - } + if (height < 0) { + continue; + } - if (oreDeposit) { - if (depositBiome != null) { - double frequencyMultiplier = oreFrequencyMultiplier(depositBiome); - if (frequencyMultiplier < 1D - && !passesOreFrequency(frequencyMultiplier, rng.d())) { - continue; - } + int y = sampleHeight( + k.heightDistribution(), rng, k.minHeight(), k.maxHeight(), + Math.min(height, worldHeight - 1)); + if (y == Integer.MIN_VALUE) { + continue; + } - double sizeMultiplier = oreSizeMultiplier(depositBiome); - if (sizeMultiplier != 1D) { - IrisObject scaledClump = deposit.clump(rng, sizeMultiplier); - int scaledDimension = scaledClump.getW(); - x = clampDepositCenter(x, scaledDimension, 16); - if (clippedHeight) { - y = clampDepositCenter(y, scaledDimension, getEngine().getHeight()); + boolean clippedHeight = k.heightDistribution() == DepositHeightDistribution.CLIPPED_UNIFORM; + if (clippedHeight && y > height - 2) + continue; + + int biomeY = Math.max(0, Math.min(worldHeight - 1, y)); + boolean oreDeposit = deposit.isOre(); + Supplier biomes = biomesAt(x, biomeY, z); + if (!deposit.matchesBiome(() -> identityOf(biomes.get().surfaceBiome()).orElse(null), + () -> biomes.get().caveBiome().flatMap(id -> identityOf(id)).orElse(null))) { + continue; + } + + if (oreDeposit && tunesOres) { + Optional caveBiome = biomes.get().caveBiome(); + if (caveBiome.isPresent()) { + double frequencyMultiplier = oreFrequencyMultiplier(caveBiome.get()); + if (frequencyMultiplier < 1D + && !passesOreFrequency(frequencyMultiplier, rng.d())) { + continue; + } + + double sizeMultiplier = oreSizeMultiplier(caveBiome.get()); + if (sizeMultiplier != 1D) { + DepositClump scaledClump = deposit.clump(rng, sizeMultiplier); + int scaledDimension = scaledClump.width(); + x = clampDepositCenter(x, scaledDimension, 16); + if (clippedHeight) { + y = clampDepositCenter(y, scaledDimension, worldHeight); + } + z = clampDepositCenter(z, scaledDimension, 16); + clump = scaledClump; } - z = clampDepositCenter(z, scaledDimension, 16); - clump = scaledClump; } } + + placeClump(k, deposit, clump, rng, x, y, z); } + } - IrisDimension dimension = getDimension(); - - for (art.arcane.volmlib.util.math.IrisBlockVector j : clump.getBlocks().keys()) { + private void placeClump(DepositGenerator k, ResolvedDeposit deposit, DepositClump clump, RNG rng, int x, int y, int z) { + for (IrisBlockVector j : clump.blocks().keys()) { int nx = j.getBlockX() + x; int ny = j.getBlockY() + y; int nz = j.getBlockZ() + z; - if (nx > 15 || nx < 0 || ny >= getEngine().getHeight() || ny < 0 || nz < 0 || nz > 15) { + if (nx > 15 || nx < 0 || ny >= worldHeight || ny < 0 || nz < 0 || nz > 15) { continue; } - int columnSurface = getDepositTerrainSurface(cx, cz, nx, nz, he, context); + int columnSurface = terrainSurface(nx, nz); if (!placementSurfaceAllows( k.placementScope(), ny, columnSurface, k.surfaceClearance())) { continue; } - PlatformBlockState current = data.get(nx, ny, nz); - if (!canReplaceDepositTarget(current)) { + BlockState current = blocks.get(nx, ny, nz); + if (!canReplaceTarget(current)) { continue; } if (!deposit.canReplace(current)) { continue; } - if (!k.replaceBedrock() && IrisProceduralBlocks.materialKey(current).equals("minecraft:bedrock")) { + if (!k.replaceBedrock() && current.material().equals(BEDROCK.material())) { continue; } if (shouldDiscardExposed( - k.discardChanceOnAirExposure(), rng.d(), isAdjacentToAir(data, nx, ny, nz))) { + k.discardChanceOnAirExposure(), rng.d(), isAdjacentToAir(blocks, nx, ny, nz, this::isAir))) { continue; } - if (chunk.get(nx, ny, nz, MatterCavern.class) == null) { - PlatformBlockState ore = clump.getBlocks().get(j); - PlatformBlockState remapped = resolveDepositVariant( - cx, cz, nx, ny, nz, ore, dimension, context); - PlatformBlockState finalBlock = remapped != null + if (caverns.get(originX + nx, ny, originZ + nz) == null) { + BlockState ore = clump.blocks().get(j); + BlockState remapped = remapped(nx, ny, nz, ore); + BlockState finalBlock = remapped != null ? remapped - : B.toDeepSlateOre(current, ore); - data.set(nx, ny, nz, finalBlock); + : oreSuiting(current, ore); + blocks.set(nx, ny, nz, finalBlock); } } } - } - private ResolvedDeposit resolved(DepositGenerator generator) { - return resolved.computeIfAbsent(generator, ignored -> ResolvedDeposit.of(generator, getEngine())); - } + /** Asks the host for the biomes of the column only if a filter or an ore tuning reads them, and only once. */ + private Supplier biomesAt(int localX, int y, int localZ) { + BiomeSample[] sample = new BiomeSample[1]; + return () -> { + if (sample[0] == null) { + sample[0] = terrain.biome(originX + localX, y, originZ + localZ); + } + return sample[0]; + }; + } - private int getDepositTerrainSurface( - int cx, int cz, int localX, int localZ, HeightMap heightMap, - ChunkContext context) { - return heightMap != null - ? heightMap.getHeight((cx << 4) + localX, (cz << 4) + localZ) - : context.getRoundedHeight(localX, localZ); + /** {@code ore}, or the variant of it that suits the rock it replaces; the platform knows the variants, so each pair is asked once. */ + private BlockState oreSuiting(BlockState rock, BlockState ore) { + return suitedOres.computeIfAbsent(rock, known -> new IdentityHashMap<>()) + .computeIfAbsent(ore, known -> registry.oreFor(rock, known)); + } + + private boolean isAir(BlockState state) { + return airStates.computeIfAbsent(state, known -> registry.hasTrait(known, BlockTrait.AIR)); + } + + private boolean canReplaceTarget(BlockState state) { + return replaceableStates.computeIfAbsent(state, known -> canReplaceDepositTarget(registry, known)); + } + + /** The height of the terrain at a column of the chunk, as the stages before the deposits see it. */ + private int terrainSurface(int localX, int localZ) { + if (!surfaceKnown) { + for (int x = 0; x < CHUNK_WIDTH; x++) { + for (int z = 0; z < CHUNK_WIDTH; z++) { + surfaceHeights[x * CHUNK_WIDTH + z] = (int) Math.round(terrain.surfaceY(originX + x, originZ + z)); + } + } + surfaceKnown = true; + } + return surfaceHeights[localX * CHUNK_WIDTH + localZ]; + } + + private BlockState remapped(int localX, int localY, int localZ, BlockState ore) { + if (!remapsOres) { + return null; + } + int worldX = originX + localX; + int worldZ = originZ + localZ; + int worldY = absoluteWorldY(minWorldY, localY); + + Optional caveBiome = terrain.biome(worldX, localY, worldZ).caveBiome(); + if (caveBiome.isPresent()) { + BlockState match = matchDepositVariant(settings.declaredBy(caveBiome.get(), DepositVariantSettings.class), ore, worldY); + if (match != null) { + return match; + } + } + + BlockState match = matchDepositVariant(levelSettings(Target.REGION, worldX, worldZ), ore, worldY); + if (match != null) { + return match; + } + + return matchDepositVariant(levelSettings(Target.DIMENSION, worldX, worldZ), ore, worldY); + } + + private Optional levelSettings(Target level, int worldX, int worldZ) { + return settings.at(DepositVariantSettings.class, worldX, worldZ).stream() + .filter(entry -> entry.level() == level) + .map(Entry::value) + .findFirst(); + } + + private BlockState matchDepositVariant(Optional owner, BlockState ore, int y) { + for (DepositVariant declared : owner.map(DepositVariantSettings::variants).orElse(List.of())) { + ResolvedDepositVariant variant = variants.computeIfAbsent(declared, rule -> new ResolvedDepositVariant(rule, registry, log)); + if (!variant.appliesAt(y)) { + continue; + } + + BlockState swapped = variant.remapOrNull(ore); + if (swapped != null) { + return swapped; + } + } + + return null; + } + + private double oreFrequencyMultiplier(ResourceId caveBiome) { + return settings.declaredBy(caveBiome, OreFrequencySettings.class).map(OreFrequencySettings::multiplier).orElse(UNCHANGED_ORE_MULTIPLIER); + } + + private double oreSizeMultiplier(ResourceId caveBiome) { + return settings.declaredBy(caveBiome, OreSizeSettings.class).map(OreSizeSettings::multiplier).orElse(UNCHANGED_ORE_MULTIPLIER); + } } static int depositSurfaceLimit(int surfaceY) { - return depositSurfaceLimit(surfaceY, 7); + return depositSurfaceLimit(surfaceY, DEFAULT_SURFACE_CLEARANCE); } static int depositSurfaceLimit(int surfaceY, int surfaceClearance) { @@ -252,8 +380,8 @@ public class DepositPlacer extends EngineAssignedComponent { return minHeight + localY; } - static boolean canReplaceDepositTarget(PlatformBlockState state) { - return state != null && !state.isAir() && !state.isFluid(); + static boolean canReplaceDepositTarget(BlockRegistry registry, BlockState state) { + return state != null && !registry.hasTrait(state, BlockTrait.AIR) && !registry.hasTrait(state, BlockTrait.FLUID); } static int sampleHeight( @@ -285,23 +413,22 @@ public class DepositPlacer extends EngineAssignedComponent { return adjacentToAir && chance > 0D && sample < Math.min(1D, chance); } - static boolean isAdjacentToAir(Hunk data, int x, int y, int z) { - return isAirAt(data, x - 1, y, z) - || isAirAt(data, x + 1, y, z) - || isAirAt(data, x, y - 1, z) - || isAirAt(data, x, y + 1, z) - || isAirAt(data, x, y, z - 1) - || isAirAt(data, x, y, z + 1); + static boolean isAdjacentToAir(Hunk data, int x, int y, int z, Predicate isAir) { + return isAirAt(data, x - 1, y, z, isAir) + || isAirAt(data, x + 1, y, z, isAir) + || isAirAt(data, x, y - 1, z, isAir) + || isAirAt(data, x, y + 1, z, isAir) + || isAirAt(data, x, y, z - 1, isAir) + || isAirAt(data, x, y, z + 1, isAir); } - private static boolean isAirAt(Hunk data, int x, int y, int z) { + private static boolean isAirAt(Hunk data, int x, int y, int z, Predicate isAir) { if (x < 0 || x >= data.getWidth() || y < 0 || y >= data.getHeight() || z < 0 || z >= data.getDepth()) { return false; } - PlatformBlockState state = data.getRaw(x, y, z); - return state == null || state.isAir(); + return isAir.test(data.get(x, y, z)); } static boolean passesOreFrequency(double multiplier, double sample) { @@ -313,63 +440,4 @@ public class DepositPlacer extends EngineAssignedComponent { int maximum = (int) (limit - dimension / 2D); return Math.max(minimum, Math.min(center, maximum)); } - - private PlatformBlockState resolveDepositVariant(int cx, int cz, int nx, int localY, int nz, PlatformBlockState ore, IrisDimension dimension, ChunkContext context) { - int worldX = (cx << 4) + nx; - int worldZ = (cz << 4) + nz; - int worldY = absoluteWorldY(getEngine().getMinHeight(), localY); - - IrisBiome biome = getEngine().getCaveBiome(worldX, localY, worldZ); - if (biome != null) { - PlatformBlockState match = matchDepositVariant(biome, ore, worldY); - if (match != null) { - return match; - } - } - - IrisRegion region = context.getRegion().get(nx, nz); - if (region != null) { - PlatformBlockState match = matchDepositVariant(region, ore, worldY); - if (match != null) { - return match; - } - } - - if (dimension != null) { - PlatformBlockState match = matchDepositVariant(dimension, ore, worldY); - if (match != null) { - return match; - } - } - - return null; - } - - private PlatformBlockState matchDepositVariant(IrisRegistrant owner, PlatformBlockState ore, int y) { - for (DepositVariant declared : variantsOf(owner)) { - ResolvedDepositVariant variant = variants.computeIfAbsent(declared, ResolvedDepositVariant::new); - if (!variant.appliesAt(y)) { - continue; - } - - PlatformBlockState swapped = variant.remapOrNull(ore); - if (swapped != null) { - return swapped; - } - } - - return null; - } - - private static List variantsOf(IrisRegistrant owner) { - return owner.getModuleSettings().find(DepositVariantSettings.class).map(DepositVariantSettings::variants).orElse(List.of()); - } - - private static double oreFrequencyMultiplier(IrisBiome biome) { - return biome.getModuleSettings().find(OreFrequencySettings.class).map(OreFrequencySettings::multiplier).orElse(UNCHANGED_ORE_MULTIPLIER); - } - - private static double oreSizeMultiplier(IrisBiome biome) { - return biome.getModuleSettings().find(OreSizeSettings.class).map(OreSizeSettings::multiplier).orElse(UNCHANGED_ORE_MULTIPLIER); - } } diff --git a/src/main/java/art/arcane/iris/engine/modifier/ResolvedDeposit.java b/src/main/java/art/arcane/iris/engine/modifier/ResolvedDeposit.java index 889239fc8..4ca78c01b 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/ResolvedDeposit.java +++ b/src/main/java/art/arcane/iris/engine/modifier/ResolvedDeposit.java @@ -15,35 +15,31 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package art.arcane.iris.engine.modifier; import art.arcane.iris.builtin.deposit.DepositBiomeScope; import art.arcane.iris.builtin.deposit.DepositGenerator; import art.arcane.iris.builtin.deposit.DepositShape; -import art.arcane.iris.engine.framework.Engine; -import art.arcane.iris.engine.object.prefab.IrisObject; -import art.arcane.iris.engine.object.procedural.IrisProceduralBlocks; -import art.arcane.iris.engine.object.world.biome.IrisBiome; -import art.arcane.iris.spi.PlatformBlockState; -import art.arcane.iris.util.common.data.B; -import art.arcane.volmlib.util.math.IrisBlockVector; -import art.arcane.volmlib.util.math.Vector3i; import art.arcane.volmlib.util.collection.KSet; import art.arcane.volmlib.util.math.BlockPosition; import art.arcane.volmlib.util.math.RNG; import com.volmit.block.BlockRegistry; +import com.volmit.block.BlockState; +import com.volmit.block.BlockTrait; +import com.volmit.resource.ResourceId; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; /** - * One {@link DepositGenerator} made ready for one engine: its palette resolved to the platform's blocks, whether it is - * an ore deposit, the blocks it may replace, and the clump shapes it places, built once per size. What the settings - * describe stays in the settings; everything that needs the platform, the pack or the world seed is here. + * One {@link DepositGenerator} made ready for one world: its palette resolved to blocks, whether it is an ore deposit, the blocks + * it may replace, and the clump shapes it places, built once per size. What the settings describe stays in the settings; + * everything that needs the pack or the world seed is here. */ final class ResolvedDeposit { private static final long FNV_OFFSET_BASIS = 0xcbf29ce484222325L; @@ -57,27 +53,18 @@ final class ResolvedDeposit { private final DepositGenerator generator; private final long depositSeed; - private final List palette; + private final List palette; private final boolean ore; - private final Set replaceableMaterials; - private final Map> clumps = new ConcurrentHashMap<>(); + private final Set replaceableMaterials; + private final Map> clumps = new ConcurrentHashMap<>(); - /** @param palette the generator's palette as the platform's blocks, in order */ - ResolvedDeposit(DepositGenerator generator, long depositSeed, List palette) { + /** @param registry the pack's blocks, which resolve the generator's palette and the blocks it may replace */ + ResolvedDeposit(DepositGenerator generator, long depositSeed, BlockRegistry registry) { this.generator = generator; this.depositSeed = depositSeed; - this.palette = List.copyOf(palette); - this.ore = palette.stream().anyMatch(PlatformBlockState::isOre); - this.replaceableMaterials = replaceableMaterials(generator.replaceableBlocks()); - } - - /** {@code generator} ready for {@code engine}: its blocks resolved through the pack, clumps seeded from the world's deposit seed. */ - static ResolvedDeposit of(DepositGenerator generator, Engine engine) { - BlockRegistry blocks = engine.getData().getPackResources().blocks(); - List palette = generator.palette().stream() - .map(spec -> B.getState(blocks.resolve(spec).key())) - .toList(); - return new ResolvedDeposit(generator, engine.getSeedManager().getDeposit(), palette); + this.palette = generator.palette().stream().map(registry::resolve).toList(); + this.ore = palette.stream().anyMatch(block -> registry.hasTrait(block, BlockTrait.ORE)); + this.replaceableMaterials = replaceableMaterials(registry, generator.replaceableBlocks()); } DepositGenerator generator() { @@ -90,13 +77,19 @@ final class ResolvedDeposit { } /** Whether the deposit may replace {@code state}: any block when it names none, otherwise only the named ones. */ - boolean canReplace(PlatformBlockState state) { - return generator.replaceableBlocks().isEmpty() || replaceableMaterials.contains(IrisProceduralBlocks.materialKey(state)); + boolean canReplace(BlockState state) { + return generator.replaceableBlocks().isEmpty() || replaceableMaterials.contains(state.material()); } - /** Whether the biome the generator's scope picks passes its include and exclude filters. */ - boolean matchesBiome(IrisBiome surfaceBiome, IrisBiome caveBiome) { - IrisBiome selected = generator.biomeScope() == DepositBiomeScope.SURFACE ? surfaceBiome : caveBiome; + /** + * Whether the biome the generator's scope picks passes its include and exclude filters. The biomes are asked for only when + * the generator has a filter, and a biome that is none (a dimension without cave biomes) matches no key. + */ + boolean matchesBiome(Supplier surfaceBiome, Supplier caveBiome) { + if (generator.includedBiomes().isEmpty() && generator.excludedBiomes().isEmpty()) { + return true; + } + BiomeIdentity selected = (generator.biomeScope() == DepositBiomeScope.SURFACE ? surfaceBiome : caveBiome).get(); if (!generator.includedBiomes().isEmpty() && !matchesAnyBiome(selected, generator.includedBiomes())) { return false; } @@ -104,12 +97,12 @@ final class ResolvedDeposit { } /** One clump to place, picked with {@code rng}. */ - IrisObject clump(RNG rng) { + DepositClump clump(RNG rng) { return clump(rng, generator.minSize(), generator.maxSize(), 0L); } /** One clump to place with every size scaled by {@code sizeMultiplier}, as a biome's ore size tuning asks. */ - IrisObject clump(RNG rng, double sizeMultiplier) { + DepositClump clump(RNG rng, double sizeMultiplier) { if (sizeMultiplier == 1D) { return clump(rng); } @@ -119,13 +112,13 @@ final class ResolvedDeposit { return clump(rng, minSize, maxSize, sizeSeed); } - private IrisObject clump(RNG rng, int minSize, int maxSize, long sizeSeed) { + private DepositClump clump(RNG rng, int minSize, int maxSize, long sizeSeed) { if (generator.shape() != DepositShape.IRIS) { return configuredClump(rng, minSize, maxSize); } - List variants = clumps.computeIfAbsent(new ClumpSizes(minSize, maxSize), sizes -> { + List variants = clumps.computeIfAbsent(new ClumpSizes(minSize, maxSize), sizes -> { RNG variantRng = new RNG(depositSeed + stableClumpSalt() + sizeSeed); - List built = new ArrayList<>(); + List built = new ArrayList<>(); for (int i = 0; i < generator.varience(); i++) { built.add(irisClump(variantRng.nextParallelRNG(VARIANT_SEED_STEP * i + VARIANT_SEED_OFFSET), sizes.minSize(), sizes.maxSize())); } @@ -155,7 +148,7 @@ final class ResolvedDeposit { hash = mix(hash, Double.doubleToLongBits(generator.perClumpSpawnChance())); hash = mix(hash, Double.doubleToLongBits(generator.discardChanceOnAirExposure())); hash = mix(hash, palette.size()); - for (PlatformBlockState block : palette) { + for (BlockState block : palette) { hash = mixString(hash, block == null ? null : block.key()); } hash = mix(hash, generator.varience()); @@ -193,7 +186,7 @@ final class ResolvedDeposit { return mixed; } - private IrisObject configuredClump(RNG rng, int minSize, int maxSize) { + private DepositClump configuredClump(RNG rng, int minSize, int maxSize) { int size = rng.i(minSize, maxSize + 1); return switch (generator.shape()) { case VANILLA_ELLIPSOID -> vanillaEllipsoid(rng, size); @@ -203,9 +196,9 @@ final class ResolvedDeposit { } /** Minecraft's ordinary ore vein: a chain of overlapping ellipsoids whose reach grows with {@code size}. */ - IrisObject vanillaEllipsoid(RNG rng, int size) { + DepositClump vanillaEllipsoid(RNG rng, int size) { if (size <= 0) { - return new IrisObject(1, 1, 1); + return new DepositClump(1, 1, 1); } float angle = rng.nextFloat() * (float) Math.PI; @@ -286,11 +279,11 @@ final class ResolvedDeposit { } } - return objectFromCells(cells, rng); + return clumpFromCells(cells, rng); } /** Minecraft's scattered ore: up to {@code size} candidate offsets, the later ones further from the centre. */ - IrisObject vanillaScattered(RNG rng, int size) { + DepositClump vanillaScattered(RNG rng, int size) { KSet cells = new KSet<>(); int candidates = rng.nextInt(Math.max(0, size) + 1); for (int i = 0; i < candidates; i++) { @@ -300,12 +293,12 @@ final class ResolvedDeposit { Math.round((rng.nextFloat() - rng.nextFloat()) * magnitude), Math.round((rng.nextFloat() - rng.nextFloat()) * magnitude))); } - return objectFromCells(cells, rng); + return clumpFromCells(cells, rng); } - private IrisObject objectFromCells(KSet cells, RNG rng) { + private DepositClump clumpFromCells(KSet cells, RNG rng) { if (cells.isEmpty()) { - return new IrisObject(1, 1, 1); + return new DepositClump(1, 1, 1); } int minX = Integer.MAX_VALUE; @@ -326,25 +319,24 @@ final class ResolvedDeposit { int extentX = Math.max(Math.abs(minX), Math.abs(maxX)); int extentY = Math.max(Math.abs(minY), Math.abs(maxY)); int extentZ = Math.max(Math.abs(minZ), Math.abs(maxZ)); - IrisObject object = new IrisObject(extentX * 2 + 1, extentY * 2 + 1, extentZ * 2 + 1); + DepositClump clump = new DepositClump(extentX * 2 + 1, extentY * 2 + 1, extentZ * 2 + 1); for (BlockPosition cell : cells) { - object.setUnsigned(cell.getX() + extentX, cell.getY() + extentY, cell.getZ() + extentZ, nextBlock(rng)); + clump.setFromCorner(cell.getX() + extentX, cell.getY() + extentY, cell.getZ() + extentZ, nextBlock(rng)); } - return object; + return clump; } /** The traditional Iris clump: a cube just large enough for the block count, filled at random cells. */ - private IrisObject irisClump(RNG rng, int minSize, int maxSize) { + private DepositClump irisClump(RNG rng, int minSize, int maxSize) { int remaining = rng.i(minSize, maxSize + 1); if (remaining == 1) { - IrisObject single = new IrisObject(1, 1, 1); - Vector3i center = single.getCenter(); - single.getBlocks().put(new IrisBlockVector(center.getX(), center.getY(), center.getZ()), nextBlock(rng)); + DepositClump single = new DepositClump(1, 1, 1); + single.setCentre(nextBlock(rng)); return single; } int dimension = Math.min(MAX_CLUMP_DIMENSION, (int) Math.ceil(Math.cbrt(remaining))); - IrisObject clump = new IrisObject(dimension, dimension, dimension); + DepositClump clump = new DepositClump(dimension, dimension, dimension); if (remaining >= dimension * dimension * dimension) { fillCube(clump, dimension, rng); return clump; @@ -357,45 +349,47 @@ final class ResolvedDeposit { continue; } remaining--; - clump.setUnsigned(cell.getX(), cell.getY(), cell.getZ(), nextBlock(rng)); + clump.setFromCorner(cell.getX(), cell.getY(), cell.getZ(), nextBlock(rng)); } return clump; } - private void fillCube(IrisObject clump, int dimension, RNG rng) { + private void fillCube(DepositClump clump, int dimension, RNG rng) { for (int z = 0; z < dimension; z++) { for (int y = 0; y < dimension; y++) { for (int x = 0; x < dimension; x++) { - clump.setUnsigned(x, y, z, nextBlock(rng)); + clump.setFromCorner(x, y, z, nextBlock(rng)); } } } } - private PlatformBlockState nextBlock(RNG rng) { + private BlockState nextBlock(RNG rng) { return palette.get(rng.i(0, palette.size())); } - private static Set replaceableMaterials(List keys) { - Set materials = new KSet<>(); + /** The materials of the blocks the pack names that this platform knows; a block it does not know replaces nothing. */ + private static Set replaceableMaterials(BlockRegistry registry, List keys) { + Set materials = new HashSet<>(); for (String key : keys) { - PlatformBlockState state = B.getStateOrNull(key, false); - if (state != null) { - materials.add(IrisProceduralBlocks.materialKey(state)); + try { + materials.add(registry.resolve(key).material()); + } catch (IllegalArgumentException unknownBlock) { + continue; } } return materials; } - private static boolean matchesAnyBiome(IrisBiome biome, List configuredBiomes) { + private static boolean matchesAnyBiome(BiomeIdentity biome, List configuredBiomes) { if (biome == null) { return false; } for (String configured : configuredBiomes) { String namespaced = configured.contains(":") ? configured : MINECRAFT_NAMESPACE + configured; - if (configured.equals(biome.getLoadKey()) - || namespaced.equals(biome.getDerivativeKey()) - || namespaced.equals(biome.getVanillaDerivativeKey())) { + if (configured.equals(biome.key()) + || namespaced.equals(biome.platformBiome()) + || namespaced.equals(biome.vanillaBiome())) { return true; } } @@ -405,4 +399,8 @@ final class ResolvedDeposit { /** The sizes one set of clump variants is built for: the generator's own, or scaled by a biome's ore tuning. */ record ClumpSizes(int minSize, int maxSize) { } + + /** What a deposit's biome filter compares: the pack key of a biome, the platform biome it is written as and the one vanilla sees. */ + record BiomeIdentity(String key, String platformBiome, String vanillaBiome) { + } } diff --git a/src/main/java/art/arcane/iris/engine/modifier/ResolvedDepositVariant.java b/src/main/java/art/arcane/iris/engine/modifier/ResolvedDepositVariant.java index 5330b3b93..86da0bc29 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/ResolvedDepositVariant.java +++ b/src/main/java/art/arcane/iris/engine/modifier/ResolvedDepositVariant.java @@ -15,17 +15,18 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package art.arcane.iris.engine.modifier; import art.arcane.iris.builtin.deposit.DepositVariant; import art.arcane.volmlib.util.cache.AtomicCache; -import art.arcane.iris.engine.object.procedural.IrisProceduralBlocks; -import art.arcane.iris.spi.PlatformBlockState; -import art.arcane.iris.util.common.data.B; +import com.volmit.ModuleLog; +import com.volmit.block.BlockRegistry; +import com.volmit.block.BlockState; +import com.volmit.resource.ResourceId; import java.util.HashMap; import java.util.Map; +import java.util.Optional; /** * One {@link DepositVariant} made ready: its source and replacement block ids resolved to the platform's blocks, built once on @@ -33,10 +34,14 @@ import java.util.Map; */ final class ResolvedDepositVariant { private final DepositVariant settings; - private final AtomicCache> remapped = new AtomicCache<>(); + private final BlockRegistry registry; + private final ModuleLog log; + private final AtomicCache> remapped = new AtomicCache<>(); - ResolvedDepositVariant(DepositVariant settings) { + ResolvedDepositVariant(DepositVariant settings, BlockRegistry registry, ModuleLog log) { this.settings = settings; + this.registry = registry; + this.log = log; } boolean appliesAt(int worldY) { @@ -44,23 +49,35 @@ final class ResolvedDepositVariant { } /** The block {@code ore} is rewritten to, or null when the rule does not remap its material. */ - PlatformBlockState remapOrNull(PlatformBlockState ore) { + BlockState remapOrNull(BlockState ore) { if (ore == null || settings.remap().isEmpty()) { return null; } - return remapped.aquire(this::resolveRemap).get(IrisProceduralBlocks.materialKey(ore)); + return remapped.aquire(this::resolveRemap).get(ore.material()); } - private Map resolveRemap() { - Map resolved = new HashMap<>(); + private Map resolveRemap() { + Map resolved = new HashMap<>(); for (Map.Entry entry : settings.remap().entrySet()) { - PlatformBlockState source = B.getStateOrNull(entry.getKey(), false); - PlatformBlockState target = B.getStateOrNull(entry.getValue(), true); - if (source == null || target == null) { + Optional source = known(entry.getKey(), false); + Optional target = known(entry.getValue(), true); + if (source.isEmpty() || target.isEmpty()) { continue; } - resolved.put(IrisProceduralBlocks.materialKey(source), target); + resolved.put(source.get().material(), target.get()); } return resolved; } + + /** The block the platform has under {@code key}; a replacement it lacks is reported once. */ + private Optional known(String key, boolean reportMissing) { + try { + return Optional.of(registry.resolve(key)); + } catch (IllegalArgumentException unknownBlock) { + if (reportMissing) { + log.warnOnce("deposit-variant-block:" + key, "Deposit variant replaces ores with " + key + ", which this server does not have"); + } + return Optional.empty(); + } + } }