rewrite auto workbench algorithm to stop dupe bugs and be smarter about item usage

This commit is contained in:
asiekierka 2015-04-13 19:40:44 +02:00
parent a11a25456c
commit 204ebfead0

View file

@ -56,6 +56,7 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory
private InventoryCraftResult craftResult = new InventoryCraftResult(); private InventoryCraftResult craftResult = new InventoryCraftResult();
private int[] bindings = new int[9]; private int[] bindings = new int[9];
private int[] bindingCounts = new int[9];
private int update = Utils.RANDOM.nextInt(); private int update = Utils.RANDOM.nextInt();
@ -326,6 +327,9 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory
private void updateCrafting() { private void updateCrafting() {
progress += UPDATE_TIME; progress += UPDATE_TIME;
for (int i = 0; i < 9; i++) {
bindingCounts[i] = 0;
}
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
ItemStack comparedStack = craftMatrix.getStackInSlot(i); ItemStack comparedStack = craftMatrix.getStackInSlot(i);
if (comparedStack == null || comparedStack.getItem() == null) { if (comparedStack == null || comparedStack.getItem() == null) {
@ -334,23 +338,45 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory
} }
if (bindings[i] == -1 || !StackHelper.isMatchingItem(inputInv.getStackInSlot(bindings[i]), comparedStack, true, true)) { if (bindings[i] == -1 || !StackHelper.isMatchingItem(inputInv.getStackInSlot(bindings[i]), comparedStack, true, true)) {
// Scan for a new binding
boolean found = false; boolean found = false;
for (int j = 0; j < 9; j++) { for (int j = 0; j < 9; j++) {
if (j == bindings[i]) { if (j == bindings[i]) {
continue; continue;
} }
if (StackHelper.isMatchingItem(inputInv.getStackInSlot(j), comparedStack, true, false)) { ItemStack inputInvStack = inputInv.getStackInSlot(j);
if (StackHelper.isMatchingItem(inputInvStack, comparedStack, true, false)
&& inputInvStack.stackSize > bindingCounts[j]) {
found = true; found = true;
bindings[i] = j; bindings[i] = j;
bindingCounts[j]++;
break; break;
} }
} }
if (!found) { if (!found) {
craftMatrix.isJammed = true;
progress = 0; progress = 0;
return; return;
} }
} else {
bindingCounts[bindings[i]]++;
}
}
for (int i = 0; i < 9; i++) {
if (bindingCounts[i] > 0) {
ItemStack stack = inputInv.getStackInSlot(i);
if (stack != null && stack.stackSize < bindingCounts[i]) {
// Do not break progress yet, instead give it a chance to rebuild
// It will quit when trying to find a valid binding to "fit in"
for (int j = 0; j < 9; j++) {
if (bindings[j] == i) {
bindings[j] = -1;
}
}
return;
}
} }
} }
@ -360,10 +386,12 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory
progress = 0; progress = 0;
craftMatrix.setUseBindings(true);
ItemStack result = craftMatrix.getRecipeOutput(); ItemStack result = craftMatrix.getRecipeOutput();
if (result != null && result.stackSize > 0) {
ItemStack resultInto = resultInv.getStackInSlot(0); ItemStack resultInto = resultInv.getStackInSlot(0);
craftMatrix.setUseBindings(true);
craftSlot.onPickupFromSlot(getInternalPlayer().get(), result); craftSlot.onPickupFromSlot(getInternalPlayer().get(), result);
if (resultInto == null) { if (resultInto == null) {
@ -371,6 +399,7 @@ public class TileAutoWorkbench extends TileBuildCraft implements ISidedInventory
} else { } else {
resultInto.stackSize += result.stackSize; resultInto.stackSize += result.stackSize;
} }
}
craftMatrix.setUseBindings(false); craftMatrix.setUseBindings(false);
craftMatrix.rebuildCache(); craftMatrix.rebuildCache();