implement biome pipeline config loader

This commit is contained in:
dfsek
2021-01-13 10:48:45 -07:00
parent fb32531584
commit 54f732176d
6 changed files with 121 additions and 86 deletions
@@ -1,5 +1,6 @@
package com.dfsek.terra.biome;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.api.math.vector.Vector2;
import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.biome.pipeline.BiomeHolder;
@@ -11,11 +12,10 @@ import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
public class StandardBiomeProvider implements BiomeProvider {
private final BiomePipeline pipeline;
private final LoadingCache<Vector2, BiomeHolder> cache = CacheBuilder.newBuilder()
private final LoadingCache<Vector2, BiomeHolder> holderCache = CacheBuilder.newBuilder()
.maximumSize(1024)
.build(
new CacheLoader<Vector2, BiomeHolder>() {
@@ -25,6 +25,22 @@ public class StandardBiomeProvider implements BiomeProvider {
}
}
);
private final LoadingCache<Vector2, TerraBiome> biomeCache = CacheBuilder.newBuilder()
.maximumSize(1024)
.build(
new CacheLoader<Vector2, TerraBiome>() {
@Override
public TerraBiome load(@NotNull Vector2 key) throws ExecutionException {
int x = FastMath.floorToInt(key.getX());
int z = FastMath.floorToInt(key.getZ());
x /= resolution;
z /= resolution;
int fdX = FastMath.floorDiv(x, pipeline.getSize());
int fdZ = FastMath.floorDiv(z, pipeline.getSize());
return holderCache.get(new Vector2(fdX, fdZ)).getBiome(x - fdX * pipeline.getSize(), z - fdZ * pipeline.getSize());
}
}
);
private int resolution = 4;
protected StandardBiomeProvider(BiomePipeline pipeline) {
@@ -33,10 +49,8 @@ public class StandardBiomeProvider implements BiomeProvider {
@Override
public TerraBiome getBiome(int x, int z) {
x /= resolution;
z /= resolution;
try {
return cache.get(new Vector2(FastMath.floorDiv(x, pipeline.getSize()), FastMath.floorDiv(z, pipeline.getSize()))).getBiome(FastMath.floorMod(x, pipeline.getSize()), FastMath.floorMod(z, pipeline.getSize()));
return biomeCache.get(new Vector2(x, z));
} catch(ExecutionException e) {
throw new RuntimeException(e);
}
@@ -50,16 +64,24 @@ public class StandardBiomeProvider implements BiomeProvider {
this.resolution = resolution;
}
public static final class StandardBiomeProviderBuilder implements BiomeProviderBuilder {
private final Function<Long, BiomePipeline> pipelineBuilder;
public interface ExceptionalFunction<I, O> {
O apply(I in) throws ConfigException;
}
public StandardBiomeProviderBuilder(Function<Long, BiomePipeline> pipelineBuilder) {
public static final class StandardBiomeProviderBuilder implements BiomeProviderBuilder {
private final ExceptionalFunction<Long, BiomePipeline> pipelineBuilder;
public StandardBiomeProviderBuilder(ExceptionalFunction<Long, BiomePipeline> pipelineBuilder) {
this.pipelineBuilder = pipelineBuilder;
}
@Override
public StandardBiomeProvider build(long seed) {
return new StandardBiomeProvider(pipelineBuilder.apply(seed));
try {
return new StandardBiomeProvider(pipelineBuilder.apply(seed));
} catch(ConfigException e) {
throw new RuntimeException(e);
}
}
}
}