Finally implement user defined variables

This commit is contained in:
dfsek 2020-11-13 12:25:40 -07:00
parent 08d1ca02f9
commit b3dac9f37e
4 changed files with 28 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import org.polydev.gaea.world.palette.Palette;
import org.polydev.gaea.world.palette.RandomPalette; import org.polydev.gaea.world.palette.RandomPalette;
import parsii.tokenizer.ParseException; import parsii.tokenizer.ParseException;
import java.util.Collections; import java.util.HashMap;
import java.util.Random; import java.util.Random;
import java.util.TreeMap; import java.util.TreeMap;
@ -19,6 +19,6 @@ public final class FailoverGenerator extends UserDefinedGenerator {
} }
public FailoverGenerator() throws ParseException { public FailoverGenerator() throws ParseException {
super("0", null, Collections.emptyList(), palette, new TreeMap<>(), false); super("0", null, new HashMap<>(), palette, new TreeMap<>(), false);
} }
} }

View File

@ -34,6 +34,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -63,7 +64,7 @@ public class ConfigPack extends YamlConfiguration {
public final boolean vanillaMobs; public final boolean vanillaMobs;
public final boolean preventSaplingOverride; public final boolean preventSaplingOverride;
public final Map<StructureTypeEnum, StructureConfig> locatable = new HashMap<>(); private final Map<StructureTypeEnum, StructureConfig> locatable = new HashMap<>();
private final Map<String, OreConfig> ores; private final Map<String, OreConfig> ores;
private final Map<String, PaletteConfig> palettes; private final Map<String, PaletteConfig> palettes;
private final Map<String, CarverConfig> carvers; private final Map<String, CarverConfig> carvers;
@ -74,6 +75,7 @@ public class ConfigPack extends YamlConfiguration {
private final TreeRegistry treeRegistry = new TreeRegistry(); private final TreeRegistry treeRegistry = new TreeRegistry();
private final FloraRegistry floraRegistry = new FloraRegistry(); private final FloraRegistry floraRegistry = new FloraRegistry();
private final Set<StructureConfig> allStructures = new HashSet<>(); private final Set<StructureConfig> allStructures = new HashSet<>();
private final Map<String, Double> definedVariables = new HashMap<>();
private final File dataFolder; private final File dataFolder;
private final String id; private final String id;
@ -106,6 +108,20 @@ public class ConfigPack extends YamlConfiguration {
Debug.info("Overriding Vanilla tree: " + entry.getKey()); Debug.info("Overriding Vanilla tree: " + entry.getKey());
} }
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()));
Debug.info("Registered variable " + entry.getKey() + " with value " + entry.getValue());
} catch(ClassCastException | NumberFormatException e) {
Debug.stack(e);
throw new ConfigException("Variable value " + entry.getValue().toString() + " could not be parsed to a double.", getID());
}
}
}
abstractBiomes = ConfigLoader.load(new File(file, "abstract" + File.separator + "biomes").toPath(), this, AbstractBiomeConfig.class); abstractBiomes = ConfigLoader.load(new File(file, "abstract" + File.separator + "biomes").toPath(), this, AbstractBiomeConfig.class);
biomes = ConfigLoader.load(new File(file, "biomes").toPath(), this, BiomeConfig.class); biomes = ConfigLoader.load(new File(file, "biomes").toPath(), this, BiomeConfig.class);
@ -178,6 +194,10 @@ public class ConfigPack extends YamlConfiguration {
return id; return id;
} }
public Map<String, Double> getDefinedVariables() {
return definedVariables;
}
public Map<String, BiomeConfig> getBiomes() { public Map<String, BiomeConfig> getBiomes() {
return biomes; return biomes;
} }

View File

@ -21,7 +21,6 @@ import parsii.tokenizer.ParseException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.TreeMap; import java.util.TreeMap;
@ -172,7 +171,7 @@ public class BiomeConfig extends TerraConfig {
try { try {
// Get UserDefinedBiome instance representing this config. // Get UserDefinedBiome instance representing this config.
UserDefinedGenerator gen = new UserDefinedGenerator(eq, elevation, Collections.emptyList(), palette.getPaletteMap(), slant, getBoolean("prevent-smooth", false)); UserDefinedGenerator gen = new UserDefinedGenerator(eq, elevation, config.getDefinedVariables(), palette.getPaletteMap(), slant, getBoolean("prevent-smooth", false));
gen.setElevationInterpolation(doElevationInterpolation); gen.setElevationInterpolation(doElevationInterpolation);
this.biome = new UserDefinedBiome(vanillaBiome, dec, gen, getBoolean("erodible", false), biomeID); this.biome = new UserDefinedBiome(vanillaBiome, dec, gen, getBoolean("erodible", false), biomeID);
} catch(ParseException e) { } catch(ParseException e) {

View File

@ -17,7 +17,6 @@ import parsii.eval.Scope;
import parsii.eval.Variable; import parsii.eval.Variable;
import parsii.tokenizer.ParseException; import parsii.tokenizer.ParseException;
import java.util.List;
import java.util.Map; import java.util.Map;
@ -39,8 +38,11 @@ public class UserDefinedGenerator extends Generator {
private boolean elevationInterpolation; private boolean elevationInterpolation;
public UserDefinedGenerator(String equation, @Nullable String elevateEquation, List<Variable> userVariables, Map<Integer, Palette<BlockData>> paletteMap, Map<Integer, Palette<BlockData>> slantPaletteMap, boolean preventSmooth) public UserDefinedGenerator(String equation, @Nullable String elevateEquation, Map<String, Double> userVariables, Map<Integer, Palette<BlockData>> paletteMap, Map<Integer, Palette<BlockData>> slantPaletteMap, boolean preventSmooth)
throws ParseException { throws ParseException {
for(Map.Entry<String, Double> entry : userVariables.entrySet()) {
s.getVariable(entry.getKey()).setValue(entry.getValue()); // Define all user variables.
}
Parser p = new Parser(); Parser p = new Parser();
p.registerFunction("noise2", n2); p.registerFunction("noise2", n2);
p.registerFunction("noise3", n3); p.registerFunction("noise3", n3);