Per-world noise gen is no longer dumb

This commit is contained in:
dfsek
2020-11-16 21:44:49 -07:00
parent 567b06fd3f
commit deb7db9b25
14 changed files with 172 additions and 118 deletions
+1
View File
@@ -60,6 +60,7 @@ val mainSourceSet: SourceSet = sourceSets["main"]
val tokenizeJavaSources = task<Copy>(name = "tokenizeJavaSources") { val tokenizeJavaSources = task<Copy>(name = "tokenizeJavaSources") {
from(mainSourceSet.allSource) { from(mainSourceSet.allSource) {
include("**/plugin.yml") include("**/plugin.yml")
println("version: $versionObj")
val tokens = mapOf("VERSION" to versionObj.toString()) val tokens = mapOf("VERSION" to versionObj.toString())
filter(org.apache.tools.ant.filters.ReplaceTokens::class, "tokens" to tokens) filter(org.apache.tools.ant.filters.ReplaceTokens::class, "tokens" to tokens)
@@ -14,7 +14,8 @@ public class NoiseConfig {
try { try {
builder.setType(FastNoiseLite.NoiseType.valueOf(section.getString("type", "OpenSimplex2"))) builder.setType(FastNoiseLite.NoiseType.valueOf(section.getString("type", "OpenSimplex2")))
.setFrequency(section.getDouble("frequency", 0.02D)) .setFrequency(section.getDouble("frequency", 0.02D))
.setRotationType3D(FastNoiseLite.RotationType3D.valueOf(section.getString("rotation", "None"))); .setRotationType3D(FastNoiseLite.RotationType3D.valueOf(section.getString("rotation", "None")))
.setSeedOffset(section.getInt("offset", 0));
dimensions = section.getInt("dimensions", 3); dimensions = section.getInt("dimensions", 3);
if(dimensions != 2 && dimensions != 3) if(dimensions != 2 && dimensions != 3)
@@ -1,7 +1,6 @@
package com.dfsek.terra.generation; package com.dfsek.terra.generation;
import com.dfsek.terra.config.genconfig.noise.NoiseConfig; import com.dfsek.terra.config.genconfig.noise.NoiseConfig;
import com.dfsek.terra.math.NoiseFunction;
import com.dfsek.terra.math.NoiseFunction2; import com.dfsek.terra.math.NoiseFunction2;
import com.dfsek.terra.math.NoiseFunction3; import com.dfsek.terra.math.NoiseFunction3;
import org.bukkit.World; import org.bukkit.World;
@@ -11,55 +10,43 @@ import parsii.eval.Scope;
import parsii.eval.Variable; import parsii.eval.Variable;
import parsii.tokenizer.ParseException; import parsii.tokenizer.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
public class ElevationEquation { public class ElevationEquation {
private static final Object noiseLock = new Object();
private final Expression delegate; private final Expression delegate;
private final Scope s = new Scope(); private final Scope s = new Scope();
private final Variable xVar = s.getVariable("x"); private final Variable xVar = s.getVariable("x");
private final Variable zVar = s.getVariable("z"); private final Variable zVar = s.getVariable("z");
private final List<NoiseFunction> noiseFunctions = new ArrayList<>(); public ElevationEquation(World w, String elevateEquation, Map<String, Double> userVariables, Map<String, NoiseConfig> noiseBuilders) {
private boolean set = true; for(Map.Entry<String, Double> entry : userVariables.entrySet()) {
s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables.
public ElevationEquation(String equation, Map<String, NoiseConfig> noiseBuilders) throws ParseException { }
Parser p = new Parser(); Parser p = new Parser();
for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) { for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) {
switch(e.getValue().getDimensions()) { switch(e.getValue().getDimensions()) {
case 2: case 2:
NoiseFunction2 function2 = new NoiseFunction2(e.getValue().getBuilder()); NoiseFunction2 function2 = new NoiseFunction2(w, e.getValue().getBuilder());
noiseFunctions.add(function2);
p.registerFunction(e.getKey(), function2); p.registerFunction(e.getKey(), function2);
break; break;
case 3: case 3:
NoiseFunction3 function3 = new NoiseFunction3(e.getValue().getBuilder()); NoiseFunction3 function3 = new NoiseFunction3(w, e.getValue().getBuilder());
noiseFunctions.add(function3);
p.registerFunction(e.getKey(), function3); p.registerFunction(e.getKey(), function3);
break; break;
} }
} }
delegate = p.parse(equation, s); try {
this.delegate = p.parse(elevateEquation, s);
} catch(ParseException e) {
throw new IllegalArgumentException();
}
} }
public double getNoise(double x, double z, World w) { public double getNoise(double x, double z) {
synchronized(noiseLock) {
xVar.setValue(x); xVar.setValue(x);
zVar.setValue(z); zVar.setValue(z);
setNoise(w.getSeed());
return delegate.evaluate(); return delegate.evaluate();
} }
} }
private void setNoise(long seed) {
if(set) {
set = false;
for(NoiseFunction n : noiseFunctions) {
n.setNoise(seed);
}
}
}
}
@@ -70,7 +70,7 @@ public class ElevationInterpolator {
} }
private double elevate(UserDefinedGenerator g, int x, int z, World w) { private double elevate(UserDefinedGenerator g, int x, int z, World w) {
if(g.getElevationEquation() != null) return g.getElevationEquation().getNoise(x, z, w); if(g.getElevationEquation(w) != null) return g.getElevationEquation(w).getNoise(x, z);
return 0; return 0;
} }
@@ -1,10 +1,8 @@
package com.dfsek.terra.generation; package com.dfsek.terra.generation;
import com.dfsek.terra.Debug;
import com.dfsek.terra.config.genconfig.noise.NoiseConfig; import com.dfsek.terra.config.genconfig.noise.NoiseConfig;
import com.dfsek.terra.math.NoiseFunction; import com.dfsek.terra.generation.config.WorldGenerator;
import com.dfsek.terra.math.NoiseFunction2; import com.dfsek.terra.math.BlankFunction;
import com.dfsek.terra.math.NoiseFunction3;
import com.dfsek.terra.util.DataUtil; import com.dfsek.terra.util.DataUtil;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
@@ -13,56 +11,53 @@ import org.polydev.gaea.biome.Generator;
import org.polydev.gaea.math.FastNoiseLite; import org.polydev.gaea.math.FastNoiseLite;
import org.polydev.gaea.math.Interpolator; import org.polydev.gaea.math.Interpolator;
import org.polydev.gaea.world.palette.Palette; import org.polydev.gaea.world.palette.Palette;
import parsii.eval.Expression;
import parsii.eval.Parser; import parsii.eval.Parser;
import parsii.eval.Scope; import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException; import parsii.tokenizer.ParseException;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class UserDefinedGenerator extends Generator { public class UserDefinedGenerator extends Generator {
private static final Object noiseLock = new Object();
private final Expression noiseExp; private final boolean preventSmooth;
private final Scope s = new Scope();
private final Variable xVar = s.getVariable("x");
private final Variable yVar = s.getVariable("y");
private final Variable zVar = s.getVariable("z");
@SuppressWarnings({"unchecked", "rawtypes", "RedundantSuppression"}) @SuppressWarnings({"unchecked", "rawtypes", "RedundantSuppression"})
private final Palette<BlockData>[] palettes = new Palette[256]; private final Palette<BlockData>[] palettes = new Palette[256];
@SuppressWarnings({"unchecked", "rawtypes", "RedundantSuppression"}) @SuppressWarnings({"unchecked", "rawtypes", "RedundantSuppression"})
private final Palette<BlockData>[] slantPalettes = new Palette[256]; private final Palette<BlockData>[] slantPalettes = new Palette[256];
private final ElevationEquation elevationEquation;
private final boolean preventSmooth;
private final String equation;
private final String elevationEquation;
private final Map<String, Double> userVariables;
private final Map<String, NoiseConfig> noiseBuilders;
private final Map<UUID, WorldGenerator> gens = new HashMap<>();
private boolean elevationInterpolation; private boolean elevationInterpolation;
private final List<NoiseFunction> noiseFunctions = new ArrayList<>();
private boolean set = true;
public UserDefinedGenerator(String equation, @Nullable String elevateEquation, Map<String, Double> userVariables, Map<Integer, Palette<BlockData>> paletteMap, Map<Integer, Palette<BlockData>> slantPaletteMap, Map<String, NoiseConfig> noiseBuilders, boolean preventSmooth) public UserDefinedGenerator(String equation, @Nullable String elevateEquation, Map<String, Double> userVariables, Map<Integer, Palette<BlockData>> paletteMap, Map<Integer, Palette<BlockData>> slantPaletteMap, Map<String, NoiseConfig> noiseBuilders, boolean preventSmooth)
throws ParseException { throws ParseException {
this.equation = equation;
this.elevationEquation = elevateEquation;
this.userVariables = userVariables;
this.noiseBuilders = noiseBuilders;
this.preventSmooth = preventSmooth;
Scope s = new Scope();
Parser p = new Parser();
for(Map.Entry<String, Double> entry : userVariables.entrySet()) { for(Map.Entry<String, Double> entry : userVariables.entrySet()) {
s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables. s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables.
} }
Parser p = new Parser();
for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) { for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) {
switch(e.getValue().getDimensions()) { int dimensions = e.getValue().getDimensions();
case 2: if(dimensions == 2 || dimensions == 3) p.registerFunction(e.getKey(), new BlankFunction(dimensions));
NoiseFunction2 function2 = new NoiseFunction2(e.getValue().getBuilder());
noiseFunctions.add(function2);
p.registerFunction(e.getKey(), function2);
break;
case 3:
NoiseFunction3 function3 = new NoiseFunction3(e.getValue().getBuilder());
noiseFunctions.add(function3);
p.registerFunction(e.getKey(), function3);
break;
}
} }
p.parse(equation, s); // Validate equation at config load time to prevent error during world load.
if(elevateEquation != null) p.parse(elevateEquation, s);
for(int y = 0; y < 256; y++) { for(int y = 0; y < 256; y++) {
Palette<BlockData> d = DataUtil.BLANK_PALETTE; Palette<BlockData> d = DataUtil.BLANK_PALETTE;
@@ -82,21 +77,7 @@ public class UserDefinedGenerator extends Generator {
} }
slantPalettes[y] = slantPalette; slantPalettes[y] = slantPalette;
} }
if(elevateEquation != null) {
Debug.info("Using elevation equation");
this.elevationEquation = new ElevationEquation(elevateEquation, noiseBuilders);
} else this.elevationEquation = null;
this.noiseExp = p.parse(equation, s);
this.preventSmooth = preventSmooth;
}
private void setNoise(long seed) {
if(set) {
set = false;
for(NoiseFunction n : noiseFunctions) {
n.setNoise(seed);
}
}
} }
/** /**
@@ -109,13 +90,7 @@ public class UserDefinedGenerator extends Generator {
*/ */
@Override @Override
public double getNoise(FastNoiseLite gen, World w, int x, int z) { public double getNoise(FastNoiseLite gen, World w, int x, int z) {
synchronized(noiseLock) { return compute(w).getNoise(x, 0, z);
xVar.setValue(x);
yVar.setValue(0);
zVar.setValue(z);
setNoise(w.getSeed());
return noiseExp.evaluate();
}
} }
/** /**
@@ -129,13 +104,11 @@ public class UserDefinedGenerator extends Generator {
*/ */
@Override @Override
public double getNoise(FastNoiseLite gen, World w, int x, int y, int z) { public double getNoise(FastNoiseLite gen, World w, int x, int y, int z) {
synchronized(noiseLock) { return compute(w).getNoise(x, y, z);
xVar.setValue(x);
yVar.setValue(y);
zVar.setValue(z);
setNoise(w.getSeed());
return noiseExp.evaluate();
} }
private WorldGenerator compute(World world) {
return gens.computeIfAbsent(world.getUID(), w -> new WorldGenerator(world, equation, elevationEquation, userVariables, noiseBuilders));
} }
/** /**
@@ -163,8 +136,8 @@ public class UserDefinedGenerator extends Generator {
return Interpolator.Type.LINEAR; return Interpolator.Type.LINEAR;
} }
public ElevationEquation getElevationEquation() { public ElevationEquation getElevationEquation(World w) {
return elevationEquation; return compute(w).getElevationEquation();
} }
public boolean interpolateElevation() { public boolean interpolateElevation() {
@@ -11,6 +11,7 @@ public class NoiseBuilder {
private double fractalLacunarity = 2.0D; private double fractalLacunarity = 2.0D;
private double pingPong = 2.0D; private double pingPong = 2.0D;
private double weightedStrength = 0.0D; private double weightedStrength = 0.0D;
private int seedOffset = 0;
private FastNoiseLite.CellularDistanceFunction cellularDistanceFunction = FastNoiseLite.CellularDistanceFunction.EuclideanSq; private FastNoiseLite.CellularDistanceFunction cellularDistanceFunction = FastNoiseLite.CellularDistanceFunction.EuclideanSq;
private FastNoiseLite.CellularReturnType cellularReturnType = FastNoiseLite.CellularReturnType.Distance; private FastNoiseLite.CellularReturnType cellularReturnType = FastNoiseLite.CellularReturnType.Distance;
@@ -22,7 +23,7 @@ public class NoiseBuilder {
private FastNoiseLite.RotationType3D rotationType3D = FastNoiseLite.RotationType3D.None; private FastNoiseLite.RotationType3D rotationType3D = FastNoiseLite.RotationType3D.None;
public FastNoiseLite build(int seed) { public FastNoiseLite build(int seed) {
FastNoiseLite noise = new FastNoiseLite(seed); FastNoiseLite noise = new FastNoiseLite(seed + seedOffset);
if(!fractalType.equals(FastNoiseLite.FractalType.None)) { if(!fractalType.equals(FastNoiseLite.FractalType.None)) {
noise.setFractalType(fractalType); noise.setFractalType(fractalType);
noise.setFractalOctaves(octaves); noise.setFractalOctaves(octaves);
@@ -107,6 +108,14 @@ public class NoiseBuilder {
return this; return this;
} }
public int getSeedOffset() {
return seedOffset;
}
public void setSeedOffset(int seedOffset) {
this.seedOffset = seedOffset;
}
public NoiseBuilder setDomainWarpAmp(double domainWarpAmp) { public NoiseBuilder setDomainWarpAmp(double domainWarpAmp) {
this.domainWarpAmp = domainWarpAmp; this.domainWarpAmp = domainWarpAmp;
return this; return this;
@@ -0,0 +1,65 @@
package com.dfsek.terra.generation.config;
import com.dfsek.terra.Debug;
import com.dfsek.terra.config.genconfig.noise.NoiseConfig;
import com.dfsek.terra.generation.ElevationEquation;
import com.dfsek.terra.math.NoiseFunction2;
import com.dfsek.terra.math.NoiseFunction3;
import org.bukkit.World;
import parsii.eval.Expression;
import parsii.eval.Parser;
import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException;
import java.util.Map;
public class WorldGenerator {
private final ElevationEquation elevationEquation;
private final Expression noiseExp;
private final Scope s = new Scope();
private final Variable xVar = s.getVariable("x");
private final Variable yVar = s.getVariable("y");
private final Variable zVar = s.getVariable("z");
public WorldGenerator(World w, String equation, String elevateEquation, Map<String, Double> userVariables, Map<String, NoiseConfig> noiseBuilders) {
for(Map.Entry<String, Double> entry : userVariables.entrySet()) {
s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables.
}
Parser p = new Parser();
for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) {
switch(e.getValue().getDimensions()) {
case 2:
NoiseFunction2 function2 = new NoiseFunction2(w, e.getValue().getBuilder());
p.registerFunction(e.getKey(), function2);
break;
case 3:
NoiseFunction3 function3 = new NoiseFunction3(w, e.getValue().getBuilder());
p.registerFunction(e.getKey(), function3);
break;
}
}
try {
this.noiseExp = p.parse(equation, s);
if(elevateEquation != null) {
Debug.info("Using elevation equation");
this.elevationEquation = new ElevationEquation(w, elevateEquation, userVariables, noiseBuilders);
} else this.elevationEquation = null;
} catch(ParseException e) {
throw new IllegalArgumentException();
}
}
public double getNoise(int x, int y, int z) {
xVar.setValue(x);
yVar.setValue(y);
zVar.setValue(z);
return noiseExp.evaluate();
}
public ElevationEquation getElevationEquation() {
return elevationEquation;
}
}
@@ -0,0 +1,29 @@
package com.dfsek.terra.math;
import parsii.eval.Expression;
import parsii.eval.Function;
import java.util.List;
public class BlankFunction implements Function {
private final int args;
public BlankFunction(int args) {
this.args = args;
}
@Override
public int getNumberOfArguments() {
return args;
}
@Override
public double eval(List<Expression> list) {
return 0;
}
@Override
public boolean isNaturalFunction() {
return true;
}
}
@@ -3,5 +3,4 @@ package com.dfsek.terra.math;
import parsii.eval.Function; import parsii.eval.Function;
public interface NoiseFunction extends Function { public interface NoiseFunction extends Function {
void setNoise(long seed);
} }
@@ -2,6 +2,7 @@ package com.dfsek.terra.math;
import com.dfsek.terra.config.base.ConfigUtil; import com.dfsek.terra.config.base.ConfigUtil;
import com.dfsek.terra.generation.config.NoiseBuilder; import com.dfsek.terra.generation.config.NoiseBuilder;
import org.bukkit.World;
import org.polydev.gaea.math.FastNoiseLite; import org.polydev.gaea.math.FastNoiseLite;
import parsii.eval.Expression; import parsii.eval.Expression;
@@ -9,11 +10,10 @@ import java.util.List;
public class NoiseFunction2 implements NoiseFunction { public class NoiseFunction2 implements NoiseFunction {
private final Cache cache = new Cache(); private final Cache cache = new Cache();
private FastNoiseLite gen; private final FastNoiseLite gen;
private final NoiseBuilder builder;
public NoiseFunction2(NoiseBuilder builder) { public NoiseFunction2(World world, NoiseBuilder builder) {
this.builder = builder; this.gen = builder.build((int) world.getSeed());
} }
@Override @Override
@@ -31,11 +31,6 @@ public class NoiseFunction2 implements NoiseFunction {
return true; return true;
} }
@Override
public void setNoise(long seed) {
this.gen = builder.build((int) seed);
}
private final class Cache { private final class Cache {
private final double[] cacheX = new double[ConfigUtil.cacheSize]; private final double[] cacheX = new double[ConfigUtil.cacheSize];
private final double[] cacheZ = new double[ConfigUtil.cacheSize]; private final double[] cacheZ = new double[ConfigUtil.cacheSize];
@@ -1,17 +1,17 @@
package com.dfsek.terra.math; package com.dfsek.terra.math;
import com.dfsek.terra.generation.config.NoiseBuilder; import com.dfsek.terra.generation.config.NoiseBuilder;
import org.bukkit.World;
import org.polydev.gaea.math.FastNoiseLite; import org.polydev.gaea.math.FastNoiseLite;
import parsii.eval.Expression; import parsii.eval.Expression;
import java.util.List; import java.util.List;
public class NoiseFunction3 implements NoiseFunction { public class NoiseFunction3 implements NoiseFunction {
private FastNoiseLite gen; private final FastNoiseLite gen;
private final NoiseBuilder builder;
public NoiseFunction3(NoiseBuilder builder) { public NoiseFunction3(World world, NoiseBuilder builder) {
this.builder = builder; this.gen = builder.build((int) world.getSeed());
} }
@Override @Override
@@ -28,9 +28,4 @@ public class NoiseFunction3 implements NoiseFunction {
public boolean isNaturalFunction() { public boolean isNaturalFunction() {
return true; return true;
} }
@Override
public void setNoise(long seed) {
this.gen = builder.build((int) seed);
}
} }
@@ -21,8 +21,8 @@ public class AirSpawn extends Requirement {
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE); UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeConfig c = wc.getBiome(b); BiomeConfig c = wc.getBiome(b);
if(y <= c.getOcean().getSeaLevel()) return false; if(y <= c.getOcean().getSeaLevel()) return false;
ElevationEquation elevationEquation = ((UserDefinedGenerator) b.getGenerator()).getElevationEquation(); ElevationEquation elevationEquation = ((UserDefinedGenerator) b.getGenerator()).getElevationEquation(getWorld());
int yf = y - ((elevationEquation == null) ? 0 : (int) elevationEquation.getNoise(x, z, getWorld())); int yf = y - ((elevationEquation == null) ? 0 : (int) elevationEquation.getNoise(x, z));
return b.getGenerator().getNoise(getNoise(), getWorld(), x, yf, z) <= 0; return b.getGenerator().getNoise(getNoise(), getWorld(), x, yf, z) <= 0;
} }
} }
@@ -16,8 +16,8 @@ public class LandSpawn extends Requirement {
public boolean matches(int x, int y, int z) { public boolean matches(int x, int y, int z) {
TerraWorld tw = TerraWorld.getWorld(getWorld()); TerraWorld tw = TerraWorld.getWorld(getWorld());
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE); UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
ElevationEquation elevationEquation = ((UserDefinedGenerator) b.getGenerator()).getElevationEquation(); ElevationEquation elevationEquation = ((UserDefinedGenerator) b.getGenerator()).getElevationEquation(getWorld());
int yf = y - ((elevationEquation == null) ? 0 : (int) elevationEquation.getNoise(x, z, getWorld())); int yf = y - ((elevationEquation == null) ? 0 : (int) elevationEquation.getNoise(x, z));
return b.getGenerator().getNoise(getNoise(), getWorld(), x, yf, z) > 0; return b.getGenerator().getNoise(getNoise(), getWorld(), x, yf, z) > 0;
} }
} }
@@ -19,8 +19,8 @@ public class OceanSpawn extends Requirement {
UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE); UserDefinedBiome b = (UserDefinedBiome) tw.getGrid().getBiome(x, z, GenerationPhase.POPULATE);
BiomeConfig c = tw.getConfig().getBiome(b); BiomeConfig c = tw.getConfig().getBiome(b);
if(y > c.getOcean().getSeaLevel()) return false; if(y > c.getOcean().getSeaLevel()) return false;
ElevationEquation elevationEquation = ((UserDefinedGenerator) b.getGenerator()).getElevationEquation(); ElevationEquation elevationEquation = ((UserDefinedGenerator) b.getGenerator()).getElevationEquation(getWorld());
int yf = y - ((elevationEquation == null) ? 0 : (int) elevationEquation.getNoise(x, z, getWorld())); int yf = y - ((elevationEquation == null) ? 0 : (int) elevationEquation.getNoise(x, z));
return b.getGenerator().getNoise(getNoise(), getWorld(), x, yf, z) <= 0; return b.getGenerator().getNoise(getNoise(), getWorld(), x, yf, z) <= 0;
} }
} }