use resolution in CachingBiomeProviders

This commit is contained in:
dfsek
2022-06-15 23:08:12 -07:00
parent cfeeb432ea
commit 9081f3a004
2 changed files with 36 additions and 11 deletions

View File

@@ -60,4 +60,9 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
public Iterable<Biome> getBiomes() {
return delegate.getBiomes();
}
@Override
public int resolution() {
return delegate.resolution();
}
}

View File

@@ -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<Biome>[] columnCache = new Column[256]; // x + z * 16
private final Column<Biome>[] 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<Biome> 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<Biome> 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];
}
}
}