use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble in project BuildCraft by BuildCraft.
the class JsonModelRule method deserialize.
public static JsonModelRule deserialize(JsonElement json, FunctionContext fnCtx, ResourceLoaderContext ctx) {
if (!json.isJsonObject()) {
throw new JsonSyntaxException("Expected an object, got " + json);
}
JsonObject obj = json.getAsJsonObject();
String when = JsonUtils.getString(obj, "when");
INodeBoolean nodeWhen = JsonVariableModelPart.convertStringToBooleanNode(when, fnCtx);
String type = JsonUtils.getString(obj, "type");
if (type.startsWith("builtin:")) {
String builtin = type.substring("builtin:".length());
if ("rotate_facing".equals(builtin)) {
fnCtx = new FunctionContext(fnCtx, ExpressionCompat.ENUM_FACING);
String from = JsonUtils.getString(obj, "from");
INodeObject<EnumFacing> nodeFrom = JsonVariableModelPart.convertStringToObjectNode(from, fnCtx, EnumFacing.class);
String to = JsonUtils.getString(obj, "to");
INodeObject<EnumFacing> nodeTo = JsonVariableModelPart.convertStringToObjectNode(to, fnCtx, EnumFacing.class);
INodeDouble[] origin;
if (obj.has("origin")) {
origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
} else {
origin = RuleRotateFacing.DEFAULT_ORIGIN;
}
return new RuleRotateFacing(nodeWhen, nodeFrom, nodeTo, origin);
} else if ("rotate".equals(builtin)) {
INodeDouble[] origin;
if (obj.has("origin")) {
origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
} else {
origin = RuleRotate.DEFAULT_ORIGIN;
}
INodeDouble[] angles = JsonVariableModelPart.readVariablePosition(obj, "angle", fnCtx);
return new RuleRotate(nodeWhen, origin, angles);
} else if ("scale".equals(builtin)) {
INodeDouble[] origin;
if (obj.has("origin")) {
origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
} else {
origin = RuleRotate.DEFAULT_ORIGIN;
}
INodeDouble[] scales = JsonVariableModelPart.readVariablePosition(obj, "scale", fnCtx);
return new RuleScale(nodeWhen, origin, scales);
} else {
throw new JsonSyntaxException("Unknown built in rule type '" + builtin + "'");
}
} else {
throw new JsonSyntaxException("Unknown rule type '" + type + "'");
}
}
use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble in project BuildCraft by BuildCraft.
the class ElementType method resolvePosition.
public static IGuiPosition resolvePosition(JsonGuiElement json, String name, IGuiPosition parent, FunctionContext ctx) {
String eqn = json.properties.get(name);
if (eqn == null) {
INodeDouble x = getEquationDouble(json, name + "[0]", ctx);
INodeDouble y = getEquationDouble(json, name + "[1]", ctx);
return IGuiPosition.create(x, y).offset(parent);
}
try {
return GenericExpressionCompiler.compileExpressionObject(IGuiPosition.class, eqn, ctx).evaluate();
} catch (InvalidExpressionException e) {
throw new JsonSyntaxException("Failed to resolve a position for " + json.fullName, e);
}
}
use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble in project BuildCraft by BuildCraft.
the class ElementType method resolveArea.
public static IGuiArea resolveArea(JsonGuiElement json, String name, IGuiPosition parent, FunctionContext ctx) {
String eqn = json.properties.get(name);
if (eqn == null) {
INodeDouble x = getEquationDouble(json, name + "[0]", ctx);
INodeDouble y = getEquationDouble(json, name + "[1]", ctx);
INodeDouble w = getEquationDouble(json, name + "[2]", ctx);
INodeDouble h = getEquationDouble(json, name + "[3]", ctx);
return IGuiArea.create(x, y, w, h).offset(parent);
}
try {
return GenericExpressionCompiler.compileExpressionObject(IGuiArea.class, eqn, ctx).evaluate();
} catch (InvalidExpressionException e) {
throw new JsonSyntaxException("Failed to resolve an area for " + json.fullName, e);
}
}
use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble in project BuildCraft by BuildCraft.
the class NodeCasting method castToDouble.
public static INodeDouble castToDouble(IExpressionNode node) throws InvalidExpressionException {
if (node instanceof INodeDouble) {
return (INodeDouble) node;
}
Class<?> type = NodeTypes.getType(node);
FunctionContext ctx = NodeTypes.getContext(type);
if (ctx == null) {
throw new InvalidExpressionException("Cannot cast " + node + " to a double!");
}
INodeFunc func = ctx.getFunction("(double)", Collections.singletonList(type));
if (func == null || NodeTypes.getType(func) != double.class) {
throw new InvalidExpressionException("Cannot cast " + node + " to a double!");
}
return (INodeDouble) func.getNode(new NodeStack(node));
}
use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble in project BuildCraft by BuildCraft.
the class ExpressionTester method bakeAndCallDouble.
private static void bakeAndCallDouble(String function, double expected, FunctionContext ctx) {
ExpressionDebugManager.debugPrintln("Testing \"" + function + "\", expecting " + expected);
INodeDouble node = bakeFunctionDouble(function, ctx);
ExpressionDebugManager.debugPrintln("To " + node);
double got = node.evaluate();
Assert.assertEquals(expected, got, 0.0001);
}
Aggregations