Applied-Energistics-2-tiler.../crafting/CraftingTreeNode.java

319 lines
6.9 KiB
Java
Raw Normal View History

package appeng.crafting;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import net.minecraft.world.World;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
import appeng.api.networking.crafting.ICraftingGrid;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.me.cluster.implementations.CraftingCPUCluster;
public class CraftingTreeNode
{
// parent node.
private CraftingTreeProcess parent;
private World world;
// what slot!
int slot;
int bytes = 0;
// what item is this?
private IAEItemStack what;
// what are the crafting patterns for this?
private ArrayList<CraftingTreeProcess> nodes = new ArrayList();
2014-08-11 07:08:31 +02:00
boolean canEmit = false;
boolean cannotUse = false;
2014-08-13 07:33:13 +02:00
long missing = 0;
2014-08-13 07:33:13 +02:00
long howManyEmitted = 0;
CraftingJob job;
IItemList<IAEItemStack> used = AEApi.instance().storage().createItemList();
boolean exhausted = false;
boolean sim;
public CraftingTreeNode(ICraftingGrid cc, CraftingJob job, IAEItemStack wat, CraftingTreeProcess par, int slot, int depth)
{
what = wat;
parent = par;
this.slot = slot;
2014-06-22 09:00:38 +02:00
this.world = job.getWorld();
this.job = job;
sim = false;
2014-08-11 07:08:31 +02:00
canEmit = cc.canEmitFor( what );
if ( canEmit )
return; // if you can emit for something, you can't make it with patterns.
for (ICraftingPatternDetails details : cc.getCraftingFor( what, parent == null ? null : parent.details, slot, world ))// in
// order.
{
2014-09-21 01:20:13 +02:00
if ( parent == null || parent.notRecursive( details ) )
nodes.add( new CraftingTreeProcess( cc, job, details, this, depth + 1, world ) );
}
2014-08-11 07:08:31 +02:00
}
public IAEItemStack getStack(long size)
{
IAEItemStack is = what.copy();
is.setStackSize( size );
return is;
}
2014-09-21 01:20:13 +02:00
boolean notRecursive(ICraftingPatternDetails details)
{
2014-09-20 22:30:12 +02:00
IAEItemStack[] o = details.getCondensedOutputs();
for (IAEItemStack i : o)
if ( i.equals( what ) )
2014-07-13 01:13:40 +02:00
return false;
2014-09-20 22:30:31 +02:00
o = details.getCondensedInputs();
2014-07-13 01:13:40 +02:00
for (IAEItemStack i : o)
if ( i.equals( what ) )
return false;
if ( parent == null )
2014-07-13 01:13:40 +02:00
return true;
2014-09-21 01:20:13 +02:00
return parent.notRecursive( details );
}
public IAEItemStack request(MECraftingInventory inv, long l, BaseActionSource src) throws CraftBranchFailure, InterruptedException
{
job.handlepausing();
List<IAEItemStack> thingsUsed = new LinkedList();
what.setStackSize( l );
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 )
{
if ( !exhausted )
{
IAEItemStack is = job.checkUse( available );
2014-07-18 21:30:29 +02:00
if ( is != null )
{
thingsUsed.add( is.copy() );
used.add( is );
}
}
bytes += available.getStackSize();
l -= available.getStackSize();
if ( l == 0 )
return available;
}
}
}
}
else
{
IAEItemStack available = inv.extractItems( what, Actionable.MODULATE, src );
if ( available != null )
{
if ( !exhausted )
{
IAEItemStack is = job.checkUse( available );
2014-07-18 21:30:29 +02:00
if ( is != null )
{
thingsUsed.add( is.copy() );
used.add( is );
}
}
bytes += available.getStackSize();
l -= available.getStackSize();
if ( l == 0 )
return available;
}
}
2014-08-13 07:33:13 +02:00
if ( canEmit )
{
IAEItemStack wat = what.copy();
wat.setStackSize( l );
howManyEmitted = wat.getStackSize();
bytes += wat.getStackSize();
return wat;
}
exhausted = true;
if ( nodes.size() == 1 )
{
CraftingTreeProcess pro = nodes.get( 0 );
while (pro.possible && l > 0)
{
IAEItemStack madeWhat = pro.getAmountCrafted( what );
pro.request( inv, pro.getTimes( l, madeWhat.getStackSize() ), src );
madeWhat.setStackSize( l );
IAEItemStack available = inv.extractItems( madeWhat, Actionable.MODULATE, src );
if ( available != null )
{
bytes += available.getStackSize();
l -= available.getStackSize();
if ( l <= 0 )
return available;
}
else
pro.possible = false; // ;P
}
}
else if ( nodes.size() > 1 )
{
for (CraftingTreeProcess pro : nodes)
{
try
{
while (pro.possible && l > 0)
{
2014-07-18 05:03:42 +02:00
MECraftingInventory subInv = new MECraftingInventory( inv, true, true, true );
pro.request( subInv, 1, src );
what.setStackSize( l );
IAEItemStack available = subInv.extractItems( what, Actionable.MODULATE, src );
if ( available != null )
{
if ( !subInv.commit( src ) )
throw new CraftBranchFailure( what, l );
bytes += available.getStackSize();
l -= available.getStackSize();
if ( l <= 0 )
return available;
}
else
pro.possible = false; // ;P
}
}
catch (CraftBranchFailure fail)
{
pro.possible = true;
}
}
}
if ( sim )
{
missing += l;
bytes += l;
IAEItemStack rv = what.copy();
rv.setStackSize( l );
return rv;
}
for (IAEItemStack o : thingsUsed)
{
job.refund( o.copy() );
o.setStackSize( -o.getStackSize() );
used.add( o );
}
throw new CraftBranchFailure( what, l );
}
public void dive(CraftingJob job)
{
if ( missing > 0 )
job.addMissing( getStack( missing ) );
2014-06-29 11:06:07 +02:00
// missing = 0;
job.addBytes( 8 + bytes );
for (CraftingTreeProcess pro : nodes)
pro.dive( job );
}
public void setSimulate()
{
sim = true;
missing = 0;
bytes = 0;
used.resetStatus();
exhausted = false;
for (CraftingTreeProcess pro : nodes)
pro.setSimulate();
}
public void setJob(MECraftingInventory storage, CraftingCPUCluster craftingCPUCluster, BaseActionSource src) throws CraftBranchFailure
{
for (IAEItemStack i : used)
{
IAEItemStack ex = storage.extractItems( i, Actionable.MODULATE, src );
if ( ex == null || ex.getStackSize() != i.getStackSize() )
throw new CraftBranchFailure( i, i.getStackSize() );
craftingCPUCluster.addStorage( ex );
}
2014-08-13 07:33:13 +02:00
if ( howManyEmitted > 0 )
{
IAEItemStack i = what.copy();
i.setStackSize( howManyEmitted );
craftingCPUCluster.addEmitable( i );
}
for (CraftingTreeProcess pro : nodes)
pro.setJob( storage, craftingCPUCluster, src );
}
2014-06-28 22:18:36 +02:00
public void getPlan(IItemList<IAEItemStack> plan)
{
2014-06-29 11:06:07 +02:00
if ( missing > 0 )
{
IAEItemStack o = what.copy();
o.setStackSize( missing );
plan.add( o );
}
2014-08-13 07:33:13 +02:00
if ( howManyEmitted > 0 )
{
IAEItemStack i = what.copy();
i.setCountRequestable( howManyEmitted );
plan.addRequestable( i );
}
2014-06-28 22:18:36 +02:00
for (IAEItemStack i : used)
plan.add( i.copy() );
for (CraftingTreeProcess pro : nodes)
pro.getPlan( plan );
}
}