Clean up parsii code

This commit is contained in:
dfsek
2020-11-17 10:51:31 -07:00
parent 4c4c31db45
commit fde8fd95f2
5 changed files with 31 additions and 50 deletions

View File

@@ -1,41 +1,22 @@
package com.dfsek.terra.generation;
import com.dfsek.terra.config.genconfig.noise.NoiseConfig;
import com.dfsek.terra.math.NoiseFunction2;
import com.dfsek.terra.math.NoiseFunction3;
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 ElevationEquation {
private final Expression delegate;
private final Scope s = new Scope();
private final Variable xVar = s.getVariable("x");
private final Variable zVar = s.getVariable("z");
private final Variable xVar;
private final Variable zVar;
public ElevationEquation(long seed, 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();
public ElevationEquation(String elevateEquation, Scope vScope, Parser p) {
Scope s = new Scope().withParent(vScope);
xVar = s.create("x");
zVar = s.create("z");
for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) {
switch(e.getValue().getDimensions()) {
case 2:
NoiseFunction2 function2 = new NoiseFunction2(seed, e.getValue().getBuilder());
p.registerFunction(e.getKey(), function2);
break;
case 3:
NoiseFunction3 function3 = new NoiseFunction3(seed, e.getValue().getBuilder());
p.registerFunction(e.getKey(), function3);
break;
}
}
try {
this.delegate = p.parse(elevateEquation, s);
} catch(ParseException e) {
@@ -43,7 +24,7 @@ public class ElevationEquation {
}
}
public double getNoise(double x, double z) {
public synchronized double getNoise(double x, double z) {
xVar.setValue(x);
zVar.setValue(z);
return delegate.evaluate();

View File

@@ -29,19 +29,20 @@ public class WorldGenerator extends Generator {
private final boolean preventSmooth;
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");
private final Variable xVar;
private final Variable yVar;
private final Variable zVar;
private boolean elevationInterpolation = true;
@SuppressWarnings({"rawtypes", "unchecked"})
public WorldGenerator(long seed, String equation, String elevateEquation, Map<String, Double> userVariables, Map<String, NoiseConfig> noiseBuilders, Palette[] palettes, Palette[] slantPalettes, boolean preventSmooth) {
for(Map.Entry<String, Double> entry : userVariables.entrySet()) {
s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables.
}
public WorldGenerator(long seed, String equation, String elevateEquation, Scope vScope, Map<String, NoiseConfig> noiseBuilders, Palette[] palettes, Palette[] slantPalettes, boolean preventSmooth) {
Parser p = new Parser();
Scope s = new Scope().withParent(vScope);
xVar = s.create("x");
yVar = s.create("y");
zVar = s.create("z");
this.preventSmooth = preventSmooth;
this.palettes = palettes;
@@ -63,7 +64,7 @@ public class WorldGenerator extends Generator {
this.noiseExp = p.parse(equation, s);
if(elevateEquation != null) {
Debug.info("Using elevation equation");
this.elevationEquation = new ElevationEquation(seed, elevateEquation, userVariables, noiseBuilders);
this.elevationEquation = new ElevationEquation(elevateEquation, vScope, p);
} else this.elevationEquation = null;
} catch(ParseException e) {
throw new IllegalArgumentException();
@@ -75,7 +76,7 @@ public class WorldGenerator extends Generator {
}
@Override
public double getNoise(FastNoiseLite fastNoiseLite, World world, int x, int z) {
public synchronized double getNoise(FastNoiseLite fastNoiseLite, World world, int x, int z) {
xVar.setValue(x);
yVar.setValue(0);
zVar.setValue(z);
@@ -83,7 +84,7 @@ public class WorldGenerator extends Generator {
}
@Override
public double getNoise(FastNoiseLite fastNoiseLite, World world, int x, int y, int z) {
public synchronized double getNoise(FastNoiseLite fastNoiseLite, World world, int x, int y, int z) {
xVar.setValue(x);
yVar.setValue(y);
zVar.setValue(z);
@@ -91,9 +92,9 @@ public class WorldGenerator extends Generator {
}
/**
* Gets the BlocPalette to generate the biome with.
* Gets the BlockPalette to generate the biome with.
*
* @return BlocPalette - The biome's palette.
* @return BlockPalette - The biome's palette.
*/
@Override
public Palette<BlockData> getPalette(int y) {