Added query{,Abs,Rel}(x,y,z,typevar,datavar) to the expression parser.

This commit is contained in:
TomyLobo
2013-09-21 14:35:57 +02:00
parent eba1114b39
commit 8badb09399
5 changed files with 155 additions and 8 deletions

View File

@ -24,9 +24,12 @@ import java.util.List;
import java.util.Map;
import java.util.Stack;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.expression.lexer.Lexer;
import com.sk89q.worldedit.expression.lexer.tokens.Token;
import com.sk89q.worldedit.expression.parser.Parser;
import com.sk89q.worldedit.expression.runtime.ExpressionEnvironment;
import com.sk89q.worldedit.expression.runtime.Constant;
import com.sk89q.worldedit.expression.runtime.EvaluationException;
import com.sk89q.worldedit.expression.runtime.Functions;
@ -43,7 +46,7 @@ import com.sk89q.worldedit.expression.runtime.Variable;
* Arithmetic: +, -, *, /, % (modulo), ^ (power), - (unary), --, ++ (prefix only)
* Comparison: <=, >=, >, <, ==, !=, ~= (near)
*
* Supported functions: abs, acos, asin, atan, atan2, cbrt, ceil, cos, cosh, exp, floor, ln, log, log10, max, max, min, min, rint, round, sin, sinh, sqrt, tan, tanh
* Supported functions: abs, acos, asin, atan, atan2, cbrt, ceil, cos, cosh, exp, floor, ln, log, log10, max, max, min, min, rint, round, sin, sinh, sqrt, tan, tanh and more. (See the Functions class or the wiki)
*
* Constants: e, pi
*
@ -65,6 +68,7 @@ public class Expression {
private final String[] variableNames;
private RValue root;
private final Functions functions = new Functions();
private ExpressionEnvironment environment;
public static Expression compile(String expression, String... variableNames) throws ExpressionException {
return new Expression(expression, variableNames);
@ -157,4 +161,12 @@ public class Expression {
public Functions getFunctions() {
return functions;
}
public ExpressionEnvironment getEnvironment() {
return environment;
}
public void setEnvironment(ExpressionEnvironment environment) {
this.environment = environment;
}
}

View File

@ -0,0 +1,16 @@
package com.sk89q.worldedit.expression.runtime;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
/**
* Represents a way to access blocks in a world. Has to accept non-rounded coordinates.
*/
public interface ExpressionEnvironment {
int getBlockType(double x, double y, double z);
int getBlockData(double x, double y, double z);
int getBlockTypeAbs(double x, double y, double z);
int getBlockDataAbs(double x, double y, double z);
int getBlockTypeRel(double x, double y, double z);
int getBlockDataRel(double x, double y, double z);
}

View File

@ -375,4 +375,60 @@ public final class Functions {
public static double randint(RValue max) throws EvaluationException {
return random.nextInt((int) Math.floor(max.getValue()));
}
private static double queryInternal(LValue type, LValue data, double typeId, double dataValue) throws EvaluationException {
// Compare to input values and determine return value
final double ret = typeId == type.getValue() && typeId == type.getValue() ? 1.0 : 0.0;
type.assign(typeId);
data.assign(dataValue);
return ret;
}
@Dynamic
public static double query(RValue x, RValue y, RValue z, LValue type, LValue data) throws EvaluationException {
final double xp = x.getValue();
final double yp = y.getValue();
final double zp = z.getValue();
final ExpressionEnvironment environment = Expression.getInstance().getEnvironment();
// Read values from world
final double typeId = environment.getBlockType(xp, yp, zp);
final double dataValue = environment.getBlockData(xp, yp, zp);
return queryInternal(type, data, typeId, dataValue);
}
@Dynamic
public static double queryAbs(RValue x, RValue y, RValue z, LValue type, LValue data) throws EvaluationException {
final double xp = x.getValue();
final double yp = y.getValue();
final double zp = z.getValue();
final ExpressionEnvironment environment = Expression.getInstance().getEnvironment();
// Read values from world
final double typeId = environment.getBlockTypeAbs(xp, yp, zp);
final double dataValue = environment.getBlockDataAbs(xp, yp, zp);
return queryInternal(type, data, typeId, dataValue);
}
@Dynamic
public static double queryRel(RValue x, RValue y, RValue z, LValue type, LValue data) throws EvaluationException {
final double xp = x.getValue();
final double yp = y.getValue();
final double zp = z.getValue();
final ExpressionEnvironment environment = Expression.getInstance().getEnvironment();
// Read values from world
final double typeId = environment.getBlockTypeRel(xp, yp, zp);
final double dataValue = environment.getBlockDataRel(xp, yp, zp);
return queryInternal(type, data, typeId, dataValue);
}
}