cleanup generation code

This commit is contained in:
dfsek
2020-12-30 02:41:57 -07:00
parent ce45bacc6f
commit d16c28aebd
19 changed files with 67 additions and 80 deletions

View File

@@ -19,7 +19,7 @@ public class AirCheck extends SpawnCheck {
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeTemplate c = b.getConfig();
if(y <= c.getSeaLevel()) return false;
double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z);
return b.getGenerator().getNoise(world, x, y, z) + elevation <= 0;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation <= 0;
}
}

View File

@@ -16,7 +16,7 @@ public class LandCheck extends SpawnCheck {
public boolean check(int x, int y, int z) {
TerraWorld tw = main.getWorld(world);
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z);
return b.getGenerator().getNoise(world, x, y, z) + elevation > 0;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation > 0;
}
}

View File

@@ -19,7 +19,7 @@ public class OceanCheck extends SpawnCheck {
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeTemplate c = b.getConfig();
if(y > c.getSeaLevel()) return false;
double elevation = ((WorldGenerator) b.getGenerator()).getElevation(x, z);
return b.getGenerator().getNoise(world, x, y, z) + elevation <= 0;
double elevation = ((WorldGenerator) b.getGenerator(world)).getElevation(x, z);
return b.getGenerator(world).getNoise(x, y, z) + elevation <= 0;
}
}

View File

@@ -21,14 +21,5 @@ public interface Biome {
*
* @return BiomeTerrain - The terrain generation instance.
*/
Generator getGenerator();
/**
* Gets the BiomeTerrain instance used to generate the biome in this world.
*
* @return BiomeTerrain - The terrain generation instance.
*/
default Generator getGenerator(World w) {
return getGenerator();
}
Generator getGenerator(World w);
}

View File

@@ -1,7 +1,6 @@
package com.dfsek.terra.api.world.biome;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.palette.Palette;
public interface Generator {
@@ -13,7 +12,7 @@ public interface Generator {
* @param z - The z coordinate.
* @return double - Noise value at the specified coordinates.
*/
double getNoise(World w, int x, int y, int z);
double getNoise(int x, int y, int z);
/**
* Gets the BlocPalette to generate the biome with.

View File

@@ -37,16 +37,6 @@ public class UserDefinedBiome implements Biome {
return vanilla;
}
/**
* Gets the Generator instance used to generate the biome.
*
* @return Generator - The terrain generation instance.
*/
@Override
public Generator getGenerator() {
return gen.build(0);
}
public String getID() {
return id;

View File

@@ -27,6 +27,8 @@ public abstract class TerraBiomeGrid extends BiomeGrid {
RADIAL, STANDARD
}
public abstract boolean isEroded(int x, int z);
public static final class TerraBiomeGridBuilder {
private final long seed;
private final ConfigPack config;

View File

@@ -15,7 +15,6 @@ import com.dfsek.terra.config.base.ConfigPackTemplate;
import net.jafama.FastMath;
public class TerraRadialBiomeGrid extends TerraBiomeGrid {
private static final int failNum = 0;
private final double radiusSq;
private final BiomeGrid internal;
private CoordinatePerturb perturb;
@@ -39,6 +38,11 @@ public class TerraRadialBiomeGrid extends TerraBiomeGrid {
return (UserDefinedGrid) zone.getGrid(x, z);
}
@Override
public boolean isEroded(int x, int z) {
return erode != null && erode.isEroded(x, z);
}
@Override
public Biome getBiome(int x, int z, GenerationPhase phase) {
int xp = x, zp = z;
@@ -54,7 +58,7 @@ public class TerraRadialBiomeGrid extends TerraBiomeGrid {
} else {
b = (UserDefinedBiome) internal.getBiome(xp, zp, phase);
}
if(erode != null && erode.isEroded(xp, zp)) return b.getErode();
if(isEroded(xp, zp)) return b.getErode();
return b;
}

View File

@@ -13,7 +13,6 @@ import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.base.ConfigPackTemplate;
public class TerraStandardBiomeGrid extends TerraBiomeGrid {
private static final int failNum = 0;
private CoordinatePerturb perturb;
private ErosionNoise erode;
@@ -33,6 +32,11 @@ public class TerraStandardBiomeGrid extends TerraBiomeGrid {
return (UserDefinedGrid) zone.getGrid(x, z);
}
@Override
public boolean isEroded(int x, int z) {
return erode != null && erode.isEroded(x, z);
}
@Override
public Biome getBiome(int x, int z, GenerationPhase phase) {
int xp = x, zp = z;
@@ -43,10 +47,11 @@ public class TerraStandardBiomeGrid extends TerraBiomeGrid {
}
UserDefinedBiome b = (UserDefinedBiome) zone.getGrid(xp, zp).getBiome(xp, zp, phase);
if(erode != null && erode.isEroded(xp, zp)) return b.getErode();
if(isEroded(xp, zp)) return b.getErode();
return b;
}
@Override
public Biome getBiome(Location l, GenerationPhase phase) {
return getBiome(l.getBlockX(), l.getBlockZ(), phase);

View File

@@ -1,7 +1,6 @@
package com.dfsek.terra.generation;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.api.math.interpolation.ChunkInterpolator3;
import com.dfsek.terra.api.math.vector.Vector3;
import com.dfsek.terra.api.platform.TerraPlugin;
import com.dfsek.terra.api.platform.block.BlockData;
@@ -23,6 +22,7 @@ import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.palette.SinglePalette;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.generation.math.Sampler;
import com.dfsek.terra.util.PaletteUtil;
import org.jetbrains.annotations.NotNull;
@@ -86,20 +86,12 @@ public class MasterChunkGenerator implements TerraChunkGenerator {
TerraWorld tw = main.getWorld(world);
com.dfsek.terra.api.world.biome.BiomeGrid grid = tw.getGrid();
try(ProfileFuture ignore = tw.getProfiler().measure("TotalChunkGenTime")) {
ChunkInterpolator3 interp;
try(ProfileFuture ignored = tw.getProfiler().measure("ChunkBaseGenTime")) {
interp = new ChunkInterpolator3(world, chunkX, chunkZ, tw.getGrid(), 16);
if(!tw.isSafe()) return chunk;
int xOrig = (chunkX << 4);
int zOrig = (chunkZ << 4);
ElevationInterpolator elevationInterpolator;
try(ProfileFuture ignored1 = tw.getProfiler().measure("ElevationTime")) {
elevationInterpolator = new ElevationInterpolator(chunkX, chunkZ, tw.getGrid(), 4);
}
Sampler sampler = new Sampler(interp, elevationInterpolator);
Sampler sampler = new Sampler(chunkX, chunkZ, tw.getGrid(), world, 4, 8);
for(byte x = 0; x < 16; x++) {
for(byte z = 0; z < 16; z++) {

View File

@@ -1,18 +0,0 @@
package com.dfsek.terra.generation;
import com.dfsek.terra.api.math.interpolation.ChunkInterpolator3;
import net.jafama.FastMath;
public class Sampler {
private final ChunkInterpolator3 interpolator;
private final ElevationInterpolator elevationInterpolator;
public Sampler(ChunkInterpolator3 interpolator, ElevationInterpolator elevationInterpolator) {
this.interpolator = interpolator;
this.elevationInterpolator = elevationInterpolator;
}
public double sample(double x, double y, double z) {
return interpolator.getNoise(x, y, z) + elevationInterpolator.getElevation(FastMath.roundToInt(x), FastMath.roundToInt(z));
}
}

View File

@@ -1,7 +1,6 @@
package com.dfsek.terra.generation.config;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.api.world.palette.Palette;
import com.dfsek.terra.biome.palette.PaletteHolder;
@@ -85,7 +84,7 @@ public class WorldGenerator implements Generator {
}
@Override
public synchronized double getNoise(World world, int x, int y, int z) {
public synchronized double getNoise(int x, int y, int z) {
xVar.setValue(x);
yVar.setValue(y);
zVar.setValue(z);

View File

@@ -1,9 +1,10 @@
package com.dfsek.terra.generation;
package com.dfsek.terra.generation.math;
import com.dfsek.terra.api.math.interpolation.Interpolator;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.biome.BiomeGrid;
import com.dfsek.terra.api.world.generation.GenerationPhase;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.generation.config.WorldGenerator;
import com.dfsek.terra.generation.math.interpolation.Interpolator;
import net.jafama.FastMath;
public class ElevationInterpolator {
@@ -11,22 +12,24 @@ public class ElevationInterpolator {
private final double[][] values = new double[18][18];
private final int xOrigin;
private final int zOrigin;
private final TerraBiomeGrid grid;
private final BiomeGrid grid;
private final int smooth;
private final int pow;
private final World world;
public ElevationInterpolator(int chunkX, int chunkZ, TerraBiomeGrid grid, int smooth) {
public ElevationInterpolator(World world, int chunkX, int chunkZ, BiomeGrid grid, int smooth) {
this.xOrigin = chunkX << 4;
this.zOrigin = chunkZ << 4;
this.grid = grid;
this.smooth = smooth;
this.pow = FastMath.log2(smooth);
this.gens = new WorldGenerator[6 + 2 * pow][6 + 2 * pow];
this.world = world;
for(int x = -pow; x < 6 + pow; x++) {
for(int z = -pow; z < 6 + pow; z++) {
gens[x + pow][z + pow] = (WorldGenerator) grid.getBiome(xOrigin + (x * smooth), zOrigin + (z * smooth), GenerationPhase.BASE).getGenerator();
gens[x + pow][z + pow] = (WorldGenerator) grid.getBiome(xOrigin + (x * smooth), zOrigin + (z * smooth), GenerationPhase.BASE).getGenerator(world);
}
}
@@ -45,7 +48,7 @@ public class ElevationInterpolator {
}
private WorldGenerator getGenerator(int x, int z) {
return (WorldGenerator) grid.getBiome(xOrigin + x, zOrigin + z, GenerationPhase.BASE).getGenerator();
return (WorldGenerator) grid.getBiome(xOrigin + x, zOrigin + z, GenerationPhase.BASE).getGenerator(world);
}
private WorldGenerator getStoredGen(int x, int z) {

View File

@@ -0,0 +1,20 @@
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 net.jafama.FastMath;
public class Sampler {
private final ChunkInterpolator3 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.elevationInterpolator = new ElevationInterpolator(world, x, z, grid, elevationSmooth);
}
public double sample(double x, double y, double z) {
return interpolator.getNoise(x, y, z) + elevationInterpolator.getElevation(FastMath.roundToInt(x), FastMath.roundToInt(z));
}
}

View File

@@ -1,9 +1,9 @@
package com.dfsek.terra.api.math.interpolation;
package com.dfsek.terra.generation.math.interpolation;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.biome.BiomeGrid;
import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.api.world.generation.GenerationPhase;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import net.jafama.FastMath;
/**
@@ -18,23 +18,23 @@ public class ChunkInterpolator3 {
private final int smooth;
/**
* Instantiates a 3D ChunkInterpolator at a pair of chunk coordinates, with a BiomeGrid and FastNoiseLite instance.
* Instantiates a 3D ChunkInterpolator at a pair of chunk coordinates.
*
* @param chunkX X coordinate of the chunk.
* @param chunkZ Z coordinate of the chunk.
* @param grid BiomeGrid to use for noise fetching.
*/
public ChunkInterpolator3(World w, int chunkX, int chunkZ, BiomeGrid grid, int smooth) {
public ChunkInterpolator3(World w, int chunkX, int chunkZ, TerraBiomeGrid grid, int smooth) {
int xOrigin = chunkX << 4;
int zOrigin = chunkZ << 4;
this.smooth = smooth;
for(int x = -1; x < 6; x++) {
for(int z = -1; z < 6; z++) {
gens[x + 1][z + 1] = grid.getBiome(xOrigin + (x * smooth), zOrigin + (z * smooth), GenerationPhase.BASE).getGenerator();
gens[x + 1][z + 1] = grid.getBiome(xOrigin + (x * smooth), zOrigin + (z * smooth), GenerationPhase.BASE).getGenerator(w);
}
}
for(int x = 0; x < 5; x++) {
for(int z = 0; z < 5; z++) {
needsBiomeInterp[x][z] = compareGens(x + 1, z + 1);
@@ -44,7 +44,7 @@ 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(w, (x * smooth) + xOrigin, y << 2, (z * smooth) + zOrigin);
noiseStorage[x + 1][z + 1][y] = gens[x + 1][z + 1].getNoise((x * smooth) + xOrigin, y << 2, (z * smooth) + zOrigin);
}
}
}

View File

@@ -1,4 +1,4 @@
package com.dfsek.terra.api.math.interpolation;
package com.dfsek.terra.generation.math.interpolation;
/**
* Class for bilinear interpolation of values arranged on a unit square.

View File

@@ -1,4 +1,4 @@
package com.dfsek.terra.api.math.interpolation;
package com.dfsek.terra.generation.math.interpolation;
/**
* Class for bilinear interpolation of values arranged on a unit square.

View File

@@ -1,6 +1,6 @@
package com.dfsek.terra.math;
import com.dfsek.terra.generation.Sampler;
import com.dfsek.terra.generation.math.Sampler;
public final class MathUtil {
private static final double CONST = 0.55;

View File

@@ -4,7 +4,7 @@ import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.world.palette.Palette;
import com.dfsek.terra.biome.palette.PaletteHolder;
import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.generation.Sampler;
import com.dfsek.terra.generation.math.Sampler;
import com.dfsek.terra.math.MathUtil;
public final class PaletteUtil {