From 83d3a3aadbb71984326610a53b5a7b77ff61194a Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Mon, 16 Jun 2014 22:36:35 -0500 Subject: [PATCH] Crafting calculator now supports damageable items, and ore dictionary stuff in primitive form. --- crafting/CraftingJob.java | 35 +++++++++++++--- crafting/CraftingTreeNode.java | 67 ++++++++++++++++++++++--------- crafting/CraftingTreeProcess.java | 58 +++++++++++++++++++++++--- helpers/PatternHelper.java | 3 ++ 4 files changed, 134 insertions(+), 29 deletions(-) diff --git a/crafting/CraftingJob.java b/crafting/CraftingJob.java index f1a7a584..756ab7cc 100644 --- a/crafting/CraftingJob.java +++ b/crafting/CraftingJob.java @@ -1,5 +1,6 @@ package appeng.crafting; +import java.util.HashMap; import java.util.HashSet; import java.util.concurrent.TimeUnit; @@ -54,6 +55,13 @@ public class CraftingJob Stopwatch timer = Stopwatch.createStarted(); tree.request( meci, what.getStackSize(), host.getActionSrc() ); tree.dive( this ); + + for (String s : opsAndMultiplier.keySet()) + { + twoIntegers ti = opsAndMultiplier.get( s ); + AELog.info( s + " * " + ti.times + " = " + (ti.perOp * ti.times) ); + } + AELog.info( "-------------" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" ); // if ( mode == Actionable.MODULATE ) // meci.moveItemsToStorage( storage ); @@ -70,7 +78,7 @@ public class CraftingJob private CraftingTreeNode getCraftingTree(CraftingCache cc, IAEItemStack what) { - return new CraftingTreeNode( cc, this, what, null, 0 ); + return new CraftingTreeNode( cc, this, what, null, -1, 0 ); } public void writeToNBT(NBTTagCompound out) @@ -78,18 +86,35 @@ public class CraftingJob } - public void addTask(IAEItemStack what, int crafts, ICraftingPatternDetails details, int depth) + public void addTask(IAEItemStack what, long crafts, ICraftingPatternDetails details, int depth) { if ( crafts > 0 ) { - AELog.info( "new task: " + Platform.getItemDisplayName( what ) + " x " + what.getStackSize() + " * " + crafts + " = " - + (what.getStackSize() * crafts) + " @ " + depth ); + postOp( "new task: " + Platform.getItemDisplayName( what ) + " x " + what.getStackSize(), what.getStackSize(), crafts ); } } public void addMissing(IAEItemStack what) { - AELog.info( "required material: " + Platform.getItemDisplayName( what ) + " x " + what.getStackSize() ); + postOp( "required material: " + Platform.getItemDisplayName( what ), 1, what.getStackSize() ); } + class twoIntegers + { + + public long perOp = 0; + public long times = 0; + }; + + HashMap opsAndMultiplier = new HashMap(); + + private void postOp(String string, long stackSize, long crafts) + { + twoIntegers ti = opsAndMultiplier.get( string ); + if ( ti == null ) + opsAndMultiplier.put( string, ti = new twoIntegers() ); + + ti.perOp = stackSize; + ti.times += crafts; + } } diff --git a/crafting/CraftingTreeNode.java b/crafting/CraftingTreeNode.java index 40ade6fe..64ced342 100644 --- a/crafting/CraftingTreeNode.java +++ b/crafting/CraftingTreeNode.java @@ -2,7 +2,9 @@ package appeng.crafting; import java.util.ArrayList; +import net.minecraft.world.World; import appeng.api.config.Actionable; +import appeng.api.config.FuzzyMode; import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.networking.security.BaseActionSource; import appeng.api.storage.data.IAEItemStack; @@ -13,6 +15,10 @@ public class CraftingTreeNode // parent node. private CraftingTreeProcess parent; + private World world; + + // what slot! + int slot; // what item is this? private IAEItemStack what; @@ -21,16 +27,18 @@ public class CraftingTreeNode private ArrayList nodes = new ArrayList(); boolean cannotUse = false; - int missing = 0; + long missing = 0; - public CraftingTreeNode(CraftingCache cc, CraftingJob job, IAEItemStack wat, CraftingTreeProcess par, int depth) { + public CraftingTreeNode(CraftingCache cc, CraftingJob job, IAEItemStack wat, CraftingTreeProcess par, int slot, int depth) { what = wat; parent = par; + this.slot = slot; + this.world = job.jobHost.getWorld(); for (ICraftingPatternDetails details : cc.getCraftingFor( what ))// in order. { if ( notRecurive( details ) ) - nodes.add( new CraftingTreeProcess( cc, job, details, this, depth + 1 ) ); + nodes.add( new CraftingTreeProcess( cc, job, details, this, depth + 1, world ) ); } } @@ -54,21 +62,41 @@ public class CraftingTreeNode return parent.notRecurive( details ); } - private long getTimes(long remaining, long stackSize) - { - return (remaining / stackSize) + (remaining % stackSize != 0 ? 1 : 0); - } - - public void request(MECraftingInventory inv, long l, BaseActionSource src) throws CraftBranchFailure + public IAEItemStack request(MECraftingInventory inv, long l, BaseActionSource src) throws CraftBranchFailure { what.setStackSize( l ); - IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src ); + if ( slot >= 0 && parent != null && parent.details.isCraftable() ) + { + for (IAEItemStack fuzz : inv.getItemList().findFuzzy( what, FuzzyMode.IGNORE_ALL )) + { + if ( parent.details.isValidItemForSlot( slot, fuzz.getItemStack(), world ) ) + { + fuzz = fuzz.copy(); + fuzz.setStackSize( l ); + IAEItemStack available = inv.extractItems( fuzz, Actionable.MODULATE, src ); - if ( available != null ) - l -= available.getStackSize(); + if ( available != null ) + { + l -= available.getStackSize(); - if ( l == 0 ) - return; + if ( l == 0 ) + return available; + } + } + } + } + else + { + IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src ); + + if ( available != null ) + { + l -= available.getStackSize(); + + if ( l == 0 ) + return available; + } + } if ( nodes.size() == 1 ) { @@ -76,17 +104,17 @@ public class CraftingTreeNode while (pro.possible && l > 0) { - pro.request( inv, getTimes( l, pro.getAmountCrafted( what ).getStackSize() ), src ); + pro.request( inv, pro.getTimes( l, pro.getAmountCrafted( what ).getStackSize() ), src ); what.setStackSize( l ); - available = inv.extractItems( what, Actionable.MODULATE, src ); + IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src ); if ( available != null ) { l -= available.getStackSize(); if ( l <= 0 ) - return; + return available; } else pro.possible = false; // ;P @@ -105,14 +133,14 @@ public class CraftingTreeNode subInv.commit( src ); what.setStackSize( l ); - available = inv.extractItems( what, Actionable.MODULATE, src ); + IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src ); if ( available != null ) { l -= available.getStackSize(); if ( l <= 0 ) - return; + return available; } else pro.possible = false; // ;P @@ -126,6 +154,7 @@ public class CraftingTreeNode } missing += l; + return what; // throw new CraftBranchFailure( what, l ); } diff --git a/crafting/CraftingTreeProcess.java b/crafting/CraftingTreeProcess.java index 5e0f9501..21acbd37 100644 --- a/crafting/CraftingTreeProcess.java +++ b/crafting/CraftingTreeProcess.java @@ -4,6 +4,9 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import appeng.api.AEApi; import appeng.api.config.Actionable; import appeng.api.networking.crafting.ICraftingPatternDetails; import appeng.api.networking.security.BaseActionSource; @@ -17,20 +20,43 @@ public class CraftingTreeProcess ICraftingPatternDetails details; CraftingJob job; - int crafts = 0; + long crafts = 0; + boolean damageable; + final private int depth; Map nodes = new HashMap(); public boolean possible = true; - public CraftingTreeProcess(CraftingCache cc, CraftingJob job, ICraftingPatternDetails details, CraftingTreeNode craftingTreeNode, int depth) { + public CraftingTreeProcess(CraftingCache cc, CraftingJob job, ICraftingPatternDetails details, CraftingTreeNode craftingTreeNode, int depth, World world) { parent = craftingTreeNode; this.details = details; this.job = job; this.depth = depth; - for (IAEItemStack part : details.getCondencedInputs()) - nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, depth + 1 ), part.getStackSize() ); + if ( details.isCraftable() ) + { + IAEItemStack list[] = details.getInputs(); + + for (int x = 0; x < list.length; x++) + { + IAEItemStack part = list[x]; + if ( part != null ) + { + ItemStack is = part.getItemStack(); + if ( is.getItem().hasContainerItem( is ) ) + damageable = true; + nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() ); + } + } + } + else + { + for (IAEItemStack part : details.getCondencedInputs()) + { + nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, -1, depth + 1 ), part.getStackSize() ); + } + } } public boolean notRecurive(ICraftingPatternDetails details) @@ -38,6 +64,13 @@ public class CraftingTreeProcess return parent.notRecurive( details ); } + long getTimes(long remaining, long stackSize) + { + if ( damageable ) + return 1; + return (remaining / stackSize) + (remaining % stackSize != 0 ? 1 : 0); + } + IAEItemStack getAmountCrafted(IAEItemStack what2) { for (IAEItemStack is : details.getCondencedOutputs()) @@ -59,7 +92,22 @@ public class CraftingTreeProcess for (Entry entry : nodes.entrySet()) { IAEItemStack item = entry.getKey().getStack( entry.getValue() ); - entry.getKey().request( inv, item.getStackSize() * i, src ); + IAEItemStack stack = entry.getKey().request( inv, item.getStackSize() * i, src ); + + if ( damageable ) + { + ItemStack is = stack.getItemStack(); + if ( stack.getItem().hasContainerItem( is ) ) + { + is = stack.getItem().getContainerItem( is ); + if ( is.isItemStackDamageable() && is.getItemDamage() == is.getMaxDamage() ) + is = null; + + IAEItemStack o = AEApi.instance().storage().createItemStack( is ); + if ( o != null ) + inv.injectItems( o, Actionable.MODULATE, src ); + } + } } // assume its possible. diff --git a/helpers/PatternHelper.java b/helpers/PatternHelper.java index 9abff908..7b4687ec 100644 --- a/helpers/PatternHelper.java +++ b/helpers/PatternHelper.java @@ -198,7 +198,10 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable