mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-12 02:36:18 +00:00
Look ma, no Bukkit API in the core package
This commit is contained in:
29
common/src/main/java/com/dfsek/terra/math/BlankFunction.java
Normal file
29
common/src/main/java/com/dfsek/terra/math/BlankFunction.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import parsii.eval.Expression;
|
||||
import parsii.eval.Function;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BlankFunction implements Function {
|
||||
private final int args;
|
||||
|
||||
public BlankFunction(int args) {
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
return args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
20
common/src/main/java/com/dfsek/terra/math/MathUtil.java
Normal file
20
common/src/main/java/com/dfsek/terra/math/MathUtil.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import com.dfsek.terra.generation.Sampler;
|
||||
|
||||
public final class MathUtil {
|
||||
private static final double CONST = 0.55;
|
||||
|
||||
public static double derivative(Sampler sampler, double x, double y, double z) {
|
||||
double baseSample = sampler.sample(x, y, z);
|
||||
|
||||
double xVal1 = (sampler.sample(x + CONST, y, z) - baseSample) / CONST;
|
||||
double xVal2 = (sampler.sample(x - CONST, y, z) - baseSample) / CONST;
|
||||
double zVal1 = (sampler.sample(x, y, z + CONST) - baseSample) / CONST;
|
||||
double zVal2 = (sampler.sample(x, y, z - CONST) - baseSample) / CONST;
|
||||
double yVal1 = (sampler.sample(x, y + CONST, z) - baseSample) / CONST;
|
||||
double yVal2 = (sampler.sample(x, y - CONST, z) - baseSample) / CONST;
|
||||
|
||||
return Math.sqrt(((xVal2 - xVal1) * (xVal2 - xVal1)) + ((zVal2 - zVal1) * (zVal2 - zVal1)) + ((yVal2 - yVal1) * (yVal2 - yVal1)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import parsii.eval.Function;
|
||||
|
||||
public interface NoiseFunction extends Function {
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import com.dfsek.terra.api.gaea.math.FastNoiseLite;
|
||||
import com.dfsek.terra.generation.config.NoiseBuilder;
|
||||
import com.dfsek.terra.util.hash.HashMapDoubleDouble;
|
||||
import parsii.eval.Expression;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NoiseFunction2 implements NoiseFunction {
|
||||
private final FastNoiseLite gen;
|
||||
private final Cache cache = new Cache();
|
||||
|
||||
public NoiseFunction2(long seed, NoiseBuilder builder) {
|
||||
this.gen = builder.build((int) seed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static class Cache extends HashMapDoubleDouble {
|
||||
private static final long serialVersionUID = 8915092734723467010L;
|
||||
private static final int cacheSize = 384;
|
||||
|
||||
public double get(FastNoiseLite noise, double x, double z) {
|
||||
double xx = x >= 0 ? x * 2 : x * -2 - 1;
|
||||
double zz = z >= 0 ? z * 2 : z * -2 - 1;
|
||||
double key = (xx >= zz) ? (xx * xx + xx + zz) : (zz * zz + xx);
|
||||
double value = this.get(key);
|
||||
if(this.size() > cacheSize) {
|
||||
this.clear();
|
||||
}
|
||||
return (value == 4.9E-324D ? addAndReturn(noise.getNoise(x, z), key) : value);
|
||||
}
|
||||
|
||||
private double addAndReturn(double value, double key) {
|
||||
this.put(key, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import com.dfsek.terra.api.gaea.math.FastNoiseLite;
|
||||
import com.dfsek.terra.generation.config.NoiseBuilder;
|
||||
import parsii.eval.Expression;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NoiseFunction3 implements NoiseFunction {
|
||||
private final FastNoiseLite gen;
|
||||
|
||||
public NoiseFunction3(long seed, NoiseBuilder builder) {
|
||||
this.gen = builder.build((int) seed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
return gen.getNoise(list.get(0).evaluate(), list.get(1).evaluate(), list.get(2).evaluate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.dfsek.terra.math;
|
||||
|
||||
import com.dfsek.terra.api.gaea.util.FastRandom;
|
||||
import parsii.eval.Expression;
|
||||
import parsii.eval.Function;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Provides access to a PRNG ({@link com.dfsek.terra.api.gaea.util.FastRandom})
|
||||
* <p>
|
||||
* Takes 1 argument, which sets the seed
|
||||
*/
|
||||
public class RandomFunction implements Function {
|
||||
@Override
|
||||
public int getNumberOfArguments() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double eval(List<Expression> list) {
|
||||
long seed = (long) list.get(0).evaluate();
|
||||
return new FastRandom(seed).nextDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNaturalFunction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user