Search in sources :

Example 1 with Argument

use of org.mariuszgromada.math.mxparser.Argument in project Structurize by ldtteam.

the class Manager method generateRandomShape.

/**
 * Randomly generates shape based on an equation.
 *
 * @param height   the height.
 * @param width    the width.
 * @param length   the length.
 * @param equation the equation.
 * @param block    the block.
 * @return the created blueprint
 */
public static Blueprint generateRandomShape(final int height, final int width, final int length, final String equation, final BlockState block) {
    Expression e = new Expression(equation);
    final Argument argumentX = new Argument("x = 0");
    final Argument argumentY = new Argument("y = 0");
    final Argument argumentZ = new Argument("z = 0");
    final Argument argumentH = new Argument("h = " + height);
    final Argument argumentW = new Argument("w = " + width);
    final Argument argumentL = new Argument("l = " + length);
    e.addArguments(argumentX, argumentY, argumentZ, argumentH, argumentW, argumentL);
    final Map<BlockPos, BlockState> posList = new HashMap<>();
    for (double x = -length / 2.0; x <= length / 2; x++) {
        for (double y = -height / 2.0; y <= height / 2; y++) {
            for (double z = -width / 2.0; z <= width / 2; z++) {
                argumentX.setArgumentValue(x);
                argumentY.setArgumentValue(y);
                argumentZ.setArgumentValue(z);
                if (e.calculate() == 1) {
                    addPosToList(new BlockPos(x + length / 2.0, y + height / 2.0, z + width / 2.0), block, posList);
                }
            }
        }
    }
    final Blueprint blueprint = new Blueprint((short) (length + 1), (short) (height + 1), (short) (width + 1));
    posList.forEach(blueprint::addBlockState);
    return blueprint;
}
Also used : BlockState(net.minecraft.block.BlockState) Argument(org.mariuszgromada.math.mxparser.Argument) Expression(org.mariuszgromada.math.mxparser.Expression) Blueprint(com.ldtteam.structures.blueprints.v1.Blueprint) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with Argument

use of org.mariuszgromada.math.mxparser.Argument in project josson by octomix.

the class FuncArithmetic method funcCalc.

private static Double funcCalc(final JsonNode node, final Expression expression, final Map<String, String> args, final int index) {
    expression.removeAllArguments();
    for (Map.Entry<String, String> arg : args.entrySet()) {
        final String path = arg.getValue();
        if (path == null) {
            continue;
        }
        final JsonNode tryNode = getNodeByPath(node, index, path);
        if (!nodeHasValue(tryNode)) {
            return null;
        }
        expression.addArguments(new Argument(arg.getKey(), tryNode.asDouble()));
    }
    if (!expression.checkSyntax()) {
        for (String missingArg : expression.getMissingUserDefinedArguments()) {
            final JsonNode tryNode = getNodeByPath(node, index, missingArg);
            if (!nodeHasValue(tryNode)) {
                return null;
            }
            expression.addArguments(new Argument(missingArg, tryNode.asDouble()));
        }
    }
    if (expression.checkSyntax()) {
        return expression.calculate();
    }
    final StringBuilder sb = new StringBuilder("Calc syntax error.");
    if (expression.getMissingUserDefinedArguments().length > 0) {
        sb.append(" Missing arguments:").append(Arrays.toString(expression.getMissingUserDefinedArguments()));
    }
    if (expression.getMissingUserDefinedFunctions().length > 0) {
        sb.append(" Missing functions:").append(Arrays.toString(expression.getMissingUserDefinedFunctions()));
    }
    if (expression.getMissingUserDefinedUnits().length > 0) {
        sb.append(" Missing units:").append(Arrays.toString(expression.getMissingUserDefinedUnits()));
    }
    throw new IllegalArgumentException(sb.toString());
}
Also used : Argument(org.mariuszgromada.math.mxparser.Argument) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map)

Aggregations

Argument (org.mariuszgromada.math.mxparser.Argument)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)1 Map (java.util.Map)1 BlockState (net.minecraft.block.BlockState)1 BlockPos (net.minecraft.util.math.BlockPos)1 Expression (org.mariuszgromada.math.mxparser.Expression)1