mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Datapack export
- /iris datapack export compiles one pack dimension into a worldgen datapack for Minecraft 26.3 and later - Bukkit and mod loaders take the same version, mode, namespace, output, format, objects and replace arguments - The default overworld mode replaces the Overworld of a new world with the default world type - Preset and dimension modes write a selectable world preset or a separate dimension - The export runs in the background and publishes the zip or folder atomically - Pack compiler stages turn noise, terrain, caves, subterrain, biomes, surface, decoration, objects and structures into a version-neutral IR - Capability lowering fits every field to 26.3 density function ranges - Disjoint min and max nodes fold to the input that always wins - The 26.3 writer emits density functions, noise settings, biomes, features, structures, presets and dimensions - Every export carries the Iris logo as its pack.png - Unused objects, templates and features are pruned from the output - The pruner keeps pack.png without reading references from its bytes - Imported pack datapacks are bundled into the export - Every approximation reports an export limit diagnostic - Generation cost is counted against a node budget and reported as a warning - Cave biomes over the wall palette limit use the closest kept palette - Jigsaw exports share the template encoder, block states, destinations and publisher with the worldgen export - DataVersion carries the pack format minor and world data version - Both platforms expose vanilla data from the server classpath - GoldenHashEngine and the export probes share one chunk spiral scan - Headless 26.3 probes measure export cost and compare column and chunk likeness against Iris - Real-codec tests decode the exported resources with the 26.3 registries - Paper registry tests run in isolated registry scopes - Command help and messages cover the export in every language file
This commit is contained in:
1 parent
b00e28e42e
commit
1ed1044474
385 files changed
+48102
-535
No files matched your search
@@ -54,7 +54,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
final class RealPackProbeSupport {
|
||||
public final class RealPackProbeSupport {
|
||||
private static final long WORKER_DRAIN_TIMEOUT_NANOS = TimeUnit.SECONDS.toNanos(120);
|
||||
private static final List<Throwable> REPORTED = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
@@ -208,7 +208,7 @@ final class RealPackProbeSupport {
|
||||
throw new IllegalArgumentException(name + " must be true or false.");
|
||||
}
|
||||
|
||||
static GeneratedChunk generateChunk(Engine engine, int chunkX, int chunkZ) {
|
||||
public static GeneratedChunk generateChunk(Engine engine, int chunkX, int chunkZ) {
|
||||
int height = engine.getTarget().getHeight();
|
||||
Hunk<NativeBlockState> blocks = Hunk.newArrayHunk(16, height, 16);
|
||||
Hunk<NativeBiome> biomes = Hunk.newArrayHunk(16, height, 16);
|
||||
@@ -220,14 +220,14 @@ final class RealPackProbeSupport {
|
||||
return new GeneratedChunk(chunkX, chunkZ, height, blocks, biomes);
|
||||
}
|
||||
|
||||
record GeneratedChunk(
|
||||
public record GeneratedChunk(
|
||||
int chunkX,
|
||||
int chunkZ,
|
||||
int height,
|
||||
Hunk<NativeBlockState> blocks,
|
||||
Hunk<NativeBiome> biomes
|
||||
) {
|
||||
GeneratedChunk {
|
||||
public GeneratedChunk {
|
||||
if (height < 1 || blocks == null || biomes == null) {
|
||||
throw new IllegalArgumentException("Generated chunk requires buffers and a positive height.");
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import art.arcane.volmlib.nativelib.item.NativeItem;
|
||||
import art.arcane.iris.spi.PlatformRegistries;
|
||||
import art.arcane.iris.spi.PlatformScheduler;
|
||||
import art.arcane.iris.spi.PlatformStructureHooks;
|
||||
import art.arcane.iris.spi.VanillaDataSource;
|
||||
import art.arcane.volmlib.nativelib.terrain.NativeWorld;
|
||||
import art.arcane.iris.generation.geometry.IrisBlockVector;
|
||||
|
||||
@@ -43,6 +44,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -88,6 +90,7 @@ public final class StubPlatform implements IrisPlatform {
|
||||
private final StubScheduler scheduler = new StubScheduler();
|
||||
private final StubStructureHooks structureHooks = new StubStructureHooks();
|
||||
private final StubBiomeWriter biomeWriter = new StubBiomeWriter();
|
||||
private final StubVanillaDataSource vanillaData = new StubVanillaDataSource();
|
||||
private final File dataFolder;
|
||||
|
||||
public StubPlatform() {
|
||||
@@ -620,6 +623,18 @@ public final class StubPlatform implements IrisPlatform {
|
||||
}
|
||||
}
|
||||
|
||||
private static final class StubVanillaDataSource implements VanillaDataSource {
|
||||
@Override
|
||||
public Optional<byte[]> read(String dataPath) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> list(String prefix) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private record StubEntityType(String key) implements NativeEntityType {
|
||||
@Override
|
||||
public String namespace() {
|
||||
@@ -783,6 +798,11 @@ public final class StubPlatform implements IrisPlatform {
|
||||
return biomeWriter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VanillaDataSource vanillaData() {
|
||||
return vanillaData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File dataFolder() {
|
||||
return dataFolder;
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public record BiomeShareReport(FidelityReport mean, SortedMap<Long, FidelityReport> seeds, Map<String, Double> irisShares,
|
||||
Map<String, Double> exportShares) {
|
||||
private static final String SEED_HEADER = "seed";
|
||||
private static final String PASSING_HEADER = "passing";
|
||||
private static final String MISSING = "-";
|
||||
|
||||
public BiomeShareReport {
|
||||
Objects.requireNonNull(mean, "mean");
|
||||
seeds = Collections.unmodifiableSortedMap(new TreeMap<>(seeds));
|
||||
if (seeds.isEmpty()) {
|
||||
throw new IllegalArgumentException("A biome share report needs at least one seed");
|
||||
}
|
||||
irisShares = Collections.unmodifiableSortedMap(new TreeMap<>(irisShares));
|
||||
exportShares = Collections.unmodifiableSortedMap(new TreeMap<>(exportShares));
|
||||
}
|
||||
|
||||
public boolean passed() {
|
||||
return mean.passed();
|
||||
}
|
||||
|
||||
public int seedsPassing(String metric) {
|
||||
int passing = 0;
|
||||
for (FidelityReport report : seeds.values()) {
|
||||
FidelityMetric found = find(report, metric);
|
||||
if (found != null && found.pass()) {
|
||||
passing++;
|
||||
}
|
||||
}
|
||||
return passing;
|
||||
}
|
||||
|
||||
public int seedsCompared(String metric) {
|
||||
int compared = 0;
|
||||
for (FidelityReport report : seeds.values()) {
|
||||
if (find(report, metric) != null) {
|
||||
compared++;
|
||||
}
|
||||
}
|
||||
return compared;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
JsonArray seedEntries = new JsonArray(seeds.size());
|
||||
for (Map.Entry<Long, FidelityReport> entry : seeds.entrySet()) {
|
||||
JsonObject seed = entry.getValue().toJsonObject();
|
||||
seed.addProperty("seed", entry.getKey());
|
||||
seedEntries.add(seed);
|
||||
}
|
||||
JsonObject passing = new JsonObject();
|
||||
for (String metric : metricNames()) {
|
||||
JsonObject counts = new JsonObject();
|
||||
counts.addProperty("passing", seedsPassing(metric));
|
||||
counts.addProperty("compared", seedsCompared(metric));
|
||||
passing.add(metric, counts);
|
||||
}
|
||||
Set<String> biomes = new TreeSet<>(irisShares.keySet());
|
||||
biomes.addAll(exportShares.keySet());
|
||||
JsonObject shares = new JsonObject();
|
||||
for (String biome : biomes) {
|
||||
JsonObject share = new JsonObject();
|
||||
share.addProperty("iris", irisShares.getOrDefault(biome, 0.0D));
|
||||
share.addProperty("export", exportShares.getOrDefault(biome, 0.0D));
|
||||
shares.add(biome, share);
|
||||
}
|
||||
JsonObject root = new JsonObject();
|
||||
root.addProperty("passed", passed());
|
||||
root.add("mean", mean.toJsonObject());
|
||||
root.add("shares", shares);
|
||||
root.add("seedsPassing", passing);
|
||||
root.add("seeds", seedEntries);
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(root);
|
||||
}
|
||||
|
||||
public String toTable() {
|
||||
List<String> names = metricNames();
|
||||
int width = SEED_HEADER.length();
|
||||
for (String name : names) {
|
||||
width = Math.max(width, name.length());
|
||||
}
|
||||
StringBuilder table = new StringBuilder();
|
||||
table.append(String.format(Locale.ROOT, "%-" + width + "s", SEED_HEADER));
|
||||
for (long seed : seeds.keySet()) {
|
||||
table.append(String.format(Locale.ROOT, " %10s", seed));
|
||||
}
|
||||
table.append(String.format(Locale.ROOT, " %8s\n", PASSING_HEADER));
|
||||
for (String name : names) {
|
||||
table.append(String.format(Locale.ROOT, "%-" + width + "s", name));
|
||||
for (FidelityReport report : seeds.values()) {
|
||||
FidelityMetric metric = find(report, name);
|
||||
table.append(metric == null ? String.format(Locale.ROOT, " %10s", MISSING) : String.format(Locale.ROOT, " %10.4f", metric.delta()));
|
||||
}
|
||||
table.append(String.format(Locale.ROOT, " %8s\n", seedsPassing(name) + "/" + seedsCompared(name)));
|
||||
}
|
||||
return table.append(mean.toTable()).toString();
|
||||
}
|
||||
|
||||
private List<String> metricNames() {
|
||||
Set<String> names = new LinkedHashSet<>();
|
||||
for (FidelityMetric metric : mean.metrics()) {
|
||||
names.add(metric.name());
|
||||
}
|
||||
for (FidelityReport report : seeds.values()) {
|
||||
for (FidelityMetric metric : report.metrics()) {
|
||||
names.add(metric.name());
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(names);
|
||||
}
|
||||
|
||||
private static FidelityMetric find(FidelityReport report, String name) {
|
||||
for (FidelityMetric metric : report.metrics()) {
|
||||
if (metric.name().equals(name)) {
|
||||
return metric;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import art.arcane.iris.generation.biome.IrisBiome;
|
||||
import art.arcane.iris.generation.terrain.IrisRegion;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class BiomeTypes {
|
||||
private static final String LAND = "land";
|
||||
private static final String SEA = "sea";
|
||||
private static final String SHORE = "shore";
|
||||
private static final String JOINER = "+";
|
||||
|
||||
private BiomeTypes() {
|
||||
}
|
||||
|
||||
public static SortedMap<String, String> of(List<IrisRegion> regions, Function<String, IrisBiome> biomes) {
|
||||
SortedMap<String, SortedSet<String>> types = new TreeMap<>();
|
||||
for (IrisRegion region : regions) {
|
||||
visit(region.getLandBiomes(), LAND, biomes, types);
|
||||
visit(region.getSeaBiomes(), SEA, biomes, types);
|
||||
visit(region.getShoreBiomes(), SHORE, biomes, types);
|
||||
}
|
||||
SortedMap<String, String> joined = new TreeMap<>();
|
||||
for (Map.Entry<String, SortedSet<String>> entry : types.entrySet()) {
|
||||
joined.put(entry.getKey(), String.join(JOINER, entry.getValue()));
|
||||
}
|
||||
return Collections.unmodifiableSortedMap(joined);
|
||||
}
|
||||
|
||||
private static void visit(List<String> roots, String type, Function<String, IrisBiome> biomes, SortedMap<String, SortedSet<String>> types) {
|
||||
Deque<String> pending = new ArrayDeque<>(roots);
|
||||
Set<String> seen = new HashSet<>();
|
||||
while (!pending.isEmpty()) {
|
||||
String key = pending.pop();
|
||||
if (!seen.add(key)) {
|
||||
continue;
|
||||
}
|
||||
types.computeIfAbsent(key, ignored -> new TreeSet<>()).add(type);
|
||||
IrisBiome biome = biomes.apply(key);
|
||||
if (biome != null) {
|
||||
pending.addAll(biome.getChildren());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
final class CaveWalls {
|
||||
private static final int CHUNK_WIDTH = 16;
|
||||
private static final int[][] SIDES = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
||||
|
||||
private CaveWalls() {
|
||||
}
|
||||
|
||||
static boolean besideCave(FidelityChunk chunk, int localX, int y, int localZ) {
|
||||
for (int[] side : SIDES) {
|
||||
int x = localX + side[0];
|
||||
int z = localZ + side[1];
|
||||
if (x < 0 || x >= CHUNK_WIDTH || z < 0 || z >= CHUNK_WIDTH) {
|
||||
continue;
|
||||
}
|
||||
if (y < chunk.surfaceY(x, z) && (chunk.open(x, y, z) || chunk.fluid(x, y, z))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
public interface ColumnSource extends AutoCloseable {
|
||||
String label();
|
||||
|
||||
int minY();
|
||||
|
||||
int maxY();
|
||||
|
||||
FidelityColumn column(int x, int z) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import art.arcane.iris.generation.validation.ChunkSpiralScan;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class ColumnSurvey {
|
||||
private ColumnSurvey() {
|
||||
}
|
||||
|
||||
public static FidelityStats scan(ColumnSource source, SurveyWindow window, int threads) {
|
||||
Objects.requireNonNull(source, "source");
|
||||
Objects.requireNonNull(window, "window");
|
||||
List<int[]> rows = new ArrayList<>(window.perAxis());
|
||||
for (int row = 0; row < window.perAxis(); row++) {
|
||||
rows.add(new int[]{row, 0});
|
||||
}
|
||||
Accumulator accumulator = new Accumulator(source, window);
|
||||
int failures = runScan(source, rows, threads, accumulator);
|
||||
if (failures > 0) {
|
||||
throw new IllegalStateException(failures + " of " + rows.size() + " survey rows could not be sampled from "
|
||||
+ source.label(), accumulator.firstFailure());
|
||||
}
|
||||
return accumulator.stats();
|
||||
}
|
||||
|
||||
private static int runScan(ColumnSource source, List<int[]> rows, int threads, Accumulator accumulator) {
|
||||
try {
|
||||
return ChunkSpiralScan.scan(rows, threads, accumulator::sample, accumulator);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Column survey of " + source.label() + " was interrupted", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Accumulator implements ChunkSpiralScan.Listener {
|
||||
private final ColumnSource source;
|
||||
private final SurveyWindow window;
|
||||
private final AtomicReference<Throwable> firstFailure = new AtomicReference<>();
|
||||
private FidelityStats stats;
|
||||
|
||||
private Accumulator(ColumnSource source, SurveyWindow window) {
|
||||
this.source = source;
|
||||
this.window = window;
|
||||
this.stats = new FidelityStats(source.minY(), source.maxY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chunkDone(int row, int ignored, boolean ok) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chunkFailed(int row, int ignored, Throwable error) {
|
||||
firstFailure.compareAndSet(null, error);
|
||||
}
|
||||
|
||||
private void sample(int row, int ignored) throws Exception {
|
||||
FidelityStats rowStats = new FidelityStats(source.minY(), source.maxY());
|
||||
int x = window.coordinate(window.centerX(), row);
|
||||
for (int column = 0; column < window.perAxis(); column++) {
|
||||
rowStats.accept(source.column(x, window.coordinate(window.centerZ(), column)));
|
||||
}
|
||||
merge(rowStats);
|
||||
}
|
||||
|
||||
private synchronized void merge(FidelityStats rowStats) {
|
||||
stats = stats.merge(rowStats);
|
||||
}
|
||||
|
||||
private synchronized FidelityStats stats() {
|
||||
return stats;
|
||||
}
|
||||
|
||||
private Throwable firstFailure() {
|
||||
return firstFailure.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
final class FidelityBlocks {
|
||||
static final String AIR = "minecraft:air";
|
||||
static final String LAVA = "minecraft:lava";
|
||||
|
||||
private static final Set<String> OPEN = Set.of(AIR, "minecraft:cave_air", "minecraft:void_air");
|
||||
private static final Set<String> FLUID = Set.of("minecraft:water", LAVA, "minecraft:bubble_column",
|
||||
"minecraft:kelp", "minecraft:kelp_plant", "minecraft:seagrass", "minecraft:tall_seagrass");
|
||||
private static final List<String> DECORATION_SUFFIXES = List.of("_log", "_wood", "_leaves", "_stem", "_hyphae",
|
||||
"_mushroom_block", "_wart_block");
|
||||
|
||||
private FidelityBlocks() {
|
||||
}
|
||||
|
||||
static String id(String stateKey) {
|
||||
int properties = stateKey.indexOf('[');
|
||||
return properties < 0 ? stateKey : stateKey.substring(0, properties);
|
||||
}
|
||||
|
||||
static boolean open(String id) {
|
||||
return OPEN.contains(id);
|
||||
}
|
||||
|
||||
static boolean lava(String id) {
|
||||
return LAVA.equals(id);
|
||||
}
|
||||
|
||||
static boolean fluid(String id) {
|
||||
return FLUID.contains(id);
|
||||
}
|
||||
|
||||
static boolean decoration(String id) {
|
||||
for (String suffix : DECORATION_SUFFIXES) {
|
||||
if (id.endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
public interface FidelityChunk {
|
||||
int surfaceY(int localX, int localZ);
|
||||
|
||||
String surfaceBiome(int localX, int localZ);
|
||||
|
||||
boolean open(int localX, int y, int localZ);
|
||||
|
||||
boolean fluid(int localX, int y, int localZ);
|
||||
|
||||
String block(int localX, int y, int localZ);
|
||||
|
||||
default boolean lava(int localX, int y, int localZ) {
|
||||
String id = block(localX, y, localZ);
|
||||
return id != null && FidelityBlocks.lava(id);
|
||||
}
|
||||
|
||||
default boolean terrainMass(int localX, int y, int localZ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
default boolean river(int localX, int localZ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean wallsKnown() {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean wall(int localX, int y, int localZ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
public record FidelityColumn(String biome, int terrainY, boolean wet) {
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public final class FidelityComparison {
|
||||
private static final double MIN_BIOME_AREA = 0.02D;
|
||||
private static final List<Integer> HEIGHT_PERCENTILES = List.of(10, 50, 90);
|
||||
private static final long MIN_BAND_COLUMNS = 64L;
|
||||
private static final String UNKNOWN_TYPE = "unknown";
|
||||
|
||||
private FidelityComparison() {
|
||||
}
|
||||
|
||||
public static FidelityReport compare(FidelitySamples columns, FidelitySamples chunks, FidelityThresholds thresholds) {
|
||||
Objects.requireNonNull(columns, "columns");
|
||||
Objects.requireNonNull(chunks, "chunks");
|
||||
Objects.requireNonNull(thresholds, "thresholds");
|
||||
List<FidelityMetric> metrics = new ArrayList<>();
|
||||
addBiomeMetrics(columns.iris().biomeShares(), columns.export().biomeShares(), thresholds.biomeShare(), metrics);
|
||||
addHeightMetrics(columns.iris(), columns.export(), thresholds.heightQuantile(), metrics);
|
||||
List<FidelityObservation> observations = new ArrayList<>();
|
||||
addCaveMetrics(chunks.iris(), chunks.export(), thresholds.caveBand(), metrics, observations);
|
||||
addWallMetric(chunks.iris(), chunks.export(), thresholds.wallShare(), metrics);
|
||||
addDecorationMetric(chunks.iris(), chunks.export(), thresholds.objectDensity(), metrics);
|
||||
addLavaObservation(chunks.iris(), chunks.export(), observations);
|
||||
addOneBlockWallObservation(chunks.iris(), chunks.export(), observations);
|
||||
return new FidelityReport(metrics, List.of(columns.sampling(), chunks.sampling()), observations);
|
||||
}
|
||||
|
||||
public static BiomeShareReport compareBiomeShares(List<FidelitySamples> seeds, Map<String, String> biomeTypes, FidelityThresholds thresholds) {
|
||||
Objects.requireNonNull(seeds, "seeds");
|
||||
Objects.requireNonNull(biomeTypes, "biomeTypes");
|
||||
Objects.requireNonNull(thresholds, "thresholds");
|
||||
if (seeds.isEmpty()) {
|
||||
throw new IllegalArgumentException("Biome shares need at least one surveyed seed");
|
||||
}
|
||||
SortedMap<Long, FidelityReport> reports = new TreeMap<>();
|
||||
List<Map<String, Double>> irisShares = new ArrayList<>(seeds.size());
|
||||
List<Map<String, Double>> exportShares = new ArrayList<>(seeds.size());
|
||||
double irisLand = 0.0D;
|
||||
double exportLand = 0.0D;
|
||||
List<Long> seedList = new ArrayList<>(seeds.size());
|
||||
long columns = 0L;
|
||||
for (FidelitySamples seed : seeds) {
|
||||
Map<String, Double> iris = seed.iris().biomeShares();
|
||||
Map<String, Double> export = seed.export().biomeShares();
|
||||
irisShares.add(iris);
|
||||
exportShares.add(export);
|
||||
double seedIrisLand = landFraction(seed.iris());
|
||||
double seedExportLand = landFraction(seed.export());
|
||||
irisLand += seedIrisLand;
|
||||
exportLand += seedExportLand;
|
||||
seedList.addAll(seed.sampling().seeds());
|
||||
columns += seed.sampling().columns();
|
||||
reports.put(seed.sampling().seeds().getFirst(), biomeReport(iris, export, seedIrisLand, seedExportLand, biomeTypes, List.of(seed.sampling()),
|
||||
thresholds));
|
||||
}
|
||||
FidelitySampling first = seeds.getFirst().sampling();
|
||||
FidelitySampling pooled = new FidelitySampling(first.scope(), first.windowBlocks(), first.strideBlocks(), seedList, columns);
|
||||
Map<String, Double> irisMean = mean(irisShares);
|
||||
Map<String, Double> exportMean = mean(exportShares);
|
||||
FidelityReport mean = biomeReport(irisMean, exportMean, irisLand / seeds.size(), exportLand / seeds.size(), biomeTypes, List.of(pooled),
|
||||
thresholds);
|
||||
return new BiomeShareReport(mean, reports, irisMean, exportMean);
|
||||
}
|
||||
|
||||
private static FidelityReport biomeReport(Map<String, Double> irisShares, Map<String, Double> exportShares, double irisLand, double exportLand,
|
||||
Map<String, String> biomeTypes, List<FidelitySampling> sampling, FidelityThresholds thresholds) {
|
||||
List<FidelityMetric> metrics = new ArrayList<>();
|
||||
addBiomeMetrics(irisShares, exportShares, thresholds.biomeShare(), metrics);
|
||||
List<FidelityObservation> observations = new ArrayList<>();
|
||||
Map<String, Double> irisTypes = typeShares(irisShares, biomeTypes);
|
||||
Map<String, Double> exportTypes = typeShares(exportShares, biomeTypes);
|
||||
Set<String> types = new TreeSet<>(irisTypes.keySet());
|
||||
types.addAll(exportTypes.keySet());
|
||||
for (String type : types) {
|
||||
observations.add(new FidelityObservation("biome.type." + type, irisTypes.getOrDefault(type, 0.0D), exportTypes.getOrDefault(type, 0.0D)));
|
||||
}
|
||||
observations.add(new FidelityObservation("height.landFraction", irisLand, exportLand));
|
||||
return new FidelityReport(metrics, sampling, observations);
|
||||
}
|
||||
|
||||
private static double landFraction(FidelityStats stats) {
|
||||
return stats.heightColumns() == 0L ? 0.0D : stats.landFraction();
|
||||
}
|
||||
|
||||
private static Map<String, Double> mean(List<Map<String, Double>> shares) {
|
||||
SortedMap<String, Double> mean = new TreeMap<>();
|
||||
for (Map<String, Double> seed : shares) {
|
||||
for (Map.Entry<String, Double> entry : seed.entrySet()) {
|
||||
mean.merge(entry.getKey(), entry.getValue() / shares.size(), Double::sum);
|
||||
}
|
||||
}
|
||||
return mean;
|
||||
}
|
||||
|
||||
private static Map<String, Double> typeShares(Map<String, Double> shares, Map<String, String> biomeTypes) {
|
||||
SortedMap<String, Double> types = new TreeMap<>();
|
||||
for (Map.Entry<String, Double> entry : shares.entrySet()) {
|
||||
types.merge(biomeTypes.getOrDefault(entry.getKey(), UNKNOWN_TYPE), entry.getValue(), Double::sum);
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
private static void addBiomeMetrics(Map<String, Double> irisShares, Map<String, Double> exportShares, double threshold, List<FidelityMetric> metrics) {
|
||||
if (irisShares.isEmpty() || exportShares.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Set<String> biomes = new TreeSet<>(irisShares.keySet());
|
||||
biomes.addAll(exportShares.keySet());
|
||||
double difference = 0.0D;
|
||||
for (String biome : biomes) {
|
||||
difference += Math.abs(exportShares.getOrDefault(biome, 0.0D) - irisShares.getOrDefault(biome, 0.0D));
|
||||
}
|
||||
double distance = difference / 2.0D;
|
||||
metrics.add(new FidelityMetric("biome.totalVariation", 0.0D, distance, distance, distance <= threshold));
|
||||
for (Map.Entry<String, Double> entry : irisShares.entrySet()) {
|
||||
double share = entry.getValue();
|
||||
if (share < MIN_BIOME_AREA) {
|
||||
continue;
|
||||
}
|
||||
double exported = exportShares.getOrDefault(entry.getKey(), 0.0D);
|
||||
double error = relativeError(share, exported);
|
||||
metrics.add(new FidelityMetric("biome.share." + entry.getKey(), share, exported, error, error <= threshold));
|
||||
}
|
||||
}
|
||||
|
||||
private static void addHeightMetrics(FidelityStats iris, FidelityStats export, double threshold, List<FidelityMetric> metrics) {
|
||||
if (iris.heightColumns() == 0L) {
|
||||
return;
|
||||
}
|
||||
if (export.heightColumns() == 0L) {
|
||||
metrics.add(new FidelityMetric("height.terrainColumns", iris.heightColumns(), 0.0D, 1.0D, false));
|
||||
return;
|
||||
}
|
||||
double range = iris.maxY() - iris.minY();
|
||||
for (int percentile : HEIGHT_PERCENTILES) {
|
||||
double irisY = iris.heightQuantile(percentile / 100.0D);
|
||||
double exportY = export.heightQuantile(percentile / 100.0D);
|
||||
double delta = Math.abs(exportY - irisY) / range;
|
||||
metrics.add(new FidelityMetric("height.p" + percentile, irisY, exportY, delta, delta <= threshold));
|
||||
}
|
||||
double irisLand = iris.landFraction();
|
||||
double exportLand = export.landFraction();
|
||||
double landDelta = Math.abs(exportLand - irisLand);
|
||||
metrics.add(new FidelityMetric("height.landFraction", irisLand, exportLand, landDelta, landDelta <= threshold));
|
||||
}
|
||||
|
||||
private static void addCaveMetrics(FidelityStats iris, FidelityStats export, double threshold, List<FidelityMetric> metrics,
|
||||
List<FidelityObservation> observations) {
|
||||
long[] irisVoxels = iris.subsurfaceVoxels();
|
||||
long[] exportVoxels = export.subsurfaceVoxels();
|
||||
long[] irisVoids = iris.voidVoxels();
|
||||
long[] exportVoids = export.voidVoxels();
|
||||
long minimum = MIN_BAND_COLUMNS * FidelityStats.BAND_HEIGHT;
|
||||
for (int band = 0; band < irisVoxels.length; band++) {
|
||||
if (irisVoxels[band] == 0L || exportVoxels[band] == 0L) {
|
||||
continue;
|
||||
}
|
||||
boolean judged = irisVoxels[band] >= minimum && exportVoxels[band] >= minimum;
|
||||
addBandShare("cave.band." + iris.bandMinY(band), iris.caveShare(band), export.caveShare(band), judged, threshold, metrics, observations);
|
||||
if (irisVoids[band] > 0L || exportVoids[band] > 0L) {
|
||||
addBandShare("void.band." + iris.bandMinY(band), iris.voidShare(band), export.voidShare(band), judged, threshold, metrics, observations);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addBandShare(String name, double irisShare, double exportShare, boolean judged, double threshold, List<FidelityMetric> metrics,
|
||||
List<FidelityObservation> observations) {
|
||||
if (!judged) {
|
||||
observations.add(new FidelityObservation(name, irisShare, exportShare));
|
||||
return;
|
||||
}
|
||||
double delta = Math.abs(exportShare - irisShare);
|
||||
metrics.add(new FidelityMetric(name, irisShare, exportShare, delta, delta <= threshold));
|
||||
}
|
||||
|
||||
private static void addWallMetric(FidelityStats iris, FidelityStats export, double threshold, List<FidelityMetric> metrics) {
|
||||
if (iris.wallSolidVoxels() == 0L || export.wallSolidVoxels() == 0L) {
|
||||
return;
|
||||
}
|
||||
double irisShare = iris.wallShare();
|
||||
double exportShare = export.wallShare();
|
||||
double error = relativeError(irisShare, exportShare);
|
||||
metrics.add(new FidelityMetric("cave.wallShare", irisShare, exportShare, error, error <= threshold));
|
||||
}
|
||||
|
||||
private static void addLavaObservation(FidelityStats iris, FidelityStats export, List<FidelityObservation> observations) {
|
||||
if (iris.deepVoxels() == 0L || export.deepVoxels() == 0L) {
|
||||
return;
|
||||
}
|
||||
observations.add(new FidelityObservation("cave.lavaShare.belowY" + FidelityStats.DEEP_LAVA_LINE, iris.deepLavaShare(), export.deepLavaShare()));
|
||||
}
|
||||
|
||||
private static void addOneBlockWallObservation(FidelityStats iris, FidelityStats export, List<FidelityObservation> observations) {
|
||||
if (iris.wallSolidVoxels() == 0L || export.wallSolidVoxels() == 0L) {
|
||||
return;
|
||||
}
|
||||
observations.add(new FidelityObservation("cave.wallShare.oneBlock", iris.oneBlockWallShare(), export.oneBlockWallShare()));
|
||||
}
|
||||
|
||||
private static void addDecorationMetric(FidelityStats iris, FidelityStats export, double threshold, List<FidelityMetric> metrics) {
|
||||
if (iris.blockColumns() == 0L || export.blockColumns() == 0L) {
|
||||
return;
|
||||
}
|
||||
double irisDensity = iris.decorationPerSquareKilometre();
|
||||
double exportDensity = export.decorationPerSquareKilometre();
|
||||
double error = relativeError(irisDensity, exportDensity);
|
||||
metrics.add(new FidelityMetric("decoration.columnsPerKm2", irisDensity, exportDensity, error, error <= threshold));
|
||||
}
|
||||
|
||||
private static double relativeError(double reference, double measured) {
|
||||
if (reference == 0.0D) {
|
||||
return measured == 0.0D ? 0.0D : 1.0D;
|
||||
}
|
||||
return Math.abs(measured - reference) / reference;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record FidelityMetric(String name, double iris, double export, double delta, boolean pass) {
|
||||
public FidelityMetric {
|
||||
Objects.requireNonNull(name, "name");
|
||||
if (!Double.isFinite(iris) || !Double.isFinite(export) || !Double.isFinite(delta)) {
|
||||
throw new IllegalArgumentException("Fidelity metric " + name + " has a non-finite value: iris=" + iris
|
||||
+ " export=" + export + " delta=" + delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record FidelityObservation(String name, double iris, double export) {
|
||||
public FidelityObservation {
|
||||
Objects.requireNonNull(name, "name");
|
||||
if (!Double.isFinite(iris) || !Double.isFinite(export)) {
|
||||
throw new IllegalArgumentException("Fidelity observation " + name + " has a non-finite value: iris=" + iris + " export=" + export);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public record FidelityReport(List<FidelityMetric> metrics, List<FidelitySampling> sampling, List<FidelityObservation> observations) {
|
||||
private static final String NAME_HEADER = "metric";
|
||||
private static final String OBSERVATION_VERDICT = "INFO";
|
||||
|
||||
public FidelityReport {
|
||||
metrics = List.copyOf(metrics);
|
||||
sampling = List.copyOf(sampling);
|
||||
observations = List.copyOf(observations);
|
||||
}
|
||||
|
||||
public boolean passed() {
|
||||
if (metrics.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (FidelityMetric metric : metrics) {
|
||||
if (!metric.pass()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return new GsonBuilder().setPrettyPrinting().create().toJson(toJsonObject());
|
||||
}
|
||||
|
||||
public JsonObject toJsonObject() {
|
||||
JsonArray entries = new JsonArray(metrics.size());
|
||||
for (FidelityMetric metric : metrics) {
|
||||
JsonObject entry = new JsonObject();
|
||||
entry.addProperty("name", metric.name());
|
||||
entry.addProperty("iris", metric.iris());
|
||||
entry.addProperty("export", metric.export());
|
||||
entry.addProperty("delta", metric.delta());
|
||||
entry.addProperty("pass", metric.pass());
|
||||
entries.add(entry);
|
||||
}
|
||||
JsonArray samplingEntries = new JsonArray(sampling.size());
|
||||
for (FidelitySampling scope : sampling) {
|
||||
JsonObject entry = new JsonObject();
|
||||
entry.addProperty("scope", scope.scope());
|
||||
entry.addProperty("windowBlocks", scope.windowBlocks());
|
||||
entry.addProperty("strideBlocks", scope.strideBlocks());
|
||||
JsonArray seeds = new JsonArray(scope.seeds().size());
|
||||
for (long seed : scope.seeds()) {
|
||||
seeds.add(seed);
|
||||
}
|
||||
entry.add("seeds", seeds);
|
||||
entry.addProperty("columns", scope.columns());
|
||||
samplingEntries.add(entry);
|
||||
}
|
||||
JsonArray notes = new JsonArray(observations.size());
|
||||
for (FidelityObservation observation : observations) {
|
||||
JsonObject entry = new JsonObject();
|
||||
entry.addProperty("name", observation.name());
|
||||
entry.addProperty("iris", observation.iris());
|
||||
entry.addProperty("export", observation.export());
|
||||
notes.add(entry);
|
||||
}
|
||||
JsonObject root = new JsonObject();
|
||||
root.addProperty("passed", passed());
|
||||
root.add("sampling", samplingEntries);
|
||||
root.add("metrics", entries);
|
||||
root.add("observations", notes);
|
||||
return root;
|
||||
}
|
||||
|
||||
public String toTable() {
|
||||
int width = NAME_HEADER.length();
|
||||
for (FidelityMetric metric : metrics) {
|
||||
width = Math.max(width, metric.name().length());
|
||||
}
|
||||
for (FidelityObservation observation : observations) {
|
||||
width = Math.max(width, observation.name().length());
|
||||
}
|
||||
String header = "%-" + width + "s %12s %12s %10s %s\n";
|
||||
String row = "%-" + width + "s %12.4f %12.4f %10.4f %s\n";
|
||||
StringBuilder table = new StringBuilder();
|
||||
for (FidelitySampling scope : sampling) {
|
||||
table.append(String.format(Locale.ROOT, "sampling %s window=%d stride=%d seeds=%s columns=%d\n", scope.scope(),
|
||||
scope.windowBlocks(), scope.strideBlocks(), scope.seeds(), scope.columns()));
|
||||
}
|
||||
table.append(String.format(Locale.ROOT, header, NAME_HEADER, "iris", "export", "delta", "result"));
|
||||
int passing = 0;
|
||||
for (FidelityMetric metric : metrics) {
|
||||
if (metric.pass()) {
|
||||
passing++;
|
||||
}
|
||||
table.append(String.format(Locale.ROOT, row, metric.name(), metric.iris(), metric.export(), metric.delta(),
|
||||
metric.pass() ? "PASS" : "FAIL"));
|
||||
}
|
||||
String note = "%-" + width + "s %12.4f %12.4f %10s %s\n";
|
||||
for (FidelityObservation observation : observations) {
|
||||
table.append(String.format(Locale.ROOT, note, observation.name(), observation.iris(), observation.export(), "", OBSERVATION_VERDICT));
|
||||
}
|
||||
if (metrics.isEmpty()) {
|
||||
return table.append("overall FAIL: no metrics were compared\n").toString();
|
||||
}
|
||||
return table.append("overall ").append(passed() ? "PASS" : "FAIL").append(": ").append(passing).append(" of ")
|
||||
.append(metrics.size()).append(" metrics passed\n").toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record FidelitySamples(FidelityStats iris, FidelityStats export, FidelitySampling sampling) {
|
||||
public FidelitySamples {
|
||||
Objects.requireNonNull(iris, "iris");
|
||||
Objects.requireNonNull(export, "export");
|
||||
Objects.requireNonNull(sampling, "sampling");
|
||||
if (iris.minY() != export.minY() || iris.maxY() != export.maxY()) {
|
||||
throw new IllegalArgumentException("Iris height range [" + iris.minY() + ", " + iris.maxY()
|
||||
+ ") differs from export height range [" + export.minY() + ", " + export.maxY() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record FidelitySampling(String scope, int windowBlocks, int strideBlocks, List<Long> seeds, long columns) {
|
||||
public FidelitySampling {
|
||||
Objects.requireNonNull(scope, "scope");
|
||||
seeds = List.copyOf(seeds);
|
||||
if (seeds.isEmpty()) {
|
||||
throw new IllegalArgumentException("Fidelity sampling '" + scope + "' lists no seed");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import art.arcane.iris.generation.validation.ChunkSpiralScan;
|
||||
import art.arcane.volmlib.util.math.ChunkSpiral;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class FidelityScanner {
|
||||
private FidelityScanner() {
|
||||
}
|
||||
|
||||
public static FidelityStats scan(FidelitySource source, int centerChunkX, int centerChunkZ, int radiusChunks, int stride, int threads) {
|
||||
Objects.requireNonNull(source, "source");
|
||||
FidelityStats.requireStride(stride);
|
||||
List<int[]> targets = ChunkSpiral.centerOut(centerChunkX, centerChunkZ, Math.max(0, radiusChunks));
|
||||
Accumulator accumulator = new Accumulator(source, stride);
|
||||
int failures = runScan(source, targets, threads, accumulator);
|
||||
if (failures > 0) {
|
||||
throw new IllegalStateException(failures + " of " + targets.size() + " chunks could not be sampled from "
|
||||
+ source.label(), accumulator.firstFailure());
|
||||
}
|
||||
return accumulator.stats();
|
||||
}
|
||||
|
||||
private static int runScan(FidelitySource source, List<int[]> targets, int threads, Accumulator accumulator) {
|
||||
try {
|
||||
return ChunkSpiralScan.scan(targets, threads, accumulator::sample, accumulator);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Fidelity scan of " + source.label() + " was interrupted", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Accumulator implements ChunkSpiralScan.Listener {
|
||||
private final FidelitySource source;
|
||||
private final int stride;
|
||||
private final AtomicReference<Throwable> firstFailure = new AtomicReference<>();
|
||||
private FidelityStats stats;
|
||||
|
||||
private Accumulator(FidelitySource source, int stride) {
|
||||
this.source = source;
|
||||
this.stride = stride;
|
||||
this.stats = new FidelityStats(source.minY(), source.maxY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chunkDone(int chunkX, int chunkZ, boolean ok) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void chunkFailed(int chunkX, int chunkZ, Throwable error) {
|
||||
firstFailure.compareAndSet(null, error);
|
||||
}
|
||||
|
||||
private void sample(int chunkX, int chunkZ) throws Exception {
|
||||
FidelityStats chunkStats = new FidelityStats(source.minY(), source.maxY());
|
||||
chunkStats.accept(source.sample(chunkX, chunkZ), stride);
|
||||
merge(chunkStats);
|
||||
}
|
||||
|
||||
private synchronized void merge(FidelityStats chunkStats) {
|
||||
stats = stats.merge(chunkStats);
|
||||
}
|
||||
|
||||
private synchronized FidelityStats stats() {
|
||||
return stats;
|
||||
}
|
||||
|
||||
private Throwable firstFailure() {
|
||||
return firstFailure.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
public interface FidelitySource extends AutoCloseable {
|
||||
String label();
|
||||
|
||||
int minY();
|
||||
|
||||
int maxY();
|
||||
|
||||
FidelityChunk sample(int chunkX, int chunkZ) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public final class FidelityStats {
|
||||
public static final int DEEP_LAVA_LINE = -54;
|
||||
private static final int CHUNK_WIDTH = 16;
|
||||
static final int BAND_HEIGHT = 16;
|
||||
private static final int MIN_ROOF_THICKNESS = 4;
|
||||
private static final double SQUARE_METRES_PER_SQUARE_KILOMETRE = 1_000_000.0D;
|
||||
|
||||
private final int minY;
|
||||
private final int maxY;
|
||||
private final long[] heightHistogram;
|
||||
private final long[] subsurfaceVoxels;
|
||||
private final long[] caveVoxels;
|
||||
private final long[] voidVoxels;
|
||||
private final SortedMap<String, Long> biomeColumns;
|
||||
private long columns;
|
||||
private long heightColumns;
|
||||
private long fluidColumns;
|
||||
private long blockColumns;
|
||||
private long decorationColumns;
|
||||
private long riverColumns;
|
||||
private long deepVoxels;
|
||||
private long deepLavaVoxels;
|
||||
private long wallSolidVoxels;
|
||||
private long wallVoxels;
|
||||
private long oneBlockWallVoxels;
|
||||
|
||||
public FidelityStats(int minY, int maxY) {
|
||||
if (maxY <= minY) {
|
||||
throw new IllegalArgumentException("Fidelity height range [" + minY + ", " + maxY + ") is empty");
|
||||
}
|
||||
this.minY = minY;
|
||||
this.maxY = maxY;
|
||||
int height = maxY - minY;
|
||||
int bands = (height + BAND_HEIGHT - 1) / BAND_HEIGHT;
|
||||
this.heightHistogram = new long[height];
|
||||
this.subsurfaceVoxels = new long[bands];
|
||||
this.caveVoxels = new long[bands];
|
||||
this.voidVoxels = new long[bands];
|
||||
this.biomeColumns = new TreeMap<>();
|
||||
}
|
||||
|
||||
public void accept(FidelityChunk chunk, int stride) {
|
||||
Objects.requireNonNull(chunk, "chunk");
|
||||
requireStride(stride);
|
||||
for (int x = 0; x < CHUNK_WIDTH; x += stride) {
|
||||
for (int z = 0; z < CHUNK_WIDTH; z += stride) {
|
||||
acceptColumn(chunk, x, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void accept(FidelityColumn column) {
|
||||
Objects.requireNonNull(column, "column");
|
||||
columns++;
|
||||
if (column.biome() != null) {
|
||||
biomeColumns.merge(column.biome(), 1L, Long::sum);
|
||||
}
|
||||
int terrainY = column.terrainY();
|
||||
if (terrainY >= maxY) {
|
||||
throw new IllegalArgumentException("Terrain Y " + terrainY + " is above the height range [" + minY + ", " + maxY + ")");
|
||||
}
|
||||
if (terrainY < minY) {
|
||||
return;
|
||||
}
|
||||
heightColumns++;
|
||||
heightHistogram[terrainY - minY]++;
|
||||
if (column.wet()) {
|
||||
fluidColumns++;
|
||||
}
|
||||
}
|
||||
|
||||
public FidelityStats merge(FidelityStats other) {
|
||||
Objects.requireNonNull(other, "other");
|
||||
if (other.minY != minY || other.maxY != maxY) {
|
||||
throw new IllegalArgumentException("Cannot merge fidelity stats over [" + minY + ", " + maxY + ") with ["
|
||||
+ other.minY + ", " + other.maxY + ")");
|
||||
}
|
||||
FidelityStats merged = new FidelityStats(minY, maxY);
|
||||
merged.add(this);
|
||||
merged.add(other);
|
||||
return merged;
|
||||
}
|
||||
|
||||
public int minY() {
|
||||
return minY;
|
||||
}
|
||||
|
||||
public int maxY() {
|
||||
return maxY;
|
||||
}
|
||||
|
||||
public long columns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public long heightColumns() {
|
||||
return heightColumns;
|
||||
}
|
||||
|
||||
public long fluidColumns() {
|
||||
return fluidColumns;
|
||||
}
|
||||
|
||||
public long blockColumns() {
|
||||
return blockColumns;
|
||||
}
|
||||
|
||||
public long decorationColumns() {
|
||||
return decorationColumns;
|
||||
}
|
||||
|
||||
public long riverColumns() {
|
||||
return riverColumns;
|
||||
}
|
||||
|
||||
public long deepVoxels() {
|
||||
return deepVoxels;
|
||||
}
|
||||
|
||||
public long deepLavaVoxels() {
|
||||
return deepLavaVoxels;
|
||||
}
|
||||
|
||||
public long wallSolidVoxels() {
|
||||
return wallSolidVoxels;
|
||||
}
|
||||
|
||||
public long wallVoxels() {
|
||||
return wallVoxels;
|
||||
}
|
||||
|
||||
public long oneBlockWallVoxels() {
|
||||
return oneBlockWallVoxels;
|
||||
}
|
||||
|
||||
public Map<String, Long> biomeColumns() {
|
||||
return Collections.unmodifiableSortedMap(biomeColumns);
|
||||
}
|
||||
|
||||
public long[] heightHistogram() {
|
||||
return heightHistogram.clone();
|
||||
}
|
||||
|
||||
public long[] subsurfaceVoxels() {
|
||||
return subsurfaceVoxels.clone();
|
||||
}
|
||||
|
||||
public long[] caveVoxels() {
|
||||
return caveVoxels.clone();
|
||||
}
|
||||
|
||||
public long[] voidVoxels() {
|
||||
return voidVoxels.clone();
|
||||
}
|
||||
|
||||
public int bandCount() {
|
||||
return subsurfaceVoxels.length;
|
||||
}
|
||||
|
||||
public int bandMinY(int band) {
|
||||
return minY + band * BAND_HEIGHT;
|
||||
}
|
||||
|
||||
public Map<String, Double> biomeShares() {
|
||||
long resolved = 0L;
|
||||
for (long count : biomeColumns.values()) {
|
||||
resolved += count;
|
||||
}
|
||||
SortedMap<String, Double> shares = new TreeMap<>();
|
||||
for (Map.Entry<String, Long> entry : biomeColumns.entrySet()) {
|
||||
shares.put(entry.getKey(), entry.getValue() / (double) resolved);
|
||||
}
|
||||
return Collections.unmodifiableSortedMap(shares);
|
||||
}
|
||||
|
||||
public double heightQuantile(double fraction) {
|
||||
if (heightColumns == 0L) {
|
||||
throw new IllegalStateException("No terrain columns were sampled");
|
||||
}
|
||||
if (!(fraction >= 0.0D && fraction <= 1.0D)) {
|
||||
throw new IllegalArgumentException("Quantile fraction must be within [0, 1], got " + fraction);
|
||||
}
|
||||
long rank = Math.max(1L, (long) Math.ceil(fraction * heightColumns));
|
||||
long seen = 0L;
|
||||
for (int index = 0; index < heightHistogram.length; index++) {
|
||||
seen += heightHistogram[index];
|
||||
if (seen >= rank) {
|
||||
return minY + index;
|
||||
}
|
||||
}
|
||||
return maxY - 1;
|
||||
}
|
||||
|
||||
public double landFraction() {
|
||||
if (heightColumns == 0L) {
|
||||
throw new IllegalStateException("No terrain columns were sampled");
|
||||
}
|
||||
return (heightColumns - fluidColumns) / (double) heightColumns;
|
||||
}
|
||||
|
||||
public double caveShare(int band) {
|
||||
return bandShare(caveVoxels, band);
|
||||
}
|
||||
|
||||
public double voidShare(int band) {
|
||||
return bandShare(voidVoxels, band);
|
||||
}
|
||||
|
||||
public double deepLavaShare() {
|
||||
if (deepVoxels == 0L) {
|
||||
throw new IllegalStateException("No subsurface voxels were sampled below Y " + DEEP_LAVA_LINE);
|
||||
}
|
||||
return deepLavaVoxels / (double) deepVoxels;
|
||||
}
|
||||
|
||||
public double wallShare() {
|
||||
if (wallSolidVoxels == 0L) {
|
||||
throw new IllegalStateException("No solid subsurface voxels reported cave walls");
|
||||
}
|
||||
return wallVoxels / (double) wallSolidVoxels;
|
||||
}
|
||||
|
||||
public double oneBlockWallShare() {
|
||||
if (wallSolidVoxels == 0L) {
|
||||
throw new IllegalStateException("No solid subsurface voxels reported cave walls");
|
||||
}
|
||||
return oneBlockWallVoxels / (double) wallSolidVoxels;
|
||||
}
|
||||
|
||||
public double decorationPerSquareKilometre() {
|
||||
if (blockColumns == 0L) {
|
||||
throw new IllegalStateException("No sampled column reported block ids");
|
||||
}
|
||||
return decorationColumns / (double) blockColumns * SQUARE_METRES_PER_SQUARE_KILOMETRE;
|
||||
}
|
||||
|
||||
static void requireStride(int stride) {
|
||||
if (stride < 1 || stride > CHUNK_WIDTH) {
|
||||
throw new IllegalArgumentException("Column stride must be within [1, " + CHUNK_WIDTH + "], got " + stride);
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptColumn(FidelityChunk chunk, int x, int z) {
|
||||
columns++;
|
||||
String biome = chunk.surfaceBiome(x, z);
|
||||
if (biome != null) {
|
||||
biomeColumns.merge(biome, 1L, Long::sum);
|
||||
}
|
||||
if (chunk.river(x, z)) {
|
||||
riverColumns++;
|
||||
}
|
||||
int surfaceY = chunk.surfaceY(x, z);
|
||||
if (surfaceY >= maxY) {
|
||||
throw new IllegalArgumentException("Surface Y " + surfaceY + " at column " + x + "," + z
|
||||
+ " is above the height range [" + minY + ", " + maxY + ")");
|
||||
}
|
||||
if (surfaceY < minY) {
|
||||
return;
|
||||
}
|
||||
int terrainY = surfaceY;
|
||||
String top = chunk.block(x, surfaceY, z);
|
||||
if (top != null) {
|
||||
blockColumns++;
|
||||
if (FidelityBlocks.decoration(top)) {
|
||||
decorationColumns++;
|
||||
terrainY = terrainBelow(chunk, x, z, surfaceY);
|
||||
}
|
||||
}
|
||||
if (terrainY < minY) {
|
||||
return;
|
||||
}
|
||||
heightColumns++;
|
||||
heightHistogram[terrainY - minY]++;
|
||||
if (terrainY + 1 < maxY && chunk.fluid(x, terrainY + 1, z)) {
|
||||
fluidColumns++;
|
||||
}
|
||||
int caveCeiling = caveCeiling(chunk, x, z, terrainY);
|
||||
boolean walls = chunk.wallsKnown() && interior(x, z);
|
||||
for (int y = minY; y < caveCeiling; y++) {
|
||||
int band = (y - minY) / BAND_HEIGHT;
|
||||
subsurfaceVoxels[band]++;
|
||||
boolean open = chunk.open(x, y, z);
|
||||
if (open) {
|
||||
if (chunk.terrainMass(x, y, z)) {
|
||||
caveVoxels[band]++;
|
||||
} else {
|
||||
voidVoxels[band]++;
|
||||
}
|
||||
}
|
||||
if (walls && !open && !chunk.fluid(x, y, z)) {
|
||||
wallSolidVoxels++;
|
||||
if (chunk.wall(x, y, z)) {
|
||||
wallVoxels++;
|
||||
}
|
||||
if (CaveWalls.besideCave(chunk, x, y, z)) {
|
||||
oneBlockWallVoxels++;
|
||||
}
|
||||
}
|
||||
if (y < DEEP_LAVA_LINE) {
|
||||
deepVoxels++;
|
||||
if (chunk.lava(x, y, z)) {
|
||||
deepLavaVoxels++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double bandShare(long[] voxels, int band) {
|
||||
if (subsurfaceVoxels[band] == 0L) {
|
||||
throw new IllegalStateException("No subsurface voxels were sampled in the band starting at Y " + bandMinY(band));
|
||||
}
|
||||
return voxels[band] / (double) subsurfaceVoxels[band];
|
||||
}
|
||||
|
||||
private static boolean interior(int x, int z) {
|
||||
return x > 0 && x < CHUNK_WIDTH - 1 && z > 0 && z < CHUNK_WIDTH - 1;
|
||||
}
|
||||
|
||||
private int terrainBelow(FidelityChunk chunk, int x, int z, int surfaceY) {
|
||||
int y = surfaceY - 1;
|
||||
while (y >= minY && aboveTerrain(chunk, x, y, z)) {
|
||||
y--;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
private int caveCeiling(FidelityChunk chunk, int x, int z, int terrainY) {
|
||||
int top = terrainY;
|
||||
while (top >= minY) {
|
||||
int bottom = top;
|
||||
while (bottom >= minY && top - bottom < MIN_ROOF_THICKNESS && !aboveTerrain(chunk, x, bottom, z)) {
|
||||
bottom--;
|
||||
}
|
||||
if (top - bottom >= MIN_ROOF_THICKNESS || bottom < minY || !decoration(chunk, x, bottom, z)) {
|
||||
return top;
|
||||
}
|
||||
top = bottom;
|
||||
while (top >= minY && aboveTerrain(chunk, x, top, z)) {
|
||||
top--;
|
||||
}
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
private static boolean decoration(FidelityChunk chunk, int x, int y, int z) {
|
||||
String id = chunk.block(x, y, z);
|
||||
return id != null && FidelityBlocks.decoration(id);
|
||||
}
|
||||
|
||||
private static boolean aboveTerrain(FidelityChunk chunk, int x, int y, int z) {
|
||||
if (chunk.open(x, y, z) || chunk.fluid(x, y, z)) {
|
||||
return true;
|
||||
}
|
||||
String id = chunk.block(x, y, z);
|
||||
return id != null && FidelityBlocks.decoration(id);
|
||||
}
|
||||
|
||||
private void add(FidelityStats other) {
|
||||
columns += other.columns;
|
||||
heightColumns += other.heightColumns;
|
||||
fluidColumns += other.fluidColumns;
|
||||
blockColumns += other.blockColumns;
|
||||
decorationColumns += other.decorationColumns;
|
||||
riverColumns += other.riverColumns;
|
||||
deepVoxels += other.deepVoxels;
|
||||
deepLavaVoxels += other.deepLavaVoxels;
|
||||
wallSolidVoxels += other.wallSolidVoxels;
|
||||
wallVoxels += other.wallVoxels;
|
||||
oneBlockWallVoxels += other.oneBlockWallVoxels;
|
||||
for (int index = 0; index < heightHistogram.length; index++) {
|
||||
heightHistogram[index] += other.heightHistogram[index];
|
||||
}
|
||||
for (int band = 0; band < subsurfaceVoxels.length; band++) {
|
||||
subsurfaceVoxels[band] += other.subsurfaceVoxels[band];
|
||||
caveVoxels[band] += other.caveVoxels[band];
|
||||
voidVoxels[band] += other.voidVoxels[band];
|
||||
}
|
||||
for (Map.Entry<String, Long> entry : other.biomeColumns.entrySet()) {
|
||||
biomeColumns.merge(entry.getKey(), entry.getValue(), Long::sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
public record FidelityThresholds(double biomeShare, double heightQuantile, double caveBand, double objectDensity, double wallShare) {
|
||||
public FidelityThresholds {
|
||||
requireThreshold("biomeShare", biomeShare);
|
||||
requireThreshold("heightQuantile", heightQuantile);
|
||||
requireThreshold("caveBand", caveBand);
|
||||
requireThreshold("objectDensity", objectDensity);
|
||||
requireThreshold("wallShare", wallShare);
|
||||
}
|
||||
|
||||
public static FidelityThresholds defaults() {
|
||||
return new FidelityThresholds(0.15D, 0.10D, 0.15D, 0.15D, 0.25D);
|
||||
}
|
||||
|
||||
private static void requireThreshold(String name, double value) {
|
||||
if (!Double.isFinite(value) || value < 0.0D) {
|
||||
throw new IllegalArgumentException("Fidelity threshold " + name + " must be a finite non-negative number, got " + value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import art.arcane.iris.generation.biome.IrisBiome;
|
||||
import art.arcane.iris.generation.runtime.Engine;
|
||||
import art.arcane.iris.generation.runtime.IrisComplex;
|
||||
import art.arcane.iris.world.IrisWorld;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class IrisEngineColumnSource implements ColumnSource {
|
||||
private final Engine engine;
|
||||
private final IrisComplex complex;
|
||||
private final int minY;
|
||||
private final int maxY;
|
||||
private final int fluidHeight;
|
||||
|
||||
public IrisEngineColumnSource(Engine engine) {
|
||||
this.engine = Objects.requireNonNull(engine, "engine");
|
||||
this.complex = engine.getComplex();
|
||||
IrisWorld world = engine.getTarget().getWorld();
|
||||
this.minY = world.minHeight();
|
||||
this.maxY = world.maxHeight();
|
||||
this.fluidHeight = (int) Math.round(complex.getFluidHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String label() {
|
||||
return "iris:" + engine.getDimension().getLoadKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int minY() {
|
||||
return minY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int maxY() {
|
||||
return maxY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FidelityColumn column(int x, int z) {
|
||||
int height = Math.min(complex.naturalTrueHeight(x, z), maxY - minY - 1);
|
||||
IrisBiome biome = complex.getNaturalTrueBiomeStream().get(x, z);
|
||||
return new FidelityColumn(biome == null ? null : biome.getLoadKey(), height + minY, height < fluidHeight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import art.arcane.iris.generation.biome.IrisBiome;
|
||||
import art.arcane.iris.generation.runtime.Engine;
|
||||
import art.arcane.iris.generation.runtime.IrisComplex;
|
||||
import art.arcane.iris.generation.terrain.Terrain3DColumn;
|
||||
import art.arcane.iris.probe.RealPackProbeSupport;
|
||||
import art.arcane.iris.world.IrisWorld;
|
||||
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class IrisEngineFidelitySource implements FidelitySource {
|
||||
private static final int CHUNK_WIDTH = 16;
|
||||
|
||||
private final Engine engine;
|
||||
private final int minY;
|
||||
private final int maxY;
|
||||
|
||||
public IrisEngineFidelitySource(Engine engine) {
|
||||
this.engine = Objects.requireNonNull(engine, "engine");
|
||||
IrisWorld world = engine.getTarget().getWorld();
|
||||
this.minY = world.minHeight();
|
||||
this.maxY = world.maxHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String label() {
|
||||
return "iris:" + engine.getDimension().getLoadKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int minY() {
|
||||
return minY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int maxY() {
|
||||
return maxY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FidelityChunk sample(int chunkX, int chunkZ) {
|
||||
return new EngineChunk(RealPackProbeSupport.generateChunk(engine, chunkX, chunkZ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
private final class EngineChunk implements FidelityChunk {
|
||||
private final RealPackProbeSupport.GeneratedChunk chunk;
|
||||
private final Map<NativeBlockState, String> ids = new IdentityHashMap<>();
|
||||
private final int[] surfaces = new int[CHUNK_WIDTH * CHUNK_WIDTH];
|
||||
private final TerrainMass[] masses = new TerrainMass[CHUNK_WIDTH * CHUNK_WIDTH];
|
||||
|
||||
private EngineChunk(RealPackProbeSupport.GeneratedChunk chunk) {
|
||||
this.chunk = chunk;
|
||||
Arrays.fill(surfaces, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int surfaceY(int localX, int localZ) {
|
||||
int index = localZ * CHUNK_WIDTH + localX;
|
||||
if (surfaces[index] == Integer.MIN_VALUE) {
|
||||
surfaces[index] = scanSurface(localX, localZ);
|
||||
}
|
||||
return surfaces[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String surfaceBiome(int localX, int localZ) {
|
||||
IrisBiome biome = engine.getComplex().getTrueBiomeStream().get((chunk.chunkX() << 4) + localX, (chunk.chunkZ() << 4) + localZ);
|
||||
return biome == null ? null : biome.getLoadKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean open(int localX, int y, int localZ) {
|
||||
return FidelityBlocks.open(block(localX, y, localZ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fluid(int localX, int y, int localZ) {
|
||||
return FidelityBlocks.fluid(block(localX, y, localZ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String block(int localX, int y, int localZ) {
|
||||
NativeBlockState state = chunk.blocks().get(localX, y - minY, localZ);
|
||||
if (state == null) {
|
||||
return FidelityBlocks.AIR;
|
||||
}
|
||||
return ids.computeIfAbsent(state, (NativeBlockState key) -> FidelityBlocks.id(key.key()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean terrainMass(int localX, int y, int localZ) {
|
||||
int index = localZ * CHUNK_WIDTH + localX;
|
||||
TerrainMass mass = masses[index];
|
||||
if (mass == null) {
|
||||
mass = TerrainMass.of(engine.getComplex(), (chunk.chunkX() << 4) + localX, (chunk.chunkZ() << 4) + localZ);
|
||||
masses[index] = mass;
|
||||
}
|
||||
return mass.solid(y - minY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wallsKnown() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wall(int localX, int y, int localZ) {
|
||||
return CaveWalls.besideCave(this, localX, y, localZ);
|
||||
}
|
||||
|
||||
private int scanSurface(int localX, int localZ) {
|
||||
for (int y = maxY - 1; y >= minY; y--) {
|
||||
String id = block(localX, y, localZ);
|
||||
if (!FidelityBlocks.open(id) && !FidelityBlocks.fluid(id)) {
|
||||
return y;
|
||||
}
|
||||
}
|
||||
return minY - 1;
|
||||
}
|
||||
}
|
||||
|
||||
private record TerrainMass(int top, Terrain3DColumn column) {
|
||||
private static TerrainMass of(IrisComplex complex, int x, int z) {
|
||||
return new TerrainMass(complex.getRoundedHeighteightStream().get(x, z), complex.terrainColumn(x, z));
|
||||
}
|
||||
|
||||
private boolean solid(int localY) {
|
||||
return localY <= top && (column == null || column.isSolid(localY));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
import art.arcane.volmlib.util.nbt.io.NBTDeserializer;
|
||||
import art.arcane.volmlib.util.nbt.io.NamedTag;
|
||||
import art.arcane.volmlib.util.nbt.mca.CompressionType;
|
||||
import art.arcane.volmlib.util.nbt.mca.MCAFileSupport;
|
||||
import art.arcane.volmlib.util.nbt.mca.MCAUtilSupport;
|
||||
import art.arcane.volmlib.util.nbt.tag.CompoundTag;
|
||||
import art.arcane.volmlib.util.nbt.tag.ListTag;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public final class RegionFileFidelitySource implements FidelitySource {
|
||||
private static final int CHUNK_WIDTH = 16;
|
||||
private static final int SECTION_VOLUME = 4096;
|
||||
private static final long SECTOR_BYTES = 4096L;
|
||||
private static final int EXTERNAL_PAYLOAD = 0x80;
|
||||
private static final Set<String> FULL_STATUS = Set.of("minecraft:full", "full");
|
||||
|
||||
private final Path regionDirectory;
|
||||
private final int minY;
|
||||
private final int maxY;
|
||||
|
||||
public RegionFileFidelitySource(Path regionDirectory, int minY, int maxY) {
|
||||
this.regionDirectory = Objects.requireNonNull(regionDirectory, "regionDirectory");
|
||||
if (maxY <= minY) {
|
||||
throw new IllegalArgumentException("Region height range [" + minY + ", " + maxY + ") is empty");
|
||||
}
|
||||
this.minY = minY;
|
||||
this.maxY = maxY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String label() {
|
||||
return regionDirectory.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int minY() {
|
||||
return minY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int maxY() {
|
||||
return maxY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FidelityChunk sample(int chunkX, int chunkZ) throws IOException {
|
||||
CompoundTag root = readChunk(chunkX, chunkZ);
|
||||
String status = root.getString("Status");
|
||||
if (!FULL_STATUS.contains(status)) {
|
||||
throw new IllegalStateException("Chunk " + chunkX + "," + chunkZ + " in " + regionDirectory
|
||||
+ " has status '" + status + "', not full");
|
||||
}
|
||||
String[] blocks = new String[CHUNK_WIDTH * CHUNK_WIDTH * (maxY - minY)];
|
||||
Arrays.fill(blocks, FidelityBlocks.AIR);
|
||||
ListTag<?> sections = root.getListTag("sections");
|
||||
if (sections != null && sections.size() > 0) {
|
||||
for (CompoundTag section : sections.asCompoundTagList()) {
|
||||
decodeSection(section, blocks, chunkX, chunkZ);
|
||||
}
|
||||
}
|
||||
return new RegionChunk(blocks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
private CompoundTag readChunk(int chunkX, int chunkZ) throws IOException {
|
||||
Path file = regionDirectory.resolve(MCAUtilSupport.createNameFromChunkLocation(chunkX, chunkZ));
|
||||
if (!Files.isRegularFile(file)) {
|
||||
throw new IllegalStateException("Region file " + file + " for chunk " + chunkX + "," + chunkZ + " does not exist");
|
||||
}
|
||||
byte[] payload;
|
||||
int compression;
|
||||
try (RandomAccessFile region = new RandomAccessFile(file.toFile(), "r")) {
|
||||
region.seek(MCAFileSupport.getChunkIndex(chunkX, chunkZ) * 4L);
|
||||
int location = region.readInt();
|
||||
int sector = location >>> 8;
|
||||
if (sector == 0 || (location & 0xFF) == 0) {
|
||||
throw new IllegalStateException("Chunk " + chunkX + "," + chunkZ + " is not stored in " + file);
|
||||
}
|
||||
region.seek(sector * SECTOR_BYTES);
|
||||
int length = region.readInt();
|
||||
compression = region.readUnsignedByte();
|
||||
if (length < 1) {
|
||||
throw new IOException("Chunk " + chunkX + "," + chunkZ + " in " + file + " has invalid length " + length);
|
||||
}
|
||||
payload = new byte[length - 1];
|
||||
region.readFully(payload);
|
||||
}
|
||||
if ((compression & EXTERNAL_PAYLOAD) != 0) {
|
||||
payload = Files.readAllBytes(regionDirectory.resolve("c." + chunkX + "." + chunkZ + ".mcc"));
|
||||
compression &= ~EXTERNAL_PAYLOAD;
|
||||
}
|
||||
return parse(payload, compression, chunkX, chunkZ);
|
||||
}
|
||||
|
||||
private CompoundTag parse(byte[] payload, int compression, int chunkX, int chunkZ) throws IOException {
|
||||
CompressionType type = CompressionType.getFromID((byte) compression);
|
||||
if (type == null) {
|
||||
throw new IOException("Chunk " + chunkX + "," + chunkZ + " in " + regionDirectory
|
||||
+ " uses unsupported compression type " + compression);
|
||||
}
|
||||
try (InputStream input = new BufferedInputStream(type.decompress(new ByteArrayInputStream(payload)))) {
|
||||
NamedTag tag = new NBTDeserializer(false).fromStream(input);
|
||||
if (tag == null || !(tag.getTag() instanceof CompoundTag root)) {
|
||||
throw new IOException("Chunk " + chunkX + "," + chunkZ + " in " + regionDirectory + " has no compound root tag");
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
private void decodeSection(CompoundTag section, String[] blocks, int chunkX, int chunkZ) {
|
||||
int baseY = section.getByte("Y") * CHUNK_WIDTH;
|
||||
CompoundTag states = section.getCompoundTag("block_states");
|
||||
if (baseY + CHUNK_WIDTH <= minY || baseY >= maxY || states == null) {
|
||||
return;
|
||||
}
|
||||
ListTag<?> paletteTag = states.getListTag("palette");
|
||||
if (paletteTag == null || paletteTag.size() == 0) {
|
||||
return;
|
||||
}
|
||||
String[] palette = new String[paletteTag.size()];
|
||||
int entry = 0;
|
||||
for (CompoundTag state : paletteTag.asCompoundTagList()) {
|
||||
palette[entry++] = state.getString("Name");
|
||||
}
|
||||
long[] data = states.getLongArray("data");
|
||||
int bits = Math.max(4, 32 - Integer.numberOfLeadingZeros(palette.length - 1));
|
||||
int perLong = 64 / bits;
|
||||
long mask = (1L << bits) - 1L;
|
||||
if (palette.length > 1 && data.length < (SECTION_VOLUME + perLong - 1) / perLong) {
|
||||
throw new IllegalStateException("Section " + section.getByte("Y") + " of chunk " + chunkX + "," + chunkZ
|
||||
+ " in " + regionDirectory + " has " + data.length + " block-state longs for a palette of " + palette.length);
|
||||
}
|
||||
for (int index = 0; index < SECTION_VOLUME; index++) {
|
||||
int y = baseY + (index >> 8);
|
||||
if (y < minY || y >= maxY) {
|
||||
continue;
|
||||
}
|
||||
int paletteIndex = palette.length == 1 ? 0 : (int) ((data[index / perLong] >>> ((index % perLong) * bits)) & mask);
|
||||
if (paletteIndex >= palette.length) {
|
||||
throw new IllegalStateException("Section " + section.getByte("Y") + " of chunk " + chunkX + "," + chunkZ
|
||||
+ " in " + regionDirectory + " references palette entry " + paletteIndex + " of " + palette.length);
|
||||
}
|
||||
blocks[offset(index & 15, y, (index >> 4) & 15)] = palette[paletteIndex];
|
||||
}
|
||||
}
|
||||
|
||||
private int offset(int localX, int y, int localZ) {
|
||||
if (y < minY || y >= maxY) {
|
||||
throw new IllegalArgumentException("Y " + y + " is outside [" + minY + ", " + maxY + ")");
|
||||
}
|
||||
return ((y - minY) * CHUNK_WIDTH + localZ) * CHUNK_WIDTH + localX;
|
||||
}
|
||||
|
||||
private final class RegionChunk implements FidelityChunk {
|
||||
private final String[] blocks;
|
||||
|
||||
private RegionChunk(String[] blocks) {
|
||||
this.blocks = blocks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int surfaceY(int localX, int localZ) {
|
||||
for (int y = maxY - 1; y >= minY; y--) {
|
||||
String id = block(localX, y, localZ);
|
||||
if (!FidelityBlocks.open(id) && !FidelityBlocks.fluid(id)) {
|
||||
return y;
|
||||
}
|
||||
}
|
||||
return minY - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String surfaceBiome(int localX, int localZ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean open(int localX, int y, int localZ) {
|
||||
return FidelityBlocks.open(block(localX, y, localZ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fluid(int localX, int y, int localZ) {
|
||||
return FidelityBlocks.fluid(block(localX, y, localZ));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String block(int localX, int y, int localZ) {
|
||||
return blocks[offset(localX, y, localZ)];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package art.arcane.iris.probe.fidelity;
|
||||
|
||||
public record SurveyWindow(int centerX, int centerZ, int size, int stride) {
|
||||
public SurveyWindow {
|
||||
if (stride < 1) {
|
||||
throw new IllegalArgumentException("Survey stride must be positive, got " + stride);
|
||||
}
|
||||
if (size < stride || size % stride != 0) {
|
||||
throw new IllegalArgumentException("Survey window " + size + " must be a positive multiple of the stride " + stride);
|
||||
}
|
||||
}
|
||||
|
||||
public int perAxis() {
|
||||
return size / stride;
|
||||
}
|
||||
|
||||
public long samples() {
|
||||
return (long) perAxis() * perAxis();
|
||||
}
|
||||
|
||||
public int coordinate(int center, int index) {
|
||||
return center - size / 2 + stride / 2 + index * stride;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user