From 9081f3a0046afa059e7c7db72e5217fdee7f187c Mon Sep 17 00:00:00 2001 From: dfsek Date: Wed, 15 Jun 2022 23:08:12 -0700 Subject: [PATCH] use resolution in CachingBiomeProviders --- .../generation/CachingBiomeProvider.java | 5 +++ .../ChunkLocalCachingBiomeProvider.java | 42 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/CachingBiomeProvider.java b/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/CachingBiomeProvider.java index ea40118a3..cb2781509 100644 --- a/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/CachingBiomeProvider.java +++ b/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/CachingBiomeProvider.java @@ -60,4 +60,9 @@ public class CachingBiomeProvider implements BiomeProvider, Handle { public Iterable getBiomes() { return delegate.getBiomes(); } + + @Override + public int resolution() { + return delegate.resolution(); + } } diff --git a/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/ChunkLocalCachingBiomeProvider.java b/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/ChunkLocalCachingBiomeProvider.java index dead2dfaa..4e365b453 100644 --- a/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/ChunkLocalCachingBiomeProvider.java +++ b/common/api/src/main/java/com/dfsek/terra/api/world/biome/generation/ChunkLocalCachingBiomeProvider.java @@ -1,14 +1,13 @@ package com.dfsek.terra.api.world.biome.generation; -import com.dfsek.terra.api.util.Column; -import com.dfsek.terra.api.util.generic.Construct; -import com.dfsek.terra.api.world.biome.Biome; -import com.dfsek.terra.api.world.info.WorldProperties; - import net.jafama.FastMath; import java.util.Optional; +import com.dfsek.terra.api.util.Column; +import com.dfsek.terra.api.world.biome.Biome; +import com.dfsek.terra.api.world.info.WorldProperties; + /** * A biome provider implementation that lazily evaluates biomes, and caches them. @@ -20,17 +19,25 @@ import java.util.Optional; */ public class ChunkLocalCachingBiomeProvider extends CachingBiomeProvider { private final BiomeChunk[] chunks = new BiomeChunk[9]; - private final Column[] columnCache = new Column[256]; // x + z * 16 + private final Column[] columnCache; // x + z * 16 private final int chunkX; private final int chunkZ; private final int height; + private final int zMul; + private final int yMul; + private final int resolution; + protected ChunkLocalCachingBiomeProvider(BiomeProvider delegate, WorldProperties worldProperties, int chunkX, int chunkZ) { super(delegate, worldProperties.getMinHeight(), worldProperties.getMaxHeight()); this.height = maxY - minY; this.chunkX = chunkX; this.chunkZ = chunkZ; + this.resolution = delegate.resolution(); + this.zMul = 16 / resolution; + this.yMul = zMul * zMul; + this.columnCache = new Column[yMul]; } @Override @@ -43,16 +50,21 @@ public class ChunkLocalCachingBiomeProvider extends CachingBiomeProvider { int localChunkX = FastMath.floorDiv(x, 16) - this.chunkX + 1; int localChunkZ = FastMath.floorDiv(z, 16) - this.chunkZ + 1; + if(localChunkX >= 0 && localChunkZ >= 0 && localChunkX <= 2 && localChunkZ <= 2) { int chunkIndex = localChunkX + localChunkZ * 3; BiomeChunk chunk = chunks[chunkIndex]; if(chunk == null) { - chunk = new BiomeChunk(height); + chunk = new BiomeChunk(height / resolution, yMul); chunks[chunkIndex] = chunk; } + + int scaledX = FastMath.floorDiv(x & 15, resolution); + int scaledY = FastMath.floorDiv(y - minY, resolution); + int scaledZ = FastMath.floorDiv(z & 15, resolution); - int biomeIndex = (x & 15) + 16 * (z & 15) + 256 * (y - minY); + int biomeIndex = scaledX + zMul * scaledZ + yMul * scaledY; Biome biome = chunk.cache[biomeIndex]; if(biome == null) { biome = delegate.getBiome(x, y, z, seed); @@ -71,8 +83,11 @@ public class ChunkLocalCachingBiomeProvider extends CachingBiomeProvider { @Override public Column getColumn(int x, int z, long seed, int min, int max) { + int scaledX = (x & 15) / resolution; + int scaledZ = (z & 15) / resolution; + if(FastMath.floorDiv(x, 16) == chunkX && FastMath.floorDiv(z, 16) == chunkZ) { - int index = (x & 15) + (16 * (z & 15)); + int index = scaledX + (zMul * scaledZ); Column column = columnCache[index]; if(column == null) { column = new BiomeColumn(this, min, max, x, z, seed); @@ -88,11 +103,16 @@ public class ChunkLocalCachingBiomeProvider extends CachingBiomeProvider { return delegate.getBiomes(); } + @Override + public int resolution() { + return resolution; + } + private static class BiomeChunk { final Biome[] cache; // x + z * 16 + y * 256 - private BiomeChunk(int height) { - this.cache = new Biome[16 * 16 * height]; + private BiomeChunk(int height, int widthSq) { + this.cache = new Biome[widthSq * height]; } } }