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-11-30 04:58:08 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
2014-11-30 04:58:08 +01:00
|
|
|
import java.util.Deque;
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.exceptions.FailedConnection;
|
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.GridNotification;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridBlock;
|
|
|
|
import appeng.api.networking.IGridCache;
|
|
|
|
import appeng.api.networking.IGridConnection;
|
2014-11-30 04:58:08 +01:00
|
|
|
import appeng.api.networking.IGridConnectionVisitor;
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridVisitor;
|
|
|
|
import appeng.api.networking.energy.IEnergyGrid;
|
|
|
|
import appeng.api.networking.events.MENetworkChannelsChanged;
|
|
|
|
import appeng.api.networking.pathing.IPathingGrid;
|
|
|
|
import appeng.api.util.AEColor;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
|
|
|
import appeng.api.util.IReadOnlyCollection;
|
|
|
|
import appeng.core.WorldSettings;
|
|
|
|
import appeng.hooks.TickHandler;
|
|
|
|
import appeng.me.pathfinding.IPathItem;
|
|
|
|
import appeng.util.ReadOnlyCollection;
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class GridNode implements IGridNode, IPathItem
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
private static final MENetworkChannelsChanged event = new MENetworkChannelsChanged();
|
|
|
|
private static final int[] channelCount = new int[] { 0, 8, 32 };
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
private final List<IGridConnection> connections = new LinkedList<IGridConnection>();
|
|
|
|
private final IGridBlock gridProxy;
|
|
|
|
// old power draw, used to diff
|
|
|
|
public double previousDraw = 0.0;
|
|
|
|
public long lastSecurityKey = -1;
|
|
|
|
public int playerID = -1;
|
|
|
|
private GridStorage myStorage = null;
|
|
|
|
private Grid myGrid;
|
|
|
|
private Object visitorIterationNumber = null;
|
2014-09-24 02:26:27 +02:00
|
|
|
// connection criteria
|
|
|
|
private int compressedData = 0;
|
2014-11-30 04:58:08 +01:00
|
|
|
private int channelData = 0;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public GridNode( IGridBlock what )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.gridProxy = what;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public IGridBlock getGridProxy()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.gridProxy;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public Grid getMyGrid()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.myGrid;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public int usedChannels()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.channelData >> 8;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public Class<? extends IGridHost> getMachineClass()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.getMachine().getClass();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void addConnection( IGridConnection gridConnection )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.connections.add( gridConnection );
|
|
|
|
if ( gridConnection.hasDirection() )
|
|
|
|
this.gridProxy.onGridNotification( GridNotification.ConnectionsChanged );
|
|
|
|
|
|
|
|
final IGridNode gn = this;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
Collections.sort( this.connections, new ConnectionComparator( gn ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void removeConnection( IGridConnection gridConnection )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.connections.remove( gridConnection );
|
|
|
|
if ( gridConnection.hasDirection() )
|
|
|
|
this.gridProxy.onGridNotification( GridNotification.ConnectionsChanged );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public boolean hasConnection( IGridNode otherSide )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( IGridConnection gc : this.connections )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( gc.a() == otherSide || gc.b() == otherSide )
|
|
|
|
return true;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
return false;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void validateGrid()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
GridSplitDetector gsd = new GridSplitDetector( this.getInternalGrid().getPivot() );
|
|
|
|
this.beginVisit( gsd );
|
|
|
|
if ( !gsd.pivotFound )
|
|
|
|
{
|
|
|
|
IGridVisitor gp = new GridPropagator( new Grid( this ) );
|
|
|
|
this.beginVisit( gp );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public void setPlayerID( int playerID )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( playerID >= 0 )
|
|
|
|
this.playerID = playerID;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public Grid getInternalGrid()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( this.myGrid == null )
|
|
|
|
this.myGrid = new Grid( this );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.myGrid;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public void beginVisit( IGridVisitor g )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
Object tracker = new Object();
|
|
|
|
|
2014-09-28 22:20:14 +02:00
|
|
|
LinkedList<GridNode> nextRun = new LinkedList<GridNode>();
|
2014-09-24 02:26:27 +02:00
|
|
|
nextRun.add( this );
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
this.visitorIterationNumber = tracker;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
if ( g instanceof IGridConnectionVisitor )
|
|
|
|
{
|
2014-09-28 22:20:14 +02:00
|
|
|
LinkedList<IGridConnection> nextConn = new LinkedList<IGridConnection>();
|
2014-11-30 04:58:08 +01:00
|
|
|
IGridConnectionVisitor gcv = ( IGridConnectionVisitor ) g;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
while ( !nextRun.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
while ( !nextConn.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
gcv.visitConnection( nextConn.poll() );
|
|
|
|
|
|
|
|
LinkedList<GridNode> thisRun = nextRun;
|
2014-09-28 22:20:14 +02:00
|
|
|
nextRun = new LinkedList<GridNode>();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( GridNode n : thisRun )
|
2014-09-24 02:26:27 +02:00
|
|
|
n.visitorConnection( tracker, g, nextRun, nextConn );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
while ( !nextRun.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
LinkedList<GridNode> thisRun = nextRun;
|
2014-09-28 22:20:14 +02:00
|
|
|
nextRun = new LinkedList<GridNode>();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( GridNode n : thisRun )
|
2014-09-24 02:26:27 +02:00
|
|
|
n.visitorNode( tracker, g, nextRun );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
@Override
|
|
|
|
public void updateState()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
EnumSet<GridFlags> set = this.gridProxy.getFlags();
|
|
|
|
|
|
|
|
this.compressedData = set.contains( GridFlags.CANNOT_CARRY ) ? 0 : ( set.contains( GridFlags.DENSE_CAPACITY ) ? 2 : 1 );
|
|
|
|
|
|
|
|
this.compressedData |= ( this.gridProxy.getGridColor().ordinal() << 3 );
|
|
|
|
|
|
|
|
for ( ForgeDirection dir : this.gridProxy.getConnectableSides() )
|
|
|
|
this.compressedData |= ( 1 << ( dir.ordinal() + 8 ) );
|
|
|
|
|
|
|
|
this.FindConnections();
|
|
|
|
this.getInternalGrid();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGridHost getMachine()
|
|
|
|
{
|
|
|
|
return this.gridProxy.getMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGrid getGrid()
|
|
|
|
{
|
|
|
|
return this.myGrid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setGrid( Grid grid )
|
|
|
|
{
|
|
|
|
if ( this.myGrid == grid )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( this.myGrid != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myGrid.remove( this );
|
|
|
|
|
|
|
|
if ( this.myGrid.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myGrid.saveState();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( IGridCache c : grid.getCaches().values() )
|
|
|
|
c.onJoin( this.myGrid.getMyStorage() );
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myGrid = grid;
|
|
|
|
this.myGrid.add( this );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
@Override
|
|
|
|
public void destroy()
|
|
|
|
{
|
|
|
|
while ( !this.connections.isEmpty() )
|
|
|
|
{
|
|
|
|
// not part of this network for real anymore.
|
|
|
|
if ( this.connections.size() == 1 )
|
|
|
|
this.setGridStorage( null );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
IGridConnection c = this.connections.listIterator().next();
|
|
|
|
GridNode otherSide = ( GridNode ) c.getOtherSide( this );
|
|
|
|
otherSide.getInternalGrid().setPivot( otherSide );
|
|
|
|
c.destroy();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
|
|
|
|
if ( this.myGrid != null )
|
|
|
|
this.myGrid.remove( this );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
@Override
|
|
|
|
public World getWorld()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.gridProxy.getLocation().getWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<ForgeDirection> getConnectedSides()
|
|
|
|
{
|
|
|
|
EnumSet<ForgeDirection> set = EnumSet.noneOf( ForgeDirection.class );
|
|
|
|
for ( IGridConnection gc : this.connections )
|
|
|
|
set.add( gc.getDirection( this ) );
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IReadOnlyCollection<IGridConnection> getConnections()
|
|
|
|
{
|
|
|
|
return new ReadOnlyCollection<IGridConnection>( this.connections );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGridBlock getGridBlock()
|
|
|
|
{
|
|
|
|
return this.gridProxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isActive()
|
|
|
|
{
|
|
|
|
IGrid g = this.getGrid();
|
|
|
|
if ( g != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
IPathingGrid pg = g.getCache( IPathingGrid.class );
|
|
|
|
IEnergyGrid eg = g.getCache( IEnergyGrid.class );
|
|
|
|
return this.meetsChannelRequirements() && eg.isNetworkPowered() && !pg.isNetworkBooting();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
@Override
|
|
|
|
public void loadFromNBT( String name, NBTTagCompound nodeData )
|
|
|
|
{
|
|
|
|
if ( this.myGrid == null )
|
|
|
|
{
|
|
|
|
NBTTagCompound node = nodeData.getCompoundTag( name );
|
|
|
|
this.playerID = node.getInteger( "p" );
|
|
|
|
this.lastSecurityKey = node.getLong( "k" );
|
|
|
|
this.setGridStorage( WorldSettings.getInstance().getGridStorage( node.getLong( "g" ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new RuntimeException( "Loading data after part of a grid, this is invalid." );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
@Override
|
|
|
|
public void saveToNBT( String name, NBTTagCompound nodeData )
|
|
|
|
{
|
|
|
|
if ( this.myStorage != null )
|
|
|
|
{
|
|
|
|
NBTTagCompound node = new NBTTagCompound();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
node.setInteger( "p", this.playerID );
|
|
|
|
node.setLong( "k", this.lastSecurityKey );
|
|
|
|
node.setLong( "g", this.myStorage.getID() );
|
|
|
|
|
|
|
|
nodeData.setTag( name, node );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
else
|
|
|
|
nodeData.removeTag( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean meetsChannelRequirements()
|
|
|
|
{
|
|
|
|
return ( !this.gridProxy.getFlags().contains( GridFlags.REQUIRE_CHANNEL ) || this.getUsedChannels() > 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasFlag( GridFlags flag )
|
|
|
|
{
|
|
|
|
return this.gridProxy.getFlags().contains( flag );
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getUsedChannels()
|
|
|
|
{
|
|
|
|
return this.channelData & 0xff;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void FindConnections()
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( !this.gridProxy.isWorldAccessible() )
|
2014-09-24 02:26:27 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
EnumSet<ForgeDirection> newSecurityConnections = EnumSet.noneOf( ForgeDirection.class );
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
DimensionalCoord dc = this.gridProxy.getLocation();
|
|
|
|
for ( ForgeDirection f : ForgeDirection.VALID_DIRECTIONS )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
IGridHost te = this.findGridHost( dc.getWorld(), dc.x + f.offsetX, dc.y + f.offsetY, dc.z + f.offsetZ );
|
2014-09-24 02:26:27 +02:00
|
|
|
if ( te != null )
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
GridNode node = ( GridNode ) te.getGridNode( f.getOpposite() );
|
2014-09-24 02:26:27 +02:00
|
|
|
if ( node == null )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
boolean isValidConnection = this.canConnect( node, f ) && node.canConnect( this, f.getOpposite() );
|
|
|
|
|
|
|
|
IGridConnection con = null; // find the connection for this
|
2014-11-30 04:58:08 +01:00
|
|
|
// direction..
|
|
|
|
for ( IGridConnection c : this.getConnections() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
if ( c.getDirection( this ) == f )
|
|
|
|
{
|
|
|
|
con = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( con != null )
|
|
|
|
{
|
2014-09-28 20:56:16 +02:00
|
|
|
IGridNode os = con.getOtherSide( this );
|
2014-09-24 02:26:27 +02:00
|
|
|
if ( os == node )
|
|
|
|
{
|
|
|
|
// if this connection is no longer valid, destroy it.
|
|
|
|
if ( !isValidConnection )
|
|
|
|
con.destroy();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con.destroy();
|
|
|
|
// throw new GridException( "invalid state found, encountered connection to phantom block." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( isValidConnection )
|
|
|
|
{
|
|
|
|
if ( node.lastSecurityKey != -1 )
|
|
|
|
newSecurityConnections.add( f );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// construct a new connection between these two nodes.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
new GridConnection( node, this, f.getOpposite() );
|
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
catch ( FailedConnection e )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
TickHandler.instance.addCallable( node.getWorld(), new MachineSecurityBreak( this ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( ForgeDirection f : newSecurityConnections )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
IGridHost te = this.findGridHost( dc.getWorld(), dc.x + f.offsetX, dc.y + f.offsetY, dc.z + f.offsetZ );
|
2014-09-24 02:26:27 +02:00
|
|
|
if ( te != null )
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
GridNode node = ( GridNode ) te.getGridNode( f.getOpposite() );
|
2014-09-24 02:26:27 +02:00
|
|
|
if ( node == null )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// construct a new connection between these two nodes.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
new GridConnection( node, this, f.getOpposite() );
|
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
catch ( FailedConnection e )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
TickHandler.instance.addCallable( node.getWorld(), new MachineSecurityBreak( this ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
private IGridHost findGridHost( World world, int x, int y, int z )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
if ( world.blockExists( x, y, z ) )
|
|
|
|
{
|
|
|
|
TileEntity te = world.getTileEntity( x, y, z );
|
|
|
|
if ( te instanceof IGridHost )
|
2014-11-30 04:58:08 +01:00
|
|
|
return ( IGridHost ) te;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public boolean canConnect( GridNode from, ForgeDirection dir )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( !this.isValidDirection( dir ) )
|
|
|
|
return false;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( !from.getColor().matches( this.getColor() ) )
|
|
|
|
return false;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
return true;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
private boolean isValidDirection( ForgeDirection dir )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return ( this.compressedData & ( 1 << ( 8 + dir.ordinal() ) ) ) > 0;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public AEColor getColor()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return AEColor.values()[( this.compressedData >> 3 ) & 0x1F];
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
private void visitorConnection( Object tracker, IGridVisitor g, Deque<GridNode> nextRun, Deque<IGridConnection> nextConnections )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( g.visitNode( this ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( IGridConnection gc : this.getConnections() )
|
|
|
|
{
|
|
|
|
GridNode gn = ( GridNode ) gc.getOtherSide( this );
|
|
|
|
GridConnection gcc = ( GridConnection ) gc;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( gcc.visitorIterationNumber != tracker )
|
|
|
|
{
|
|
|
|
gcc.visitorIterationNumber = tracker;
|
|
|
|
nextConnections.add( gc );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( tracker == gn.visitorIterationNumber )
|
|
|
|
continue;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
gn.visitorIterationNumber = tracker;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
nextRun.add( gn );
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
private void visitorNode( Object tracker, IGridVisitor g, Deque<GridNode> nextRun )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( g.visitNode( this ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
for ( IGridConnection gc : this.getConnections() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
GridNode gn = ( GridNode ) gc.getOtherSide( this );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( tracker == gn.visitorIterationNumber )
|
|
|
|
continue;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
gn.visitorIterationNumber = tracker;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
nextRun.add( gn );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public GridStorage getGridStorage()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.myStorage;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void setGridStorage( GridStorage s )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.myStorage = s;
|
|
|
|
this.channelData = 0;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public IPathItem getControllerRoute()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( this.connections.isEmpty() || this.getFlags().contains( GridFlags.CANNOT_CARRY ) )
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return ( IPathItem ) this.connections.get( 0 );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public void setControllerRoute( IPathItem fast, boolean zeroOut )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( zeroOut )
|
|
|
|
this.channelData &= ~0xff;
|
|
|
|
|
|
|
|
int idx = this.connections.indexOf( fast );
|
|
|
|
if ( idx > 0 )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.connections.remove( fast );
|
|
|
|
this.connections.add( 0, ( IGridConnection ) fast );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canSupportMoreChannels()
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.getUsedChannels() < this.getMaxChannels();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public int getMaxChannels()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return channelCount[this.compressedData & 0x03];
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-30 04:58:08 +01:00
|
|
|
public IReadOnlyCollection<IPathItem> getPossibleOptions()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return ( ReadOnlyCollection ) this.getConnections();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
@Override
|
|
|
|
public void incrementChannelCount( int usedChannels )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.channelData += usedChannels;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public EnumSet<GridFlags> getFlags()
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.gridProxy.getFlags();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void finalizeChannels()
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( this.getFlags().contains( GridFlags.CANNOT_CARRY ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
return;
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( this.getLastUsedChannels() != this.getUsedChannels() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.channelData &= 0xff;
|
|
|
|
this.channelData |= this.channelData << 8;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
if ( this.getInternalGrid() != null )
|
|
|
|
this.getInternalGrid().postEventTo( this, event );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public int getLastUsedChannels()
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return ( this.channelData >> 8 ) & 0xff;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
private static class MachineSecurityBreak implements Callable<Void>
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
private final GridNode node;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public MachineSecurityBreak( GridNode node )
|
|
|
|
{
|
|
|
|
this.node = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Void call() throws Exception
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
this.node.getMachine().securityBreak();
|
|
|
|
|
|
|
|
return null;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
|
|
|
|
private static class ConnectionComparator implements Comparator<IGridConnection>
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
private final IGridNode gn;
|
|
|
|
|
|
|
|
public ConnectionComparator( IGridNode gn )
|
|
|
|
{
|
|
|
|
this.gn = gn;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare( IGridConnection o1, IGridConnection o2 )
|
|
|
|
{
|
|
|
|
boolean preferredA = o1.getOtherSide( this.gn ).hasFlag( GridFlags.PREFERRED );
|
|
|
|
boolean preferredB = o2.getOtherSide( this.gn ).hasFlag( GridFlags.PREFERRED );
|
|
|
|
|
|
|
|
return preferredA == preferredB ? 0 : ( preferredA ? -1 : 1 );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getPlayerID()
|
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
return this.playerID;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|