More work on crafting calculations.

This commit is contained in:
AlgorithmX2 2014-06-05 23:26:01 -05:00
parent f6d7762725
commit 3b394face2
3 changed files with 169 additions and 20 deletions

View file

@ -4,10 +4,12 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
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;
@ -47,33 +49,74 @@ public class CraftingJob implements ICraftingParent
CraftingCache cc = host.getGrid().getCache( CraftingCache.class );
IStorageGrid sg = host.getGrid().getCache( IStorageGrid.class );
IItemList<IAEItemStack> available = AEApi.instance().storage().createItemList();
IItemList<IAEItemStack> missing = AEApi.instance().storage().createItemList();
calculateCrafting( cc, this, sg.getItemInventory().getAvailableItems( available ), missing, what, mode );
if ( ! missing.isEmpty() )
MECraftingInventory meci = new MECraftingInventory( sg.getItemInventory() );
calculateCrafting( cc, this, meci, missing, what, mode );
if ( !missing.isEmpty() )
{
if ( mode == Actionable.MODULATE )
{
IMEInventory<IAEItemStack> netStorage = sg.getItemInventory();
Iterator<IAEItemStack> i = storage.iterator();
while ( i.hasNext() )
while (i.hasNext())
{
IAEItemStack item = i.next();
netStorage.injectItems( item, mode, host.getActionSrc() );
}
}
throw new CraftingMissingItemsException( missing );
}
if ( mode == Actionable.MODULATE )
meci.moveItemsToStorage( storage );
}
public void calculateCrafting(CraftingCache cc, ICraftingParent parent, IMEInventory<IAEItemStack> inv, IItemList<IAEItemStack> missing, IAEItemStack what,
Actionable mode)
{
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;
}
}
public void calculateCrafting( CraftingCache cc, ICraftingParent parent, IItemList<IAEItemStack> available, IItemList<IAEItemStack> missing, IAEItemStack what, Actionable mode) {
private void extractItems(IAEItemStack[] requirements, IMEInventory<IAEItemStack> inv, IItemList<IAEItemStack> storage2, IItemList<IAEItemStack> missing)
{
for (IAEItemStack is : requirements)
{
inv.extractItems( is, Actionable.MODULATE, jobHost.getActionSrc() );
}
}
private boolean canMake(IAEItemStack[] requirements, IMEInventory<IAEItemStack> inv)
{
for (IAEItemStack is : requirements)
{
IAEItemStack avail = inv.extractItems( is, Actionable.SIMULATE, jobHost.getActionSrc() );
}
return true;
}
public Collection<CraftingTask> getBottom()

View file

@ -0,0 +1,91 @@
package appeng.crafting;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
public class MECraftingInventory implements IMEInventory<IAEItemStack>
{
final IMEInventory<IAEItemStack> target;
final IItemList<IAEItemStack> localCache;
final IItemList<IAEItemStack> extractedCache;
public MECraftingInventory(IMEInventory<IAEItemStack> target) {
this.target = target;
extractedCache = AEApi.instance().storage().createItemList();
localCache = target.getAvailableItems( AEApi.instance().storage().createItemList() );
}
@Override
public IAEItemStack injectItems(IAEItemStack input, Actionable mode, BaseActionSource src)
{
if ( input == null )
return null;
if ( mode == Actionable.SIMULATE )
localCache.add( input );
return null;
}
@Override
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src)
{
if ( request == null )
return null;
IAEItemStack list = localCache.findPrecise( request );
if ( list == null || list.getStackSize() == 0 )
return null;
if ( mode == Actionable.MODULATE )
extractedCache.add( request );
if ( list.getStackSize() >= request.getStackSize() )
{
if ( mode == Actionable.MODULATE )
list.decStackSize( request.getStackSize() );
return request;
}
IAEItemStack ret = request.copy();
ret.setStackSize( list.getStackSize() );
if ( mode == Actionable.MODULATE )
list.reset();
return ret;
}
@Override
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out)
{
for (IAEItemStack is : localCache)
out.add( is );
return out;
}
public IItemList<IAEItemStack> getItemList()
{
return localCache;
}
@Override
public StorageChannel getChannel()
{
return StorageChannel.ITEMS;
}
public void moveItemsToStorage(IItemList<IAEItemStack> storage)
{
// TODO Auto-generated method stub
}
}

View file

@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
@ -40,6 +42,7 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
IGrid grid;
HashMap<ICraftingPatternDetails, List<ICraftingMedium>> craftingMethods = new HashMap();
HashMap<IAEItemStack, Set<ICraftingPatternDetails>> craftableItems = new HashMap();
boolean updateList = false;
@ -129,15 +132,15 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
IStorageGrid sg = grid.getCache( IStorageGrid.class );
// update the stuff that was in the list...
for (ICraftingPatternDetails details : craftingMethods.keySet())
for (IAEItemStack out : details.getOutputs())
{
out.reset();
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() );
}
for (IAEItemStack out : craftableItems.keySet())
{
out.reset();
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() );
}
// erase list.
craftingMethods.clear();
craftableItems.clear();
// re-create list..
for (ICraftingProvider cp : providers)
@ -149,6 +152,13 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
{
out.reset();
out.setCraftable( true );
Set<ICraftingPatternDetails> methods = craftableItems.get( out );
if ( methods == null )
methods = craftableItems.put( out, new TreeSet() );
methods.add( details );
sg.postAlterationOfStoredItems( StorageChannel.ITEMS, out, new BaseActionSource() );
}
}
@ -195,9 +205,9 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
public IItemList getAvailableItems(IItemList out)
{
// add craftable items!
for (ICraftingPatternDetails details : craftingMethods.keySet())
for (IAEItemStack st : details.getOutputs())
out.addCrafting( st );
// for (ICraftingPatternDetails details : craftingMethods.keySet())
for (IAEItemStack st : craftableItems.keySet())
out.addCrafting( st );
return out;
}
@ -245,4 +255,9 @@ public class CraftingCache implements IGridCache, ICraftingProviderHelper, ICell
return 0;
}
public Set<ICraftingPatternDetails> getCraftingFor(IAEItemStack what)
{
return craftableItems.get( what );
}
}