Search in sources :

Example 6 with FunctionContext

use of buildcraft.lib.expression.FunctionContext 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 7 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeContainer method deserialize0.

@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    boolean scissor = resolveEquationBool(json, "limit", ctx, false);
    if (scissor) {
        IGuiArea area = resolveArea(json, "area", parent, ctx);
        return new GuiElementContainerScissor(gui, area);
    } else {
        IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
        return new GuiElementContainerResizing(gui, pos);
    }
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) GuiElementContainerScissor(buildcraft.lib.gui.elem.GuiElementContainerScissor) GuiElementContainerResizing(buildcraft.lib.gui.elem.GuiElementContainerResizing) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

Example 8 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ExpressionTester method testFunctions.

@Test
public void testFunctions() throws InvalidExpressionException {
    FunctionContext ctx = DefaultContexts.createWithAll();
    compileFuncLong(ctx, "one", "1");
    compileFuncLong(ctx, "same", "value", Argument.argLong("value"));
    compileFuncDouble(ctx, "same", "value", Argument.argDouble("value"));
    compileFuncDouble(ctx, "powertwo", "pow(2,input)", Argument.argDouble("input"));
    compileFuncDouble(ctx, "subtract", "l - r", Argument.argDouble("l"), Argument.argDouble("r"));
    compileFuncDouble(ctx, "tuple", "a + b + c", Argument.argDouble("a"), Argument.argDouble("b"), Argument.argDouble("c"));
    compileFuncDouble(ctx, "powlong", "pow((same(a + 1) - 1) , (same(b) * one()))", Argument.argDouble("a"), Argument.argDouble("b"));
    bakeAndCallDouble("one()", 1, ctx);
    bakeAndCallDouble("oNe()", 1, ctx);
    bakeAndCallDouble("same(0)", 0, ctx);
    bakeAndCallDouble("same(one())", 1, ctx);
    bakeAndCallDouble("same(pow(2,5))", 32, ctx);
    bakeAndCallDouble("powerTwo(5)", 32, ctx);
    bakeAndCallDouble("powertwo(6)", 64, ctx);
    bakeAndCallDouble("subtract(3, 1)", 2, ctx);
    bakeAndCallDouble("subtract(1, 3)", -2, ctx);
    bakeAndCallDouble("subtract(1, -3)", 4, ctx);
    bakeAndCallDouble("subtract(1, -3)", 4, ctx);
    bakeAndCallDouble("tuple(1, 2, 3)", 6, ctx);
    bakeAndCallDouble("tuple(3, 2, 1)", 6, ctx);
    bakeAndCallDouble("tuple(-7, 1, 0)", -6, ctx);
    bakeAndCallDouble("tuple(1, 3, 2)", 6, ctx);
    bakeAndCallDouble("powLong(3, 3)", 27, ctx);
}
Also used : FunctionContext(buildcraft.lib.expression.FunctionContext) Test(org.junit.Test)

Example 9 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class NodeCasting method castToType.

public static IExpressionNode castToType(IExpressionNode node, Class<?> to) throws InvalidExpressionException {
    Class<?> from = NodeTypes.getType(node);
    if (from == to) {
        return node;
    }
    FunctionContext castingContext = new FunctionContext(NodeTypes.getContext(from), NodeTypes.getContext(to));
    INodeFunc caster = castingContext.getFunction("(" + NodeTypes.getName(to) + ")", Collections.singletonList(from));
    if (caster == null) {
        if (to == String.class) {
            return new NodeCastToString(node);
        }
        throw new InvalidExpressionException("Cannot cast from " + NodeTypes.getName(from) + " to " + NodeTypes.getName(to));
    }
    NodeStack stack = new NodeStack(node);
    stack.setRecorder(Collections.singletonList(from), caster);
    IExpressionNode casted = caster.getNode(stack);
    stack.checkAndRemoveRecorder();
    Class<?> actual = NodeTypes.getType(casted);
    if (actual != to) {
        throw new IllegalStateException("The caster " + caster + " didn't produce the correct result! (Expected " + to + ", but got " + actual + ")");
    }
    return casted;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 10 with FunctionContext

use of buildcraft.lib.expression.FunctionContext 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)

Aggregations

FunctionContext (buildcraft.lib.expression.FunctionContext)26 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)8 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)7 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)6 NodeStack (buildcraft.lib.expression.NodeStack)5 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)5 ResourceLocation (net.minecraft.util.ResourceLocation)5 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)4 INodeFunc (buildcraft.lib.expression.api.INodeFunc)4 INodeStack (buildcraft.lib.expression.api.INodeStack)4 IButtonBehaviour (buildcraft.lib.gui.button.IButtonBehaviour)4 IButtonClickEventListener (buildcraft.lib.gui.button.IButtonClickEventListener)4 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)4 Test (org.junit.Test)4 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 GuiBC8 (buildcraft.lib.gui.GuiBC8)3 ISimpleDrawable (buildcraft.lib.gui.ISimpleDrawable)3 BuildCraftJsonGui (buildcraft.lib.gui.json.BuildCraftJsonGui)3 TypedKeyMap (buildcraft.lib.misc.collect.TypedKeyMap)3