heavily tweaked auto-crafting
This commit is contained in:
@@ -65,6 +65,7 @@ public class CraftingJob implements Runnable, ICraftingJob
|
||||
private final Object monitor = new Object();
|
||||
private final Stopwatch tickSpreadingWatch = Stopwatch.createUnstarted();
|
||||
private final Stopwatch craftingTreeWatch = Stopwatch.createUnstarted();
|
||||
private final ICraftingGrid cc;
|
||||
private CraftingTreeNode tree;
|
||||
private final IAEItemStack output;
|
||||
private boolean simulate = false;
|
||||
@@ -90,7 +91,7 @@ public class CraftingJob implements Runnable, ICraftingJob
|
||||
|
||||
this.callback = callback;
|
||||
|
||||
final ICraftingGrid cc = grid.getCache( ICraftingGrid.class );
|
||||
this.cc = grid.getCache( ICraftingGrid.class );
|
||||
final GridStorageCache sg = grid.getCache( IStorageGrid.class );
|
||||
this.original = new MECraftingInventory( sg.getExtractableList( actionSrc ) );
|
||||
|
||||
@@ -159,13 +160,13 @@ public class CraftingJob implements Runnable, ICraftingJob
|
||||
TickHandler.INSTANCE.registerCraftingSimulation( this.world, this );
|
||||
this.handlePausing();
|
||||
|
||||
craftingTreeWatch.start();
|
||||
|
||||
final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true );
|
||||
craftingInventory.ignore( this.output );
|
||||
|
||||
this.availableCheck = new MECraftingInventory( this.original, false, false, false );
|
||||
craftingTreeWatch.start();
|
||||
this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc );
|
||||
craftingTreeWatch.stop();
|
||||
this.getTree().dive( this );
|
||||
|
||||
for( final String s : this.opsAndMultiplier.keySet() )
|
||||
@@ -174,8 +175,14 @@ public class CraftingJob implements Runnable, ICraftingJob
|
||||
AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) );
|
||||
}
|
||||
|
||||
craftingTreeWatch.stop();
|
||||
this.logCraftingJob( "real, success", craftingTreeWatch );
|
||||
if( actionSrc.player().isPresent() )
|
||||
{
|
||||
this.logCraftingJob( "simulated, success", craftingTreeWatch );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.logCraftingJob( "real, success", craftingTreeWatch );
|
||||
}
|
||||
}
|
||||
catch( final CraftBranchFailure e )
|
||||
{
|
||||
@@ -185,14 +192,14 @@ public class CraftingJob implements Runnable, ICraftingJob
|
||||
{
|
||||
if( actionSrc.player().isPresent() )
|
||||
{
|
||||
craftingTreeWatch.reset().start();
|
||||
final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true );
|
||||
craftingInventory.ignore( this.output );
|
||||
|
||||
this.availableCheck = new MECraftingInventory( this.original, false, false, false );
|
||||
|
||||
this.getTree().setSimulate();
|
||||
this.availableCheck = new MECraftingInventory( this.original, false, false, false );
|
||||
craftingTreeWatch.reset().start();
|
||||
this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc );
|
||||
craftingTreeWatch.stop();
|
||||
this.getTree().dive( this );
|
||||
|
||||
for( final String s : this.opsAndMultiplier.keySet() )
|
||||
@@ -201,12 +208,10 @@ public class CraftingJob implements Runnable, ICraftingJob
|
||||
AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) );
|
||||
}
|
||||
|
||||
craftingTreeWatch.stop();
|
||||
this.logCraftingJob( "simulate", craftingTreeWatch );
|
||||
this.logCraftingJob( "simulated, failed", craftingTreeWatch );
|
||||
}
|
||||
else
|
||||
{
|
||||
craftingTreeWatch.stop();
|
||||
this.logCraftingJob( "real, failed", craftingTreeWatch );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
@@ -53,14 +52,14 @@ public class CraftingTreeNode
|
||||
private final IAEItemStack what;
|
||||
// what are the crafting patterns for this?
|
||||
private final ArrayList<CraftingTreeProcess> nodes = new ArrayList<>();
|
||||
private final ICraftingGrid cc;
|
||||
private final int depth;
|
||||
private int bytes = 0;
|
||||
private boolean canEmit = false;
|
||||
private long missing = 0;
|
||||
private long howManyEmitted = 0;
|
||||
private boolean exhausted = false;
|
||||
|
||||
private boolean sim;
|
||||
|
||||
public CraftingTreeNode( final ICraftingGrid cc, final CraftingJob job, final IAEItemStack wat, final CraftingTreeProcess par, final int slot, final int depth )
|
||||
{
|
||||
this.what = wat;
|
||||
@@ -68,9 +67,18 @@ public class CraftingTreeNode
|
||||
this.slot = slot;
|
||||
this.world = job.getWorld();
|
||||
this.job = job;
|
||||
this.sim = false;
|
||||
this.cc = cc;
|
||||
this.depth = depth;
|
||||
|
||||
this.canEmit = cc.canEmitFor( this.what );
|
||||
}
|
||||
|
||||
public void addNode()
|
||||
{
|
||||
if( !nodes.isEmpty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if( this.canEmit )
|
||||
{
|
||||
@@ -89,19 +97,42 @@ public class CraftingTreeNode
|
||||
|
||||
IAEItemStack request( final MECraftingInventory inv, long l, final IActionSource src ) throws CraftBranchFailure, InterruptedException
|
||||
{
|
||||
addNode();
|
||||
this.job.handlePausing();
|
||||
if( this.canEmit )
|
||||
{
|
||||
final IAEItemStack wat = this.what.copy();
|
||||
wat.setStackSize( l );
|
||||
|
||||
this.howManyEmitted = wat.getStackSize();
|
||||
this.bytes += wat.getStackSize();
|
||||
|
||||
return wat;
|
||||
}
|
||||
|
||||
final IItemList<IAEItemStack> inventoryList = inv.getItemList();
|
||||
final List<IAEItemStack> thingsUsed = new ArrayList<>();
|
||||
|
||||
this.what.setStackSize( l );
|
||||
|
||||
if( this.getSlot() >= 0 && this.parent != null && this.parent.details.isCraftable() )
|
||||
{
|
||||
Collection<IAEItemStack> itemList = new ArrayList<>();
|
||||
|
||||
if( this.parent.getContainerItems() != null && !this.parent.getContainerItems().findFuzzy( this.what, FuzzyMode.IGNORE_ALL ).isEmpty() )
|
||||
if( this.what.getItem().hasContainerItem( this.what.getDefinition() ) )
|
||||
{
|
||||
itemList = inventoryList.findFuzzy( this.what, FuzzyMode.IGNORE_ALL );
|
||||
itemList.addAll( inventoryList.findFuzzy( this.what, FuzzyMode.IGNORE_ALL ) );
|
||||
|
||||
if( this.parent.details.canSubstitute() )
|
||||
{
|
||||
for( IAEItemStack is : inventoryList )
|
||||
{
|
||||
if( is.fuzzyComparison( this.what, FuzzyMode.IGNORE_ALL ) )
|
||||
{
|
||||
itemList.add( is );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -110,6 +141,10 @@ public class CraftingTreeNode
|
||||
{
|
||||
itemList.add( item );
|
||||
}
|
||||
if( this.parent.details.canSubstitute() )
|
||||
{
|
||||
itemList.addAll( inventoryList.findFuzzy( this.what, FuzzyMode.IGNORE_ALL ) );
|
||||
}
|
||||
}
|
||||
|
||||
for( IAEItemStack fuzz : itemList )
|
||||
@@ -172,17 +207,6 @@ public class CraftingTreeNode
|
||||
}
|
||||
}
|
||||
|
||||
if( this.canEmit )
|
||||
{
|
||||
final IAEItemStack wat = this.what.copy();
|
||||
wat.setStackSize( l );
|
||||
|
||||
this.howManyEmitted = wat.getStackSize();
|
||||
this.bytes += wat.getStackSize();
|
||||
|
||||
return wat;
|
||||
}
|
||||
|
||||
this.exhausted = true;
|
||||
|
||||
if( this.nodes.size() == 1 )
|
||||
@@ -192,11 +216,9 @@ public class CraftingTreeNode
|
||||
while ( pro.possible && l > 0 )
|
||||
{
|
||||
final IAEItemStack madeWhat = pro.getAmountCrafted( this.what );
|
||||
|
||||
pro.request( inv, pro.getTimes( l, madeWhat.getStackSize() ), src );
|
||||
|
||||
madeWhat.setStackSize( l );
|
||||
|
||||
final IAEItemStack available = inv.extractItems( madeWhat, Actionable.MODULATE, src );
|
||||
|
||||
if( available != null )
|
||||
@@ -257,10 +279,10 @@ public class CraftingTreeNode
|
||||
}
|
||||
}
|
||||
|
||||
if( this.sim )
|
||||
if( job.isSimulation() )
|
||||
{
|
||||
this.missing += l;
|
||||
this.bytes += l;
|
||||
this.missing += l;
|
||||
final IAEItemStack rv = this.what.copy();
|
||||
rv.setStackSize( l );
|
||||
return rv;
|
||||
@@ -306,7 +328,6 @@ public class CraftingTreeNode
|
||||
|
||||
void setSimulate()
|
||||
{
|
||||
this.sim = true;
|
||||
this.missing = 0;
|
||||
this.bytes = 0;
|
||||
this.used.resetStatus();
|
||||
|
||||
@@ -20,15 +20,13 @@ package appeng.crafting;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import appeng.api.config.FuzzyMode;
|
||||
import appeng.me.cache.CraftingGridCache;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import com.google.common.collect.ImmutableCollection;
|
||||
import com.google.common.collect.Lists;
|
||||
import it.unimi.dsi.fastutil.objects.Object2LongArrayMap;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
@@ -52,11 +50,11 @@ public class CraftingTreeProcess
|
||||
private final CraftingJob job;
|
||||
private final Object2LongArrayMap<CraftingTreeNode> nodes = new Object2LongArrayMap<>();
|
||||
private final int depth;
|
||||
private final ICraftingGrid cc;
|
||||
private final World world;
|
||||
boolean possible = true;
|
||||
private long crafts = 0;
|
||||
private IItemList<IAEItemStack> containerItems;
|
||||
private boolean limitQty;
|
||||
private List<IAEItemStack> containerItemsList;
|
||||
private long bytes = 0;
|
||||
|
||||
public CraftingTreeProcess( final ICraftingGrid cc, final CraftingJob job, final ICraftingPatternDetails details, final CraftingTreeNode craftingTreeNode, final int depth )
|
||||
@@ -65,20 +63,22 @@ public class CraftingTreeProcess
|
||||
this.details = details;
|
||||
this.job = job;
|
||||
this.depth = depth;
|
||||
final World world = job.getWorld();
|
||||
this.cc = cc;
|
||||
this.world = job.getWorld();
|
||||
}
|
||||
|
||||
public void addProcess()
|
||||
{
|
||||
if( !nodes.isEmpty() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
final IAEItemStack[] list = details.getInputs();
|
||||
|
||||
for( final IAEItemStack part : details.getCondensedInputs() )
|
||||
{
|
||||
if( part.getItem().hasContainerItem( part.getDefinition() ) )
|
||||
{
|
||||
if( containerItems == null )
|
||||
{
|
||||
containerItems = AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList();
|
||||
containerItemsList = new ArrayList<>();
|
||||
}
|
||||
containerItems.add( part );
|
||||
this.limitQty = true;
|
||||
//break;
|
||||
}
|
||||
@@ -96,13 +96,33 @@ public class CraftingTreeProcess
|
||||
if( part.equals( comparePart ) )
|
||||
{
|
||||
boolean isPartContainer = false;
|
||||
if( containerItems != null && !containerItems.findFuzzy( list[x], FuzzyMode.IGNORE_ALL ).isEmpty() )
|
||||
if( part.getItem().hasContainerItem( part.getDefinition() ) )
|
||||
{
|
||||
part = list[x];
|
||||
isPartContainer = true;
|
||||
}
|
||||
long wantedSize = part.getStackSize();
|
||||
|
||||
if( isPartContainer && wantedSize > 0 )
|
||||
{
|
||||
if( details.canSubstitute() && cc.getCraftingFor( part, details, x, world ).isEmpty() )
|
||||
{
|
||||
IItemList<IAEItemStack> aa = ( (CraftingGridCache) cc ).getAvailableItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() );
|
||||
for( IAEItemStack is : aa )
|
||||
{
|
||||
if( is.fuzzyComparison( part, FuzzyMode.IGNORE_ALL ) )
|
||||
{
|
||||
wantedSize -= 1;
|
||||
this.nodes.put( new CraftingTreeNode( cc, job, is.copy().setStackSize( 1 ), this, x, depth + 1 ), 1 );
|
||||
if( wantedSize == 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( !isPartContainer )
|
||||
{
|
||||
IAEItemStack found = job.checkAvailable( part );
|
||||
@@ -270,11 +290,6 @@ public class CraftingTreeProcess
|
||||
}
|
||||
}
|
||||
|
||||
IItemList<IAEItemStack> getContainerItems()
|
||||
{
|
||||
return this.containerItems;
|
||||
}
|
||||
|
||||
boolean notRecursive()
|
||||
{
|
||||
return this.parent == null || this.parent.notRecursive();
|
||||
@@ -291,28 +306,34 @@ public class CraftingTreeProcess
|
||||
|
||||
void request( final MECraftingInventory inv, final long amountOfTimes, final IActionSource src ) throws CraftBranchFailure, InterruptedException
|
||||
{
|
||||
addProcess();
|
||||
this.job.handlePausing();
|
||||
List<IAEItemStack> containerItems = null;
|
||||
|
||||
// request and remove inputs...
|
||||
for( final Entry<CraftingTreeNode, Long> entry : this.nodes.object2LongEntrySet() )
|
||||
{
|
||||
final IAEItemStack stack = entry.getKey().request( inv, entry.getValue() * amountOfTimes, src );
|
||||
|
||||
if( containerItems != null && !this.containerItems.findFuzzy( stack, FuzzyMode.IGNORE_ALL ).isEmpty() )
|
||||
if( stack.getItem().hasContainerItem( stack.getDefinition() ) )
|
||||
{
|
||||
final ItemStack is = Platform.getContainerItem( stack.createItemStack() );
|
||||
final IAEItemStack o = AEItemStack.fromItemStack( is );
|
||||
if( o != null )
|
||||
{
|
||||
if( containerItems == null )
|
||||
{
|
||||
containerItems = new ArrayList<>();
|
||||
}
|
||||
this.bytes++;
|
||||
containerItemsList.add( o );
|
||||
containerItems.add( o );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( containerItems != null )
|
||||
{
|
||||
for( IAEItemStack i : containerItemsList )
|
||||
for( IAEItemStack i : containerItems )
|
||||
{
|
||||
inv.injectItems( i, Actionable.MODULATE, src );
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ public class MECraftingInventory implements IMEInventory<IAEItemStack>
|
||||
this.localCache = new ItemListIgnoreCrafting<>( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() );
|
||||
for( final IAEItemStack is : target.getStorageList() )
|
||||
{
|
||||
this.localCache.add( target.extractItems( is, Actionable.SIMULATE, src ) );
|
||||
this.localCache.add( target.extractItems( is.copy().setStackSize( Long.MAX_VALUE ), Actionable.SIMULATE, src ) );
|
||||
}
|
||||
|
||||
this.par = null;
|
||||
|
||||
Reference in New Issue
Block a user