Search in sources :

Example 1 with Expression

use of org.mariuszgromada.math.mxparser.Expression 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 Expression

use of org.mariuszgromada.math.mxparser.Expression in project AdvancedChatBox by DarkKronicle.

the class CalculatorSuggestor method suggest.

@Override
public Optional<List<AdvancedSuggestions>> suggest(String text) {
    if (!text.contains("[") || !text.contains("]")) {
        return Optional.empty();
    }
    List<StringMatch> matches = SearchUtils.findMatches(text, BRACKET_REGEX, FindType.REGEX).orElse(null);
    if (matches == null) {
        return Optional.empty();
    }
    int last = -1;
    ArrayList<AdvancedSuggestions> suggest = new ArrayList<>();
    for (StringMatch m : matches) {
        if (m.start < last || m.end - m.start < 1) {
            // Don't want overlapping matches (just in case) or too small
            continue;
        }
        last = m.end;
        String string = m.match.substring(1, m.match.length() - 1);
        Expression expression = new Expression(string);
        double val = expression.calculate();
        String message = NAN;
        if (!Double.isNaN(val)) {
            message = String.valueOf(val);
        }
        StringRange range = new StringRange(m.start, m.end);
        suggest.add(new AdvancedSuggestions(range, new ArrayList<>(Collections.singleton(new AdvancedSuggestion(range, message)))));
    }
    if (suggest.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(suggest);
}
Also used : Expression(org.mariuszgromada.math.mxparser.Expression) ArrayList(java.util.ArrayList) AdvancedSuggestion(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion) StringMatch(io.github.darkkronicle.advancedchatcore.util.StringMatch) AdvancedSuggestions(io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions) StringRange(com.mojang.brigadier.context.StringRange)

Aggregations

Expression (org.mariuszgromada.math.mxparser.Expression)2 Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)1 StringRange (com.mojang.brigadier.context.StringRange)1 AdvancedSuggestion (io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestion)1 AdvancedSuggestions (io.github.darkkronicle.advancedchatbox.chat.AdvancedSuggestions)1 StringMatch (io.github.darkkronicle.advancedchatcore.util.StringMatch)1 ArrayList (java.util.ArrayList)1 BlockState (net.minecraft.block.BlockState)1 BlockPos (net.minecraft.util.math.BlockPos)1 Argument (org.mariuszgromada.math.mxparser.Argument)1