mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-07 16:26:13 +00:00
paralithic implementation
This commit is contained in:
@@ -13,10 +13,11 @@ dependencies {
|
||||
"shadedApi"("org.apache.commons:commons-rng-core:1.3")
|
||||
"shadedApi"("commons-io:commons-io:2.4")
|
||||
|
||||
"shadedApi"("com.scireum:parsii:1.2.1")
|
||||
"shadedApi"("com.dfsek:Paralithic:0.1.0+21c6ff6")
|
||||
"shadedApi"("com.dfsek:Tectonic:1.2.3")
|
||||
"shadedApi"("net.jafama:jafama:2.3.2")
|
||||
"shadedApi"("org.yaml:snakeyaml:1.27")
|
||||
"shadedApi"("org.ow2.asm:asm:9.0")
|
||||
|
||||
"compileOnly"("com.googlecode.json-simple:json-simple:1.1")
|
||||
|
||||
|
||||
@@ -1,37 +1,31 @@
|
||||
package com.dfsek.terra.api.math.noise.samplers;
|
||||
|
||||
import com.dfsek.paralithic.Expression;
|
||||
import com.dfsek.paralithic.eval.parser.Parser;
|
||||
import com.dfsek.paralithic.eval.parser.Scope;
|
||||
import com.dfsek.paralithic.eval.tokenizer.ParseException;
|
||||
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.math.parsii.defined.UserDefinedFunction;
|
||||
import com.dfsek.terra.api.math.parsii.noise.NoiseFunction2;
|
||||
import com.dfsek.terra.api.math.parsii.noise.NoiseFunction3;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import com.dfsek.terra.config.loaders.config.function.FunctionTemplate;
|
||||
import parsii.eval.Expression;
|
||||
import parsii.eval.Parser;
|
||||
import parsii.eval.Scope;
|
||||
import parsii.eval.Variable;
|
||||
import parsii.tokenizer.ParseException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Sampler implementation using parsii expression
|
||||
*/
|
||||
public class ExpressionSampler implements NoiseSampler {
|
||||
private final Expression expression;
|
||||
private final Variable x;
|
||||
private final Variable y;
|
||||
private final Variable z;
|
||||
|
||||
public ExpressionSampler(String equation, Scope parent, long seed, Map<String, NoiseSeeded> functions, Map<String, FunctionTemplate> definedFunctions) throws ParseException {
|
||||
Parser parser = new Parser();
|
||||
Scope scope = new Scope().withParent(parent);
|
||||
|
||||
this.x = scope.create("x");
|
||||
this.y = scope.create("y");
|
||||
this.z = scope.create("z");
|
||||
scope.addInvocationVariable("x");
|
||||
scope.addInvocationVariable("y");
|
||||
scope.addInvocationVariable("z");
|
||||
|
||||
functions.forEach((id, noise) -> {
|
||||
switch(noise.getDimensions()) {
|
||||
@@ -49,25 +43,21 @@ public class ExpressionSampler implements NoiseSampler {
|
||||
FunctionTemplate fun = entry.getValue();
|
||||
|
||||
Scope functionScope = new Scope().withParent(parent);
|
||||
List<Variable> variables = fun.getArgs().stream().map(functionScope::create).collect(Collectors.toList());
|
||||
|
||||
parser.registerFunction(id, new UserDefinedFunction(parser.parse(fun.getFunction(), functionScope), variables));
|
||||
parser.registerFunction(id, new UserDefinedFunction(parser.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
|
||||
}
|
||||
|
||||
this.expression = parser.parse(equation, scope);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized double getNoise(double x, double y) {
|
||||
public double getNoise(double x, double y) {
|
||||
return getNoise(x, 0, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized double getNoise(double x, double y, double z) {
|
||||
this.x.setValue(x);
|
||||
this.y.setValue(y);
|
||||
this.z.setValue(z);
|
||||
return expression.evaluate();
|
||||
public double getNoise(double x, double y, double z) {
|
||||
return expression.evaluate(x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package com.dfsek.terra.api.math.parsii;
|
||||
|
||||
import parsii.eval.Expression;
|
||||
import parsii.eval.Function;
|
||||
import com.dfsek.paralithic.function.dynamic.DynamicFunction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BlankFunction implements Function {
|
||||
public class BlankFunction implements DynamicFunction {
|
||||
private final int args;
|
||||
|
||||
public BlankFunction(int args) {
|
||||
@@ -13,17 +10,17 @@ public class BlankFunction implements Function {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
public int getArgNumber() {
|
||||
return args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
public double eval(double... d) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
public boolean isStateless() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,31 @@
|
||||
package com.dfsek.terra.api.math.parsii.defined;
|
||||
|
||||
import parsii.eval.Expression;
|
||||
import parsii.eval.Function;
|
||||
import parsii.eval.Variable;
|
||||
import com.dfsek.paralithic.Expression;
|
||||
import com.dfsek.paralithic.function.dynamic.DynamicFunction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class UserDefinedFunction implements Function {
|
||||
public class UserDefinedFunction implements DynamicFunction {
|
||||
private final Expression expression;
|
||||
private final List<Variable> variables;
|
||||
private final int args;
|
||||
|
||||
public UserDefinedFunction(Expression expression, List<Variable> variables) {
|
||||
public UserDefinedFunction(Expression expression, int args) {
|
||||
this.expression = expression;
|
||||
this.variables = variables;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double eval(double... args) {
|
||||
return expression.evaluate(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
return variables.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized double eval(List<Expression> args) {
|
||||
for(int i = 0; i < variables.size(); i++) {
|
||||
variables.get(i).setValue(args.get(i).evaluate());
|
||||
}
|
||||
return expression.evaluate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
public boolean isStateless() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getArgNumber() {
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.dfsek.terra.api.math.parsii.noise;
|
||||
|
||||
import parsii.eval.Function;
|
||||
import com.dfsek.paralithic.function.dynamic.DynamicFunction;
|
||||
|
||||
public interface NoiseFunction extends Function {
|
||||
public interface NoiseFunction extends DynamicFunction {
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@ package com.dfsek.terra.api.math.parsii.noise;
|
||||
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import com.dfsek.terra.util.hash.HashMapDoubleDouble;
|
||||
import parsii.eval.Expression;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class NoiseFunction2 implements NoiseFunction {
|
||||
private final NoiseSampler gen;
|
||||
@@ -16,27 +15,17 @@ public class NoiseFunction2 implements NoiseFunction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
public int getArgNumber() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
return cache.get(gen, list.get(0).evaluate(), list.get(1).evaluate());
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate without cache. For testing.
|
||||
*
|
||||
* @param list Parameters.
|
||||
* @return Result.
|
||||
*/
|
||||
public double evalNoCache(List<Expression> list) {
|
||||
return gen.getNoise(list.get(0).evaluate(), list.get(1).evaluate());
|
||||
public double eval(double... args) {
|
||||
return cache.get(gen, args[0], args[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
public boolean isStateless() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -55,7 +44,7 @@ public class NoiseFunction2 implements NoiseFunction {
|
||||
return (value == 4.9E-324D ? addAndReturn(noise.getNoise(x, z), key) : value);
|
||||
}
|
||||
|
||||
private double addAndReturn(double value, double key) {
|
||||
private synchronized double addAndReturn(double value, double key) {
|
||||
this.put(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,6 @@ package com.dfsek.terra.api.math.parsii.noise;
|
||||
|
||||
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import parsii.eval.Expression;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NoiseFunction3 implements NoiseFunction {
|
||||
private final NoiseSampler gen;
|
||||
@@ -14,17 +11,17 @@ public class NoiseFunction3 implements NoiseFunction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
public int getArgNumber() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
return gen.getNoise(list.get(0).evaluate(), list.get(1).evaluate(), list.get(2).evaluate());
|
||||
public double eval(double... args) {
|
||||
return gen.getNoise(args[0], args[1], args[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
public boolean isStateless() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.dfsek.terra.config.builder;
|
||||
|
||||
import com.dfsek.paralithic.eval.parser.Scope;
|
||||
import com.dfsek.paralithic.eval.tokenizer.ParseException;
|
||||
import com.dfsek.terra.api.math.noise.NoiseSampler;
|
||||
import com.dfsek.terra.api.math.noise.samplers.ConstantSampler;
|
||||
import com.dfsek.terra.api.math.noise.samplers.ExpressionSampler;
|
||||
@@ -7,8 +9,6 @@ import com.dfsek.terra.api.util.seeded.NoiseSeeded;
|
||||
import com.dfsek.terra.api.world.palette.holder.PaletteHolder;
|
||||
import com.dfsek.terra.config.loaders.config.function.FunctionTemplate;
|
||||
import com.dfsek.terra.world.generation.WorldGenerator;
|
||||
import parsii.eval.Scope;
|
||||
import parsii.tokenizer.ParseException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.dfsek.terra.config.factories;
|
||||
|
||||
import com.dfsek.paralithic.eval.parser.Scope;
|
||||
import com.dfsek.terra.api.core.TerraPlugin;
|
||||
import com.dfsek.terra.biome.TerraBiome;
|
||||
import com.dfsek.terra.biome.UserDefinedBiome;
|
||||
@@ -7,7 +8,6 @@ import com.dfsek.terra.config.builder.GeneratorBuilder;
|
||||
import com.dfsek.terra.config.loaders.config.function.FunctionTemplate;
|
||||
import com.dfsek.terra.config.pack.ConfigPack;
|
||||
import com.dfsek.terra.config.templates.BiomeTemplate;
|
||||
import parsii.eval.Scope;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -35,7 +35,7 @@ public class BiomeFactory implements TerraFactory<BiomeTemplate, TerraBiome> {
|
||||
generatorBuilder.setSlantPalettes(template.getSlantPalette());
|
||||
|
||||
Scope vars = new Scope().withParent(pack.getVarScope());
|
||||
template.getVariables().forEach((id, val) -> vars.create(id).setValue(val));
|
||||
template.getVariables().forEach(vars::create);
|
||||
generatorBuilder.setVarScope(vars);
|
||||
|
||||
generatorBuilder.setInterpolateElevation(template.interpolateElevation());
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.dfsek.terra.config.pack;
|
||||
|
||||
import com.dfsek.paralithic.eval.parser.Scope;
|
||||
import com.dfsek.tectonic.abstraction.AbstractConfigLoader;
|
||||
import com.dfsek.tectonic.exception.ConfigException;
|
||||
import com.dfsek.tectonic.exception.LoadException;
|
||||
@@ -49,7 +50,6 @@ import com.dfsek.terra.world.population.items.TerraStructure;
|
||||
import com.dfsek.terra.world.population.items.ores.Ore;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import parsii.eval.Scope;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
@@ -154,7 +154,7 @@ public class ConfigPack implements LoaderRegistrar {
|
||||
private void load(long start, TerraPlugin main) throws ConfigException {
|
||||
main.packPreLoadCallback(this);
|
||||
for(Map.Entry<String, Double> var : template.getVariables().entrySet()) {
|
||||
varScope.create(var.getKey()).setValue(var.getValue());
|
||||
varScope.create(var.getKey(), var.getValue());
|
||||
}
|
||||
loader.open("structures/data", ".tstructure")
|
||||
.thenNames(ids -> ids.forEach(id -> main.getLogger().severe("Found .tstructure file \"" + id + "\". This file must be updated to the newer .tesf format to work properly. See version 3.0 release notes for instructions.")))
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.dfsek.terra.config.templates;
|
||||
|
||||
import com.dfsek.paralithic.eval.parser.Parser;
|
||||
import com.dfsek.paralithic.eval.parser.Scope;
|
||||
import com.dfsek.paralithic.eval.tokenizer.ParseException;
|
||||
import com.dfsek.tectonic.annotations.Abstractable;
|
||||
import com.dfsek.tectonic.annotations.Default;
|
||||
import com.dfsek.tectonic.annotations.Value;
|
||||
@@ -26,16 +29,11 @@ import com.dfsek.terra.world.population.items.TerraStructure;
|
||||
import com.dfsek.terra.world.population.items.flora.FloraLayer;
|
||||
import com.dfsek.terra.world.population.items.ores.OreHolder;
|
||||
import com.dfsek.terra.world.population.items.tree.TreeLayer;
|
||||
import parsii.eval.Parser;
|
||||
import parsii.eval.Scope;
|
||||
import parsii.eval.Variable;
|
||||
import parsii.tokenizer.ParseException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings({"FieldMayBeFinal", "unused"})
|
||||
public class BiomeTemplate extends AbstractableTemplate implements ValidatedConfigTemplate {
|
||||
@@ -333,12 +331,12 @@ public class BiomeTemplate extends AbstractableTemplate implements ValidatedConf
|
||||
Parser tester = new Parser();
|
||||
Scope testScope = new Scope().withParent(pack.getVarScope());
|
||||
|
||||
variables.forEach((id, val) -> testScope.create(id).setValue(val));
|
||||
variables.forEach(testScope::create);
|
||||
|
||||
testScope.addInvocationVariable("x");
|
||||
testScope.addInvocationVariable("y");
|
||||
testScope.addInvocationVariable("z");
|
||||
|
||||
testScope.create("x");
|
||||
testScope.create("y");
|
||||
testScope.create("z");
|
||||
testScope.create("seed");
|
||||
|
||||
pack.getTemplate().getNoiseBuilderMap().forEach((id, builder) -> tester.registerFunction(id, new BlankFunction(builder.getDimensions()))); // Register dummy functions
|
||||
|
||||
@@ -349,16 +347,17 @@ public class BiomeTemplate extends AbstractableTemplate implements ValidatedConf
|
||||
FunctionTemplate fun = entry.getValue();
|
||||
|
||||
Scope functionScope = new Scope().withParent(testScope);
|
||||
List<Variable> variables = fun.getArgs().stream().map(functionScope::create).collect(Collectors.toList());
|
||||
fun.getArgs().forEach(functionScope::addInvocationVariable);
|
||||
|
||||
try {
|
||||
tester.registerFunction(id, new UserDefinedFunction(tester.parse(fun.getFunction(), functionScope), variables));
|
||||
tester.registerFunction(id, new UserDefinedFunction(tester.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
|
||||
} catch(ParseException e) {
|
||||
throw new ValidationException("Invalid function: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
System.out.println("Testing " + id);
|
||||
tester.parse(noiseEquation, testScope);
|
||||
} catch(ParseException e) {
|
||||
throw new ValidationException("Invalid noise equation: ", e);
|
||||
|
||||
@@ -24,6 +24,7 @@ public class TerraWorld {
|
||||
|
||||
|
||||
public TerraWorld(World w, ConfigPack c, TerraPlugin main) {
|
||||
c.getBiomeRegistry().forEach(biome -> biome.getGenerator(w)); // Load all gens to cache
|
||||
config = c;
|
||||
profiler = new WorldProfiler(w);
|
||||
this.provider = config.getTemplate().getProviderBuilder().build(w.getSeed());
|
||||
|
||||
@@ -33,7 +33,7 @@ public class TerraCommand extends Command {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "com/dfsek/terra";
|
||||
return "terra";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user