Applied-Energistics-2-tiler.../src/main/java/appeng/me/GridConnection.java

261 lines
6.5 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.me;
2014-09-24 02:26:27 +02:00
import java.util.Arrays;
import java.util.EnumSet;
import net.minecraftforge.common.util.ForgeDirection;
2014-09-24 02:26:27 +02:00
import appeng.api.exceptions.FailedConnection;
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.DimensionalCoord;
2014-09-24 02:26:27 +02:00
import appeng.api.util.IReadOnlyCollection;
import appeng.core.AEConfig;
import appeng.core.AELog;
import appeng.core.features.AEFeature;
import appeng.me.pathfinding.IPathItem;
import appeng.util.Platform;
import appeng.util.ReadOnlyCollection;
2014-09-24 02:26:27 +02:00
public class GridConnection implements IGridConnection, IPathItem
{
2015-01-01 22:13:10 +01:00
private static final MENetworkChannelsChanged EVENT = new MENetworkChannelsChanged();
2014-09-24 02:26:27 +02:00
private GridNode sideA;
private ForgeDirection fromAtoB;
private GridNode sideB;
Object visitorIterationNumber = null;
public int channelData = 0;
public GridConnection( IGridNode aNode, IGridNode bNode, ForgeDirection fromAtoB ) throws FailedConnection
{
2014-09-24 02:26:27 +02:00
GridNode a = ( GridNode ) aNode;
GridNode b = ( GridNode ) bNode;
2014-09-24 02:26:27 +02:00
if ( Platform.securityCheck( a, b ) )
{
if ( AEConfig.instance.isFeatureEnabled( AEFeature.LogSecurityAudits ) )
{
final DimensionalCoord aCoordinates = a.getGridBlock().getLocation();
final DimensionalCoord bCoordinates = b.getGridBlock().getLocation();
AELog.info( "Security audit 1 failed at [%s] belonging to player [id=%d]", aCoordinates.toString(), a.playerID );
AELog.info( "Security audit 2 failed at [%s] belonging to player [id=%d]", bCoordinates.toString(), b.playerID );
2014-09-24 02:26:27 +02:00
}
throw new FailedConnection();
}
if ( a == null || b == null )
throw new GridException( "Connection Forged Between null entities." );
if ( a.hasConnection( b ) || b.hasConnection( a ) )
{
final String aCoords = a.getGridBlock().getLocation().toString();
final String bCoords = b.getGridBlock().getLocation().toString();
2014-09-24 02:26:27 +02:00
throw new GridException( String.format( "Connection already exists between node at [%s] and node at [%s].", aCoords, bCoords ) );
}
2014-12-29 15:13:47 +01:00
this.sideA = a;
2014-09-24 02:26:27 +02:00
this.fromAtoB = fromAtoB;
2014-12-29 15:13:47 +01:00
this.sideB = b;
2014-09-24 02:26:27 +02:00
if ( b.getMyGrid() == null )
2014-09-24 02:26:27 +02:00
{
b.setGrid( a.getInternalGrid() );
}
else
{
if ( a.getMyGrid() == null )
2014-09-24 02:26:27 +02:00
{
GridPropagator gp = new GridPropagator( b.getInternalGrid() );
a.beginVisit( gp );
}
else if ( b.getMyGrid() == null )
2014-09-24 02:26:27 +02:00
{
GridPropagator gp = new GridPropagator( a.getInternalGrid() );
b.beginVisit( gp );
}
2014-12-29 15:13:47 +01:00
else if ( this.isNetworkABetter( a, b ) )
2014-09-24 02:26:27 +02:00
{
GridPropagator gp = new GridPropagator( a.getInternalGrid() );
b.beginVisit( gp );
}
else
{
GridPropagator gp = new GridPropagator( b.getInternalGrid() );
a.beginVisit( gp );
}
}
// a connection was destroyed RE-PATH!!
2014-12-29 15:13:47 +01:00
IPathingGrid p = this.sideA.getInternalGrid().getCache( IPathingGrid.class );
2014-09-24 02:26:27 +02:00
p.repath();
2014-12-29 15:13:47 +01:00
this.sideA.addConnection( this );
this.sideB.addConnection( this );
2014-09-24 02:26:27 +02:00
}
private boolean isNetworkABetter( GridNode a, GridNode b )
2014-09-24 02:26:27 +02:00
{
return a.getMyGrid().getPriority() > b.getMyGrid().getPriority() || a.getMyGrid().size() > b.getMyGrid().size();
2014-09-24 02:26:27 +02:00
}
@Override
public void destroy()
{
// a connection was destroyed RE-PATH!!
2014-12-29 15:13:47 +01:00
IPathingGrid p = this.sideA.getInternalGrid().getCache( IPathingGrid.class );
2014-09-24 02:26:27 +02:00
p.repath();
2014-12-29 15:13:47 +01:00
this.sideA.removeConnection( this );
this.sideB.removeConnection( this );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.sideA.validateGrid();
this.sideB.validateGrid();
2014-09-24 02:26:27 +02:00
}
@Override
public IGridNode a()
{
2014-12-29 15:13:47 +01:00
return this.sideA;
2014-09-24 02:26:27 +02:00
}
@Override
public ForgeDirection getDirection( IGridNode side )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( this.fromAtoB == ForgeDirection.UNKNOWN )
return this.fromAtoB;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.sideA == side )
return this.fromAtoB;
2014-09-24 02:26:27 +02:00
else
2014-12-29 15:13:47 +01:00
return this.fromAtoB.getOpposite();
2014-09-24 02:26:27 +02:00
}
@Override
public IGridNode b()
{
2014-12-29 15:13:47 +01:00
return this.sideB;
2014-09-24 02:26:27 +02:00
}
@Override
public IGridNode getOtherSide( IGridNode gridNode )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
if ( gridNode == this.sideA )
return this.sideB;
if ( gridNode == this.sideB )
return this.sideA;
2014-09-24 02:26:27 +02:00
throw new GridException( "Invalid Side of Connection" );
}
@Override
public boolean hasDirection()
{
2014-12-29 15:13:47 +01:00
return this.fromAtoB != ForgeDirection.UNKNOWN;
2014-09-24 02:26:27 +02:00
}
@Override
public IReadOnlyCollection<IPathItem> getPossibleOptions()
{
return new ReadOnlyCollection<IPathItem>( Arrays.asList( ( IPathItem ) this.a(), ( IPathItem ) this.b() ) );
2014-09-24 02:26:27 +02:00
}
@Override
public void incrementChannelCount( int usedChannels )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.channelData += usedChannels;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean canSupportMoreChannels()
{
2014-12-29 15:13:47 +01:00
return this.getLastUsedChannels() < 32; // max, PERIOD.
2014-09-24 02:26:27 +02:00
}
@Override
public int getUsedChannels()
{
return ( this.channelData >> 8 ) & 0xff;
2014-09-24 02:26:27 +02:00
}
public int getLastUsedChannels()
{
2014-12-29 15:13:47 +01:00
return this.channelData & 0xff;
2014-09-24 02:26:27 +02:00
}
@Override
public IPathItem getControllerRoute()
{
2014-12-29 15:13:47 +01:00
if ( this.sideA.getFlags().contains( GridFlags.CANNOT_CARRY ) )
2014-09-24 02:26:27 +02:00
return null;
2014-12-29 15:13:47 +01:00
return this.sideA;
2014-09-24 02:26:27 +02:00
}
@Override
public void setControllerRoute( IPathItem fast, boolean zeroOut )
2014-09-24 02:26:27 +02:00
{
if ( zeroOut )
2014-12-29 15:13:47 +01:00
this.channelData &= ~0xff;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.sideB == fast )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
GridNode tmp = this.sideA;
this.sideA = this.sideB;
this.sideB = tmp;
this.fromAtoB = this.fromAtoB.getOpposite();
2014-09-24 02:26:27 +02:00
}
}
@Override
public void finalizeChannels()
{
2014-12-29 15:13:47 +01:00
if ( this.getUsedChannels() != this.getLastUsedChannels() )
2014-09-24 02:26:27 +02:00
{
this.channelData &= 0xff;
2014-12-29 15:13:47 +01:00
this.channelData |= this.channelData << 8;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.sideA.getInternalGrid() != null )
2015-01-01 22:13:10 +01:00
this.sideA.getInternalGrid().postEventTo( this.sideA, EVENT );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.sideB.getInternalGrid() != null )
2015-01-01 22:13:10 +01:00
this.sideB.getInternalGrid().postEventTo( this.sideB, EVENT );
2014-09-24 02:26:27 +02:00
}
}
@Override
public EnumSet<GridFlags> getFlags()
{
return EnumSet.noneOf( GridFlags.class );
}
}