From 635088a53c2bb03b295fdfd8e75eac5fb25fade9 Mon Sep 17 00:00:00 2001 From: Pixirora Date: Tue, 12 Sep 2023 23:35:23 -0400 Subject: [PATCH] Stop directly storing chunk length in Coordinates This will make them use up a bit less memory, though this change is done as the only place that chunk lengths are ever not 16 is during biome resolution, so it makes more sense to just enter the side length manually in those cases and not store that value elsewhere. --- .../janerator/graphing/Coordinate.java | 26 ++++++++++++------- .../worldgen/FullGeneratorLookup.java | 4 +-- .../janerator/worldgen/GeneratorLookup.java | 9 ++++--- .../worldgen/WrappedBiomeResolver.java | 2 +- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java b/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java index bf18c95..4f3e32f 100644 --- a/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java +++ b/src/main/java/net/pixaurora/janerator/graphing/Coordinate.java @@ -5,7 +5,7 @@ import org.joml.Vector2i; -public record Coordinate(int x, int z, int scale) { +public record Coordinate(int x, int z) { private static final Vector2i[] NEIGHBOR_OFFSETS = new Vector2i[]{ new Vector2i(1, 0), new Vector2i(1, 1), @@ -17,32 +17,40 @@ public record Coordinate(int x, int z, int scale) { new Vector2i(1, -1) }; - public Coordinate(int x, int z) { - this(x, z, 16); + public int toListIndex(int chunkLength) { + return chunkLength * GraphingUtils.mod(this.x, chunkLength) + GraphingUtils.mod(this.z, chunkLength); } public int toListIndex() { - return this.scale * GraphingUtils.mod(this.x, this.scale) + GraphingUtils.mod(this.z, this.scale); + return this.toListIndex(16); } - public static Coordinate fromListIndex(int index, int scale) { - return new Coordinate(Math.floorDiv(index, scale), GraphingUtils.mod(index, scale), scale); + public static Coordinate fromListIndex(int index, int chunkLength) { + return new Coordinate(Math.floorDiv(index, chunkLength), GraphingUtils.mod(index, chunkLength)); } public static Coordinate fromListIndex(int index) { return fromListIndex(index, 16); } + public boolean isLegal(int chunkLength) { + return 0 <= this.x && this.x < chunkLength && 0 <= this.z && this.z < chunkLength; + } + public boolean isLegal() { - return 0 <= this.x && this.x < this.scale && 0 <= this.z && this.z < this.scale; + return this.isLegal(16); + } + + public Coordinate makeLegal(int chunkLength) { + return new Coordinate(GraphingUtils.mod(this.x, chunkLength), GraphingUtils.mod(this.z, chunkLength)); } public Coordinate makeLegal() { - return new Coordinate(GraphingUtils.mod(this.x, this.scale), GraphingUtils.mod(this.z, this.scale), this.scale); + return this.makeLegal(16); } public Coordinate offsetBy(Vector2i delta) { - return new Coordinate(this.x + delta.x, this.z + delta.y, this.scale); + return new Coordinate(this.x + delta.x, this.z + delta.y); } public List getNeighbors() { diff --git a/src/main/java/net/pixaurora/janerator/worldgen/FullGeneratorLookup.java b/src/main/java/net/pixaurora/janerator/worldgen/FullGeneratorLookup.java index 74ee654..0871bba 100644 --- a/src/main/java/net/pixaurora/janerator/worldgen/FullGeneratorLookup.java +++ b/src/main/java/net/pixaurora/janerator/worldgen/FullGeneratorLookup.java @@ -8,8 +8,8 @@ public class FullGeneratorLookup extends GeneratorLookup { private GeneratorLookup biomeScale; public FullGeneratorLookup(List blockLevelMapping, List biomeLevel) { - super(blockLevelMapping); - this.biomeScale = new GeneratorLookup(biomeLevel); + super(blockLevelMapping, 16); + this.biomeScale = new GeneratorLookup(biomeLevel, 4); } public GeneratorLookup atBiomeScale() { diff --git a/src/main/java/net/pixaurora/janerator/worldgen/GeneratorLookup.java b/src/main/java/net/pixaurora/janerator/worldgen/GeneratorLookup.java index 11a497f..a8befc2 100644 --- a/src/main/java/net/pixaurora/janerator/worldgen/GeneratorLookup.java +++ b/src/main/java/net/pixaurora/janerator/worldgen/GeneratorLookup.java @@ -13,14 +13,17 @@ public class GeneratorLookup { private List generatorMap; + private int chunkLength; private Map selections; private ChunkGenerator fallbackGenerator; public GeneratorLookup( - List generatorMap + List generatorMap, + int chunkLength ) { this.generatorMap = generatorMap; + this.chunkLength = chunkLength; this.selections = this.generatorMap .stream() @@ -38,11 +41,11 @@ public int size() { } public ChunkGenerator getAt(Coordinate coordinate) { - return this.generatorMap.get(coordinate.toListIndex()); + return this.generatorMap.get(coordinate.makeLegal(this.chunkLength).toListIndex(this.chunkLength)); } public ChunkGenerator getAt(BlockPos pos) { - return this.getAt(new Coordinate(pos.getX(), pos.getZ()).makeLegal()); + return this.getAt(new Coordinate(pos.getX(), pos.getZ())); } public Collection getAllSelections() { diff --git a/src/main/java/net/pixaurora/janerator/worldgen/WrappedBiomeResolver.java b/src/main/java/net/pixaurora/janerator/worldgen/WrappedBiomeResolver.java index 2119bf5..051514b 100644 --- a/src/main/java/net/pixaurora/janerator/worldgen/WrappedBiomeResolver.java +++ b/src/main/java/net/pixaurora/janerator/worldgen/WrappedBiomeResolver.java @@ -87,7 +87,7 @@ public static BiomeResolver getBiomeResolverForGenerator(ChunkGenerator generato } public Holder getNoiseBiome(int x, int y, int z, Climate.Sampler sampler) { - ChunkGenerator generator = this.generators.getAt(new Coordinate(x, z, 4)); + ChunkGenerator generator = this.generators.getAt(new Coordinate(x, z)); return biomeResolvers.get( generator