Search in sources :

Example 1 with SimplePredicate

use of com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate in project Multiblocked by CleanroomMC.

the class TemplateBuilderWidget method onSelected.

@SideOnly(Side.CLIENT)
public void onSelected(ItemStack itemStack, int slot) {
    if (this.selected != itemStack) {
        this.selected = itemStack;
        this.selectedSlot = slot;
        if (selected != null && isRemote()) {
            this.pos = null;
            this.facing = null;
            templateButton.setVisible(true);
            if (ItemBlueprint.isRaw(itemStack)) {
                BlockPos[] poses = ItemBlueprint.getPos(itemStack);
                World world = table.getWorld();
                sceneWidget.createScene(world);
                if (poses != null && world.isAreaLoaded(poses[0], poses[1])) {
                    Set<BlockPos> rendered = new HashSet<>();
                    for (int x = poses[0].getX(); x <= poses[1].getX(); x++) {
                        for (int y = poses[0].getY(); y <= poses[1].getY(); y++) {
                            for (int z = poses[0].getZ(); z <= poses[1].getZ(); z++) {
                                if (!world.isAirBlock(new BlockPos(x, y, z))) {
                                    rendered.add(new BlockPos(x, y, z));
                                }
                            }
                        }
                    }
                    sceneWidget.setRenderedCore(rendered, null);
                }
            } else if (itemStack.getSubCompound("pattern") != null) {
                String json = itemStack.getSubCompound("pattern").getString("json");
                JsonBlockPattern pattern = Multiblocked.GSON.fromJson(json, JsonBlockPattern.class);
                int[] centerOffset = pattern.getCenterOffset();
                String[][] patternString = pattern.pattern;
                Set<BlockPos> rendered = new HashSet<>();
                TrackedDummyWorld world = new TrackedDummyWorld();
                sceneWidget.createScene(world);
                int offset = Math.max(patternString.length, Math.max(patternString[0].length, patternString[0][0].length()));
                for (int i = 0; i < patternString.length; i++) {
                    for (int j = 0; j < patternString[0].length; j++) {
                        for (int k = 0; k < patternString[0][0].length(); k++) {
                            char symbol = patternString[i][j].charAt(k);
                            BlockPos pos = pattern.getActualPosOffset(k - centerOffset[2], j - centerOffset[1], i - centerOffset[0], EnumFacing.NORTH).add(offset, offset, offset);
                            world.addBlock(pos, new BlockInfo(MbdComponents.DummyComponentBlock));
                            DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getTileEntity(pos);
                            ComponentDefinition definition = null;
                            assert tileEntity != null;
                            if (pattern.symbolMap.containsKey(symbol)) {
                                Set<BlockInfo> candidates = new HashSet<>();
                                for (String s : pattern.symbolMap.get(symbol)) {
                                    SimplePredicate predicate = pattern.predicates.get(s);
                                    if (predicate instanceof PredicateComponent && ((PredicateComponent) predicate).definition != null) {
                                        definition = ((PredicateComponent) predicate).definition;
                                        break;
                                    } else if (predicate != null && predicate.candidates != null) {
                                        candidates.addAll(Arrays.asList(predicate.candidates.get()));
                                    }
                                }
                                definition = getComponentDefinition(definition, candidates);
                            }
                            if (definition != null) {
                                tileEntity.setDefinition(definition);
                            }
                            tileEntity.isFormed = false;
                            tileEntity.setWorld(world);
                            tileEntity.validate();
                            rendered.add(pos);
                        }
                    }
                }
                sceneWidget.setRenderedCore(rendered, null);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) World(net.minecraft.world.World) ItemBlueprint(com.cleanroommc.multiblocked.api.item.ItemBlueprint) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) JsonBlockPattern(com.cleanroommc.multiblocked.api.pattern.JsonBlockPattern) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) HashSet(java.util.HashSet) ComponentDefinition(com.cleanroommc.multiblocked.api.definition.ComponentDefinition) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 2 with SimplePredicate

use of com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate in project Multiblocked by CleanroomMC.

the class ControllerWidget method updateScene.

@SideOnly(Side.CLIENT)
private void updateScene(JsonBlockPattern jsonPattern) {
    if (thread != null) {
        thread.interrupt();
        thread = null;
    }
    TrackedDummyWorld world = new TrackedDummyWorld();
    tiles.clear();
    sceneWidget.createScene(world);
    ImageWidget imageWidget;
    sceneWidget.addWidget(imageWidget = new ImageWidget(0, 0, sceneWidget.getSize().width, sceneWidget.getSize().height));
    imageWidget.setVisible(jsonPattern.pattern.length * jsonPattern.pattern[0].length * jsonPattern.pattern[0][0].length() > 1000);
    thread = new Thread(() -> {
        int[] centerOffset = jsonPattern.getCenterOffset();
        String[][] pattern = jsonPattern.pattern;
        Set<BlockPos> posSet = new HashSet<>();
        int offset = Math.max(pattern.length, Math.max(pattern[0].length, pattern[0][0].length()));
        int sum = jsonPattern.pattern.length * jsonPattern.pattern[0].length * jsonPattern.pattern[0][0].length();
        AtomicDouble progress = new AtomicDouble(0);
        imageWidget.setImage(new TextTexture("building scene!").setSupplier(() -> "building scene! " + String.format("%.1f", progress.get()) + "%%").setWidth(sceneWidget.getSize().width));
        int count = 0;
        for (int i = 0; i < pattern.length; i++) {
            for (int j = 0; j < pattern[0].length; j++) {
                for (int k = 0; k < pattern[0][0].length(); k++) {
                    if (Thread.interrupted()) {
                        sceneWidget.waitToRemoved(imageWidget);
                        return;
                    }
                    count++;
                    progress.set(count * 100.0 / sum);
                    char symbol = pattern[i][j].charAt(k);
                    BlockPos pos = jsonPattern.getActualPosOffset(k - centerOffset[2], j - centerOffset[1], i - centerOffset[0], EnumFacing.NORTH).add(offset, offset, offset);
                    world.addBlock(pos, new BlockInfo(MbdComponents.DummyComponentBlock));
                    DummyComponentTileEntity tileEntity = (DummyComponentTileEntity) world.getTileEntity(pos);
                    ComponentDefinition definition = null;
                    assert tileEntity != null;
                    boolean disableFormed = false;
                    if (jsonPattern.symbolMap.containsKey(symbol)) {
                        Set<BlockInfo> candidates = new HashSet<>();
                        for (String s : jsonPattern.symbolMap.get(symbol)) {
                            SimplePredicate predicate = jsonPattern.predicates.get(s);
                            if (predicate instanceof PredicateComponent && ((PredicateComponent) predicate).definition != null) {
                                definition = ((PredicateComponent) predicate).definition;
                                disableFormed |= predicate.disableRenderFormed;
                                break;
                            } else if (predicate != null && predicate.candidates != null) {
                                candidates.addAll(Arrays.asList(predicate.candidates.get()));
                                disableFormed |= predicate.disableRenderFormed;
                            }
                        }
                        definition = TemplateBuilderWidget.getComponentDefinition(definition, candidates);
                    }
                    if (definition != null) {
                        tileEntity.setDefinition(definition);
                        if (disableFormed) {
                            definition.formedRenderer = new BlockStateRenderer(Blocks.AIR.getDefaultState());
                        }
                    }
                    tileEntity.isFormed = isFormed;
                    tileEntity.setWorld(world);
                    tileEntity.validate();
                    posSet.add(pos);
                    tiles.add(tileEntity);
                }
            }
        }
        Minecraft.getMinecraft().addScheduledTask(() -> {
            sceneWidget.setRenderedCore(posSet, null);
            sceneWidget.waitToRemoved(imageWidget);
        });
        thread = null;
    });
    thread.start();
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) TrackedDummyWorld(com.cleanroommc.multiblocked.client.util.TrackedDummyWorld) PredicateComponent(com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) BlockStateRenderer(com.cleanroommc.multiblocked.client.renderer.impl.BlockStateRenderer) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) BlockPos(net.minecraft.util.math.BlockPos) DummyComponentTileEntity(com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity) ComponentDefinition(com.cleanroommc.multiblocked.api.definition.ComponentDefinition) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 3 with SimplePredicate

use of com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate in project Multiblocked by CleanroomMC.

the class BlockPattern method getPreview.

public BlockInfo[][][] getPreview(int[] repetition) {
    Map<SimplePredicate, Integer> cacheGlobal = new HashMap<>();
    Map<BlockPos, BlockInfo> blocks = new HashMap<>();
    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int minZ = Integer.MAX_VALUE;
    int maxX = Integer.MIN_VALUE;
    int maxY = Integer.MIN_VALUE;
    int maxZ = Integer.MIN_VALUE;
    for (int l = 0, x = 0; l < this.fingerLength; l++) {
        for (int r = 0; r < repetition[l]; r++) {
            // Checking single slice
            for (int y = 0; y < this.thumbLength; y++) {
                for (int z = 0; z < this.palmLength; z++) {
                    TraceabilityPredicate predicate = this.blockMatches[l][y][z];
                    boolean find = false;
                    BlockInfo[] infos = null;
                    // check global and previewCount
                    for (SimplePredicate limit : predicate.limited) {
                        if (limit.minCount == -1 && limit.previewCount == -1)
                            continue;
                        if (cacheGlobal.getOrDefault(limit, 0) < limit.previewCount) {
                            if (!cacheGlobal.containsKey(limit)) {
                                cacheGlobal.put(limit, 1);
                            } else if (cacheGlobal.get(limit) < limit.previewCount) {
                                cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                            } else {
                                continue;
                            }
                        } else if (limit.minCount > 0) {
                            if (!cacheGlobal.containsKey(limit)) {
                                cacheGlobal.put(limit, 1);
                            } else if (cacheGlobal.get(limit) < limit.minCount) {
                                cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                            } else {
                                continue;
                            }
                        } else {
                            continue;
                        }
                        infos = limit.candidates == null ? null : limit.candidates.get();
                        find = true;
                        break;
                    }
                    if (!find) {
                        // check common with previewCount
                        for (SimplePredicate common : predicate.common) {
                            if (common.previewCount > 0) {
                                if (!cacheGlobal.containsKey(common)) {
                                    cacheGlobal.put(common, 1);
                                } else if (cacheGlobal.get(common) < common.previewCount) {
                                    cacheGlobal.put(common, cacheGlobal.get(common) + 1);
                                } else {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                            infos = common.candidates == null ? null : common.candidates.get();
                            find = true;
                            break;
                        }
                    }
                    if (!find) {
                        // check without previewCount
                        for (SimplePredicate common : predicate.common) {
                            if (common.previewCount == -1) {
                                infos = common.candidates == null ? null : common.candidates.get();
                                find = true;
                                break;
                            }
                        }
                    }
                    if (!find) {
                        // check max
                        for (SimplePredicate limit : predicate.limited) {
                            if (limit.previewCount != -1) {
                                continue;
                            } else if (limit.maxCount != -1) {
                                if (cacheGlobal.getOrDefault(limit, 0) < limit.maxCount) {
                                    if (!cacheGlobal.containsKey(limit)) {
                                        cacheGlobal.put(limit, 1);
                                    } else {
                                        cacheGlobal.put(limit, cacheGlobal.get(limit) + 1);
                                    }
                                } else {
                                    continue;
                                }
                            }
                            infos = limit.candidates == null ? null : limit.candidates.get();
                            break;
                        }
                    }
                    BlockInfo info = infos == null || infos.length == 0 ? BlockInfo.EMPTY : infos[0];
                    BlockPos pos = setActualRelativeOffset(z, y, x, EnumFacing.NORTH);
                    blocks.put(pos, info);
                    minX = Math.min(pos.getX(), minX);
                    minY = Math.min(pos.getY(), minY);
                    minZ = Math.min(pos.getZ(), minZ);
                    maxX = Math.max(pos.getX(), maxX);
                    maxY = Math.max(pos.getY(), maxY);
                    maxZ = Math.max(pos.getZ(), maxZ);
                }
            }
            x++;
        }
    }
    BlockInfo[][][] result = (BlockInfo[][][]) Array.newInstance(BlockInfo.class, maxX - minX + 1, maxY - minY + 1, maxZ - minZ + 1);
    int finalMinX = minX;
    int finalMinY = minY;
    int finalMinZ = minZ;
    blocks.forEach((pos, info) -> {
        if (info.getTileEntity() instanceof ComponentTileEntity<?>) {
            ComponentTileEntity<?> componentTileEntity = (ComponentTileEntity<?>) info.getTileEntity();
            boolean find = false;
            for (EnumFacing enumFacing : FACINGS) {
                if (componentTileEntity.isValidFrontFacing(enumFacing)) {
                    if (!blocks.containsKey(pos.offset(enumFacing))) {
                        componentTileEntity.setFrontFacing(enumFacing);
                        find = true;
                        break;
                    }
                }
            }
            if (!find) {
                for (EnumFacing enumFacing : FACINGS) {
                    BlockInfo blockInfo = blocks.get(pos.offset(enumFacing));
                    if (blockInfo != null && blockInfo.getBlockState().getBlock() == Blocks.AIR && componentTileEntity.isValidFrontFacing(enumFacing)) {
                        componentTileEntity.setFrontFacing(enumFacing);
                        break;
                    }
                }
            }
        }
        result[pos.getX() - finalMinX][pos.getY() - finalMinY][pos.getZ() - finalMinZ] = info;
    });
    return result;
}
Also used : HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) EnumFacing(net.minecraft.util.EnumFacing) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) ComponentTileEntity(com.cleanroommc.multiblocked.api.tile.ComponentTileEntity) BlockPos(net.minecraft.util.math.BlockPos)

Example 4 with SimplePredicate

use of com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate in project Multiblocked by CleanroomMC.

the class TraceabilityPredicate method setMinGlobalLimited.

/**
 * Set the minimum number of candidate blocks.
 */
@ZenMethod
public TraceabilityPredicate setMinGlobalLimited(int min) {
    limited.addAll(common);
    common.clear();
    for (SimplePredicate predicate : limited) {
        predicate.minCount = min;
    }
    return this;
}
Also used : SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) ZenMethod(stanhebben.zenscript.annotations.ZenMethod)

Example 5 with SimplePredicate

use of com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate in project Multiblocked by CleanroomMC.

the class TraceabilityPredicate method test.

public boolean test(MultiblockState blockWorldState) {
    blockWorldState.io = IO.BOTH;
    boolean flag = false;
    for (SimplePredicate predicate : limited) {
        if (predicate.testLimited(blockWorldState)) {
            flag = true;
        }
    }
    return flag || common.stream().anyMatch(predicate -> predicate.test(blockWorldState));
}
Also used : Arrays(java.util.Arrays) Predicate(java.util.function.Predicate) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ZenClass(stanhebben.zenscript.annotations.ZenClass) ArrayList(java.util.ArrayList) BlockInfo(com.cleanroommc.multiblocked.api.pattern.util.BlockInfo) OperatorType(stanhebben.zenscript.annotations.OperatorType) List(java.util.List) IO(com.cleanroommc.multiblocked.api.capability.IO) ZenOperator(stanhebben.zenscript.annotations.ZenOperator) ZenMethod(stanhebben.zenscript.annotations.ZenMethod) Comparator(java.util.Comparator) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate) ZenRegister(crafttweaker.annotations.ZenRegister) SimplePredicate(com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate)

Aggregations

SimplePredicate (com.cleanroommc.multiblocked.api.pattern.predicates.SimplePredicate)13 BlockInfo (com.cleanroommc.multiblocked.api.pattern.util.BlockInfo)8 BlockPos (net.minecraft.util.math.BlockPos)7 ArrayList (java.util.ArrayList)5 PredicateComponent (com.cleanroommc.multiblocked.api.pattern.predicates.PredicateComponent)4 TrackedDummyWorld (com.cleanroommc.multiblocked.client.util.TrackedDummyWorld)4 EnumFacing (net.minecraft.util.EnumFacing)4 World (net.minecraft.world.World)4 ComponentTileEntity (com.cleanroommc.multiblocked.api.tile.ComponentTileEntity)3 ControllerTileEntity (com.cleanroommc.multiblocked.api.tile.ControllerTileEntity)3 DummyComponentTileEntity (com.cleanroommc.multiblocked.api.tile.DummyComponentTileEntity)3 Long2ObjectOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 IBlockState (net.minecraft.block.state.IBlockState)3 ItemStack (net.minecraft.item.ItemStack)3 TileEntity (net.minecraft.tileentity.TileEntity)3 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)3