Crafting Calculator mostly works now - is missing features however.

This commit is contained in:
AlgorithmX2
2014-06-16 00:48:43 -05:00
parent e0ebec1a60
commit 92f728be67
16 changed files with 465 additions and 292 deletions
+34 -136
View File
@@ -1,25 +1,22 @@
package appeng.crafting;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import net.minecraft.nbt.NBTTagCompound;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.core.AELog;
import appeng.me.cache.CraftingCache;
import appeng.util.Platform;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.base.Stopwatch;
public class CraftingJob implements ICraftingParent
public class CraftingJob
{
IAEItemStack output;
@@ -28,170 +25,71 @@ public class CraftingJob implements ICraftingParent
HashSet<IAEItemStack> prophecies;
Multimap<IAEItemStack, CraftingTask> bottom;
CraftingTask top;
ICraftingHost jobHost;
public CraftingJob(ICraftingHost host, NBTTagCompound data) {
jobHost = host;
storage = AEApi.instance().storage().createItemList();
prophecies = new HashSet();
bottom = ArrayListMultimap.create();
}
public CraftingJob(ICraftingHost host, IAEItemStack what, Actionable mode) throws CraftingMissingItemsException {
public CraftingJob(ICraftingHost host, IAEItemStack what, Actionable mode) {
jobHost = host;
output = what.copy();
storage = AEApi.instance().storage().createItemList();
prophecies = new HashSet();
bottom = ArrayListMultimap.create();
CraftingCache cc = host.getGrid().getCache( CraftingCache.class );
IStorageGrid sg = host.getGrid().getCache( IStorageGrid.class );
IItemList<IAEItemStack> missing = AEApi.instance().storage().createItemList();
MECraftingInventory meci = new MECraftingInventory( sg.getItemInventory() );
MECraftingInventory meci = new MECraftingInventory( sg.getItemInventory(), true, false, true );
meci.ignore( what );
calculateCrafting( cc, this, meci, missing, what, mode );
CraftingTreeNode tree = getCraftingTree( cc, what );
if ( !missing.isEmpty() )
try
{
if ( mode == Actionable.MODULATE )
{
IMEInventory<IAEItemStack> netStorage = sg.getItemInventory();
Iterator<IAEItemStack> i = storage.iterator();
while (i.hasNext())
{
IAEItemStack item = i.next();
netStorage.injectItems( item, mode, host.getActionSrc() );
}
}
throw new CraftingMissingItemsException( missing );
Stopwatch timer = Stopwatch.createStarted();
tree.request( meci, what.getStackSize(), host.getActionSrc() );
tree.dive( this );
AELog.info( "-------------" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" );
// if ( mode == Actionable.MODULATE )
// meci.moveItemsToStorage( storage );
}
catch (CraftBranchFailure e)
{
AELog.error( e );
}
catch (CraftingCalculationFailure f)
{
AELog.error( f );
}
if ( mode == Actionable.MODULATE )
meci.moveItemsToStorage( storage );
}
public void calculateCrafting(CraftingCache cc, ICraftingParent parent, IMEInventory<IAEItemStack> inv, IItemList<IAEItemStack> missing, IAEItemStack what,
Actionable mode)
private CraftingTreeNode getCraftingTree(CraftingCache cc, IAEItemStack what)
{
Set<ICraftingPatternDetails> patterns = cc.getCraftingFor( what );
for (ICraftingPatternDetails details : patterns)
{
IAEItemStack[] requirements = details.getCondencedInputs();
if ( canMake( requirements, inv ) )
{
extractItems( requirements, inv, storage, missing );
return;
}
}
for (ICraftingPatternDetails details : patterns)
{
IAEItemStack[] requirements = details.getCondencedInputs();
extractItems( requirements, inv, storage, missing );
return;
}
return new CraftingTreeNode( cc, this, what, null, 0 );
}
private void extractItems(IAEItemStack[] requirements, IMEInventory<IAEItemStack> inv, IItemList<IAEItemStack> dest, IItemList<IAEItemStack> missing)
{
for (IAEItemStack is : requirements)
{
IAEItemStack avail = inv.extractItems( is, Actionable.MODULATE, jobHost.getActionSrc() );
if ( avail == null )
{
missing.add( is );
continue;
}
if ( avail.getStackSize() != is.getStackSize() )
{
IAEItemStack ais = avail.copy();
ais.setStackSize( is.getStackSize() - avail.getStackSize() );
missing.add( ais );
}
dest.add( avail );
}
}
private boolean canMake(IAEItemStack[] requirements, IMEInventory<IAEItemStack> inv)
{
for (IAEItemStack is : requirements)
{
IAEItemStack avail = inv.extractItems( is, Actionable.SIMULATE, jobHost.getActionSrc() );
if ( avail == null )
return false;
if ( avail.getStackSize() != is.getStackSize() )
return false;
}
return true;
}
public Collection<CraftingTask> getBottom()
{
return bottom.values();
}
public IAEItemStack addProgress(IAEItemStack progress)
{
Collection<CraftingTask> task = bottom.get( progress );
if ( task == null )
return progress;
Iterator<CraftingTask> i = task.iterator();
while (i.hasNext())
{
// adding to the first item..
progress = i.next().addProgress( progress );
// if nothing remains return
if ( progress == null )
return null;
// if something remains continue..
i = task.iterator();
}
return null;
}
@Override
public void writeToNBT(NBTTagCompound out)
{
}
@Override
public void removeChild(CraftingTask craftingTask)
public void addTask(IAEItemStack what, int crafts, ICraftingPatternDetails details, int depth)
{
// TODO Auto-generated method stub
if ( crafts > 0 )
{
AELog.info( "new task: " + Platform.getItemDisplayName( what ) + " x " + what.getStackSize() + " * " + crafts + " = "
+ (what.getStackSize() * crafts) + " @ " + depth );
}
}
public ICraftingHost getHost()
public void addMissing(IAEItemStack what)
{
// TODO Auto-generated method stub
return null;
}
@Override
public Collection<? extends ICraftingParent> getSubJobs()
{
return Collections.singleton( top );
AELog.info( "required material: " + Platform.getItemDisplayName( what ) + " x " + what.getStackSize() );
}
}