2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
package appeng.crafting;
|
|
|
|
|
2014-11-28 04:36:46 +01:00
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
import net.minecraft.inventory.InventoryCrafting;
|
2014-06-17 05:36:35 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.world.World;
|
2014-06-22 09:00:38 +02:00
|
|
|
import net.minecraft.world.WorldServer;
|
2014-11-28 04:36:46 +01:00
|
|
|
|
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.api.config.Actionable;
|
2014-07-03 06:48:18 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingGrid;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
2014-06-28 22:18:36 +02:00
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-06-22 09:00:38 +02:00
|
|
|
import appeng.container.ContainerNull;
|
2014-06-19 08:28:45 +02:00
|
|
|
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
2014-06-22 09:00:38 +02:00
|
|
|
import appeng.util.Platform;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
|
|
|
public class CraftingTreeProcess
|
|
|
|
{
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
World world;
|
2014-09-29 09:54:34 +02:00
|
|
|
final CraftingTreeNode parent;
|
|
|
|
final ICraftingPatternDetails details;
|
|
|
|
final CraftingJob job;
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
long crafts = 0;
|
2014-09-07 01:36:56 +02:00
|
|
|
boolean containerItems;
|
|
|
|
boolean limitQty;
|
2014-09-28 11:47:17 +02:00
|
|
|
boolean fullSimulation;
|
2014-06-17 05:36:35 +02:00
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
private long bytes = 0;
|
2014-06-16 07:48:43 +02:00
|
|
|
final private int depth;
|
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
final Map<CraftingTreeNode, Long> nodes = new HashMap<CraftingTreeNode, Long>();
|
2014-06-16 07:48:43 +02:00
|
|
|
public boolean possible = true;
|
|
|
|
|
2014-11-28 04:36:46 +01:00
|
|
|
public CraftingTreeProcess( ICraftingGrid cc, CraftingJob job, ICraftingPatternDetails details, CraftingTreeNode craftingTreeNode, int depth ) {
|
2014-12-29 15:13:47 +01:00
|
|
|
this.parent = craftingTreeNode;
|
2014-06-16 07:48:43 +02:00
|
|
|
this.details = details;
|
|
|
|
this.job = job;
|
|
|
|
this.depth = depth;
|
2014-11-28 04:36:46 +01:00
|
|
|
World world = job.getWorld();
|
2014-06-16 07:48:43 +02:00
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
if ( details.isCraftable() )
|
|
|
|
{
|
|
|
|
IAEItemStack list[] = details.getInputs();
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
|
|
|
IAEItemStack[] is = details.getInputs();
|
|
|
|
for (int x = 0; x < ic.getSizeInventory(); x++)
|
|
|
|
ic.setInventorySlotContents( x, is[x] == null ? null : is[x].getItemStack() );
|
|
|
|
|
|
|
|
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) world ), details.getOutput( ic, world ), ic );
|
|
|
|
|
|
|
|
for (int x = 0; x < ic.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
ItemStack g = ic.getStackInSlot( x );
|
|
|
|
if ( g != null && g.stackSize > 1 )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.fullSimulation = true;
|
2014-06-22 09:00:38 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 22:30:31 +02:00
|
|
|
for ( IAEItemStack part : details.getCondensedInputs() )
|
2014-06-17 05:36:35 +02:00
|
|
|
{
|
2014-09-15 20:14:53 +02:00
|
|
|
ItemStack g = part.getItemStack();
|
2014-06-22 09:00:38 +02:00
|
|
|
|
2014-09-15 20:14:53 +02:00
|
|
|
boolean isAnInput = false;
|
2014-09-20 22:30:12 +02:00
|
|
|
for ( IAEItemStack a : details.getCondensedOutputs() )
|
2014-09-15 20:14:53 +02:00
|
|
|
{
|
|
|
|
if ( g != null && a != null && a.equals( g ) )
|
|
|
|
isAnInput = true;
|
|
|
|
}
|
2014-09-07 01:36:56 +02:00
|
|
|
|
2014-09-15 20:14:53 +02:00
|
|
|
if ( isAnInput )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.limitQty = true;
|
2014-09-07 01:36:56 +02:00
|
|
|
|
2014-09-15 20:14:53 +02:00
|
|
|
if ( g.getItem().hasContainerItem( g ) )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.limitQty = this.containerItems = true;
|
2014-07-14 04:21:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean complicated = false;
|
2014-06-22 09:00:38 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.containerItems || complicated )
|
2014-07-14 04:21:23 +02:00
|
|
|
{
|
|
|
|
for (int x = 0; x < list.length; x++)
|
|
|
|
{
|
|
|
|
IAEItemStack part = list[x];
|
|
|
|
if ( part != null )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() );
|
2014-07-14 04:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-04 12:32:33 +01:00
|
|
|
// this is minor different then below, this slot uses the pattern, but kinda fudges it.
|
2014-09-20 22:30:31 +02:00
|
|
|
for (IAEItemStack part : details.getCondensedInputs())
|
2014-07-14 04:21:23 +02:00
|
|
|
{
|
2014-07-14 04:43:00 +02:00
|
|
|
for (int x = 0; x < list.length; x++)
|
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
IAEItemStack comparePart = list[x];
|
|
|
|
if ( part != null && part.equals( comparePart ) )
|
2014-07-14 04:43:00 +02:00
|
|
|
{
|
|
|
|
// use the first slot...
|
2014-12-29 15:13:47 +01:00
|
|
|
this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, x, depth + 1 ), part.getStackSize() );
|
2014-07-14 04:43:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 05:36:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-20 22:30:31 +02:00
|
|
|
for ( IAEItemStack part : details.getCondensedInputs() )
|
2014-09-07 01:36:56 +02:00
|
|
|
{
|
2014-09-15 20:14:53 +02:00
|
|
|
ItemStack g = part.getItemStack();
|
2014-09-07 01:36:56 +02:00
|
|
|
|
2014-09-15 20:14:53 +02:00
|
|
|
boolean isAnInput = false;
|
2014-09-20 22:30:12 +02:00
|
|
|
for (IAEItemStack a : details.getCondensedOutputs())
|
2014-09-15 20:14:53 +02:00
|
|
|
{
|
|
|
|
if ( g != null && a != null && a.equals( g ) )
|
|
|
|
isAnInput = true;
|
2014-09-07 01:36:56 +02:00
|
|
|
}
|
2014-09-15 20:14:53 +02:00
|
|
|
|
|
|
|
if ( isAnInput )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.limitQty = true;
|
2014-09-07 01:36:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-20 22:30:31 +02:00
|
|
|
for (IAEItemStack part : details.getCondensedInputs())
|
2014-06-17 05:36:35 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.nodes.put( new CraftingTreeNode( cc, job, part.copy(), this, -1, depth + 1 ), part.getStackSize() );
|
2014-06-17 05:36:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 01:20:13 +02:00
|
|
|
public boolean notRecursive(ICraftingPatternDetails details)
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.parent == null || this.parent.notRecursive( details );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
long getTimes(long remaining, long stackSize)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.limitQty || this.fullSimulation )
|
2014-06-17 05:36:35 +02:00
|
|
|
return 1;
|
|
|
|
return (remaining / stackSize) + (remaining % stackSize != 0 ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
IAEItemStack getAmountCrafted(IAEItemStack what2)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IAEItemStack is : this.details.getCondensedOutputs())
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
|
|
|
if ( is.equals( what2 ) )
|
|
|
|
{
|
|
|
|
what2 = what2.copy();
|
|
|
|
what2.setStackSize( is.getStackSize() );
|
|
|
|
return what2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 06:00:12 +02:00
|
|
|
// more fuzzy!
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IAEItemStack is : this.details.getCondensedOutputs())
|
2014-08-02 06:00:12 +02:00
|
|
|
{
|
|
|
|
if ( is.getItem() == what2.getItem() && (is.getItem().isDamageable() || is.getItemDamage() == what2.getItemDamage()) )
|
|
|
|
{
|
|
|
|
what2 = is.copy();
|
|
|
|
what2.setStackSize( is.getStackSize() );
|
|
|
|
return what2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
throw new RuntimeException( "Crafting Tree construction failed." );
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
public void request(MECraftingInventory inv, long i, BaseActionSource src) throws CraftBranchFailure, InterruptedException
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.job.handlePausing();
|
2014-06-18 08:23:37 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.fullSimulation )
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
InventoryCrafting ic = new InventoryCrafting( new ContainerNull(), 3, 3 );
|
2014-06-17 05:36:35 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
for (Entry<CraftingTreeNode, Long> entry : this.nodes.entrySet())
|
2014-06-17 05:36:35 +02:00
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
IAEItemStack item = entry.getKey().getStack( entry.getValue() );
|
|
|
|
IAEItemStack stack = entry.getKey().request( inv, item.getStackSize(), src );
|
|
|
|
|
|
|
|
ic.setInventorySlotContents( entry.getKey().slot, stack.getItemStack() );
|
|
|
|
}
|
2014-06-17 05:36:35 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
FMLCommonHandler.instance().firePlayerCraftingEvent( Platform.getPlayer( (WorldServer) this.world ), this.details.getOutput( ic, this.world ), ic );
|
2014-06-22 09:00:38 +02:00
|
|
|
|
|
|
|
for (int x = 0; x < ic.getSizeInventory(); x++)
|
|
|
|
{
|
|
|
|
ItemStack is = ic.getStackInSlot( x );
|
|
|
|
is = Platform.getContainerItem( is );
|
|
|
|
|
|
|
|
IAEItemStack o = AEApi.instance().storage().createItemStack( is );
|
|
|
|
if ( o != null )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.bytes++;
|
2014-06-22 09:00:38 +02:00
|
|
|
inv.injectItems( o, Actionable.MODULATE, src );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
2014-06-22 09:00:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// request and remove inputs...
|
2014-12-29 15:13:47 +01:00
|
|
|
for (Entry<CraftingTreeNode, Long> entry : this.nodes.entrySet())
|
2014-06-22 09:00:38 +02:00
|
|
|
{
|
|
|
|
IAEItemStack item = entry.getKey().getStack( entry.getValue() );
|
|
|
|
IAEItemStack stack = entry.getKey().request( inv, item.getStackSize() * i, src );
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
if ( this.containerItems )
|
2014-06-22 09:00:38 +02:00
|
|
|
{
|
|
|
|
ItemStack is = Platform.getContainerItem( stack.getItemStack() );
|
2014-06-17 05:36:35 +02:00
|
|
|
IAEItemStack o = AEApi.instance().storage().createItemStack( is );
|
|
|
|
if ( o != null )
|
2014-06-23 06:50:56 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.bytes++;
|
2014-06-17 05:36:35 +02:00
|
|
|
inv.injectItems( o, Actionable.MODULATE, src );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
2014-06-17 05:36:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// assume its possible.
|
|
|
|
|
|
|
|
// add crafting results..
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IAEItemStack out : this.details.getCondensedOutputs())
|
2014-06-16 07:48:43 +02:00
|
|
|
{
|
|
|
|
IAEItemStack o = out.copy();
|
|
|
|
o.setStackSize( o.getStackSize() * i );
|
|
|
|
inv.injectItems( o, Actionable.MODULATE, src );
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.crafts += i;
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void dive(CraftingJob job)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
job.addTask( this.getAmountCrafted( this.parent.getStack( 1 ) ), this.crafts, this.details, this.depth );
|
|
|
|
for (CraftingTreeNode pro : this.nodes.keySet())
|
2014-06-16 07:48:43 +02:00
|
|
|
pro.dive( job );
|
2014-06-23 06:50:56 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
job.addBytes( 8 + this.crafts + this.bytes );
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|
2014-06-18 08:23:37 +02:00
|
|
|
|
|
|
|
public void setSimulate()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.crafts = 0;
|
|
|
|
this.bytes = 0;
|
2014-06-18 08:23:37 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
for (CraftingTreeNode pro : this.nodes.keySet())
|
2014-06-18 08:23:37 +02:00
|
|
|
pro.setSimulate();
|
|
|
|
}
|
2014-06-19 08:28:45 +02:00
|
|
|
|
|
|
|
public void setJob(MECraftingInventory storage, CraftingCPUCluster craftingCPUCluster, BaseActionSource src) throws CraftBranchFailure
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
craftingCPUCluster.addCrafting( this.details, this.crafts );
|
2014-06-19 08:28:45 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
for (CraftingTreeNode pro : this.nodes.keySet())
|
2014-06-19 08:28:45 +02:00
|
|
|
pro.setJob( storage, craftingCPUCluster, src );
|
|
|
|
}
|
2014-06-28 22:18:36 +02:00
|
|
|
|
|
|
|
public void getPlan(IItemList<IAEItemStack> plan)
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
for (IAEItemStack i : this.details.getOutputs())
|
2014-06-28 22:18:36 +02:00
|
|
|
{
|
|
|
|
i = i.copy();
|
2014-12-29 15:13:47 +01:00
|
|
|
i.setCountRequestable( i.getStackSize() * this.crafts );
|
2014-06-28 22:18:36 +02:00
|
|
|
plan.addRequestable( i );
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
for (CraftingTreeNode pro : this.nodes.keySet())
|
2014-06-28 22:18:36 +02:00
|
|
|
pro.getPlan( plan );
|
|
|
|
}
|
2014-06-16 07:48:43 +02:00
|
|
|
}
|