From 60ba7e644c9cb5b79c9503273cf1c6418f4b55d9 Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 5 Nov 2026 23:13:22 +0100 Subject: [PATCH] Add the studio map's core views: tiles, labels and hover The 2D views of the coming studio app (biomes, regions, height, land, sea and cave biomes) as core code. A tile says what each pixel shows (its biome and region, and heights for shading) with a ring of neighbour pixels, so the app styles it without asking again; the hover describes one column. --- .../iris/core/studio/map/HeightLattice.java | 94 ++++++++ .../arcane/iris/core/studio/map/MapHover.java | 61 ++++++ .../arcane/iris/core/studio/map/MapLabel.java | 48 +++++ .../arcane/iris/core/studio/map/MapTile.java | 57 +++++ .../iris/core/studio/map/MapTileRenderer.java | 139 ++++++++++++ .../arcane/iris/core/studio/map/MapView.java | 108 ++++++++++ .../arcane/iris/core/studio/map/TileGrid.java | 32 +++ .../core/studio/map/MapTileRendererTest.java | 202 ++++++++++++++++++ 8 files changed, 741 insertions(+) create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/HeightLattice.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/MapHover.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/MapLabel.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/MapTile.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/MapTileRenderer.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/MapView.java create mode 100644 core/src/main/java/art/arcane/iris/core/studio/map/TileGrid.java create mode 100644 core/src/test/java/art/arcane/iris/core/studio/map/MapTileRendererTest.java diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/HeightLattice.java b/core/src/main/java/art/arcane/iris/core/studio/map/HeightLattice.java new file mode 100644 index 000000000..ce9269a1f --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/HeightLattice.java @@ -0,0 +1,94 @@ +/* + * 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.core.studio.map; + +import art.arcane.iris.util.project.stream.ProceduralStream; + +/** + * Terrain heights from samples on a coarse lattice, smoothly interpolated in between. Iris heights are costly to + * sample, and shading turns the steps of cheaper shortcuts into visible creases, so a smooth curve through fewer exact + * samples looks better and draws an order of magnitude faster than sampling every cell. The lattice lines up across + * tiles, so slopes continue seamlessly from one tile into the next. + */ +final class HeightLattice { + static final int LATTICE_PIXELS = 4; + private static final int LATTICE_MARGIN = 2; + private static final int CURVE_POINTS = 4; + + private final TileGrid grid; + private final ProceduralStream stream; + private final int pointsPerSide; + private final double[] points; + + HeightLattice(TileGrid grid, ProceduralStream stream) { + this.grid = grid; + this.stream = stream; + this.pointsPerSide = (grid.size() - 2) / LATTICE_PIXELS + LATTICE_MARGIN * 2 + 1; + this.points = new double[pointsPerSide * pointsPerSide]; + } + + float[] interpolatedHeights() { + samplePoints(); + int size = grid.size(); + float[] heights = new float[size * size]; + for (int gridZ = 0; gridZ < size; gridZ++) { + for (int gridX = 0; gridX < size; gridX++) { + heights[gridZ * size + gridX] = (float) heightAt(gridX - 1, gridZ - 1); + } + } + return heights; + } + + // Lattice point 0 sits on the tile's first pixel; the margin covers the ring and the curve's reach. + private void samplePoints() { + double latticeStep = grid.step() * LATTICE_PIXELS; + double firstX = grid.worldX(1) - LATTICE_MARGIN * latticeStep; + double firstZ = grid.worldZ(1) - LATTICE_MARGIN * latticeStep; + for (int pointZ = 0; pointZ < pointsPerSide; pointZ++) { + MapTileRenderer.checkCancelled(grid.cancelled()); + for (int pointX = 0; pointX < pointsPerSide; pointX++) { + points[pointZ * pointsPerSide + pointX] = stream.getDouble(firstX + pointX * latticeStep, firstZ + pointZ * latticeStep); + } + } + } + + private double heightAt(int pixelX, int pixelZ) { + int cellX = Math.floorDiv(pixelX, LATTICE_PIXELS); + int cellZ = Math.floorDiv(pixelZ, LATTICE_PIXELS); + double fractionX = (pixelX - cellX * LATTICE_PIXELS) / (double) LATTICE_PIXELS; + double fractionZ = (pixelZ - cellZ * LATTICE_PIXELS) / (double) LATTICE_PIXELS; + int baseX = cellX + LATTICE_MARGIN; + int baseZ = cellZ + LATTICE_MARGIN; + double[] rows = new double[CURVE_POINTS]; + for (int row = 0; row < CURVE_POINTS; row++) { + int offset = (baseZ - 1 + row) * pointsPerSide + baseX - 1; + rows[row] = catmullRom(points[offset], points[offset + 1], points[offset + 2], points[offset + 3], fractionX); + } + return catmullRom(rows[0], rows[1], rows[2], rows[3], fractionZ); + } + + private static double catmullRom(double before, double start, double end, double after, double fraction) { + double squared = fraction * fraction; + double cubed = squared * fraction; + return 0.5D * (2D * start + + (end - before) * fraction + + (2D * before - 5D * start + 4D * end - after) * squared + + (3D * start - before - 3D * end + after) * cubed); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/MapHover.java b/core/src/main/java/art/arcane/iris/core/studio/map/MapHover.java new file mode 100644 index 000000000..7372b2843 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/MapHover.java @@ -0,0 +1,61 @@ +/* + * 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.core.studio.map; + +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.render.RenderType; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.spi.PlatformBiome; + +/** + * What the map knows about the column under the cursor. + * + * @param biome the base biome, as the biome view shows it; null where the stream has none, as for the others + * @param vanillaBiome the platform biome the column gets, such as {@code minecraft:plains} + * @param slopeDegrees how steep the terrain is, 0 for flat + */ +public record MapHover(double worldX, double worldZ, MapLabel biome, MapLabel caveBiome, MapLabel region, + String vanillaBiome, double height, double fluidHeight, double slopeDegrees) { + // IrisComplex measures slope as the height change over this many blocks. + private static final double SLOPE_STREAM_RANGE_BLOCKS = 3D; + + public static MapHover sample(Engine engine, double worldX, double worldZ) { + IrisComplex complex = engine.getComplex(); + IrisBiome biome = complex.getBaseBiomeStream().get(worldX, worldZ); + IrisBiome caveBiome = complex.getCaveBiomeStream().get(worldX, worldZ); + IrisRegion region = complex.getRegionStream().get(worldX, worldZ); + PlatformBiome vanillaBiome = complex.getTrueBiomeDerivativeStream().get(worldX, worldZ); + double slope = complex.getSlopeStream().getDouble(worldX, worldZ); + IrisDimension dimension = engine.getDimension(); + return new MapHover( + worldX, + worldZ, + biome == null ? null : MapLabel.of(engine, biome, RenderType.BIOME), + caveBiome == null ? null : MapLabel.of(engine, caveBiome, RenderType.CAVE_LAND), + region == null ? null : MapLabel.of(engine, region), + vanillaBiome == null ? null : vanillaBiome.key(), + complex.getHeightStream().getDouble(worldX, worldZ), + dimension == null ? 0D : dimension.getFluidHeight(), + Math.toDegrees(Math.atan(slope / SLOPE_STREAM_RANGE_BLOCKS)) + ); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/MapLabel.java b/core/src/main/java/art/arcane/iris/core/studio/map/MapLabel.java new file mode 100644 index 000000000..e593e24d8 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/MapLabel.java @@ -0,0 +1,48 @@ +/* + * 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.core.studio.map; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.render.RenderType; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; + +/** + * A biome or region as the map shows it. + * + * @param key the pack key, such as {@code plains/meadow} + * @param color its colour on the map, as {@code 0xRRGGBB} + */ +public record MapLabel(Kind kind, String key, String name, int color) { + private static final int RGB = 0xFFFFFF; + + static MapLabel of(Engine engine, IrisBiome biome, RenderType colorType) { + return new MapLabel(Kind.BIOME, biome.getLoadKey(), biome.getName(), biome.getColor(engine, colorType).getRGB() & RGB); + } + + static MapLabel of(Engine engine, IrisRegion region) { + return new MapLabel(Kind.REGION, region.getLoadKey(), region.getName(), + region.getColor(engine.getComplex(), RenderType.REGION).getRGB() & RGB); + } + + public enum Kind { + BIOME, + REGION + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/MapTile.java b/core/src/main/java/art/arcane/iris/core/studio/map/MapTile.java new file mode 100644 index 000000000..6abf98080 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/MapTile.java @@ -0,0 +1,57 @@ +/* + * 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.core.studio.map; + +import java.util.List; + +/** + * A square of the map: what each pixel shows, not how it looks, so the studio styles it (shading, highlights, borders) + * without asking again. The grids hold {@code (size + 2)²} cells row by row: the tile's pixels inside a ring of the + * neighbouring tiles' edge pixels, so edges and slopes continue across tiles. + * + * @param subjects the index in {@code labels} of each cell's biome, or its region in the region view; + * {@link #NO_LABEL} where the stream had none + * @param regions the index in {@code labels} of each cell's region; the same array as {@code subjects} in the region view + * @param heights each cell's terrain height, or null when it was not asked for + */ +public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind, List labels, + short[] subjects, short[] regions, float[] heights) { + public static final short NO_LABEL = -1; + + public int gridSize() { + return size + 2; + } + + /** The grid index of a pixel; pixel coordinates run from -1 to {@code size}, the ring being the neighbours'. */ + public int gridIndex(int pixelX, int pixelZ) { + return (pixelZ + 1) * gridSize() + pixelX + 1; + } + + public MapLabel subjectAt(int pixelX, int pixelZ) { + return label(subjects[gridIndex(pixelX, pixelZ)]); + } + + public MapLabel regionAt(int pixelX, int pixelZ) { + return label(regions[gridIndex(pixelX, pixelZ)]); + } + + private MapLabel label(short index) { + return index == NO_LABEL ? null : labels.get(index); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/MapTileRenderer.java b/core/src/main/java/art/arcane/iris/core/studio/map/MapTileRenderer.java new file mode 100644 index 000000000..86bc08c2b --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/MapTileRenderer.java @@ -0,0 +1,139 @@ +/* + * 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.core.studio.map; + +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.render.RenderType; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.util.project.stream.ProceduralStream; + +import java.util.ArrayList; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.CancellationException; +import java.util.function.BooleanSupplier; + +/** Draws {@link MapTile}s of one engine's world. Safe to use from several threads; each tile is drawn on its caller's. */ +public final class MapTileRenderer { + private final Engine engine; + + public MapTileRenderer(Engine engine) { + this.engine = Objects.requireNonNull(engine, "engine"); + } + + /** + * The square of {@code size} pixels whose top left pixel samples ({@code startX}, {@code startZ}). + * + * @param withHeights also sample terrain heights, for shading; the height view always has them + * @throws CancellationException as soon as {@code cancelled} answers true + */ + public MapTile render(MapView view, double startX, double startZ, double blocksPerPixel, int size, + boolean withHeights, BooleanSupplier cancelled) { + if (!Double.isFinite(startX) || !Double.isFinite(startZ) || !Double.isFinite(blocksPerPixel) || blocksPerPixel <= 0D) { + throw new IllegalArgumentException("Map tile coordinates and scale must be finite"); + } + if (size < 1) { + throw new IllegalArgumentException("Map tile size must be positive"); + } + Objects.requireNonNull(view, "view"); + Objects.requireNonNull(cancelled, "cancelled"); + checkCancelled(cancelled); + + TileGrid grid = new TileGrid(size + 2, startX - blocksPerPixel, startZ - blocksPerPixel, blocksPerPixel, cancelled); + TileLabels labels = new TileLabels(engine, view.colorType()); + short[] regions = sampleRegions(grid, labels); + short[] subjects = view.labelsRegions() ? regions : sampleBiomes(grid, labels, view.biomeStream(engine.getComplex())); + float[] heights = withHeights || view == MapView.HEIGHT + ? new HeightLattice(grid, engine.getComplex().getHeightStream()).interpolatedHeights() + : null; + MapLabel.Kind subjectKind = view.labelsRegions() ? MapLabel.Kind.REGION : MapLabel.Kind.BIOME; + return new MapTile(size, blocksPerPixel, subjectKind, labels.labels(), subjects, regions, heights); + } + + private static short[] sampleRegions(TileGrid grid, TileLabels labels) { + ProceduralStream stream = labels.engine.getComplex().getRegionStream(); + return everyCell(grid, (x, z) -> labels.indexOf(stream.get(x, z))); + } + + private static short[] sampleBiomes(TileGrid grid, TileLabels labels, ProceduralStream stream) { + return everyCell(grid, (x, z) -> labels.indexOf(stream.get(x, z))); + } + + // A fractured child style speckles finer than any square a few samples could vouch for, so every cell is asked. + private static short[] everyCell(TileGrid grid, CellLabeler labeler) { + int size = grid.size(); + short[] cells = new short[size * size]; + for (int gridZ = 0; gridZ < size; gridZ++) { + checkCancelled(grid.cancelled()); + double worldZ = grid.worldZ(gridZ); + for (int gridX = 0; gridX < size; gridX++) { + cells[gridZ * size + gridX] = labeler.indexAt(grid.worldX(gridX), worldZ); + } + } + return cells; + } + + static void checkCancelled(BooleanSupplier cancelled) { + if (cancelled.getAsBoolean() || Thread.currentThread().isInterrupted()) { + throw new CancellationException("Map tile render cancelled"); + } + } + + @FunctionalInterface + private interface CellLabeler { + short indexAt(double worldX, double worldZ); + } + + /** The biomes and regions one tile meets, each labelled once, in the order the tile met them. */ + private static final class TileLabels { + private final Engine engine; + private final RenderType biomeColorType; + private final Map indices = new IdentityHashMap<>(); + private final List labels = new ArrayList<>(); + + private TileLabels(Engine engine, RenderType biomeColorType) { + this.engine = engine; + this.biomeColorType = biomeColorType; + } + + short indexOf(IrisBiome biome) { + return biome == null ? MapTile.NO_LABEL : indices.computeIfAbsent(biome, known -> add(MapLabel.of(engine, biome, biomeColorType))); + } + + short indexOf(IrisRegion region) { + return region == null ? MapTile.NO_LABEL : indices.computeIfAbsent(region, known -> add(MapLabel.of(engine, region))); + } + + private short add(MapLabel label) { + if (labels.size() == Short.MAX_VALUE) { + return MapTile.NO_LABEL; + } + labels.add(label); + return (short) (labels.size() - 1); + } + + List labels() { + return List.copyOf(labels); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/MapView.java b/core/src/main/java/art/arcane/iris/core/studio/map/MapView.java new file mode 100644 index 000000000..9b4c3b2b9 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/MapView.java @@ -0,0 +1,108 @@ +/* + * 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.core.studio.map; + +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.render.RenderType; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.util.project.stream.ProceduralStream; + +import java.util.Arrays; +import java.util.Optional; + +/** The studio map's views of a world drawn from its engine streams, one tile at a time. */ +public enum MapView { + BIOMES("biomes", "Biomes", "The biome at each spot, in the biome's colour", Group.WORLD, RenderType.BIOME), + REGIONS("regions", "Regions", "The region each spot belongs to", Group.WORLD, RenderType.REGION), + HEIGHT("height", "Height", "Terrain height, from deep water to snowy peaks", Group.WORLD, RenderType.HEIGHT), + LAND_BIOMES("landBiomes", "Land biomes", "The land biome each spot would get, also where the sea is", + Group.BIOME_LAYERS, RenderType.BIOME_LAND), + SEA_BIOMES("seaBiomes", "Sea biomes", "The sea biome each spot would get, also where the land is", + Group.BIOME_LAYERS, RenderType.BIOME_SEA), + CAVE_BIOMES("caveBiomes", "Cave biomes", "The cave biome under each spot", Group.BIOME_LAYERS, RenderType.CAVE_LAND); + + private final String id; + private final String label; + private final String description; + private final Group group; + private final RenderType colorType; + + MapView(String id, String label, String description, Group group, RenderType colorType) { + this.id = id; + this.label = label; + this.description = description; + this.group = group; + this.colorType = colorType; + } + + public String id() { + return id; + } + + public String label() { + return label; + } + + public String description() { + return description; + } + + public Group group() { + return group; + } + + public static Optional byId(String id) { + return Arrays.stream(values()).filter(view -> view.id.equals(id)).findFirst(); + } + + /** Whether each spot is labelled with its region rather than a biome. */ + boolean labelsRegions() { + return this == REGIONS; + } + + /** The colouring biomes and regions get in this view; the height view colours its biomes as the biome view does. */ + RenderType colorType() { + return this == HEIGHT ? RenderType.BIOME : colorType; + } + + /** The biome each spot is labelled with: the base biome, before the height corrects it near coasts. */ + ProceduralStream biomeStream(IrisComplex complex) { + return switch (this) { + case LAND_BIOMES -> complex.getLandBiomeStream(); + case SEA_BIOMES -> complex.getSeaBiomeStream(); + case CAVE_BIOMES -> complex.getCaveBiomeStream(); + case BIOMES, REGIONS, HEIGHT -> complex.getBaseBiomeStream(); + }; + } + + public enum Group { + WORLD("World"), + BIOME_LAYERS("Biome layers"); + + private final String label; + + Group(String label) { + this.label = label; + } + + public String label() { + return label; + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/TileGrid.java b/core/src/main/java/art/arcane/iris/core/studio/map/TileGrid.java new file mode 100644 index 000000000..f29ed0280 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/TileGrid.java @@ -0,0 +1,32 @@ +/* + * 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.core.studio.map; + +import java.util.function.BooleanSupplier; + +/** The cells of a tile with its ring: {@code size} per side, cell (0, 0) sampling ({@code startX}, {@code startZ}). */ +record TileGrid(int size, double startX, double startZ, double step, BooleanSupplier cancelled) { + double worldX(int gridX) { + return startX + gridX * step; + } + + double worldZ(int gridZ) { + return startZ + gridZ * step; + } +} diff --git a/core/src/test/java/art/arcane/iris/core/studio/map/MapTileRendererTest.java b/core/src/test/java/art/arcane/iris/core/studio/map/MapTileRendererTest.java new file mode 100644 index 000000000..4d4c63793 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/studio/map/MapTileRendererTest.java @@ -0,0 +1,202 @@ +package art.arcane.iris.core.studio.map; + +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.render.RenderType; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.engine.object.world.region.IrisRegion; +import art.arcane.iris.spi.PlatformBiome; +import art.arcane.iris.util.project.stream.ProceduralStream; +import org.junit.Before; +import org.junit.Test; + +import java.awt.Color; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CancellationException; +import java.util.function.DoubleBinaryOperator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.mockito.ArgumentMatchers.anyDouble; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +/** A world split at x = 0: plains in the warm region west of it, desert in the dry region east of it. */ +public class MapTileRendererTest { + private static final int SIZE = 8; + + private final Engine engine = mock(Engine.class); + private final IrisComplex complex = mock(IrisComplex.class); + private final IrisBiome plains = biome("plains", "Plains", Color.GREEN); + private final IrisBiome desert = biome("desert", "Desert", Color.YELLOW); + private final IrisBiome cavern = biome("cavern", "Cavern", Color.DARK_GRAY); + private IrisRegion warm; + private IrisRegion dry; + + @Before + public void splitTheWorld() { + warm = region("warm", "Warm", Color.RED); + dry = region("dry", "Dry", Color.ORANGE); + when(engine.getComplex()).thenReturn(complex); + ProceduralStream base = stream((x, z) -> x < 0 ? plains : desert); + ProceduralStream regions = stream((x, z) -> x < 0 ? warm : dry); + ProceduralStream caves = stream((x, z) -> cavern); + when(complex.getBaseBiomeStream()).thenReturn(base); + when(complex.getRegionStream()).thenReturn(regions); + when(complex.getCaveBiomeStream()).thenReturn(caves); + heights((x, z) -> 64D + Math.sin(x / 7D) * 20D + Math.cos(z / 5D) * 10D); + } + + @Test + public void eachCellNamesItsBiomeAndRegionAndEachLabelIsListedOnce() { + MapTile tile = render(MapView.BIOMES, -4D, 0D, 1D, false); + + assertEquals(MapLabel.Kind.BIOME, tile.subjectKind()); + assertEquals("plains", tile.subjectAt(0, 0).key()); + assertEquals("desert", tile.subjectAt(4, 0).key()); + assertEquals("warm", tile.regionAt(3, 7).key()); + assertEquals("dry", tile.regionAt(7, 7).key()); + assertEquals(List.of("warm", "dry", "plains", "desert"), tile.labels().stream().map(MapLabel::key).toList()); + assertEquals(Color.GREEN.getRGB() & 0xFFFFFF, tile.subjectAt(0, 0).color()); + assertNull("heights only when asked", tile.heights()); + } + + @Test + public void theRingSamplesTheNeighbouringTilesEdgePixels() { + MapTile tile = render(MapView.BIOMES, 0D, 0D, 2D, false); + + assertEquals("the pixel left of the tile is at x = -2", "plains", tile.subjectAt(-1, 3).key()); + assertEquals("desert", tile.subjectAt(0, 3).key()); + } + + @Test + public void theRegionViewLabelsEachSpotWithItsRegion() { + MapTile tile = render(MapView.REGIONS, -4D, 0D, 1D, false); + + assertEquals(MapLabel.Kind.REGION, tile.subjectKind()); + assertSame(tile.regions(), tile.subjects()); + assertEquals("warm", tile.subjectAt(0, 0).key()); + assertEquals(Color.RED.getRGB() & 0xFFFFFF, tile.subjectAt(0, 0).color()); + } + + @Test + public void biomeLayersLabelWithTheirOwnStreamAndColouring() { + MapTile tile = render(MapView.CAVE_BIOMES, -4D, 0D, 1D, false); + + assertEquals("cavern", tile.subjectAt(0, 0).key()); + assertEquals("warm", tile.regionAt(0, 0).key()); + } + + @Test + public void theHeightViewAlwaysHasHeightsAndAFlatWorldStaysFlat() { + heights((x, z) -> 70D); + + MapTile tile = render(MapView.HEIGHT, 0D, 0D, 1D, false); + + for (float height : tile.heights()) { + assertEquals(70F, height, 1e-4F); + } + } + + @Test + public void heightsOnALatticePointAreExactAndAgreeAcrossTiles() { + MapTile west = render(MapView.BIOMES, 0D, 0D, 3D, true); + MapTile east = render(MapView.BIOMES, SIZE * 3D, 0D, 3D, true); + ProceduralStream exact = complex.getHeightStream(); + + assertEquals(exact.getDouble(12D, 12D), west.heights()[west.gridIndex(4, 4)], 1e-4); + for (int pixelZ = -1; pixelZ <= SIZE; pixelZ++) { + assertEquals(east.heights()[east.gridIndex(0, pixelZ)], west.heights()[west.gridIndex(SIZE, pixelZ)], 1e-4F); + assertEquals(west.heights()[west.gridIndex(SIZE - 1, pixelZ)], east.heights()[east.gridIndex(-1, pixelZ)], 1e-4F); + } + } + + @Test + public void aCancelledTileStopsBeforeSamplingTheEngine() { + Engine untouched = mock(Engine.class); + + assertThrows(CancellationException.class, + () -> new MapTileRenderer(untouched).render(MapView.BIOMES, 0D, 0D, 1D, SIZE, false, () -> true)); + verifyNoInteractions(untouched); + } + + @Test + public void theHoverDescribesTheColumn() { + PlatformBiome vanilla = mock(PlatformBiome.class); + when(vanilla.key()).thenReturn("minecraft:desert"); + ProceduralStream vanillaBiomes = stream((x, z) -> vanilla); + when(complex.getTrueBiomeDerivativeStream()).thenReturn(vanillaBiomes); + ProceduralStream slopes = numbers((x, z) -> 3D); + when(complex.getSlopeStream()).thenReturn(slopes); + IrisDimension dimension = mock(IrisDimension.class); + when(dimension.getFluidHeight()).thenReturn(63); + when(engine.getDimension()).thenReturn(dimension); + + MapHover hover = MapHover.sample(engine, 10D, 0D); + + assertEquals("desert", hover.biome().key()); + assertEquals("cavern", hover.caveBiome().key()); + assertEquals("dry", hover.region().key()); + assertEquals("minecraft:desert", hover.vanillaBiome()); + assertEquals(complex.getHeightStream().getDouble(10D, 0D), hover.height(), 1e-9); + assertEquals(63D, hover.fluidHeight(), 0D); + assertEquals(45D, hover.slopeDegrees(), 1e-9); + } + + @Test + public void viewsAreFoundByTheirIds() { + for (MapView view : MapView.values()) { + assertEquals(Optional.of(view), MapView.byId(view.id())); + } + assertEquals(Optional.empty(), MapView.byId("nothing")); + } + + private MapTile render(MapView view, double startX, double startZ, double blocksPerPixel, boolean withHeights) { + return new MapTileRenderer(engine).render(view, startX, startZ, blocksPerPixel, SIZE, withHeights, () -> false); + } + + private void heights(DoubleBinaryOperator height) { + ProceduralStream stream = numbers(height); + when(complex.getHeightStream()).thenReturn(stream); + } + + private IrisBiome biome(String key, String name, Color color) { + IrisBiome biome = mock(IrisBiome.class); + when(biome.getLoadKey()).thenReturn(key); + when(biome.getName()).thenReturn(name); + when(biome.getColor(org.mockito.ArgumentMatchers.eq(engine), org.mockito.ArgumentMatchers.any(RenderType.class))).thenReturn(color); + return biome; + } + + private IrisRegion region(String key, String name, Color color) { + IrisRegion region = mock(IrisRegion.class); + when(region.getLoadKey()).thenReturn(key); + when(region.getName()).thenReturn(name); + when(region.getColor(complex, RenderType.REGION)).thenReturn(color); + return region; + } + + @SuppressWarnings("unchecked") + private static ProceduralStream stream(Sampler sampler) { + ProceduralStream stream = mock(ProceduralStream.class); + when(stream.get(anyDouble(), anyDouble())).thenAnswer(call -> sampler.at(call.getArgument(0), call.getArgument(1))); + return stream; + } + + @SuppressWarnings("unchecked") + private static ProceduralStream numbers(DoubleBinaryOperator sampler) { + ProceduralStream stream = mock(ProceduralStream.class); + when(stream.getDouble(anyDouble(), anyDouble())).thenAnswer(call -> sampler.applyAsDouble(call.getArgument(0), call.getArgument(1))); + return stream; + } + + @FunctionalInterface + private interface Sampler { + T at(double x, double z); + } +}