From 8b4140ef08b50ac2e5ff96a6f0fd68a4148801c4 Mon Sep 17 00:00:00 2001 From: Towdium Date: Mon, 29 Feb 2016 19:19:05 +0800 Subject: [PATCH] Almost done with the new calculating core --- .../core/Calculator.java | 300 ++---------------- .../core/CostRecord.java | 196 ++++++++++++ .../core/ItemRecord.java | 26 ++ .../gui/guis/calculator/GuiCalculator.java | 22 +- 4 files changed, 260 insertions(+), 284 deletions(-) create mode 100644 src/main/java/pers/towdium/justEnoughCalculation/core/CostRecord.java diff --git a/src/main/java/pers/towdium/justEnoughCalculation/core/Calculator.java b/src/main/java/pers/towdium/justEnoughCalculation/core/Calculator.java index 29911e6..510e42c 100644 --- a/src/main/java/pers/towdium/justEnoughCalculation/core/Calculator.java +++ b/src/main/java/pers/towdium/justEnoughCalculation/core/Calculator.java @@ -1,299 +1,53 @@ package pers.towdium.justEnoughCalculation.core; -import com.google.common.collect.ImmutableList; import net.minecraft.item.ItemStack; import pers.towdium.justEnoughCalculation.JustEnoughCalculation; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; /** * @author Towdium + * This is calculating core v3.0, I call it global coordinating calculation XD. + * The procudure might be more complex, but can avoid some unexpected results. */ public class Calculator { - List operateRecords; - OperateRecord currentOperate; - List calculators; - List catalystBuffer; - ItemRecord itemRecord; + List costRecords; - - public Calculator (){} - - public Calculator (ItemStack itemStack, int amount){ - this(new ItemRecord(itemStack, amount, false), new LinkedList(), new LinkedList(), true); - } - - public Calculator (ItemRecord itemRecord, List operateRecords, List catalystBuffer, boolean exceedMode){ - this.catalystBuffer = catalystBuffer; - this.itemRecord = itemRecord; - this.operateRecords = operateRecords; - for (Recipe recipe : JustEnoughCalculation.proxy.getPlayerHandler().getAllRecipeOf(new ItemStack(itemRecord.item,1, itemRecord.meta), null)){ - if(checkRecipeValid(recipe)){ - long count = getOperateCount(recipe, exceedMode); - this.currentOperate = new OperateRecord(recipe, count); - ImmutableList.Builder builder = new ImmutableList.Builder<>(); - builder.addAll(operateRecords); - builder.add(currentOperate); - ImmutableList records = builder.build(); - ImmutableList.Builder calculatorBuilder = new ImmutableList.Builder<>(); - for(ItemStack itemStack : recipe.input){ - if(itemStack != null){ - calculatorBuilder.add(new Calculator( - new ItemRecord(itemStack, count*recipe.getInputAmount(itemStack), false), records, catalystBuffer, true - )); + public Calculator(ItemStack itemStack, long amount){ + costRecords = new ArrayList<>(); + costRecords.add(new CostRecord(new ItemRecord(itemStack, amount, false))); + List cancellableItems = costRecords.get(0).getCancellableItems(); + LOOP2: + while(cancellableItems.size() != 0){ + // all the items possible tp cancel + for(ItemRecord itemRecord : cancellableItems){ + // all the recipes for one item + LOOP1: + for(Recipe recipe : JustEnoughCalculation.proxy.getPlayerHandler().getAllRecipeOf(itemRecord.toItemStack(), null)){ + CostRecord record = new CostRecord(costRecords.get(costRecords.size()-1), new CostRecord(recipe, getCount(itemRecord, recipe))); + for(CostRecord costRecord : costRecords){ + if(costRecord.equals(record)){ + continue LOOP1; + } + costRecords.add(record); + cancellableItems = record.getCancellableItems(); + continue LOOP2; } } - this.calculators = calculatorBuilder.build(); - return; } + break; } - catalystBuffer.add(itemRecord); - } - - @Override - public String toString() { - return "Calculator:" + itemRecord.item.getUnlocalizedName() + " x " + itemRecord.amount; } public CostRecord getCost(){ - if(currentOperate != null){ - CostRecord record = getCurrentCost(); - for(Calculator calculator : calculators){ - if(calculator.currentOperate != null){ - CostRecord buffer = calculator.getCost(); - record.merge(buffer); - } - } - return record; - }else { - return new CostRecord(); - } + return costRecords.get(costRecords.size()-1); } - public CostRecord getCurrentCost(){ - List output = new LinkedList<>(); - List input = new LinkedList<>(); - for(ItemStack itemStack : currentOperate.recipe.output){ - if(itemStack != null){ - output.add(new ItemRecord(itemStack, currentOperate.count*ItemStackWrapper.getUnifiedAmount(itemStack), false)); - } - } - for(ItemStack itemStack : currentOperate.recipe.input){ - if(itemStack != null){ - input.add(new ItemRecord(itemStack, currentOperate.count*ItemStackWrapper.getUnifiedAmount(itemStack), false)); - } - } - return new Calculator.CostRecord(output, input); + protected long getCount(ItemRecord itemRecord, Recipe recipe){ + long a = recipe.getOutputAmount(itemRecord.toItemStack()); + return (long) ((double)itemRecord.amount/a + 0.5f); } - public CostRecord getEmptyCost(){ - return new CostRecord(); - } - protected boolean checkRecipeValid(Recipe recipe){ - for (OperateRecord record : operateRecords){ - if(record.recipe.equals(recipe) && record.count == getOperateCount(recipe)){ - return false; - } - } - return true; - } - - protected long getOperateCount(Recipe recipe){ - return (int)Math.ceil((float)itemRecord.amount/itemRecord.getOutputAmount(recipe)); - } - - protected long getOperateCount(Recipe recipe, boolean exceed){ - if(exceed){ - return (int)Math.ceil((float)itemRecord.amount/itemRecord.getOutputAmount(recipe)); - } else { - return itemRecord.amount/itemRecord.getOutputAmount(recipe); - } - - - } - - public class CostRecord{ - List output; - List input; - List catalyst; - - public CostRecord(){ - output = new ArrayList<>(); - input = new ArrayList<>(); - catalyst = new ArrayList<>(); - } - - public CostRecord(List output, List input) { - this.output = output; - this.input = input; - } - - public void merge(CostRecord costRecord){ - LOOP1: - for(ItemRecord record : costRecord.output){ - for(ItemRecord record1 : output){ - if(record1.add(record)) - continue LOOP1; - } - output.add(record); - } - LOOP2: - for(ItemRecord record : costRecord.input){ - for(ItemRecord record1 : input){ - if(record1.add(record)) - continue LOOP2; - } - input.add(record); - } - } - - public void unify(){ - cancelIO(); - for (int i = 0; i < output.size(); i++) { - ItemRecord itemRecord = output.get(i); - if (!itemRecord.isSameType(Calculator.this.itemRecord)) { - CostRecord record = new Calculator(itemRecord, new LinkedList(), new LinkedList(), true).getCost(); - record.cancelIO(); - if (this.canCancelBy(record)) { - List buffer = record.input; - record.input = record.output; - record.output = buffer; - this.merge(record); - this.cancelIO(); - } else { - record = new Calculator(itemRecord, new LinkedList(), new LinkedList(), false).getCost(); - record.cancelIO(); - if(this.canCancelBy(record)) { - List buffer = record.input; - record.input = record.output; - record.output = buffer; - this.merge(record); - this.cancelIO(); - } - } - } - } - cancelEmpty(); - } - - protected boolean canCancelBy(CostRecord record){ - LOOP1: - for(ItemRecord itemRecIn : record.output){ - for(ItemRecord itemRec : output){ - if(itemRec.isSameType(itemRecIn) && itemRec.amount>=itemRecIn.amount){ - continue LOOP1; - } - } - return false; - } - LOOP2: - for(ItemRecord itemRecIn : record.input){ - for(ItemRecord itemRec : input){ - if(itemRec.isSameType(itemRecIn) && itemRec.amount>=itemRecIn.amount){ - continue LOOP2; - } - } - return false; - } - return true; - } - - protected void cancelIO(){ - ImmutableList.Builder catalystBuilder = new ImmutableList.Builder<>(); - if(catalyst != null){ - catalystBuilder.addAll(catalyst); - } - for (ItemRecord inputRec : input) { - for (ItemRecord outputRec : output) { - if (inputRec.cancel(outputRec) && inputRec.amount == 0) { - LOOP: - for (ItemRecord itemRecord : Calculator.this.catalystBuffer){ - if(itemRecord.isSameType(inputRec)){ - if(catalyst != null){ - for (ItemRecord exist : catalyst) { - if (itemRecord.isSameType(exist)) { - continue LOOP; - } - } - } - catalystBuilder.add(inputRec.copy()); - break; - } - } - } - } - } - catalyst = catalystBuilder.build(); - } - - protected void cancelEmpty(){ - ImmutableList.Builder builder = new ImmutableList.Builder<>(); - for(ItemRecord record : output){ - if(record.amount != 0){ - builder.add(record); - } - } - output = builder.build(); - builder = new ImmutableList.Builder<>(); - for(ItemRecord record : input){ - if(record.amount != 0){ - builder.add(record); - } - } - input = builder.build(); - } - - public ItemStack[] getOutputStack(){ - ItemStack[] outputStack = new ItemStack[output.size()]; - int i = 0; - for (ItemRecord record : output){ - outputStack[i] = record.toItemStack(); - i++; - } - return outputStack; - } - - public ItemStack[] getInputStack(){ - ItemStack[] inputStack = new ItemStack[input.size()]; - int i = 0; - for (ItemRecord record : input){ - inputStack[i] = record.toItemStack(); - i++; - } - return inputStack; - } - - public ItemStack[] getCatalystStack(){ - ItemStack[] catalystStack = new ItemStack[catalyst.size()]; - int i = 0; - for (ItemRecord record : catalyst){ - catalystStack[i] = record.toItemStack(); - i++; - } - return catalystStack; - } - } } - -class OperateRecord{ - Recipe recipe; - long count; - - public OperateRecord(Recipe recipe, long count) { - this.recipe = recipe; - this.count = count; - } - - @Override - public boolean equals(Object obj) { - if(!(obj instanceof OperateRecord)){ - return false; - } else { - OperateRecord record = (OperateRecord) obj; - return record.recipe.equals(recipe) && record.count == count; - } - } -} \ No newline at end of file diff --git a/src/main/java/pers/towdium/justEnoughCalculation/core/CostRecord.java b/src/main/java/pers/towdium/justEnoughCalculation/core/CostRecord.java new file mode 100644 index 0000000..0f403c8 --- /dev/null +++ b/src/main/java/pers/towdium/justEnoughCalculation/core/CostRecord.java @@ -0,0 +1,196 @@ +package pers.towdium.justEnoughCalculation.core; + +import com.google.common.collect.ImmutableList; +import net.minecraft.item.ItemStack; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * @author Towdium + */ +public class CostRecord{ + List output; + List input; + List catalyst; + + public CostRecord(ItemRecord itemRecord){ + output = new ArrayList<>(); + input = new ArrayList<>(); + catalyst = new LinkedList<>(); + input.add(itemRecord); + } + + public CostRecord(Recipe recipe, long count){ + output = new ArrayList<>(); + input = new ArrayList<>(); + catalyst = new LinkedList<>(); + for(ItemStack itemStack : recipe.output){ + if(itemStack != null){ + output.add(new ItemRecord(itemStack, count)); + } + } + for(ItemStack itemStack : recipe.input){ + if(itemStack != null){ + input.add(new ItemRecord(itemStack, count)); + catalyst.add(new ItemRecord(itemStack, count)); + } + } + } + + /** + * Generate CostRecord merging two CostRecords + * ItemRecord with amount 0 may be left there + */ + public CostRecord(CostRecord costRecord1, CostRecord costRecord2){ + output = new ArrayList<>(); + output.addAll(costRecord1.output); + input = new ArrayList<>(); + input.addAll(costRecord1.input); + catalyst = new LinkedList<>(); + catalyst.addAll(costRecord1.catalyst); + // merge output + LOOP: + for(ItemRecord itemRecordAdd : costRecord2.output){ + for(ItemRecord itemRecordExt : output){ + if(itemRecordAdd.isSameType(itemRecordExt)){ + itemRecordExt.add(itemRecordAdd); + continue LOOP; + } + } + output.add(itemRecordAdd); + } + // merge input + LOOP: + for(ItemRecord itemRecordAdd : costRecord2.input){ + for(ItemRecord itemRecordExt : input){ + if(itemRecordAdd.isSameType(itemRecordExt)){ + itemRecordExt.add(itemRecordAdd); + continue LOOP; + } + } + input.add(itemRecordAdd); + } + // cancel catalyst by output + LOOP: + for(ItemRecord itemRecordAdd : costRecord2.output){ + for (int i = 0; i < catalyst.size(); i++) { + ItemRecord itemRecordExt = catalyst.get(i); + if (itemRecordAdd.isSameType(itemRecordExt) && itemRecordExt.amount <= itemRecordAdd.amount) { + catalyst.remove(i); + continue LOOP; + }else if(itemRecordAdd.isSameType(itemRecordExt)) { + itemRecordExt.amount -= itemRecordAdd.amount; + continue LOOP; + } + } + } + // merge catalyst + LOOP: + for(ItemRecord itemRecordAdd : costRecord2.catalyst){ + for (ItemRecord itemRecordExt : catalyst) { + if (itemRecordAdd.isSameType(itemRecordExt)) { + itemRecordExt.amount += itemRecordAdd.amount; + continue LOOP; + } + } + catalyst.add(itemRecordAdd); + } + // cancel input and output + for(ItemRecord itemRecordIn : input){ + for(ItemRecord itemRecordOut : output){ + if(itemRecordIn.isSameType(itemRecordOut)){ + long a = itemRecordIn.amount; + long b = itemRecordOut.amount; + itemRecordIn.amount -= a>b ? b : a; + itemRecordOut.amount -= a>b ? b : a; + } + } + } + } + + public ImmutableList getCancellableItems(){ + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + for(ItemRecord itemRecord : input){ + if(!itemRecord.isLocked() && itemRecord.amount != 0){ + builder.add(itemRecord); + } + } + return builder.build(); + } + + public ImmutableList getOutputStack(){ + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + for(ItemRecord itemRecord : output){ + if(itemRecord.amount != 0){ + builder.add(itemRecord.toItemStack()); + } + } + return builder.build(); + } + + public ImmutableList getInputStack(){ + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + for(ItemRecord itemRecord : input){ + if(itemRecord.amount != 0){ + builder.add(itemRecord.toItemStack()); + } + } + return builder.build(); + } + + public ImmutableList getCatalystStack(){ + ImmutableList.Builder builder = new ImmutableList.Builder<>(); + LOOP: + for(ItemRecord itemRecord : catalyst){ + if(itemRecord.amount != 0){ + for(ItemRecord record : output){ + if(itemRecord.isSameType(record)){ + continue LOOP; + } + } + itemRecord.amount = 0; + builder.add(itemRecord.toItemStack()); + } + } + return builder.build(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof CostRecord) { + for (ItemRecord itemRecordIn : output) { + for (ItemRecord itemRecordExt : ((CostRecord) obj).output) { + if ((itemRecordExt.isSameType(itemRecordIn) && itemRecordExt.amount != itemRecordIn.amount) || !itemRecordExt.isSameType(itemRecordIn)) { + return false; + } + } + } + for (ItemRecord itemRecordIn : input) { + for (ItemRecord itemRecordExt : ((CostRecord) obj).input) { + if ((itemRecordExt.isSameType(itemRecordIn) && itemRecordExt.amount != itemRecordIn.amount)|| !itemRecordExt.isSameType(itemRecordIn)) { + return false; + } + } + } + for (ItemRecord itemRecordIn : ((CostRecord) obj).output) { + for (ItemRecord itemRecordExt : output) { + if ((itemRecordExt.isSameType(itemRecordIn) && itemRecordExt.amount != itemRecordIn.amount) || !itemRecordExt.isSameType(itemRecordIn)) { + return false; + } + } + } + for (ItemRecord itemRecordIn : ((CostRecord) obj).input) { + for (ItemRecord itemRecordExt : input) { + if ((itemRecordExt.isSameType(itemRecordIn) && itemRecordExt.amount != itemRecordIn.amount)|| !itemRecordExt.isSameType(itemRecordIn)) { + return false; + } + } + } + return true; + } else { + return false; + } + } +} diff --git a/src/main/java/pers/towdium/justEnoughCalculation/core/ItemRecord.java b/src/main/java/pers/towdium/justEnoughCalculation/core/ItemRecord.java index 85752e4..94a1a09 100644 --- a/src/main/java/pers/towdium/justEnoughCalculation/core/ItemRecord.java +++ b/src/main/java/pers/towdium/justEnoughCalculation/core/ItemRecord.java @@ -12,7 +12,25 @@ public class ItemRecord{ int meta; long amount; boolean approx; + boolean locked; + /** + * Generate ItemRecord based on the ItemStack and multiply the amount + */ + public ItemRecord(ItemStack itemStack, long multiplier){ + item = itemStack.getItem(); + meta = itemStack.getMetadata(); + amount = ItemStackWrapper.getUnifiedAmount(itemStack) * multiplier; + approx = itemStack.hasTagCompound() && itemStack.getTagCompound().getBoolean("approx"); + } + + public ItemRecord(ItemStack itemStack){ + this(itemStack, 1); + } + + /** + * Generate ItemRecord with customized amount + */ public ItemRecord(ItemStack itemStack, long amount, boolean approx){ item = itemStack.getItem(); meta = itemStack.getMetadata(); @@ -94,6 +112,14 @@ public class ItemRecord{ return itemStack; } + public boolean isLocked() { + return locked; + } + + public void setLocked(boolean locked) { + this.locked = locked; + } + @Override public boolean equals(Object obj) { if(obj instanceof ItemRecord){ diff --git a/src/main/java/pers/towdium/justEnoughCalculation/gui/guis/calculator/GuiCalculator.java b/src/main/java/pers/towdium/justEnoughCalculation/gui/guis/calculator/GuiCalculator.java index e4a088b..1398cd1 100644 --- a/src/main/java/pers/towdium/justEnoughCalculation/gui/guis/calculator/GuiCalculator.java +++ b/src/main/java/pers/towdium/justEnoughCalculation/gui/guis/calculator/GuiCalculator.java @@ -9,6 +9,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import pers.towdium.justEnoughCalculation.JustEnoughCalculation; import pers.towdium.justEnoughCalculation.core.Calculator; +import pers.towdium.justEnoughCalculation.core.CostRecord; import pers.towdium.justEnoughCalculation.core.ItemStackWrapper; import pers.towdium.justEnoughCalculation.gui.commom.GuiTooltipScreen; import pers.towdium.justEnoughCalculation.gui.commom.recipe.ContainerRecipe; @@ -20,6 +21,7 @@ import pers.towdium.justEnoughCalculation.gui.guis.recipeViewer.GuiRecipeViewer; import pers.towdium.justEnoughCalculation.network.packets.PacketSyncRecord; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; @@ -35,7 +37,7 @@ public class GuiCalculator extends GuiTooltipScreen{ GuiButton buttonMode; GuiButton buttonCalculate; GuiButton buttonView; - Calculator.CostRecord costRecord; + CostRecord costRecord; int activeSlot = -1; int page = 1; int total = 0; @@ -210,17 +212,17 @@ public class GuiCalculator extends GuiTooltipScreen{ if(costRecord != null){ switch (mode){ case OUTPUT: - total = (costRecord.getOutputStack().length+35)/36; + total = (costRecord.getOutputStack().size()+35)/36; fillSlotsWith(costRecord.getOutputStack(), (page-1)*36);break; case INPUT: - total = (costRecord.getInputStack().length+35)/36; + total = (costRecord.getInputStack().size()+35)/36; fillSlotsWith(costRecord.getInputStack(), (page-1)*36);break; case CATALYST: - total = (costRecord.getCatalystStack().length+35)/36; + total = (costRecord.getCatalystStack().size()+35)/36; fillSlotsWith(costRecord.getCatalystStack(), (page-1)*36);break; } }else { - fillSlotsWith(new ItemStack[0], 0); + fillSlotsWith(new ArrayList(), 0); total = 0; } buttonLeft.enabled = page != 1; @@ -248,11 +250,11 @@ public class GuiCalculator extends GuiTooltipScreen{ } } - private void fillSlotsWith(ItemStack[] itemStacks, int start){ + private void fillSlotsWith(List itemStacks, int start){ int pos = 1; for(int i=start; i