Search in sources :

Example 1 with INodeDouble

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 + "'");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) EnumFacing(net.minecraft.util.EnumFacing) JsonObject(com.google.gson.JsonObject) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 2 with INodeDouble

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);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

Example 3 with INodeDouble

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);
    }
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble)

Example 4 with INodeDouble

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));
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 5 with INodeDouble

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);
}
Also used : INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble)

Aggregations

INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)12 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)6 FunctionContext (buildcraft.lib.expression.FunctionContext)5 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)4 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)4 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)4 NodeStack (buildcraft.lib.expression.NodeStack)3 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 INodeFunc (buildcraft.lib.expression.api.INodeFunc)3 INodeStack (buildcraft.lib.expression.api.INodeStack)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 NodeConstantDouble (buildcraft.lib.expression.node.value.NodeConstantDouble)2 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)2 ISprite (buildcraft.api.core.render.ISprite)1 SpriteRaw (buildcraft.lib.client.sprite.SpriteRaw)1 SubSpriteChanging (buildcraft.lib.client.sprite.SubSpriteChanging)1 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)1 INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)1 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)1 IVariableNode (buildcraft.lib.expression.api.IVariableNode)1