2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2014-07-06 23:00:05 +02:00
|
|
|
import java.util.EnumSet;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridBlock;
|
|
|
|
import appeng.api.networking.IGridConnection;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridMultiblock;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
|
|
|
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
2014-02-21 07:10:38 +01:00
|
|
|
import appeng.api.networking.events.MENetworkChannelChanged;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.events.MENetworkControllerChange;
|
2014-02-21 07:10:38 +01:00
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.pathing.ControllerState;
|
|
|
|
import appeng.api.networking.pathing.IPathingGrid;
|
|
|
|
import appeng.api.util.DimensionalCoord;
|
|
|
|
import appeng.me.GridConnection;
|
|
|
|
import appeng.me.GridNode;
|
|
|
|
import appeng.me.pathfinding.AdHocChannelUpdater;
|
|
|
|
import appeng.me.pathfinding.ControllerChannelUpdater;
|
|
|
|
import appeng.me.pathfinding.ControllerValidator;
|
|
|
|
import appeng.me.pathfinding.IPathItem;
|
|
|
|
import appeng.me.pathfinding.PathSegment;
|
|
|
|
import appeng.tile.networking.TileController;
|
|
|
|
|
|
|
|
public class PathGridCache implements IPathingGrid
|
|
|
|
{
|
|
|
|
|
|
|
|
boolean recalculateControllerNextTick = true;
|
|
|
|
boolean updateNetwork = true;
|
|
|
|
boolean booting = false;
|
|
|
|
|
|
|
|
LinkedList<PathSegment> active = new LinkedList();
|
|
|
|
|
|
|
|
ControllerState controllerState = ControllerState.NO_CONTROLLER;
|
|
|
|
|
|
|
|
int instance = Integer.MIN_VALUE;
|
|
|
|
|
|
|
|
int ticksUntilReady = 20;
|
|
|
|
Set<TileController> controllers = new HashSet();
|
|
|
|
Set<IGridNode> requireChannels = new HashSet();
|
2014-07-06 23:00:05 +02:00
|
|
|
Set<IGridNode> blockDense = new HashSet();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
final IGrid myGrid;
|
|
|
|
private HashSet<IPathItem> semiOpen = new HashSet();
|
|
|
|
private HashSet<IPathItem> closedList = new HashSet();
|
|
|
|
|
2014-03-18 04:58:58 +01:00
|
|
|
public int channelsByBlocks = 0;
|
|
|
|
public double channelPowerUsage = 0.0;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public PathGridCache(IGrid g) {
|
|
|
|
myGrid = g;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void onUpdateTick()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( recalculateControllerNextTick )
|
|
|
|
{
|
|
|
|
recalcController();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( updateNetwork )
|
|
|
|
{
|
|
|
|
if ( !booting )
|
|
|
|
myGrid.postEvent( new MENetworkBootingStatusChange() );
|
|
|
|
|
|
|
|
booting = true;
|
|
|
|
updateNetwork = false;
|
|
|
|
instance++;
|
|
|
|
|
|
|
|
if ( controllerState == ControllerState.NO_CONTROLLER )
|
|
|
|
{
|
|
|
|
int requiredChannels = calculateRequiredChanels();
|
|
|
|
int used = requiredChannels;
|
|
|
|
if ( requiredChannels > 8 )
|
|
|
|
used = 0;
|
|
|
|
|
|
|
|
int nodes = myGrid.getNodes().size();
|
2014-04-23 04:49:03 +02:00
|
|
|
ticksUntilReady = 20 + Math.max( 0, nodes / 100 - 20 );
|
2014-03-18 04:58:58 +01:00
|
|
|
channelsByBlocks = nodes * used;
|
|
|
|
channelPowerUsage = (double) channelsByBlocks / 128.0;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
myGrid.getPivot().beginVisition( new AdHocChannelUpdater( used ) );
|
|
|
|
}
|
|
|
|
else if ( controllerState == ControllerState.CONTROLLER_CONFLICT )
|
|
|
|
{
|
|
|
|
ticksUntilReady = 20;
|
|
|
|
myGrid.getPivot().beginVisition( new AdHocChannelUpdater( 0 ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int nodes = myGrid.getNodes().size();
|
2014-04-23 04:49:03 +02:00
|
|
|
ticksUntilReady = 20 + Math.max( 0, nodes / 100 - 20 );
|
2013-12-27 23:59:59 +01:00
|
|
|
closedList = new HashSet();
|
|
|
|
semiOpen = new HashSet();
|
|
|
|
|
2014-02-04 17:11:29 +01:00
|
|
|
// myGrid.getPivot().beginVisition( new AdHocChannelUpdater( 0 )
|
|
|
|
// );
|
|
|
|
for (IGridNode node : myGrid.getMachines( TileController.class ))
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
closedList.add( (IPathItem) node );
|
|
|
|
for (IGridConnection gcc : node.getConnections())
|
|
|
|
{
|
|
|
|
GridConnection gc = (GridConnection) gcc;
|
|
|
|
if ( !(gc.getOtherSide( node ).getMachine() instanceof TileController) )
|
|
|
|
{
|
|
|
|
List open = new LinkedList();
|
|
|
|
closedList.add( gc );
|
|
|
|
open.add( gc );
|
|
|
|
gc.setControllerRoute( (GridNode) node, true );
|
2014-03-18 04:58:58 +01:00
|
|
|
active.add( new PathSegment( this, open, semiOpen, closedList ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !active.isEmpty() || ticksUntilReady > 0 )
|
|
|
|
{
|
|
|
|
Iterator<PathSegment> i = active.iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
PathSegment pat = i.next();
|
|
|
|
if ( pat.step() )
|
|
|
|
{
|
|
|
|
pat.isDead = true;
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ticksUntilReady--;
|
|
|
|
|
|
|
|
if ( active.isEmpty() && ticksUntilReady <= 0 )
|
|
|
|
{
|
|
|
|
if ( controllerState == ControllerState.CONTROLLER_ONLINE )
|
|
|
|
{
|
|
|
|
for (TileController tc : controllers)
|
|
|
|
{
|
|
|
|
tc.getGridNode( ForgeDirection.UNKNOWN ).beginVisition( new ControllerChannelUpdater() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
booting = false;
|
2014-03-18 04:58:58 +01:00
|
|
|
channelPowerUsage = (double) channelsByBlocks / 128.0;
|
2013-12-27 23:59:59 +01:00
|
|
|
myGrid.postEvent( new MENetworkBootingStatusChange() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int calculateRequiredChanels()
|
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
semiOpen.clear();
|
|
|
|
|
|
|
|
for (IGridNode nodes : requireChannels)
|
|
|
|
{
|
|
|
|
if ( !semiOpen.contains( nodes ) )
|
|
|
|
{
|
2014-07-06 23:00:05 +02:00
|
|
|
IGridBlock gb = nodes.getGridBlock();
|
|
|
|
EnumSet<GridFlags> flags = gb.getFlags();
|
|
|
|
|
|
|
|
if ( flags.contains( GridFlags.DENSE_CHANNEL ) && !blockDense.isEmpty() )
|
|
|
|
return 9;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
depth++;
|
|
|
|
|
2014-07-06 23:00:05 +02:00
|
|
|
if ( flags.contains( GridFlags.MULTIBLOCK ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
IGridMultiblock gmb = (IGridMultiblock) gb;
|
|
|
|
Iterator<IGridNode> i = gmb.getMultiblockNodes();
|
|
|
|
while (i.hasNext())
|
|
|
|
semiOpen.add( (IPathItem) i.next() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void repath()
|
|
|
|
{
|
|
|
|
// clean up...
|
|
|
|
active.clear();
|
|
|
|
|
2014-03-18 04:58:58 +01:00
|
|
|
channelsByBlocks = 0;
|
2013-12-27 23:59:59 +01:00
|
|
|
updateNetwork = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void removeNode(IGridNode gridNode, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof TileController )
|
|
|
|
{
|
|
|
|
controllers.remove( machine );
|
|
|
|
recalculateControllerNextTick = true;
|
|
|
|
}
|
|
|
|
|
2014-07-06 23:00:05 +02:00
|
|
|
EnumSet<GridFlags> flags = gridNode.getGridBlock().getFlags();
|
|
|
|
|
|
|
|
if ( flags.contains( GridFlags.REQUIRE_CHANNEL ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
requireChannels.remove( gridNode );
|
|
|
|
|
2014-07-06 23:00:05 +02:00
|
|
|
if ( flags.contains( GridFlags.CANNOT_CARRY_DENSE ) )
|
|
|
|
blockDense.remove( gridNode );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
repath();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void addNode(IGridNode gridNode, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof TileController )
|
|
|
|
{
|
|
|
|
controllers.add( (TileController) machine );
|
|
|
|
recalculateControllerNextTick = true;
|
|
|
|
}
|
|
|
|
|
2014-07-06 23:00:05 +02:00
|
|
|
EnumSet<GridFlags> flags = gridNode.getGridBlock().getFlags();
|
|
|
|
|
|
|
|
if ( flags.contains( GridFlags.REQUIRE_CHANNEL ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
requireChannels.add( gridNode );
|
|
|
|
|
2014-07-06 23:00:05 +02:00
|
|
|
if ( flags.contains( GridFlags.CANNOT_CARRY_DENSE ) )
|
|
|
|
blockDense.add( gridNode );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
repath();
|
|
|
|
}
|
|
|
|
|
2014-02-21 07:10:38 +01:00
|
|
|
@MENetworkEventSubscribe
|
|
|
|
void updateNodReq(MENetworkChannelChanged ev)
|
|
|
|
{
|
|
|
|
IGridNode gridNode = ev.node;
|
|
|
|
|
|
|
|
if ( gridNode.getGridBlock().getFlags().contains( GridFlags.REQUIRE_CHANNEL ) )
|
|
|
|
requireChannels.add( gridNode );
|
|
|
|
else
|
|
|
|
requireChannels.remove( gridNode );
|
|
|
|
|
|
|
|
repath();
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
private void recalcController()
|
|
|
|
{
|
|
|
|
recalculateControllerNextTick = false;
|
|
|
|
ControllerState old = controllerState;
|
|
|
|
|
|
|
|
if ( controllers.isEmpty() )
|
|
|
|
{
|
|
|
|
controllerState = ControllerState.NO_CONTROLLER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IGridNode startingNode = controllers.iterator().next().getGridNode( ForgeDirection.UNKNOWN );
|
|
|
|
if ( startingNode == null )
|
|
|
|
{
|
|
|
|
controllerState = ControllerState.CONTROLLER_CONFLICT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DimensionalCoord dc = startingNode.getGridBlock().getLocation();
|
|
|
|
ControllerValidator cv = new ControllerValidator( dc.x, dc.y, dc.z );
|
|
|
|
|
|
|
|
startingNode.beginVisition( cv );
|
|
|
|
|
|
|
|
if ( cv.isValid && cv.found == controllers.size() )
|
|
|
|
controllerState = ControllerState.CONTROLLER_ONLINE;
|
|
|
|
else
|
|
|
|
controllerState = ControllerState.CONTROLLER_CONFLICT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( old != controllerState )
|
|
|
|
{
|
|
|
|
myGrid.postEvent( new MENetworkControllerChange() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ControllerState getControllerState()
|
|
|
|
{
|
|
|
|
return controllerState;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isNetworkBooting()
|
|
|
|
{
|
2014-01-21 16:59:07 +01:00
|
|
|
return !active.isEmpty() && booting == false;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSplit(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onJoin(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void populateGridStorage(IGridStorage storage)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|