Search in sources :

Example 1 with GridInstance

use of mcjty.rftoolscontrol.logic.grid.GridInstance in project RFToolsControl by McJty.

the class GuiProgrammer method mergeProgram.

private void mergeProgram(ProgramCardInstance instance, GridPos pos) {
    // Find the left-most/top-most icon in this program
    GridPos leftTop = new GridPos(10000, 10000);
    int posx;
    int posy;
    if (pos == null) {
        posx = 0;
        posy = 0;
        leftTop = new GridPos(0, 0);
    } else {
        posx = pos.getX();
        posy = pos.getY();
        for (Map.Entry<GridPos, GridInstance> entry : instance.getGridInstances().entrySet()) {
            int x = entry.getKey().getX();
            int y = entry.getKey().getY();
            if (x < leftTop.getX()) {
                leftTop = entry.getKey();
            } else if (x == leftTop.getX() && y < leftTop.getY()) {
                leftTop = entry.getKey();
            }
        }
        if (leftTop.getX() > 1000) {
            // Nothing to do
            return;
        }
    }
    // Check if the program fits in the grid
    for (Map.Entry<GridPos, GridInstance> entry : instance.getGridInstances().entrySet()) {
        int x = entry.getKey().getX() - leftTop.getX() + posx;
        int y = entry.getKey().getY() - leftTop.getY() + posy;
        if (!checkValidGridPos(new GridPos(x, y))) {
            GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "No room for clipboard here!");
            return;
        }
        if (getHolder(x, y).getIcon() != null) {
            GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "No room for clipboard here!");
            return;
        }
    }
    // There is room
    for (Map.Entry<GridPos, GridInstance> entry : instance.getGridInstances().entrySet()) {
        int x = entry.getKey().getX() - leftTop.getX() + posx;
        int y = entry.getKey().getY() - leftTop.getY() + posy;
        loadGridInstance(entry, x, y);
    }
}
Also used : GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos)

Example 2 with GridInstance

use of mcjty.rftoolscontrol.logic.grid.GridInstance in project RFToolsControl by McJty.

the class ProgramValidator method validate.

public static List<Pair<GridPos, String>> validate(ProgramCardInstance program) {
    List<Pair<GridPos, String>> errors = new ArrayList<>();
    Map<GridPos, GridInstance> grid = program.getGridInstances();
    // Find all unreachable instances:
    Set<GridPos> reachableLocations = new HashSet<>();
    for (Map.Entry<GridPos, GridInstance> entry : grid.entrySet()) {
        GridInstance g = entry.getValue();
        if (g.getPrimaryConnection() != null) {
            reachableLocations.add(g.getPrimaryConnection().offset(entry.getKey()));
        }
        if (g.getSecondaryConnection() != null) {
            reachableLocations.add(g.getSecondaryConnection().offset(entry.getKey()));
        }
    }
    for (Map.Entry<GridPos, GridInstance> entry : grid.entrySet()) {
        GridInstance g = entry.getValue();
        Opcode opcode = Opcodes.OPCODES.get(g.getId());
        GridPos p = entry.getKey();
        if (!opcode.isEvent() && !reachableLocations.contains(p)) {
            errors.add(Pair.of(p, "Unreachable: " + p.getX() + "," + p.getY()));
        }
    }
    // Find all missing required parameters:
    for (Map.Entry<GridPos, GridInstance> entry : grid.entrySet()) {
        GridPos p = entry.getKey();
        GridInstance g = entry.getValue();
        Opcode opcode = Opcodes.OPCODES.get(g.getId());
        List<ParameterDescription> descriptions = opcode.getParameters();
        List<Parameter> parameters = g.getParameters();
        for (int i = 0; i < descriptions.size(); i++) {
            ParameterDescription desc = descriptions.get(i);
            Parameter par = i < parameters.size() ? parameters.get(i) : null;
            if (!desc.isOptional()) {
                if (par == null || par.getParameterValue() == null || (par.getParameterValue().isConstant() && par.getParameterValue().getValue() == null)) {
                    errors.add(Pair.of(p, "Missing parameter (" + desc.getName() + "): " + p.getX() + "," + p.getY()));
                }
            }
        }
    }
    return errors;
}
Also used : Opcode(mcjty.rftoolscontrol.api.code.Opcode) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with GridInstance

use of mcjty.rftoolscontrol.logic.grid.GridInstance in project RFToolsControl by McJty.

the class GuiProgrammer method loadGridInstance.

private void loadGridInstance(Map.Entry<GridPos, GridInstance> entry, int x, int y) {
    GridInstance gridInstance = entry.getValue();
    IIcon icon = ICONS.get(gridInstance.getId());
    if (icon == null) {
        // Ignore missing icon
        Logging.logError("Opcode with id '" + gridInstance.getId() + "' is missing!");
        return;
    }
    icon = icon.clone();
    if (gridInstance.getPrimaryConnection() != null) {
        icon.addOverlay(CONNECTION_ICONS.get(gridInstance.getPrimaryConnection()));
    }
    if (gridInstance.getSecondaryConnection() != null) {
        icon.addOverlay(CONNECTION_ICONS.get(gridInstance.getSecondaryConnection()));
    }
    Opcode opcode = Opcodes.OPCODES.get(icon.getID());
    List<Parameter> parameters = gridInstance.getParameters();
    for (int i = 0; i < parameters.size(); i++) {
        String name = opcode.getParameters().get(i).getName();
        icon.addData(name, parameters.get(i).getParameterValue());
    }
    loading = true;
    getHolder(x, y).setIcon(icon);
    loading = false;
}
Also used : GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) IIcon(mcjty.lib.gui.icons.IIcon) Opcode(mcjty.rftoolscontrol.api.code.Opcode) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter)

Example 4 with GridInstance

use of mcjty.rftoolscontrol.logic.grid.GridInstance in project RFToolsControl by McJty.

the class CompiledCard method compile.

public static CompiledCard compile(ProgramCardInstance cardInstance) {
    if (cardInstance == null) {
        return null;
    }
    CompiledCard card = new CompiledCard();
    Map<GridPos, GridInstance> gridInstances = cardInstance.getGridInstances();
    // First find the indices of all compiled grid instances
    Map<GridPos, Integer> posToIndex = new HashMap<>();
    for (Map.Entry<GridPos, GridInstance> entry : gridInstances.entrySet()) {
        GridPos location = entry.getKey();
        posToIndex.put(location, posToIndex.size());
    }
    // Index of a dummy stop opcode that we can use to go too in case there is no real connection
    int stopIdx = posToIndex.size();
    for (Map.Entry<GridPos, GridInstance> entry : gridInstances.entrySet()) {
        GridPos location = entry.getKey();
        GridInstance grid = entry.getValue();
        String id = grid.getId();
        Opcode opcode = Opcodes.OPCODES.get(id);
        GridPos primaryOutput = grid.getPrimaryConnection() != null ? grid.getPrimaryConnection().offset(location) : null;
        GridPos secondaryOutput = grid.getSecondaryConnection() != null ? grid.getSecondaryConnection().offset(location) : null;
        CompiledOpcode.Builder opcodeBuilder = CompiledOpcode.builder().opcode(opcode);
        opcodeBuilder.grid(location.getX(), location.getY());
        if (primaryOutput != null && posToIndex.containsKey(primaryOutput)) {
            opcodeBuilder.primaryIndex(posToIndex.get(primaryOutput));
        } else {
            opcodeBuilder.primaryIndex(stopIdx);
        }
        if (secondaryOutput != null && posToIndex.containsKey(secondaryOutput)) {
            opcodeBuilder.secondaryIndex(posToIndex.get(secondaryOutput));
        } else {
            opcodeBuilder.secondaryIndex(stopIdx);
        }
        List<ParameterDescription> parameters = opcode.getParameters();
        boolean single = false;
        for (int i = 0; i < grid.getParameters().size(); i++) {
            Parameter parameter = grid.getParameters().get(i);
            if (i < parameters.size() && "single".equals(parameters.get(i).getName())) {
                single = TypeConverters.convertToBool(parameter);
            }
            opcodeBuilder.parameter(parameter);
        }
        if (opcode.isEvent()) {
            card.events.putIfAbsent(opcode, new ArrayList<>());
            card.events.get(opcode).add(new CompiledEvent(card.opcodes.size(), single));
        }
        card.opcodes.add(opcodeBuilder.build());
    }
    card.opcodes.add(CompiledOpcode.builder().opcode(Opcodes.DO_STOP_OR_RESUME).build());
    for (Opcode opcode : Opcodes.OPCODES.values()) {
        if (!card.events.containsKey(opcode)) {
            card.events.put(opcode, Collections.emptyList());
        }
    }
    return card;
}
Also used : Opcode(mcjty.rftoolscontrol.api.code.Opcode) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Aggregations

GridInstance (mcjty.rftoolscontrol.logic.grid.GridInstance)4 Opcode (mcjty.rftoolscontrol.api.code.Opcode)3 Parameter (mcjty.rftoolscontrol.api.parameters.Parameter)3 GridPos (mcjty.rftoolscontrol.logic.grid.GridPos)3 ParameterDescription (mcjty.rftoolscontrol.api.parameters.ParameterDescription)2 IIcon (mcjty.lib.gui.icons.IIcon)1 Pair (org.apache.commons.lang3.tuple.Pair)1