Applied-Energistics-2-tiler.../me/cluster/implementations/CraftingCPUCluster.java

627 lines
15 KiB
Java
Raw Normal View History

package appeng.me.cluster.implementations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
2014-06-22 09:00:38 +02:00
import net.minecraft.world.WorldServer;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
2014-06-28 07:20:12 +02:00
import appeng.api.config.PowerMultiplier;
2014-05-18 05:19:23 +02:00
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
2014-05-18 05:19:23 +02:00
import appeng.api.networking.IGridNode;
import appeng.api.networking.crafting.CraftingItemList;
import appeng.api.networking.crafting.ICraftingMedium;
import appeng.api.networking.crafting.ICraftingPatternDetails;
2014-06-28 07:20:12 +02:00
import appeng.api.networking.energy.IEnergyGrid;
2014-05-18 05:19:23 +02:00
import appeng.api.networking.events.MENetworkCraftingCpuChange;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.storage.IBaseMonitor;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitorHandlerReceiver;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.WorldCoord;
import appeng.container.ContainerNull;
import appeng.core.AELog;
import appeng.crafting.CraftBranchFailure;
import appeng.crafting.CraftingJob;
import appeng.crafting.MECraftingInventory;
import appeng.me.cache.CraftingCache;
import appeng.me.cluster.IAECluster;
2014-05-14 04:42:14 +02:00
import appeng.tile.crafting.TileCraftingTile;
2014-06-22 09:00:38 +02:00
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
2014-06-22 09:00:38 +02:00
import cpw.mods.fml.common.FMLCommonHandler;
public class CraftingCPUCluster implements IAECluster, IBaseMonitor<IAEItemStack>
{
private final HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object>();
protected Iterator<Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> getListeners()
{
return listeners.entrySet().iterator();
}
protected void postChange(IAEItemStack diff, BaseActionSource src)
{
Iterator<Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> i = getListeners();
while (i.hasNext())
{
Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object> o = i.next();
IMEMonitorHandlerReceiver<IAEItemStack> recv = o.getKey();
if ( recv.isValid( o.getValue() ) )
recv.postChange( null, diff, src );
else
i.remove();
}
}
/**
* add a new Listener to the monitor, be sure to properly remove yourself when your done.
*/
@Override
public void addListener(IMEMonitorHandlerReceiver<IAEItemStack> l, Object verificationToken)
{
listeners.put( l, verificationToken );
}
/**
* remove a Listener to the monitor.
*/
@Override
public void removeListener(IMEMonitorHandlerReceiver<IAEItemStack> l)
{
listeners.remove( l );
}
public WorldCoord min;
public WorldCoord max;
public boolean isDestroyed = false;
2014-06-22 09:00:38 +02:00
private boolean isComplete = true;
class TaskProgress
{
int value;
};
2014-05-14 04:42:14 +02:00
private LinkedList<TileCraftingTile> tiles = new LinkedList();
int accelerator = 0;
private LinkedList<TileCraftingTile> storage = new LinkedList<TileCraftingTile>();
private LinkedList<TileCraftingTile> status = new LinkedList<TileCraftingTile>();
public void getListOfItem(IItemList<IAEItemStack> list, CraftingItemList whichList)
{
switch (whichList)
{
case ACTIVE:
for (IAEItemStack ais : waitingFor)
list.add( ais );
break;
case PENDING:
for (Entry<ICraftingPatternDetails, TaskProgress> t : tasks.entrySet())
{
for (IAEItemStack ais : t.getKey().getCondencedOutputs())
{
ais = ais.copy();
ais.setStackSize( ais.getStackSize() * t.getValue().value );
list.add( ais );
}
}
break;
case STORAGE:
inventory.getAvailableItems( list );
break;
default:
case ALL:
inventory.getAvailableItems( list );
for (IAEItemStack ais : waitingFor)
list.add( ais );
for (Entry<ICraftingPatternDetails, TaskProgress> t : tasks.entrySet())
{
for (IAEItemStack ais : t.getKey().getCondencedOutputs())
{
ais = ais.copy();
ais.setStackSize( ais.getStackSize() * t.getValue().value );
list.add( ais );
}
}
break;
}
}
/**
* crafting job info
*/
MECraftingInventory inventory = new MECraftingInventory();
IAEItemStack finalOutput;
2014-06-22 09:00:38 +02:00
BaseActionSource mySrc;
boolean waiting = false;
Map<ICraftingPatternDetails, TaskProgress> tasks = new HashMap<ICraftingPatternDetails, TaskProgress>();
IItemList<IAEItemStack> waitingFor = AEApi.instance().storage().createItemList();
@Override
public Iterator<IGridHost> getTiles()
{
2014-05-14 04:42:14 +02:00
return (Iterator) tiles.iterator();
}
public CraftingCPUCluster(WorldCoord _min, WorldCoord _max) {
min = _min;
max = _max;
}
@Override
public void updateStatus(boolean updateGrid)
{
2014-05-14 04:42:14 +02:00
for (TileCraftingTile r : tiles)
{
r.updateMeta();
}
}
@Override
public void destroy()
{
if ( isDestroyed )
return;
isDestroyed = true;
2014-05-18 05:19:23 +02:00
boolean posted = false;
2014-05-14 04:42:14 +02:00
for (TileCraftingTile r : tiles)
{
2014-05-18 05:19:23 +02:00
IGridNode n = r.getActionableNode();
if ( n != null && posted == false )
{
IGrid g = n.getGrid();
if ( g != null )
{
g.postEvent( new MENetworkCraftingCpuChange( n ) );
posted = true;
}
}
2014-05-14 04:42:14 +02:00
r.updateStatus( null );
}
}
public void addTile(TileCraftingTile te)
{
tiles.add( te );
if ( te.isStorage() )
storage.add( te );
else if ( te.isStatus() )
status.add( te );
else if ( te.isAccelerator() )
accelerator++;
}
public boolean canAccept(IAEStack input)
{
if ( input instanceof IAEItemStack )
{
IAEItemStack is = waitingFor.findPrecise( (IAEItemStack) input );
if ( is != null && is.getStackSize() > 0 )
return true;
}
return false;
}
public IAEStack injectItems(IAEStack input, Actionable type, BaseActionSource src)
{
if ( input instanceof IAEItemStack && type == Actionable.MODULATE )
{
IAEItemStack what = (IAEItemStack) input;
IAEItemStack is = waitingFor.findPrecise( what );
if ( is != null && is.getStackSize() > 0 )
{
waiting = false;
if ( is.getStackSize() >= input.getStackSize() )
{
is.decStackSize( input.getStackSize() );
2014-06-22 09:00:38 +02:00
// AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() +
// " remaining : "
// + (is.getStackSize() + getRemainingTasks()) + " total left : waiting: " + (waiting ? "yes" :
// "no") );
if ( finalOutput.equals( input ) )
2014-06-22 09:00:38 +02:00
{
finalOutput.decStackSize( input.getStackSize() );
if ( finalOutput.getStackSize() <= 0 )
completeJob();
return input; // ignore it.
2014-06-22 09:00:38 +02:00
}
// 2000
return inventory.injectItems( what, type, src );
}
IAEItemStack insert = what.copy();
insert.setStackSize( is.getStackSize() );
what.decStackSize( is.getStackSize() );
2014-06-22 09:00:38 +02:00
// AELog.info( "Task: " + is.getStackSize() + " remaining : " + getRemainingTasks() + " remaining : " +
// (is.getStackSize() + getRemainingTasks())
// + " total left : waiting: " + (waiting ? "yes" : "no") );
is.setStackSize( 0 );
if ( finalOutput.equals( input ) )
2014-06-22 09:00:38 +02:00
{
finalOutput.decStackSize( input.getStackSize() );
if ( finalOutput.getStackSize() <= 0 )
completeJob();
return input; // ignore it.
2014-06-22 09:00:38 +02:00
}
inventory.injectItems( insert, type, src );
return what;
}
}
return input;
}
2014-06-22 09:00:38 +02:00
public IGrid getGrid()
{
for (TileCraftingTile r : tiles)
return r.getActionableNode().getGrid();
throw new RuntimeException( "No tiles inside a cpu cluster." );
}
private void completeJob()
{
AELog.info( "marking job as complete" );
isComplete = true;
}
private boolean canCraft(ICraftingPatternDetails details, IAEItemStack[] condencedInputs)
{
2014-06-22 09:00:38 +02:00
for (IAEItemStack g : condencedInputs)
{
2014-06-22 09:00:38 +02:00
if ( details.isCraftable() )
{
boolean found = false;
for (IAEItemStack fuzz : inventory.getItemList().findFuzzy( g, FuzzyMode.IGNORE_ALL ))
{
fuzz = fuzz.copy();
fuzz.setStackSize( g.getStackSize() );
IAEItemStack ais = inventory.extractItems( fuzz, Actionable.SIMULATE, null );
ItemStack is = ais == null ? null : ais.getItemStack();
if ( is != null && is.stackSize == g.getStackSize() )
{
found = true;
break;
}
}
if ( !found )
return false;
}
else
{
IAEItemStack ais = inventory.extractItems( g.copy(), Actionable.SIMULATE, null );
ItemStack is = ais == null ? null : ais.getItemStack();
if ( is == null || is.stackSize < g.getStackSize() )
return false;
}
}
2014-06-22 09:00:38 +02:00
return true;
}
2014-06-28 07:20:12 +02:00
public void cancel()
{
isComplete = true;
tasks.clear();
waitingFor.resetStatus();
}
public void updateCraftingLogic(IGrid grid, IEnergyGrid eg, CraftingCache cc)
{
2014-06-22 09:00:38 +02:00
if ( isComplete )
{
if ( inventory.getItemList().isEmpty() )
return;
IStorageGrid sg = grid.getCache( IStorageGrid.class );
IMEInventory<IAEItemStack> ii = sg.getItemInventory();
for (IAEItemStack is : inventory.getItemList())
{
is = inventory.extractItems( is.copy(), Actionable.MODULATE, mySrc );
if ( is != null )
is = ii.injectItems( is, Actionable.MODULATE, mySrc );
if ( is != null )
inventory.injectItems( is, Actionable.MODULATE, mySrc );
}
if ( inventory.getItemList().isEmpty() )
inventory = new MECraftingInventory();
return;
}
waiting = false;
if ( waiting || tasks.isEmpty() ) // nothing to do here...
return;
2014-06-28 07:20:12 +02:00
int remainingOperations = accelerator + 1 + 90;
boolean didsomething = false;
for (Entry<ICraftingPatternDetails, TaskProgress> e : tasks.entrySet())
{
if ( e.getValue().value <= 0 )
continue;
ICraftingPatternDetails details = e.getKey();
2014-06-22 09:00:38 +02:00
if ( canCraft( details, details.getCondencedInputs() ) )
{
InventoryCrafting ic = null;
for (ICraftingMedium m : cc.getMediums( e.getKey() ))
{
if ( e.getValue().value <= 0 )
continue;
if ( !m.isBusy() )
{
if ( ic == null )
{
2014-06-28 07:20:12 +02:00
IAEItemStack[] input = details.getInputs();
double sum = 0;
for (int x = 0; x < input.length; x++)
{
if ( input[x] != null )
sum += input[x].getStackSize();
}
// power...
if ( eg.extractAEPower( sum, Actionable.MODULATE, PowerMultiplier.CONFIG ) < sum - 0.01 )
continue;
ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
2014-06-22 09:00:38 +02:00
boolean found = false;
for (int x = 0; x < input.length; x++)
{
2014-06-22 09:00:38 +02:00
if ( input[x] != null )
{
2014-06-22 09:00:38 +02:00
found = false;
if ( details.isCraftable() )
{
for (IAEItemStack fuzz : inventory.getItemList().findFuzzy( input[x], FuzzyMode.IGNORE_ALL ))
{
fuzz = fuzz.copy();
fuzz.setStackSize( input[x].getStackSize() );
2014-06-22 09:00:38 +02:00
IAEItemStack ais = inventory.extractItems( fuzz, Actionable.MODULATE, mySrc );
ItemStack is = ais == null ? null : ais.getItemStack();
if ( is != null && details.isValidItemForSlot( x, is, getWorld() ) )
{
ic.setInventorySlotContents( x, is );
found = true;
break;
}
}
}
else
{
2014-06-22 09:00:38 +02:00
IAEItemStack ais = inventory.extractItems( input[x].copy(), Actionable.MODULATE, mySrc );
ItemStack is = ais == null ? null : ais.getItemStack();
if ( is != null )
{
ic.setInventorySlotContents( x, is );
found = true;
continue;
}
}
if ( !found )
break;
}
2014-06-22 09:00:38 +02:00
}
if ( !found )
{
// put stuff back..
for (int x = 0; x < ic.getSizeInventory(); x++)
{
ItemStack is = ic.getStackInSlot( x );
if ( is != null )
inventory.injectItems( AEItemStack.create( is ), Actionable.MODULATE, mySrc );
}
ic = null;
break;
}
}
if ( m.pushPattern( details, ic ) )
{
didsomething = true;
remainingOperations--;
for (IAEItemStack out : details.getCondencedOutputs())
waitingFor.add( out.copy() );
2014-06-22 09:00:38 +02:00
if ( details.isCraftable() )
{
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) getWorld() ),
details.getOutput( ic, getWorld() ), ic );
for (int x = 0; x < ic.getSizeInventory(); x++)
{
ItemStack output = Platform.getContainerItem( ic.getStackInSlot( x ) );
if ( output != null )
waitingFor.add( AEItemStack.create( output ) );
}
}
ic = null; // hand off complete!
e.getValue().value--;
if ( e.getValue().value <= 0 )
continue;
if ( remainingOperations == 0 )
return;
}
}
}
if ( ic != null )
{
// put stuff back..
for (int x = 0; x < ic.getSizeInventory(); x++)
{
ItemStack is = ic.getStackInSlot( x );
if ( is != null )
2014-06-22 09:00:38 +02:00
inventory.injectItems( AEItemStack.create( is ), Actionable.MODULATE, mySrc );
}
}
}
}
if ( remainingOperations > 0 && didsomething == false )
waiting = true;
}
private World getWorld()
{
return null;
}
public boolean submitJob(IGrid g, CraftingJob job, BaseActionSource src)
{
if ( !tasks.isEmpty() || !waitingFor.isEmpty() )
return false;
IStorageGrid sg = g.getCache( IStorageGrid.class );
IMEInventory<IAEItemStack> storage = sg.getItemInventory();
MECraftingInventory ci = new MECraftingInventory( storage, true, false, false );
try
{
waitingFor.resetStatus();
job.tree.setJob( ci, this, src );
ci.commit( src );
finalOutput = job.getOutput();
waiting = false;
2014-06-22 09:00:38 +02:00
isComplete = false;
mySrc = src;
return true;
}
catch (CraftBranchFailure e)
{
tasks.clear();
inventory.getItemList().resetStatus();
AELog.error( e );
}
return false;
}
public void addStorage(IAEItemStack extractItems)
{
inventory.injectItems( extractItems, Actionable.MODULATE, null );
}
public void addCrafting(ICraftingPatternDetails details, long crafts)
{
TaskProgress i = tasks.get( details );
if ( i == null )
tasks.put( details, i = new TaskProgress() );
i.value += crafts;
}
2014-06-22 09:00:38 +02:00
public boolean isBusy()
{
Iterator<Entry<ICraftingPatternDetails, TaskProgress>> i = tasks.entrySet().iterator();
while (i.hasNext())
{
if ( i.next().getValue().value <= 0 )
i.remove();
}
return !tasks.isEmpty() || !waitingFor.isEmpty();
}
public IAEItemStack getItemStack(IAEItemStack what, CraftingItemList storage2)
{
IAEItemStack is = null;
switch (storage2)
{
case STORAGE:
is = inventory.getItemList().findPrecise( what );
break;
case ACTIVE:
is = waitingFor.findPrecise( what );
break;
case PENDING:
is = what.copy();
is.setStackSize( 0 );
for (Entry<ICraftingPatternDetails, TaskProgress> t : tasks.entrySet())
{
for (IAEItemStack ais : t.getKey().getCondencedOutputs())
{
if ( ais.equals( is ) )
is.setStackSize( is.getStackSize() + ais.getStackSize() * t.getValue().value );
}
}
break;
default:
case ALL:
throw new RuntimeException( "Invalid Operation" );
}
if ( is != null )
return is.copy();
is = what.copy();
is.setStackSize( 0 );
return is;
}
2014-06-28 07:20:12 +02:00
}