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

@ -25,6 +25,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import parsii.eval.Scope;
import java.io.File;
import java.io.IOException;
@ -74,8 +75,8 @@ public class ConfigPack extends YamlConfiguration {
private final TreeRegistry treeRegistry = new TreeRegistry();
private final FloraRegistry floraRegistry = new FloraRegistry();
private final Set<StructureConfig> allStructures = new HashSet<>();
private final Map<String, Double> definedVariables = new HashMap<>();
private final Map<String, NoiseConfig> noiseBuilders = new HashMap<>();
private final Scope vScope;
private final File dataFolder;
private final String id;
@ -115,11 +116,12 @@ public class ConfigPack extends YamlConfiguration {
Debug.info("Overriding Vanilla tree: " + entry.getKey());
}
vScope = new Scope();
if(contains("variables")) {
Map<String, Object> vars = Objects.requireNonNull(getConfigurationSection("variables")).getValues(false);
for(Map.Entry<String, Object> entry : vars.entrySet()) {
try {
definedVariables.put(entry.getKey(), Double.valueOf(entry.getValue().toString()));
vScope.getVariable(entry.getKey()).setValue(Double.parseDouble(entry.getValue().toString()));
Debug.info("Registered variable " + entry.getKey() + " with value " + entry.getValue());
} catch(ClassCastException | NumberFormatException e) {
Debug.stack(e);
@ -232,8 +234,8 @@ public class ConfigPack extends YamlConfiguration {
return id;
}
public Map<String, Double> getDefinedVariables() {
return definedVariables;
public Scope getVariableScope() {
return vScope;
}
public Map<String, BiomeConfig> getBiomes() {

View File

@ -170,7 +170,7 @@ public class BiomeConfig extends TerraConfig {
try {
// Get UserDefinedBiome instance representing this config.
GeneratorOptions gen = new GeneratorOptions(eq, elevation, config.getDefinedVariables(), palette.getPaletteMap(), slant, config.getNoiseBuilders(), getBoolean("prevent-smooth", false), doElevationInterpolation);
GeneratorOptions gen = new GeneratorOptions(eq, elevation, config.getVariableScope(), palette.getPaletteMap(), slant, config.getNoiseBuilders(), getBoolean("prevent-smooth", false), doElevationInterpolation);
this.biome = new UserDefinedBiome(vanillaBiome, dec, gen, getBoolean("erodible", false), biomeID);
} catch(ParseException e) {
e.printStackTrace();

View File

@ -27,12 +27,12 @@ public class GeneratorOptions {
private final String equation;
private final String elevationEquation;
private final Map<String, Double> userVariables;
private final Scope userVariables;
private final Map<String, NoiseConfig> noiseBuilders;
private final boolean elevationInterpolation;
public GeneratorOptions(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, boolean elevationInterpolation)
public GeneratorOptions(String equation, @Nullable String elevateEquation, Scope userVariables, Map<Integer, Palette<BlockData>> paletteMap, Map<Integer, Palette<BlockData>> slantPaletteMap, Map<String, NoiseConfig> noiseBuilders, boolean preventSmooth, boolean elevationInterpolation)
throws ParseException {
this.equation = equation;
this.elevationEquation = elevateEquation;
@ -40,11 +40,8 @@ public class GeneratorOptions {
this.noiseBuilders = noiseBuilders;
this.preventSmooth = preventSmooth;
Scope s = new Scope();
Scope s = new Scope().withParent(userVariables);
Parser p = new Parser();
for(Map.Entry<String, Double> entry : userVariables.entrySet()) {
s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables.
}
for(Map.Entry<String, NoiseConfig> e : noiseBuilders.entrySet()) {
int dimensions = e.getValue().getDimensions();
if(dimensions == 2 || dimensions == 3) p.registerFunction(e.getKey(), new BlankFunction(dimensions));

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) {