2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.EnumSet;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.exceptions.FailedConnection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.IGridConnection;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.events.MENetworkChannelsChanged;
|
|
|
|
import appeng.api.networking.pathing.IPathingGrid;
|
|
|
|
import appeng.api.util.IReadOnlyCollection;
|
2014-09-05 05:08:07 +02:00
|
|
|
import appeng.core.AEConfig;
|
|
|
|
import appeng.core.AELog;
|
|
|
|
import appeng.core.features.AEFeature;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.me.pathfinding.IPathItem;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.util.Platform;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.util.ReadOnlyCollection;
|
|
|
|
|
|
|
|
public class GridConnection implements IGridConnection, IPathItem
|
|
|
|
{
|
|
|
|
|
|
|
|
final static private MENetworkChannelsChanged event = new MENetworkChannelsChanged();
|
|
|
|
|
|
|
|
private GridNode sideA;
|
|
|
|
private ForgeDirection fromAtoB;
|
|
|
|
private GridNode sideB;
|
|
|
|
|
2014-04-23 04:49:03 +02:00
|
|
|
Object visitorIterationNumber = null;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
public int channelData = 0;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
public GridConnection(IGridNode aNode, IGridNode bNode, ForgeDirection fromAtoB) throws FailedConnection {
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
GridNode a = (GridNode) aNode;
|
|
|
|
GridNode b = (GridNode) bNode;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
if ( Platform.securityCheck( a, b ) )
|
2014-09-05 05:08:07 +02:00
|
|
|
{
|
|
|
|
if ( AEConfig.instance.isFeatureEnabled( AEFeature.LogSecurityAudits ) )
|
|
|
|
{
|
|
|
|
AELog.info( "Audit Failed 1: " + a.getGridBlock().getLocation() );
|
|
|
|
AELog.info( "Audit Failed 2: " + b.getGridBlock().getLocation() );
|
|
|
|
}
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
throw new FailedConnection();
|
2014-09-05 05:08:07 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( a == null || b == null )
|
2014-09-21 01:26:11 +02:00
|
|
|
throw new GridException( "Connection Forged Between null entities." );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
if ( a.hasConnection( b ) || b.hasConnection( a ) )
|
|
|
|
throw new GridException( "Connection already exists." );
|
|
|
|
|
|
|
|
sideA = a;
|
|
|
|
this.fromAtoB = fromAtoB;
|
|
|
|
sideB = b;
|
|
|
|
|
|
|
|
if ( b.myGrid == null )
|
|
|
|
{
|
|
|
|
b.setGrid( a.getInternalGrid() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( a.myGrid == null )
|
|
|
|
{
|
|
|
|
GridPropagator gp = new GridPropagator( b.getInternalGrid() );
|
2014-09-20 22:37:36 +02:00
|
|
|
a.beginVisit( gp );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else if ( b.myGrid == null )
|
|
|
|
{
|
|
|
|
GridPropagator gp = new GridPropagator( a.getInternalGrid() );
|
2014-09-20 22:37:36 +02:00
|
|
|
b.beginVisit( gp );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
2014-06-08 18:26:33 +02:00
|
|
|
else if ( isNetworkABetter( a, b ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
GridPropagator gp = new GridPropagator( a.getInternalGrid() );
|
2014-09-20 22:37:36 +02:00
|
|
|
b.beginVisit( gp );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GridPropagator gp = new GridPropagator( b.getInternalGrid() );
|
2014-09-20 22:37:36 +02:00
|
|
|
a.beginVisit( gp );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a connection was destroyed RE-PATH!!
|
|
|
|
IPathingGrid p = sideA.getInternalGrid().getCache( IPathingGrid.class );
|
|
|
|
p.repath();
|
|
|
|
|
|
|
|
sideA.addConnection( this );
|
|
|
|
sideB.addConnection( this );
|
|
|
|
}
|
|
|
|
|
2014-06-08 18:26:33 +02:00
|
|
|
private boolean isNetworkABetter(GridNode a, GridNode b)
|
|
|
|
{
|
|
|
|
return ((Grid) a.myGrid).isImportant > ((Grid) b.myGrid).isImportant || a.myGrid.size() > b.myGrid.size();
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public void destroy()
|
|
|
|
{
|
|
|
|
// a connection was destroyed RE-PATH!!
|
|
|
|
IPathingGrid p = sideA.getInternalGrid().getCache( IPathingGrid.class );
|
|
|
|
p.repath();
|
|
|
|
|
|
|
|
sideA.removeConnection( this );
|
|
|
|
sideB.removeConnection( this );
|
|
|
|
|
|
|
|
sideA.validateGrid();
|
|
|
|
sideB.validateGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGridNode a()
|
|
|
|
{
|
|
|
|
return sideA;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ForgeDirection getDirection(IGridNode side)
|
|
|
|
{
|
|
|
|
if ( fromAtoB == ForgeDirection.UNKNOWN )
|
|
|
|
return fromAtoB;
|
|
|
|
|
|
|
|
if ( sideA == side )
|
|
|
|
return fromAtoB;
|
|
|
|
else
|
|
|
|
return fromAtoB.getOpposite();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGridNode b()
|
|
|
|
{
|
|
|
|
return sideB;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGridNode getOtherSide(IGridNode gridNode)
|
|
|
|
{
|
|
|
|
if ( gridNode == sideA )
|
|
|
|
return sideB;
|
|
|
|
if ( gridNode == sideB )
|
|
|
|
return sideA;
|
|
|
|
|
|
|
|
throw new GridException( "Invalid Side of Connection" );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasDirection()
|
|
|
|
{
|
|
|
|
return fromAtoB != ForgeDirection.UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IReadOnlyCollection<IPathItem> getPossibleOptions()
|
|
|
|
{
|
|
|
|
return new ReadOnlyCollection( Arrays.asList( new IPathItem[] { (IPathItem) a(), (IPathItem) b() } ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void incrementChannelCount(int usedChannels)
|
|
|
|
{
|
|
|
|
channelData += usedChannels;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canSupportMoreChannels()
|
|
|
|
{
|
|
|
|
return getLastUsedChannels() < 32; // max, PERIOD.
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getUsedChannels()
|
|
|
|
{
|
|
|
|
return (channelData >> 8) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLastUsedChannels()
|
|
|
|
{
|
|
|
|
return channelData & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IPathItem getControllerRoute()
|
|
|
|
{
|
|
|
|
if ( sideA.getFlags().contains( GridFlags.CANNOT_CARRY ) )
|
|
|
|
return null;
|
|
|
|
return sideA;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setControllerRoute(IPathItem fast, boolean zeroOut)
|
|
|
|
{
|
|
|
|
if ( zeroOut )
|
|
|
|
channelData &= ~0xff;
|
|
|
|
|
|
|
|
if ( sideB == fast )
|
|
|
|
{
|
|
|
|
GridNode tmp = sideA;
|
|
|
|
sideA = sideB;
|
|
|
|
sideB = tmp;
|
|
|
|
fromAtoB = fromAtoB.getOpposite();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void finalizeChannels()
|
|
|
|
{
|
|
|
|
if ( getUsedChannels() != getLastUsedChannels() )
|
|
|
|
{
|
|
|
|
channelData = (channelData & 0xff);
|
|
|
|
channelData |= channelData << 8;
|
|
|
|
|
|
|
|
if ( sideA.getInternalGrid() != null )
|
|
|
|
sideA.getInternalGrid().postEventTo( sideA, event );
|
|
|
|
|
|
|
|
if ( sideB.getInternalGrid() != null )
|
|
|
|
sideB.getInternalGrid().postEventTo( sideB, event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<GridFlags> getFlags()
|
|
|
|
{
|
|
|
|
return EnumSet.noneOf( GridFlags.class );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|