From a785d7b8921fa62df6e152ce97c9ac49811646a6 Mon Sep 17 00:00:00 2001 From: dfsek Date: Sat, 2 Jan 2021 20:19:08 -0700 Subject: [PATCH] add 2d biomes support --- .../terra/api/world/biome/Generator.java | 3 ++ .../config/builder/GeneratorBuilder.java | 22 +++++++++++++- .../terra/config/factories/BiomeFactory.java | 2 ++ .../terra/config/templates/BiomeTemplate.java | 18 +++++++++++ .../generation/config/WorldGenerator.java | 30 ++++++++++++++----- .../dfsek/terra/generation/math/Sampler.java | 6 ++-- ...erpolator3.java => ChunkInterpolator.java} | 20 ++++++++++--- 7 files changed, 86 insertions(+), 15 deletions(-) rename common/src/main/java/com/dfsek/terra/generation/math/interpolation/{ChunkInterpolator3.java => ChunkInterpolator.java} (81%) diff --git a/common/src/main/java/com/dfsek/terra/api/world/biome/Generator.java b/common/src/main/java/com/dfsek/terra/api/world/biome/Generator.java index 4478b9667..794838860 100644 --- a/common/src/main/java/com/dfsek/terra/api/world/biome/Generator.java +++ b/common/src/main/java/com/dfsek/terra/api/world/biome/Generator.java @@ -21,4 +21,7 @@ public interface Generator { */ Palette getPalette(int y); + boolean is2d(); + + double get2dBase(); } diff --git a/common/src/main/java/com/dfsek/terra/config/builder/GeneratorBuilder.java b/common/src/main/java/com/dfsek/terra/config/builder/GeneratorBuilder.java index 42cf84ac6..1767ce2f6 100644 --- a/common/src/main/java/com/dfsek/terra/config/builder/GeneratorBuilder.java +++ b/common/src/main/java/com/dfsek/terra/config/builder/GeneratorBuilder.java @@ -27,9 +27,29 @@ public class GeneratorBuilder { private boolean interpolateElevation; + private boolean noise2d; + + private double base; + public WorldGenerator build(long seed) { - return gens.computeIfAbsent(seed, k -> new WorldGenerator(seed, noiseEquation, elevationEquation, varScope, noiseBuilderMap, palettes, slantPalettes, interpolateElevation)); + return gens.computeIfAbsent(seed, k -> new WorldGenerator(seed, noiseEquation, elevationEquation, varScope, noiseBuilderMap, palettes, slantPalettes, interpolateElevation, noise2d, base)); + } + + public boolean isNoise2d() { + return noise2d; + } + + public void setNoise2d(boolean noise2d) { + this.noise2d = noise2d; + } + + public double getBase() { + return base; + } + + public void setBase(double base) { + this.base = base; } public String getNoiseEquation() { diff --git a/common/src/main/java/com/dfsek/terra/config/factories/BiomeFactory.java b/common/src/main/java/com/dfsek/terra/config/factories/BiomeFactory.java index b0c41c1f4..b4d158c9b 100644 --- a/common/src/main/java/com/dfsek/terra/config/factories/BiomeFactory.java +++ b/common/src/main/java/com/dfsek/terra/config/factories/BiomeFactory.java @@ -23,6 +23,8 @@ public class BiomeFactory implements TerraFactory noiseBuilders, PaletteHolder palettes, PaletteHolder slantPalettes, boolean elevationInterpolation, boolean noise2d, double base) { + this.palettes = palettes; + this.slantPalettes = slantPalettes; + + this.elevationInterpolation = elevationInterpolation; + this.noise2d = noise2d; + this.base = base; - public WorldGenerator(long seed, String equation, String elevateEquation, Scope vScope, Map noiseBuilders, PaletteHolder palettes, PaletteHolder slantPalettes, boolean elevationInterpolation) { Parser p = new Parser(); p.registerFunction("rand", new RandomFunction()); Parser ep = new Parser(); @@ -38,14 +47,11 @@ public class WorldGenerator implements Generator { Scope s = new Scope().withParent(vScope); xVar = s.create("x"); - yVar = s.create("y"); + if(!noise2d) yVar = s.create("y"); + else yVar = null; zVar = s.create("z"); s.create("seed").setValue(seed); - this.palettes = palettes; - this.slantPalettes = slantPalettes; - - this.elevationInterpolation = elevationInterpolation; for(Map.Entry e : noiseBuilders.entrySet()) { switch(e.getValue().getDimensions()) { @@ -86,7 +92,7 @@ public class WorldGenerator implements Generator { @Override public synchronized double getNoise(int x, int y, int z) { xVar.setValue(x); - yVar.setValue(y); + if(!noise2d) yVar.setValue(y); zVar.setValue(z); return noiseExp.evaluate(); } @@ -101,6 +107,16 @@ public class WorldGenerator implements Generator { return palettes.getPalette(y); } + @Override + public boolean is2d() { + return noise2d; + } + + @Override + public double get2dBase() { + return base; + } + public Palette getSlantPalette(int y) { return slantPalettes.getPalette(y); } diff --git a/common/src/main/java/com/dfsek/terra/generation/math/Sampler.java b/common/src/main/java/com/dfsek/terra/generation/math/Sampler.java index 63f517bd2..51a3aeec0 100644 --- a/common/src/main/java/com/dfsek/terra/generation/math/Sampler.java +++ b/common/src/main/java/com/dfsek/terra/generation/math/Sampler.java @@ -2,15 +2,15 @@ package com.dfsek.terra.generation.math; import com.dfsek.terra.api.platform.world.World; import com.dfsek.terra.biome.grid.master.TerraBiomeGrid; -import com.dfsek.terra.generation.math.interpolation.ChunkInterpolator3; +import com.dfsek.terra.generation.math.interpolation.ChunkInterpolator; import net.jafama.FastMath; public class Sampler { - private final ChunkInterpolator3 interpolator; + private final ChunkInterpolator interpolator; private final ElevationInterpolator elevationInterpolator; public Sampler(int x, int z, TerraBiomeGrid grid, World world, int elevationSmooth, int generationSmooth) { - this.interpolator = new ChunkInterpolator3(world, x, z, grid, generationSmooth); + this.interpolator = new ChunkInterpolator(world, x, z, grid, generationSmooth); this.elevationInterpolator = new ElevationInterpolator(world, x, z, grid, elevationSmooth); } diff --git a/common/src/main/java/com/dfsek/terra/generation/math/interpolation/ChunkInterpolator3.java b/common/src/main/java/com/dfsek/terra/generation/math/interpolation/ChunkInterpolator.java similarity index 81% rename from common/src/main/java/com/dfsek/terra/generation/math/interpolation/ChunkInterpolator3.java rename to common/src/main/java/com/dfsek/terra/generation/math/interpolation/ChunkInterpolator.java index 5e8a64273..e97e9d151 100644 --- a/common/src/main/java/com/dfsek/terra/generation/math/interpolation/ChunkInterpolator3.java +++ b/common/src/main/java/com/dfsek/terra/generation/math/interpolation/ChunkInterpolator.java @@ -10,7 +10,7 @@ import net.jafama.FastMath; * Class to abstract away the 16 Interpolators needed to generate a chunk.
* Contains method to get interpolated noise at a coordinate within the chunk. */ -public class ChunkInterpolator3 { +public class ChunkInterpolator { private final Interpolator3[][][] interpGrid = new Interpolator3[4][64][4]; private final Generator[][] gens = new Generator[7][7]; private final boolean[][] needsBiomeInterp = new boolean[5][5]; @@ -24,7 +24,7 @@ public class ChunkInterpolator3 { * @param chunkZ Z coordinate of the chunk. * @param grid BiomeGrid to use for noise fetching. */ - public ChunkInterpolator3(World w, int chunkX, int chunkZ, TerraBiomeGrid grid, int smooth) { + public ChunkInterpolator(World w, int chunkX, int chunkZ, TerraBiomeGrid grid, int smooth) { int xOrigin = chunkX << 4; int zOrigin = chunkZ << 4; this.smooth = smooth; @@ -43,8 +43,16 @@ public class ChunkInterpolator3 { for(byte x = -1; x < 6; x++) { for(byte z = -1; z < 6; z++) { - for(int y = 0; y < 65; y++) { - noiseStorage[x + 1][z + 1][y] = gens[x + 1][z + 1].getNoise((x * smooth) + xOrigin, y << 2, (z * smooth) + zOrigin); + Generator generator = gens[x + 1][z + 1]; + if(generator.is2d()) { + double n = generator.getNoise((x * smooth) + xOrigin, 0, (z * smooth) + zOrigin); + for(int y = 0; y < 65; y++) { + noiseStorage[x + 1][z + 1][y] = n + noise2dExtrude(y << 2, generator.get2dBase()); + } + } else { + for(int y = 0; y < 65; y++) { + noiseStorage[x + 1][z + 1][y] = generator.getNoise((x * smooth) + xOrigin, y << 2, (z * smooth) + zOrigin); + } } } } @@ -113,4 +121,8 @@ public class ChunkInterpolator3 { public double getNoise(double x, double y, double z) { return interpGrid[reRange(((int) x) / smooth, 3)][reRange(((int) y) / 4, 63)][reRange(((int) z) / smooth, 3)].trilerp((x % smooth) / smooth, (y % 4) / 4, (z % smooth) / smooth); } + + private static double noise2dExtrude(double y, double base) { + return ((-FastMath.pow2((y / base))) + 1); + } }