Search in sources :

Example 1 with ProgWidgetCoordinateOperator

use of me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinateOperator in project pnc-repressurized by TeamPneumatic.

the class GuiProgrammer method generateRelativeOperators.

/**
 * @param baseWidget
 * @param simulate
 * @return true if successful
 */
private boolean generateRelativeOperators(ProgWidgetCoordinateOperator baseWidget, List<String> tooltip, boolean simulate) {
    BlockPos baseCoord = ProgWidgetCoordinateOperator.calculateCoordinate(baseWidget, 0, baseWidget.getOperator());
    Map<BlockPos, String> offsetToVariableNames = new HashMap<>();
    for (IProgWidget widget : te.progWidgets) {
        if (widget instanceof ProgWidgetArea) {
            ProgWidgetArea area = (ProgWidgetArea) widget;
            if (area.getCoord1Variable().equals("") && (area.x1 != 0 || area.y1 != 0 || area.z1 != 0)) {
                BlockPos offset = new BlockPos(area.x1 - baseCoord.getX(), area.y1 - baseCoord.getY(), area.z1 - baseCoord.getZ());
                String var = getOffsetVariable(offsetToVariableNames, baseWidget.getVariable(), offset);
                if (!simulate)
                    area.setCoord1Variable(var);
            }
            if (area.getCoord2Variable().equals("") && (area.x2 != 0 || area.y2 != 0 || area.z2 != 0)) {
                BlockPos offset = new BlockPos(area.x2 - baseCoord.getX(), area.y2 - baseCoord.getY(), area.z2 - baseCoord.getZ());
                String var = getOffsetVariable(offsetToVariableNames, baseWidget.getVariable(), offset);
                if (!simulate)
                    area.setCoord2Variable(var);
            }
        } else if (widget instanceof ProgWidgetCoordinate && baseWidget.getConnectedParameters()[0] != widget) {
            ProgWidgetCoordinate coordinate = (ProgWidgetCoordinate) widget;
            if (!coordinate.isUsingVariable()) {
                BlockPos c = coordinate.getCoordinate();
                String chunkString = "(" + c.getX() + ", " + c.getY() + ", " + c.getZ() + ")";
                if (PneumaticCraftUtils.distBetween(c, 0, 0, 0) < 64) {
                    // When the coordinate value is close to 0, there's a low chance it means a position, and rather an offset.
                    if (tooltip != null)
                        tooltip.add(I18n.format("gui.programmer.button.convertToRelative.coordIsNotChangedWarning", chunkString));
                } else {
                    if (tooltip != null)
                        tooltip.add(I18n.format("gui.programmer.button.convertToRelative.coordIsChangedWarning", chunkString));
                    if (!simulate) {
                        BlockPos offset = new BlockPos(c.getX() - baseCoord.getX(), c.getY() - baseCoord.getY(), c.getZ() - baseCoord.getZ());
                        String var = getOffsetVariable(offsetToVariableNames, baseWidget.getVariable(), offset);
                        coordinate.setVariable(var);
                        coordinate.setUsingVariable(true);
                    }
                }
            }
        }
    }
    if (offsetToVariableNames.size() > 0) {
        ProgWidgetCoordinateOperator firstOperator = null;
        ProgWidgetCoordinateOperator prevOperator = baseWidget;
        int x = baseWidget.getX();
        for (Map.Entry<BlockPos, String> entry : offsetToVariableNames.entrySet()) {
            ProgWidgetCoordinateOperator operator = new ProgWidgetCoordinateOperator();
            operator.setVariable(entry.getValue());
            int y = prevOperator.getY() + prevOperator.getHeight() / 2;
            operator.setX(x);
            operator.setY(y);
            if (!isValidPlaced(operator))
                return false;
            ProgWidgetCoordinate coordinatePiece1 = new ProgWidgetCoordinate();
            coordinatePiece1.setX(x + prevOperator.getWidth() / 2);
            coordinatePiece1.setY(y);
            coordinatePiece1.setVariable(baseWidget.getVariable());
            coordinatePiece1.setUsingVariable(true);
            if (!isValidPlaced(coordinatePiece1))
                return false;
            ProgWidgetCoordinate coordinatePiece2 = new ProgWidgetCoordinate();
            coordinatePiece2.setX(x + prevOperator.getWidth() / 2 + coordinatePiece1.getWidth() / 2);
            coordinatePiece2.setY(y);
            coordinatePiece2.setCoordinate(entry.getKey());
            if (!isValidPlaced(coordinatePiece2))
                return false;
            if (!simulate) {
                te.progWidgets.add(operator);
                te.progWidgets.add(coordinatePiece1);
                te.progWidgets.add(coordinatePiece2);
            }
            if (firstOperator == null)
                firstOperator = operator;
            prevOperator = operator;
        }
        if (!simulate) {
            NetworkHandler.sendToServer(new PacketProgrammerUpdate(te));
            TileEntityProgrammer.updatePuzzleConnections(te.progWidgets);
        }
        return true;
    } else {
        // When there's nothing to place there's always room.
        return true;
    }
}
Also used : IProgWidget(me.desht.pneumaticcraft.common.progwidgets.IProgWidget) HashMap(java.util.HashMap) ProgWidgetArea(me.desht.pneumaticcraft.common.progwidgets.ProgWidgetArea) ProgWidgetCoordinateOperator(me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinateOperator) BlockPos(net.minecraft.util.math.BlockPos) PacketProgrammerUpdate(me.desht.pneumaticcraft.common.network.PacketProgrammerUpdate) ProgWidgetCoordinate(me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinate) Map(java.util.Map) HashMap(java.util.HashMap) Point(java.awt.Point)

Example 2 with ProgWidgetCoordinateOperator

use of me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinateOperator in project pnc-repressurized by TeamPneumatic.

the class GuiProgrammer method updateConvertRelativeState.

private void updateConvertRelativeState() {
    convertToRelativeButton.enabled = false;
    List<String> tooltip = new ArrayList<>();
    tooltip.add("gui.programmer.button.convertToRelative.desc");
    boolean startFound = false;
    for (IProgWidget startWidget : te.progWidgets) {
        if (startWidget instanceof ProgWidgetStart) {
            startFound = true;
            IProgWidget widget = startWidget.getOutputWidget();
            if (widget instanceof ProgWidgetCoordinateOperator) {
                ProgWidgetCoordinateOperator operatorWidget = (ProgWidgetCoordinateOperator) widget;
                if (!operatorWidget.getVariable().equals("")) {
                    try {
                        if (generateRelativeOperators(operatorWidget, tooltip, true)) {
                            convertToRelativeButton.enabled = true;
                        } else {
                            tooltip.add("gui.programmer.button.convertToRelative.notEnoughRoom");
                        }
                    } catch (NullPointerException e) {
                        tooltip.add("gui.programmer.button.convertToRelative.cantHaveVariables");
                    }
                } else {
                    tooltip.add("gui.programmer.button.convertToRelative.noVariableName");
                }
            } else {
                tooltip.add("gui.programmer.button.convertToRelative.noBaseCoordinate");
            }
        }
    }
    if (!startFound)
        tooltip.add("gui.programmer.button.convertToRelative.noStartPiece");
    List<String> localizedTooltip = new ArrayList<>();
    for (String s : tooltip) {
        localizedTooltip.addAll(PneumaticCraftUtils.convertStringIntoList(I18n.format(s), 40));
    }
    convertToRelativeButton.setTooltipText(localizedTooltip);
}
Also used : IProgWidget(me.desht.pneumaticcraft.common.progwidgets.IProgWidget) ProgWidgetCoordinateOperator(me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinateOperator) ArrayList(java.util.ArrayList) ProgWidgetStart(me.desht.pneumaticcraft.common.progwidgets.ProgWidgetStart)

Aggregations

IProgWidget (me.desht.pneumaticcraft.common.progwidgets.IProgWidget)2 ProgWidgetCoordinateOperator (me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinateOperator)2 Point (java.awt.Point)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PacketProgrammerUpdate (me.desht.pneumaticcraft.common.network.PacketProgrammerUpdate)1 ProgWidgetArea (me.desht.pneumaticcraft.common.progwidgets.ProgWidgetArea)1 ProgWidgetCoordinate (me.desht.pneumaticcraft.common.progwidgets.ProgWidgetCoordinate)1 ProgWidgetStart (me.desht.pneumaticcraft.common.progwidgets.ProgWidgetStart)1 BlockPos (net.minecraft.util.math.BlockPos)1