Applied-Energistics-2-tiler.../src/main/java/appeng/crafting/CraftingJob.java

409 lines
10 KiB
Java
Raw Normal View History

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>.
*/
package appeng.crafting;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
2015-12-24 02:07:03 +01:00
import com.google.common.base.Stopwatch;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
2014-06-22 09:00:38 +02:00
import net.minecraft.world.World;
2015-12-24 02:07:03 +01:00
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;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
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;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.security.PlayerSource;
2014-06-05 05:54:01 +02:00
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.DimensionalCoord;
import appeng.core.AELog;
import appeng.hooks.TickHandler;
public class CraftingJob implements Runnable, ICraftingJob
{
private static final String LOG_CRAFTING_JOB = "CraftingJob (%s) issued by %s requesting [%s] using %s bytes took %s ms";
private static final String LOG_MACHINE_SOURCE_DETAILS = "Machine[object=%s, %s]";
private final MECraftingInventory original;
private final World world;
private final IItemList<IAEItemStack> crafting = AEApi.instance().storage().createItemList();
private final IItemList<IAEItemStack> missing = AEApi.instance().storage().createItemList();
private final HashMap<String, TwoIntegers> opsAndMultiplier = new HashMap<String, TwoIntegers>();
private final Object monitor = new Object();
private final Stopwatch watch = Stopwatch.createUnstarted();
private CraftingTreeNode tree;
private final IAEItemStack output;
private boolean simulate = false;
private MECraftingInventory availableCheck;
private long bytes = 0;
private final BaseActionSource actionSrc;
private final ICraftingCallback callback;
private boolean running = false;
private boolean done = false;
private int time = 5;
private int incTime = Integer.MAX_VALUE;
2015-09-30 14:24:40 +02:00
private World wrapWorld( final World w )
{
return w;
2014-06-05 02:47:13 +02:00
}
2015-09-30 14:24:40 +02:00
public CraftingJob( final World w, final IGrid grid, final BaseActionSource actionSrc, final IAEItemStack what, final ICraftingCallback callback )
{
2014-12-29 15:13:47 +01:00
this.world = this.wrapWorld( w );
this.output = what.copy();
this.actionSrc = actionSrc;
2014-06-05 05:54:01 +02:00
this.callback = callback;
2015-09-30 14:24:40 +02:00
final ICraftingGrid cc = grid.getCache( ICraftingGrid.class );
final IStorageGrid sg = grid.getCache( IStorageGrid.class );
2014-12-29 15:13:47 +01:00
this.original = new MECraftingInventory( sg.getItemInventory(), actionSrc, false, false, false );
this.setTree( this.getCraftingTree( cc, what ) );
2014-12-29 15:13:47 +01:00
this.availableCheck = null;
2014-06-06 06:26:01 +02:00
}
2015-09-30 14:24:40 +02:00
private CraftingTreeNode getCraftingTree( final ICraftingGrid cc, final IAEItemStack what )
2014-06-22 09:00:38 +02:00
{
return new CraftingTreeNode( cc, this, what, null, -1, 0 );
2014-06-22 09:00:38 +02:00
}
void refund( final IAEItemStack o )
2014-06-06 06:26:01 +02:00
{
this.availableCheck.injectItems( o, Actionable.MODULATE, this.actionSrc );
2014-06-06 06:26:01 +02:00
}
IAEItemStack checkUse( final IAEItemStack available )
{
return this.availableCheck.extractItems( available, Actionable.MODULATE, this.actionSrc );
}
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound out )
2014-06-06 06:26:01 +02:00
{
}
void addTask( IAEItemStack what, final long crafts, final ICraftingPatternDetails details, final int depth )
{
if( crafts > 0 )
{
what = what.copy();
what.setStackSize( what.getStackSize() * crafts );
2014-12-29 15:13:47 +01:00
this.crafting.add( what );
}
}
void addMissing( IAEItemStack what )
{
what = what.copy();
2014-12-29 15:13:47 +01:00
this.missing.add( what );
}
@Override
public void run()
{
try
{
try
{
2015-01-01 22:13:10 +01:00
TickHandler.INSTANCE.registerCraftingSimulation( this.world, this );
2014-12-29 15:13:47 +01:00
this.handlePausing();
2015-09-30 14:24:40 +02:00
final Stopwatch timer = Stopwatch.createStarted();
2015-09-30 14:24:40 +02:00
final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true );
2014-12-29 15:13:47 +01:00
craftingInventory.ignore( this.output );
2014-12-29 15:13:47 +01:00
this.availableCheck = new MECraftingInventory( this.original, false, false, false );
this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc );
this.getTree().dive( this );
2015-09-30 14:24:40 +02:00
for( final String s : this.opsAndMultiplier.keySet() )
{
2015-09-30 14:24:40 +02:00
final TwoIntegers ti = this.opsAndMultiplier.get( s );
AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) );
}
this.logCraftingJob( "real", timer );
// if ( mode == Actionable.MODULATE )
// craftingInventory.moveItemsToStorage( storage );
}
2015-09-30 14:24:40 +02:00
catch( final CraftBranchFailure e )
{
2014-12-29 15:13:47 +01:00
this.simulate = true;
try
{
2015-09-30 14:24:40 +02:00
final Stopwatch timer = Stopwatch.createStarted();
final MECraftingInventory craftingInventory = new MECraftingInventory( this.original, true, false, true );
2014-12-29 15:13:47 +01:00
craftingInventory.ignore( this.output );
2014-12-29 15:13:47 +01:00
this.availableCheck = new MECraftingInventory( this.original, false, false, false );
this.getTree().setSimulate();
this.getTree().request( craftingInventory, this.output.getStackSize(), this.actionSrc );
this.getTree().dive( this );
2015-09-30 14:24:40 +02:00
for( final String s : this.opsAndMultiplier.keySet() )
{
2015-09-30 14:24:40 +02:00
final TwoIntegers ti = this.opsAndMultiplier.get( s );
AELog.crafting( s + " * " + ti.times + " = " + ( ti.perOp * ti.times ) );
}
this.logCraftingJob( "simulate", timer );
}
2015-09-30 14:24:40 +02:00
catch( final CraftBranchFailure e1 )
{
AELog.debug( e1 );
}
2015-09-30 14:24:40 +02:00
catch( final CraftingCalculationFailure f )
{
AELog.debug( f );
}
2015-09-30 14:24:40 +02:00
catch( final InterruptedException e1 )
{
AELog.crafting( "Crafting calculation canceled." );
2014-12-29 15:13:47 +01:00
this.finish();
return;
}
}
2015-09-30 14:24:40 +02:00
catch( final CraftingCalculationFailure f )
{
AELog.debug( f );
}
2015-09-30 14:24:40 +02:00
catch( final InterruptedException e1 )
{
AELog.crafting( "Crafting calculation canceled." );
2014-12-29 15:13:47 +01:00
this.finish();
return;
}
AELog.craftingDebug( "crafting job now done" );
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
{
2014-12-29 15:13:47 +01:00
this.finish();
throw new IllegalStateException( t );
}
2014-12-29 15:13:47 +01:00
this.finish();
}
void handlePausing() throws InterruptedException
{
if( this.incTime > 100 )
{
this.incTime = 0;
synchronized( this.monitor )
{
if( this.watch.elapsed( TimeUnit.MICROSECONDS ) > this.time )
{
this.running = false;
this.watch.stop();
this.monitor.notify();
}
if( !this.running )
{
AELog.craftingDebug( "crafting job will now sleep" );
while( !this.running )
{
this.monitor.wait();
}
AELog.craftingDebug( "crafting job now active" );
}
}
if( Thread.interrupted() )
2015-04-29 02:30:53 +02:00
{
throw new InterruptedException();
2015-04-29 02:30:53 +02:00
}
}
this.incTime++;
}
private void finish()
{
if( this.callback != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.callback.calculationComplete( this );
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.availableCheck = null;
synchronized( this.monitor )
{
2014-12-29 15:13:47 +01:00
this.running = false;
this.done = true;
this.monitor.notify();
}
}
@Override
2014-06-22 09:00:38 +02:00
public boolean isSimulation()
{
2014-12-29 15:13:47 +01:00
return this.simulate;
2014-06-22 09:00:38 +02:00
}
@Override
public long getByteTotal()
{
return this.bytes;
}
@Override
2015-09-30 14:24:40 +02:00
public void populatePlan( final IItemList<IAEItemStack> plan )
{
if( this.getTree() != null )
2015-04-29 02:30:53 +02:00
{
this.getTree().getPlan( plan );
2015-04-29 02:30:53 +02:00
}
}
@Override
public IAEItemStack getOutput()
{
return this.output;
}
public boolean isDone()
{
2014-12-29 15:13:47 +01:00
return this.done;
}
World getWorld()
2014-06-22 09:00:38 +02:00
{
2014-12-29 15:13:47 +01:00
return this.world;
2014-06-22 09:00:38 +02:00
}
/**
* returns true if this needs more simulation.
2015-02-03 12:04:13 +01:00
*
2014-09-27 23:17:47 +02:00
* @param milli milliseconds of simulation
*
2014-09-27 23:17:47 +02:00
* @return true if this needs more simulation
*/
2015-09-30 14:24:40 +02:00
public boolean simulateFor( final int milli )
{
2014-12-29 15:13:47 +01:00
this.time = milli;
synchronized( this.monitor )
{
if( this.done )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
this.watch.reset();
this.watch.start();
this.running = true;
AELog.craftingDebug( "main thread is now going to sleep" );
2014-12-29 15:13:47 +01:00
this.monitor.notify();
while( this.running )
{
try
{
2014-12-29 15:13:47 +01:00
this.monitor.wait();
}
2015-09-30 14:24:40 +02:00
catch( final InterruptedException ignored )
{
}
}
AELog.craftingDebug( "main thread is now active" );
}
return true;
}
void addBytes( final long crafts )
{
2014-12-29 15:13:47 +01:00
this.bytes += crafts;
}
public CraftingTreeNode getTree()
{
return this.tree;
}
private void setTree( final CraftingTreeNode tree )
{
this.tree = tree;
}
private void logCraftingJob( String type, Stopwatch timer )
{
if( AELog.isCraftingLogEnabled() )
{
final String itemToOutput = this.output.toString();
final long elapsedTime = timer.elapsed( TimeUnit.MILLISECONDS );
final String actionSource;
if( this.actionSrc instanceof MachineSource )
{
final IActionHost machineSource = ( (MachineSource) this.actionSrc ).via;
final IGridNode actionableNode = machineSource.getActionableNode();
final IGridHost machine = actionableNode.getMachine();
final DimensionalCoord location = actionableNode.getGridBlock().getLocation();
actionSource = String.format( LOG_MACHINE_SOURCE_DETAILS, machine, location );
}
else if( this.actionSrc instanceof PlayerSource )
{
final PlayerSource source = (PlayerSource) this.actionSrc;
final EntityPlayer player = source.player;
actionSource = player.toString();
}
else
{
actionSource = "[unknown source]";
}
AELog.crafting( LOG_CRAFTING_JOB, type, actionSource, itemToOutput, this.bytes, elapsedTime );
}
}
private static class TwoIntegers
{
private final long perOp = 0;
private final long times = 0;
}
}