mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-04 23:06:05 +00:00
move generator options to addon
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.dfsek.terra.addons.chunkgenerator.config;
|
||||
|
||||
import com.dfsek.tectonic.annotations.Default;
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
import com.dfsek.tectonic.loading.object.ObjectTemplate;
|
||||
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
|
||||
|
||||
public class BiomeNoiseConfigTemplate implements ObjectTemplate<BiomeNoiseProperties> {
|
||||
@Value("noise")
|
||||
private @Meta NoiseSampler baseSampler;
|
||||
|
||||
@Value("elevation.equation")
|
||||
@Default
|
||||
private @Meta NoiseSampler elevationSampler = NoiseSampler.zero();
|
||||
|
||||
@Value("carving.sampler")
|
||||
@Default
|
||||
private @Meta NoiseSampler carvingSampler = NoiseSampler.zero();
|
||||
|
||||
@Value("blend.distance")
|
||||
@Default
|
||||
private @Meta int blendDistance = 3;
|
||||
|
||||
@Value("blend.weight")
|
||||
@Default
|
||||
private @Meta double blendWeight = 1;
|
||||
|
||||
@Value("blend.step")
|
||||
@Default
|
||||
private @Meta int blendStep = 4;
|
||||
|
||||
@Value("elevation.weight")
|
||||
@Default
|
||||
private @Meta double elevationWeight = 1;
|
||||
|
||||
@Override
|
||||
public BiomeNoiseProperties get() {
|
||||
return new BiomeNoiseProperties(baseSampler, elevationSampler, carvingSampler, blendDistance, blendStep, blendWeight, elevationWeight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.dfsek.terra.addons.chunkgenerator.config;
|
||||
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.properties.Properties;
|
||||
|
||||
|
||||
public record BiomeNoiseProperties(NoiseSampler base,
|
||||
NoiseSampler elevation,
|
||||
NoiseSampler carving,
|
||||
int blendDistance,
|
||||
int blendStep,
|
||||
double blendWeight,
|
||||
double elevationWeight) implements Properties {
|
||||
|
||||
}
|
||||
@@ -8,15 +8,13 @@
|
||||
package com.dfsek.terra.addons.chunkgenerator.generation.generators;
|
||||
|
||||
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.dfsek.terra.addons.chunkgenerator.PaletteUtil;
|
||||
import com.dfsek.terra.addons.chunkgenerator.generation.math.PaletteUtil;
|
||||
import com.dfsek.terra.addons.chunkgenerator.generation.math.samplers.SamplerProviderImpl;
|
||||
import com.dfsek.terra.addons.chunkgenerator.palette.PaletteInfo;
|
||||
import com.dfsek.terra.api.Platform;
|
||||
@@ -26,7 +24,7 @@ import com.dfsek.terra.api.profiler.ProfileFrame;
|
||||
import com.dfsek.terra.api.util.math.Sampler;
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
import com.dfsek.terra.api.world.WritableWorld;
|
||||
import com.dfsek.terra.api.world.biome.GenerationSettings;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
|
||||
import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;
|
||||
@@ -75,16 +73,14 @@ public class NoiseChunkGenerator3D implements ChunkGenerator {
|
||||
|
||||
PaletteInfo paletteInfo = biome.getContext().get(PaletteInfo.class);
|
||||
|
||||
GenerationSettings generationSettings = biome.getGenerator();
|
||||
|
||||
int sea = paletteInfo.getSeaLevel();
|
||||
Palette seaPalette = paletteInfo.getOcean();
|
||||
|
||||
BlockState data;
|
||||
for(int y = world.getMaxHeight() - 1; y >= world.getMinHeight(); y--) {
|
||||
if(sampler.sample(x, y, z) > 0) {
|
||||
data = PaletteUtil.getPalette(x, y, z, generationSettings, sampler, paletteInfo).get(paletteLevel, cx, y, cz,
|
||||
seed);
|
||||
data = PaletteUtil.getPalette(x, y, z, sampler, paletteInfo).get(paletteLevel, cx, y, cz,
|
||||
seed);
|
||||
chunk.setBlock(x, y, z, data);
|
||||
|
||||
paletteLevel++;
|
||||
@@ -112,7 +108,7 @@ public class NoiseChunkGenerator3D implements ChunkGenerator {
|
||||
Sampler sampler = samplerCache.get(x, z, world);
|
||||
|
||||
PaletteInfo paletteInfo = biome.getContext().get(PaletteInfo.class);
|
||||
Palette palette = PaletteUtil.getPalette(x, y, z, biome.getGenerator(), sampler, paletteInfo);
|
||||
Palette palette = PaletteUtil.getPalette(x, y, z, sampler, paletteInfo);
|
||||
int fdX = FastMath.floorMod(x, 16);
|
||||
int fdZ = FastMath.floorMod(z, 16);
|
||||
double noise = sampler.sample(fdX, y, fdZ);
|
||||
|
||||
@@ -5,18 +5,17 @@
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.chunkgenerator;
|
||||
package com.dfsek.terra.addons.chunkgenerator.generation.math;
|
||||
|
||||
import com.dfsek.terra.addons.chunkgenerator.palette.PaletteInfo;
|
||||
import com.dfsek.terra.addons.chunkgenerator.palette.SlantHolder;
|
||||
import com.dfsek.terra.api.util.MathUtil;
|
||||
import com.dfsek.terra.api.util.math.Sampler;
|
||||
import com.dfsek.terra.api.world.biome.GenerationSettings;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.Palette;
|
||||
|
||||
|
||||
public final class PaletteUtil {
|
||||
public static Palette getPalette(int x, int y, int z, GenerationSettings c, Sampler sampler, PaletteInfo paletteInfo) {
|
||||
public static Palette getPalette(int x, int y, int z, Sampler sampler, PaletteInfo paletteInfo) {
|
||||
SlantHolder slant = paletteInfo.getSlantHolder();
|
||||
if(slant != null) {
|
||||
double slope = MathUtil.derivative(sampler, x, y, z);
|
||||
@@ -9,26 +9,26 @@ package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.dfsek.terra.addons.chunkgenerator.config.BiomeNoiseProperties;
|
||||
import com.dfsek.terra.api.util.mutable.MutableInteger;
|
||||
import com.dfsek.terra.api.world.biome.GenerationSettings;
|
||||
|
||||
|
||||
public interface ChunkInterpolator {
|
||||
default double computeNoise(Map<GenerationSettings, MutableInteger> gens, double x, double y, double z) {
|
||||
default double computeNoise(Map<BiomeNoiseProperties, MutableInteger> gens, double x, double y, double z) {
|
||||
double n = 0;
|
||||
double div = 0;
|
||||
for(Map.Entry<GenerationSettings, MutableInteger> entry : gens.entrySet()) {
|
||||
GenerationSettings gen = entry.getKey();
|
||||
for(Map.Entry<BiomeNoiseProperties, MutableInteger> entry : gens.entrySet()) {
|
||||
BiomeNoiseProperties gen = entry.getKey();
|
||||
int weight = entry.getValue().get();
|
||||
double noise = computeNoise(gen, x, y, z);
|
||||
|
||||
n += noise * weight;
|
||||
div += gen.getWeight() * weight;
|
||||
div += gen.blendWeight() * weight;
|
||||
}
|
||||
return n / div;
|
||||
}
|
||||
|
||||
double computeNoise(GenerationSettings generationSettings, double x, double y, double z);
|
||||
double computeNoise(BiomeNoiseProperties generationSettings, double x, double y, double z);
|
||||
|
||||
/**
|
||||
* Gets the noise at a pair of internal chunk coordinates.
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020-2021 Polyhedral Development
|
||||
*
|
||||
* The Terra Core Addons are licensed under the terms of the MIT License. For more details,
|
||||
* reference the LICENSE file in this module's root directory.
|
||||
*/
|
||||
|
||||
package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation;
|
||||
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
import net.jafama.FastMath;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import com.dfsek.terra.api.util.mutable.MutableInteger;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.biome.GenerationSettings;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
|
||||
/**
|
||||
* Class to abstract away the Interpolators needed to generate a chunk.<br>
|
||||
* Contains method to get interpolated noise at a coordinate within the chunk.
|
||||
*/
|
||||
public class ChunkInterpolator2D implements ChunkInterpolator {
|
||||
private final Interpolator[][] interpGrid = new Interpolator[4][4];
|
||||
private final BiFunction<GenerationSettings, Vector3, Double> noiseGetter;
|
||||
|
||||
/**
|
||||
* Instantiates a 3D ChunkInterpolator3D at a pair of chunk coordinates.
|
||||
*
|
||||
* @param chunkX X coordinate of the chunk.
|
||||
* @param chunkZ Z coordinate of the chunk.
|
||||
* @param provider Biome Provider to use for biome fetching.
|
||||
*/
|
||||
public ChunkInterpolator2D(ServerWorld w, int chunkX, int chunkZ, BiomeProvider provider,
|
||||
BiFunction<GenerationSettings, Vector3, Double> noiseGetter) {
|
||||
this.noiseGetter = noiseGetter;
|
||||
int xOrigin = chunkX << 4;
|
||||
int zOrigin = chunkZ << 4;
|
||||
|
||||
long seed = w.getSeed();
|
||||
|
||||
double[][] noiseStorage = new double[5][5];
|
||||
|
||||
for(int x = 0; x < 5; x++) {
|
||||
for(int z = 0; z < 5; z++) {
|
||||
GenerationSettings generationSettings = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2), seed).getGenerator();
|
||||
Map<GenerationSettings, MutableInteger> genMap = new HashMap<>();
|
||||
|
||||
int step = generationSettings.getBlendStep();
|
||||
int blend = generationSettings.getBlendDistance();
|
||||
|
||||
for(int xi = -blend; xi <= blend; xi++) {
|
||||
for(int zi = -blend; zi <= blend; zi++) {
|
||||
genMap.computeIfAbsent(
|
||||
provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step), seed).getGenerator(),
|
||||
g -> new MutableInteger(0)).increment(); // Increment by 1
|
||||
}
|
||||
}
|
||||
|
||||
noiseStorage[x][z] = computeNoise(genMap, (x << 2) + xOrigin, 0, (z << 2) + zOrigin);
|
||||
}
|
||||
}
|
||||
|
||||
for(int x = 0; x < 4; x++) {
|
||||
for(int z = 0; z < 4; z++) {
|
||||
interpGrid[x][z] = new Interpolator(
|
||||
noiseStorage[x][z],
|
||||
noiseStorage[x + 1][z],
|
||||
noiseStorage[x][z + 1],
|
||||
noiseStorage[x + 1][z + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int reRange(int value, int high) {
|
||||
return FastMath.max(FastMath.min(value, high), 0);
|
||||
}
|
||||
|
||||
public double computeNoise(GenerationSettings generationSettings, double x, double y, double z) {
|
||||
return noiseGetter.apply(generationSettings, new Vector3(x, y, z));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the noise at a pair of internal chunk coordinates.
|
||||
*
|
||||
* @param x The internal X coordinate (0-15).
|
||||
* @param z The internal Z coordinate (0-15).
|
||||
*
|
||||
* @return double - The interpolated noise at the coordinates.
|
||||
*/
|
||||
@Override
|
||||
public double getNoise(double x, double y, double z) {
|
||||
return interpGrid[reRange(((int) x) / 4, 3)][reRange(((int) z) / 4, 3)].bilerp((x % 4) / 4, (z % 4) / 4);
|
||||
}
|
||||
|
||||
public double getNoise(int x, int y, int z) {
|
||||
return interpGrid[x / 4][z / 4].bilerp((double) (x % 4) / 4, (double) (z % 4) / 4);
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,9 @@ import net.jafama.FastMath;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import com.dfsek.terra.addons.chunkgenerator.config.BiomeNoiseProperties;
|
||||
import com.dfsek.terra.api.util.mutable.MutableInteger;
|
||||
import com.dfsek.terra.api.util.vector.Vector3;
|
||||
import com.dfsek.terra.api.world.biome.GenerationSettings;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
|
||||
@@ -25,24 +23,24 @@ import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
*/
|
||||
public class ChunkInterpolator3D implements ChunkInterpolator {
|
||||
private final Interpolator3[][][] interpGrid;
|
||||
private final BiFunction<GenerationSettings, Vector3, Double> noiseGetter;
|
||||
private final long seed;
|
||||
|
||||
private final int min;
|
||||
private final int max;
|
||||
|
||||
/**
|
||||
* Instantiates a 3D ChunkInterpolator3D at a pair of chunk coordinates.
|
||||
* @param chunkX X coordinate of the chunk.
|
||||
*
|
||||
* @param chunkX X coordinate of the chunk.
|
||||
* @param chunkZ Z coordinate of the chunk.
|
||||
* @param provider Biome Provider to use for biome fetching.
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
public ChunkInterpolator3D(long seed, int chunkX, int chunkZ, BiomeProvider provider,
|
||||
BiFunction<GenerationSettings, Vector3, Double> noiseGetter, int min, int max) {
|
||||
this.noiseGetter = noiseGetter;
|
||||
public ChunkInterpolator3D(long seed, int chunkX, int chunkZ, BiomeProvider provider, int min, int max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.seed = seed;
|
||||
|
||||
int xOrigin = chunkX << 4;
|
||||
int zOrigin = chunkZ << 4;
|
||||
@@ -58,16 +56,19 @@ public class ChunkInterpolator3D implements ChunkInterpolator {
|
||||
|
||||
for(int x = 0; x < 5; x++) {
|
||||
for(int z = 0; z < 5; z++) {
|
||||
GenerationSettings generationSettings = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2), seed).getGenerator();
|
||||
Map<GenerationSettings, MutableInteger> genMap = new HashMap<>();
|
||||
BiomeNoiseProperties generationSettings = provider.getBiome(xOrigin + (x << 2), zOrigin + (z << 2), seed).getContext().get(
|
||||
BiomeNoiseProperties.class);
|
||||
Map<BiomeNoiseProperties, MutableInteger> genMap = new HashMap<>();
|
||||
|
||||
int step = generationSettings.getBlendStep();
|
||||
int blend = generationSettings.getBlendDistance();
|
||||
int step = generationSettings.blendStep();
|
||||
int blend = generationSettings.blendDistance();
|
||||
|
||||
for(int xi = -blend; xi <= blend; xi++) {
|
||||
for(int zi = -blend; zi <= blend; zi++) {
|
||||
genMap.computeIfAbsent(
|
||||
provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step), seed).getGenerator(),
|
||||
provider.getBiome(xOrigin + (x << 2) + (xi * step), zOrigin + (z << 2) + (zi * step), seed)
|
||||
.getContext()
|
||||
.get(BiomeNoiseProperties.class),
|
||||
g -> new MutableInteger(0)).increment(); // Increment by 1
|
||||
}
|
||||
}
|
||||
@@ -99,8 +100,8 @@ public class ChunkInterpolator3D implements ChunkInterpolator {
|
||||
return FastMath.max(FastMath.min(value, high), 0);
|
||||
}
|
||||
|
||||
public double computeNoise(GenerationSettings generationSettings, double x, double y, double z) {
|
||||
return noiseGetter.apply(generationSettings, new Vector3(x, y, z));
|
||||
public double computeNoise(BiomeNoiseProperties generationSettings, double x, double y, double z) {
|
||||
return generationSettings.base().noise(seed, x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation;
|
||||
|
||||
import com.dfsek.terra.api.world.ServerWorld;
|
||||
import com.dfsek.terra.api.world.World;
|
||||
import com.dfsek.terra.api.world.biome.GenerationSettings;
|
||||
import com.dfsek.terra.addons.chunkgenerator.config.BiomeNoiseProperties;
|
||||
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
|
||||
|
||||
|
||||
@@ -20,12 +18,12 @@ public class ElevationInterpolator {
|
||||
int xOrigin = chunkX << 4;
|
||||
int zOrigin = chunkZ << 4;
|
||||
|
||||
GenerationSettings[][] gens = new GenerationSettings[18 + 2 * smooth][18 + 2 * smooth];
|
||||
BiomeNoiseProperties[][] gens = new BiomeNoiseProperties[18 + 2 * smooth][18 + 2 * smooth];
|
||||
|
||||
// Precompute generators.
|
||||
for(int x = -1 - smooth; x <= 16 + smooth; x++) {
|
||||
for(int z = -1 - smooth; z <= 16 + smooth; z++) {
|
||||
gens[x + 1 + smooth][z + 1 + smooth] = provider.getBiome(xOrigin + x, zOrigin + z, seed).getGenerator();
|
||||
gens[x + 1 + smooth][z + 1 + smooth] = provider.getBiome(xOrigin + x, zOrigin + z, seed).getContext().get(BiomeNoiseProperties.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,9 +33,9 @@ public class ElevationInterpolator {
|
||||
double div = 0;
|
||||
for(int xi = -smooth; xi <= smooth; xi++) {
|
||||
for(int zi = -smooth; zi <= smooth; zi++) {
|
||||
GenerationSettings gen = gens[x + 1 + smooth + xi][z + 1 + smooth + zi];
|
||||
noise += gen.getElevationSampler().noise(seed, xOrigin + x, zOrigin + z) * gen.getElevationWeight();
|
||||
div += gen.getElevationWeight();
|
||||
BiomeNoiseProperties gen = gens[x + 1 + smooth + xi][z + 1 + smooth + zi];
|
||||
noise += gen.elevation().noise(seed, xOrigin + x, zOrigin + z) * gen.elevationWeight();
|
||||
div += gen.elevationWeight();
|
||||
}
|
||||
}
|
||||
values[x + 1][z + 1] = noise / div;
|
||||
|
||||
@@ -20,9 +20,7 @@ public class Sampler3D implements Sampler {
|
||||
private final ElevationInterpolator elevationInterpolator;
|
||||
|
||||
public Sampler3D(int x, int z, long seed, int minHeight, int maxHeight, BiomeProvider provider, int elevationSmooth) {
|
||||
this.interpolator = new ChunkInterpolator3D(seed, x, z, provider, (generator, coord) -> generator.getBaseSampler()
|
||||
.noise(coord,
|
||||
seed),
|
||||
this.interpolator = new ChunkInterpolator3D(seed, x, z, provider,
|
||||
minHeight, maxHeight);
|
||||
this.elevationInterpolator = new ElevationInterpolator(seed, x, z, provider, elevationSmooth);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user