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);
}
}
}
}
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();
}
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;
}
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;
}
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));
}
Aggregations