2014-06-04 06:37:47 +02:00
|
|
|
package appeng.crafting;
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
import java.util.HashMap;
|
2014-06-04 06:37:47 +02:00
|
|
|
import java.util.HashSet;
|
2014-06-16 07:48:43 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2014-06-04 06:37:47 +02:00
|
|
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-06-22 09:00:38 +02:00
|
|
|
import net.minecraft.world.World;
|
2014-06-05 05:54:01 +02:00
|
|
|
import appeng.api.AEApi;
|
2014-06-05 02:47:13 +02:00
|
|
|
import appeng.api.config.Actionable;
|
2014-07-03 06:48:18 +02:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.crafting.ICraftingCallback;
|
|
|
|
import appeng.api.networking.crafting.ICraftingGrid;
|
|
|
|
import appeng.api.networking.crafting.ICraftingJob;
|
2014-06-06 06:26:01 +02:00
|
|
|
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
2014-06-18 08:23:37 +02:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
2014-06-05 05:54:01 +02:00
|
|
|
import appeng.api.networking.storage.IStorageGrid;
|
2014-06-04 06:37:47 +02:00
|
|
|
import appeng.api.storage.data.IAEItemStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
2014-06-16 07:48:43 +02:00
|
|
|
import appeng.core.AELog;
|
2014-06-23 06:50:56 +02:00
|
|
|
import appeng.hooks.TickHandler;
|
2014-06-04 06:37:47 +02:00
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
import com.google.common.base.Stopwatch;
|
2014-06-04 06:37:47 +02:00
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
public class CraftingJob implements Runnable, ICraftingJob
|
2014-06-04 06:37:47 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
IAEItemStack output;
|
|
|
|
|
|
|
|
IItemList<IAEItemStack> storage;
|
|
|
|
|
|
|
|
HashSet<IAEItemStack> prophecies;
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
boolean simulate = false;
|
|
|
|
final MECraftingInventory original;
|
|
|
|
|
2014-07-17 03:31:23 +02:00
|
|
|
MECraftingInventory availableCheck;
|
2014-06-18 08:23:37 +02:00
|
|
|
public CraftingTreeNode tree;
|
|
|
|
private BaseActionSource actionSrc;
|
2014-07-03 06:48:18 +02:00
|
|
|
private ICraftingCallback callback;
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
long bytes = 0;
|
2014-06-22 09:00:38 +02:00
|
|
|
World world;
|
2014-06-18 08:23:37 +02:00
|
|
|
|
2014-06-19 08:28:45 +02:00
|
|
|
public IAEItemStack getOutput()
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2014-09-19 03:31:11 +02:00
|
|
|
public CraftingJob(World w, NBTTagCompound data)
|
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
world = wrapWorld( w );
|
2014-06-05 05:54:01 +02:00
|
|
|
storage = AEApi.instance().storage().createItemList();
|
|
|
|
prophecies = new HashSet();
|
2014-06-18 08:23:37 +02:00
|
|
|
original = null;
|
2014-06-20 09:12:39 +02:00
|
|
|
availableCheck = null;
|
|
|
|
}
|
|
|
|
|
2014-07-18 06:02:19 +02:00
|
|
|
public void refund(IAEItemStack o)
|
|
|
|
{
|
|
|
|
availableCheck.injectItems( o, Actionable.MODULATE, this.actionSrc );
|
|
|
|
}
|
|
|
|
|
2014-06-20 09:12:39 +02:00
|
|
|
public IAEItemStack checkUse(IAEItemStack available)
|
|
|
|
{
|
|
|
|
return availableCheck.extractItems( available, Actionable.MODULATE, this.actionSrc );
|
2014-06-05 02:47:13 +02:00
|
|
|
}
|
|
|
|
|
2014-09-19 03:31:11 +02:00
|
|
|
public CraftingJob(World w, IGrid grid, BaseActionSource actionSrc, IAEItemStack what, ICraftingCallback callback)
|
|
|
|
{
|
2014-06-22 09:00:38 +02:00
|
|
|
world = wrapWorld( w );
|
2014-06-05 05:54:01 +02:00
|
|
|
output = what.copy();
|
|
|
|
storage = AEApi.instance().storage().createItemList();
|
|
|
|
prophecies = new HashSet();
|
2014-07-03 06:48:18 +02:00
|
|
|
this.actionSrc = actionSrc;
|
2014-06-05 05:54:01 +02:00
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
this.callback = callback;
|
|
|
|
ICraftingGrid cc = grid.getCache( ICraftingGrid.class );
|
|
|
|
IStorageGrid sg = grid.getCache( IStorageGrid.class );
|
2014-09-19 18:20:06 +02:00
|
|
|
original = new MECraftingInventory( sg.getItemInventory(), actionSrc, false, false, false );
|
2014-09-19 03:31:11 +02:00
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
tree = getCraftingTree( cc, what );
|
2014-07-17 03:31:23 +02:00
|
|
|
availableCheck = null;
|
2014-06-06 06:26:01 +02:00
|
|
|
}
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
private World wrapWorld(World w)
|
|
|
|
{
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
private CraftingTreeNode getCraftingTree(ICraftingGrid cc, IAEItemStack what)
|
2014-06-06 06:26:01 +02:00
|
|
|
{
|
2014-06-17 05:36:35 +02:00
|
|
|
return new CraftingTreeNode( cc, this, what, null, -1, 0 );
|
2014-06-06 06:26:01 +02:00
|
|
|
}
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
public long getByteTotal()
|
|
|
|
{
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
public void writeToNBT(NBTTagCompound out)
|
2014-06-06 06:26:01 +02:00
|
|
|
{
|
|
|
|
|
2014-06-04 06:37:47 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
IItemList<IAEItemStack> crafting = AEApi.instance().storage().createItemList();
|
|
|
|
IItemList<IAEItemStack> missing = AEApi.instance().storage().createItemList();
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
public void addTask(IAEItemStack what, long crafts, ICraftingPatternDetails details, int depth)
|
2014-06-04 06:37:47 +02:00
|
|
|
{
|
2014-06-16 07:48:43 +02:00
|
|
|
if ( crafts > 0 )
|
2014-06-04 06:37:47 +02:00
|
|
|
{
|
2014-06-18 08:23:37 +02:00
|
|
|
what = what.copy();
|
|
|
|
what.setStackSize( what.getStackSize() * crafts );
|
|
|
|
crafting.add( what );
|
2014-06-04 06:37:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-16 07:48:43 +02:00
|
|
|
public void addMissing(IAEItemStack what)
|
2014-06-04 06:37:47 +02:00
|
|
|
{
|
2014-06-18 08:23:37 +02:00
|
|
|
what = what.copy();
|
|
|
|
missing.add( what );
|
2014-06-04 06:37:47 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 05:36:35 +02:00
|
|
|
class twoIntegers
|
|
|
|
{
|
|
|
|
|
|
|
|
public long perOp = 0;
|
|
|
|
public long times = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
HashMap<String, twoIntegers> opsAndMultiplier = new HashMap();
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-06-23 06:50:56 +02:00
|
|
|
TickHandler.instance.registerCraftingSimulation( world, this );
|
|
|
|
handlepausing();
|
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
Stopwatch timer = Stopwatch.createStarted();
|
2014-06-23 06:50:56 +02:00
|
|
|
|
2014-06-18 08:23:37 +02:00
|
|
|
MECraftingInventory meci = new MECraftingInventory( original, true, false, true );
|
|
|
|
meci.ignore( output );
|
|
|
|
|
2014-07-17 03:31:23 +02:00
|
|
|
availableCheck = new MECraftingInventory( original, false, false, false );
|
2014-06-18 08:23:37 +02:00
|
|
|
tree.request( meci, output.getStackSize(), actionSrc );
|
|
|
|
tree.dive( this );
|
|
|
|
|
|
|
|
for (String s : opsAndMultiplier.keySet())
|
|
|
|
{
|
|
|
|
twoIntegers ti = opsAndMultiplier.get( s );
|
2014-07-05 00:43:53 +02:00
|
|
|
AELog.crafting( s + " * " + ti.times + " = " + (ti.perOp * ti.times) );
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 00:43:53 +02:00
|
|
|
AELog.crafting( "------------- " + getByteTotal() + "b real" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" );
|
2014-06-23 06:50:56 +02:00
|
|
|
// if ( mode == Actionable.MODULATE )
|
|
|
|
// meci.moveItemsToStorage( storage );
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
2014-06-23 06:50:56 +02:00
|
|
|
catch (CraftBranchFailure e)
|
2014-06-18 08:23:37 +02:00
|
|
|
{
|
2014-06-23 06:50:56 +02:00
|
|
|
simulate = true;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Stopwatch timer = Stopwatch.createStarted();
|
|
|
|
MECraftingInventory meci = new MECraftingInventory( original, true, false, true );
|
|
|
|
meci.ignore( output );
|
|
|
|
|
2014-07-17 03:31:23 +02:00
|
|
|
availableCheck = new MECraftingInventory( original, false, false, false );
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
tree.setSimulate();
|
|
|
|
tree.request( meci, output.getStackSize(), actionSrc );
|
|
|
|
tree.dive( this );
|
|
|
|
|
|
|
|
for (String s : opsAndMultiplier.keySet())
|
|
|
|
{
|
|
|
|
twoIntegers ti = opsAndMultiplier.get( s );
|
2014-07-05 00:43:53 +02:00
|
|
|
AELog.crafting( s + " * " + ti.times + " = " + (ti.perOp * ti.times) );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 00:43:53 +02:00
|
|
|
AELog.crafting( "------------- " + getByteTotal() + "b simulate" + timer.elapsed( TimeUnit.MILLISECONDS ) + "ms" );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
|
|
|
catch (CraftBranchFailure e1)
|
|
|
|
{
|
|
|
|
AELog.error( e1 );
|
|
|
|
}
|
|
|
|
catch (CraftingCalculationFailure f)
|
|
|
|
{
|
|
|
|
AELog.error( f );
|
|
|
|
}
|
|
|
|
catch (InterruptedException e1)
|
|
|
|
{
|
2014-07-05 00:43:53 +02:00
|
|
|
AELog.crafting( "Crafting calculation canceled." );
|
2014-06-23 06:50:56 +02:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
|
|
|
catch (CraftingCalculationFailure f)
|
|
|
|
{
|
|
|
|
AELog.error( f );
|
|
|
|
}
|
|
|
|
catch (InterruptedException e1)
|
|
|
|
{
|
2014-07-05 00:43:53 +02:00
|
|
|
AELog.crafting( "Crafting calculation canceled." );
|
2014-06-23 06:50:56 +02:00
|
|
|
finish();
|
2014-06-18 08:23:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-23 06:50:56 +02:00
|
|
|
|
|
|
|
log( "crafting job now done" );
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
2014-06-23 06:50:56 +02:00
|
|
|
catch (Throwable t)
|
2014-06-18 08:23:37 +02:00
|
|
|
{
|
2014-06-23 06:50:56 +02:00
|
|
|
finish();
|
|
|
|
throw new RuntimeException( t );
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
2014-06-23 06:50:56 +02:00
|
|
|
|
|
|
|
finish();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void finish()
|
|
|
|
{
|
2014-07-03 06:48:18 +02:00
|
|
|
if ( callback != null )
|
|
|
|
callback.calculationComplete( this );
|
|
|
|
|
2014-07-17 03:31:23 +02:00
|
|
|
availableCheck = null;
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
synchronized (monitor)
|
2014-06-18 08:23:37 +02:00
|
|
|
{
|
2014-06-23 06:50:56 +02:00
|
|
|
running = false;
|
|
|
|
done = true;
|
|
|
|
monitor.notify();
|
2014-06-18 08:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
2014-06-19 08:28:45 +02:00
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
public boolean isSimulation()
|
|
|
|
{
|
|
|
|
return simulate;
|
|
|
|
}
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
public boolean isDone()
|
|
|
|
{
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2014-06-22 09:00:38 +02:00
|
|
|
public World getWorld()
|
|
|
|
{
|
|
|
|
return world;
|
|
|
|
}
|
|
|
|
|
2014-06-23 06:50:56 +02:00
|
|
|
private boolean running = false;
|
|
|
|
private boolean done = false;
|
|
|
|
private Object monitor = new Object();
|
|
|
|
private Stopwatch watch = Stopwatch.createUnstarted();
|
|
|
|
private int time = 5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns true if this needs more simulation.
|
|
|
|
*
|
|
|
|
* @param milli
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean simulateFor(int milli)
|
|
|
|
{
|
|
|
|
time = milli;
|
|
|
|
|
|
|
|
synchronized (monitor)
|
|
|
|
{
|
|
|
|
if ( isDone() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
watch.reset();
|
|
|
|
watch.start();
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
log( "main thread is now going to sleep" );
|
|
|
|
|
|
|
|
monitor.notify();
|
|
|
|
|
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
monitor.wait();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log( "main thread is now active" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int incTime = Integer.MAX_VALUE;
|
|
|
|
|
|
|
|
public void handlepausing() throws InterruptedException
|
|
|
|
{
|
|
|
|
if ( incTime++ > 100 )
|
|
|
|
{
|
|
|
|
incTime = 0;
|
|
|
|
|
|
|
|
synchronized (monitor)
|
|
|
|
{
|
|
|
|
if ( watch.elapsed( TimeUnit.MICROSECONDS ) > time )
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
watch.stop();
|
|
|
|
monitor.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !running )
|
|
|
|
{
|
|
|
|
log( "crafting job will now sleep" );
|
|
|
|
|
|
|
|
while (!running)
|
|
|
|
{
|
|
|
|
monitor.wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
log( "crafting job now active" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Thread.interrupted() )
|
|
|
|
throw new InterruptedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void log(String string)
|
|
|
|
{
|
2014-07-05 00:43:53 +02:00
|
|
|
// AELog.crafting( string );
|
2014-06-23 06:50:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addBytes(long crafts)
|
|
|
|
{
|
|
|
|
bytes += crafts;
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:48:18 +02:00
|
|
|
@Override
|
|
|
|
public void populatePlan(IItemList<IAEItemStack> plan)
|
|
|
|
{
|
|
|
|
if ( tree != null )
|
|
|
|
tree.getPlan( plan );
|
|
|
|
}
|
|
|
|
|
2014-06-04 06:37:47 +02:00
|
|
|
}
|