From 882cce5ff2ff01e3bbe62a110ec25897d13a2eaf Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 28 Jul 2014 02:15:41 -0700 Subject: [PATCH] Add noise functions to the expression evaluator. --- .../expression/runtime/Functions.java | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sk89q/worldedit/internal/expression/runtime/Functions.java b/src/main/java/com/sk89q/worldedit/internal/expression/runtime/Functions.java index 636bcd8d0..1a5981855 100644 --- a/src/main/java/com/sk89q/worldedit/internal/expression/runtime/Functions.java +++ b/src/main/java/com/sk89q/worldedit/internal/expression/runtime/Functions.java @@ -19,11 +19,19 @@ package com.sk89q.worldedit.internal.expression.runtime; +import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.internal.expression.Expression; import com.sk89q.worldedit.internal.expression.runtime.Function.Dynamic; +import com.sk89q.worldedit.math.noise.PerlinNoise; +import com.sk89q.worldedit.math.noise.RidgedMultiFractalNoise; +import com.sk89q.worldedit.math.noise.VoronoiNoise; import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; /** * Contains all functions that can be used in expressions. @@ -375,6 +383,62 @@ public final class Functions { return random.nextInt((int) Math.floor(max.getValue())); } + private static final ThreadLocal localPerlin = new ThreadLocal() { + @Override + protected PerlinNoise initialValue() { + return new PerlinNoise(); + } + }; + + public static double perlin(RValue seed, RValue x, RValue y, RValue z, RValue frequency, RValue octaves, RValue persistence) throws EvaluationException { + PerlinNoise perlin = localPerlin.get(); + try { + perlin.setSeed((int) seed.getValue()); + perlin.setFrequency(frequency.getValue()); + perlin.setOctaveCount((int) octaves.getValue()); + perlin.setPersistence(persistence.getValue()); + } catch (IllegalArgumentException e) { + throw new EvaluationException(0, "Perlin noise error: " + e.getMessage()); + } + return perlin.noise(new Vector(x.getValue(), y.getValue(), z.getValue())); + } + + private static final ThreadLocal localVoronoi = new ThreadLocal() { + @Override + protected VoronoiNoise initialValue() { + return new VoronoiNoise(); + } + }; + + public static double voronoi(RValue seed, RValue x, RValue y, RValue z, RValue frequency) throws EvaluationException { + VoronoiNoise voronoi = localVoronoi.get(); + try { + voronoi.setSeed((int) seed.getValue()); + voronoi.setFrequency(frequency.getValue()); + } catch (IllegalArgumentException e) { + throw new EvaluationException(0, "Voronoi error: " + e.getMessage()); + } + return voronoi.noise(new Vector(x.getValue(), y.getValue(), z.getValue())); + } + + private static final ThreadLocal localRidgedMulti = new ThreadLocal() { + @Override + protected RidgedMultiFractalNoise initialValue() { + return new RidgedMultiFractalNoise(); + } + }; + + public static double ridgedmulti(RValue seed, RValue x, RValue y, RValue z, RValue frequency, RValue octaves) throws EvaluationException { + RidgedMultiFractalNoise ridgedMulti = localRidgedMulti.get(); + try { + ridgedMulti.setSeed((int) seed.getValue()); + ridgedMulti.setFrequency(frequency.getValue()); + ridgedMulti.setOctaveCount((int) octaves.getValue()); + } catch (IllegalArgumentException e) { + throw new EvaluationException(0, "Ridged multi error: " + e.getMessage()); + } + return ridgedMulti.noise(new Vector(x.getValue(), y.getValue(), z.getValue())); + } private static double queryInternal(RValue type, RValue data, double typeId, double dataValue) throws EvaluationException { // Compare to input values and determine return value