From fde8fd95f263f9eaddcb8162a747aa8b3284d4b6 Mon Sep 17 00:00:00 2001 From: dfsek Date: Tue, 17 Nov 2020 10:51:31 -0700 Subject: [PATCH] Clean up parsii code --- .../dfsek/terra/config/base/ConfigPack.java | 10 +++--- .../config/genconfig/biome/BiomeConfig.java | 2 +- .../genconfig/biome/GeneratorOptions.java | 9 ++--- .../terra/generation/ElevationEquation.java | 33 ++++--------------- .../generation/config/WorldGenerator.java | 27 +++++++-------- 5 files changed, 31 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java index 17932fcf5..9cd605b2e 100644 --- a/src/main/java/com/dfsek/terra/config/base/ConfigPack.java +++ b/src/main/java/com/dfsek/terra/config/base/ConfigPack.java @@ -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 allStructures = new HashSet<>(); - private final Map definedVariables = new HashMap<>(); private final Map 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 vars = Objects.requireNonNull(getConfigurationSection("variables")).getValues(false); for(Map.Entry 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 getDefinedVariables() { - return definedVariables; + public Scope getVariableScope() { + return vScope; } public Map getBiomes() { diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java index 8182cb28a..1c59271d8 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/BiomeConfig.java @@ -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(); diff --git a/src/main/java/com/dfsek/terra/config/genconfig/biome/GeneratorOptions.java b/src/main/java/com/dfsek/terra/config/genconfig/biome/GeneratorOptions.java index dc62ec946..a5705ebce 100644 --- a/src/main/java/com/dfsek/terra/config/genconfig/biome/GeneratorOptions.java +++ b/src/main/java/com/dfsek/terra/config/genconfig/biome/GeneratorOptions.java @@ -27,12 +27,12 @@ public class GeneratorOptions { private final String equation; private final String elevationEquation; - private final Map userVariables; + private final Scope userVariables; private final Map noiseBuilders; private final boolean elevationInterpolation; - public GeneratorOptions(String equation, @Nullable String elevateEquation, Map userVariables, Map> paletteMap, Map> slantPaletteMap, Map noiseBuilders, boolean preventSmooth, boolean elevationInterpolation) + public GeneratorOptions(String equation, @Nullable String elevateEquation, Scope userVariables, Map> paletteMap, Map> slantPaletteMap, Map 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 entry : userVariables.entrySet()) { - s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables. - } for(Map.Entry e : noiseBuilders.entrySet()) { int dimensions = e.getValue().getDimensions(); if(dimensions == 2 || dimensions == 3) p.registerFunction(e.getKey(), new BlankFunction(dimensions)); diff --git a/src/main/java/com/dfsek/terra/generation/ElevationEquation.java b/src/main/java/com/dfsek/terra/generation/ElevationEquation.java index 4adb15c5a..0b97cb34e 100644 --- a/src/main/java/com/dfsek/terra/generation/ElevationEquation.java +++ b/src/main/java/com/dfsek/terra/generation/ElevationEquation.java @@ -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 userVariables, Map noiseBuilders) { - for(Map.Entry 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 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(); diff --git a/src/main/java/com/dfsek/terra/generation/config/WorldGenerator.java b/src/main/java/com/dfsek/terra/generation/config/WorldGenerator.java index c2d1ee9f1..bd2624ba6 100644 --- a/src/main/java/com/dfsek/terra/generation/config/WorldGenerator.java +++ b/src/main/java/com/dfsek/terra/generation/config/WorldGenerator.java @@ -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 userVariables, Map noiseBuilders, Palette[] palettes, Palette[] slantPalettes, boolean preventSmooth) { - for(Map.Entry 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 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 getPalette(int y) {