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

453 lines
12 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.cache;
2014-09-24 02:26:27 +02:00
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import net.minecraftforge.common.util.ForgeDirection;
2014-12-29 21:59:05 +01:00
2014-09-24 02:26:27 +02: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;
import appeng.api.networking.events.MENetworkChannelChanged;
import appeng.api.networking.events.MENetworkControllerChange;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.pathing.ControllerState;
import appeng.api.networking.pathing.IPathingGrid;
import appeng.api.util.DimensionalCoord;
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
import appeng.core.stats.Achievements;
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;
import appeng.util.Platform;
2014-09-24 02:26:27 +02:00
public class PathGridCache implements IPathingGrid
{
private final LinkedList<PathSegment> active = new LinkedList<PathSegment>();
private final Set<TileController> controllers = new HashSet<TileController>();
private final Set<IGridNode> requireChannels = new HashSet<IGridNode>();
private final Set<IGridNode> blockDense = new HashSet<IGridNode>();
private final IGrid myGrid;
private int channelsInUse = 0;
private int channelsByBlocks = 0;
private double channelPowerUsage = 0.0;
private boolean recalculateControllerNextTick = true;
private boolean updateNetwork = true;
private boolean booting = false;
private ControllerState controllerState = ControllerState.NO_CONTROLLER;
private int ticksUntilReady = 20;
private int lastChannels = 0;
2014-09-28 22:20:14 +02:00
private HashSet<IPathItem> semiOpen = new HashSet<IPathItem>();
2014-09-24 02:26:27 +02:00
public PathGridCache( final IGrid g )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.myGrid = g;
2014-09-24 02:26:27 +02:00
}
@Override
public void onUpdateTick()
{
if( this.recalculateControllerNextTick )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.recalcController();
2014-09-24 02:26:27 +02:00
}
if( this.updateNetwork )
2014-09-24 02:26:27 +02:00
{
if( !this.booting )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.myGrid.postEvent( new MENetworkBootingStatusChange() );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.booting = true;
this.updateNetwork = false;
this.setChannelsInUse( 0 );
2014-09-24 02:26:27 +02:00
if( !AEConfig.instance.isFeatureEnabled( AEFeature.Channels ) )
2014-09-24 02:26:27 +02:00
{
final int used = this.calculateRequiredChannels();
2014-09-24 02:26:27 +02:00
final int nodes = this.myGrid.getNodes().size();
2014-12-29 15:13:47 +01:00
this.ticksUntilReady = 20 + Math.max( 0, nodes / 100 - 20 );
this.setChannelsByBlocks( nodes * used );
this.setChannelPowerUsage( this.getChannelsByBlocks() / 128.0 );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.myGrid.getPivot().beginVisit( new AdHocChannelUpdater( used ) );
2014-09-24 02:26:27 +02:00
}
else if( this.controllerState == ControllerState.NO_CONTROLLER )
2014-09-24 02:26:27 +02:00
{
final int requiredChannels = this.calculateRequiredChannels();
2014-09-24 02:26:27 +02:00
int used = requiredChannels;
if( requiredChannels > 8 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
used = 0;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
final int nodes = this.myGrid.getNodes().size();
this.setChannelsInUse( used );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.ticksUntilReady = 20 + Math.max( 0, nodes / 100 - 20 );
this.setChannelsByBlocks( nodes * used );
this.setChannelPowerUsage( this.getChannelsByBlocks() / 128.0 );
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.myGrid.getPivot().beginVisit( new AdHocChannelUpdater( used ) );
2014-09-24 02:26:27 +02:00
}
else if( this.controllerState == ControllerState.CONTROLLER_CONFLICT )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.ticksUntilReady = 20;
this.myGrid.getPivot().beginVisit( new AdHocChannelUpdater( 0 ) );
2014-09-24 02:26:27 +02:00
}
else
{
final int nodes = this.myGrid.getNodes().size();
2014-12-29 15:13:47 +01:00
this.ticksUntilReady = 20 + Math.max( 0, nodes / 100 - 20 );
final HashSet<IPathItem> closedList = new HashSet<IPathItem>();
2014-12-29 15:13:47 +01:00
this.semiOpen = new HashSet<IPathItem>();
2014-09-24 02:26:27 +02:00
// myGrid.getPivot().beginVisit( new AdHocChannelUpdater( 0 )
// );
for( final IGridNode node : this.myGrid.getMachines( TileController.class ) )
2014-09-24 02:26:27 +02:00
{
closedList.add( (IPathItem) node );
for( final IGridConnection gcc : node.getConnections() )
2014-09-24 02:26:27 +02:00
{
final GridConnection gc = (GridConnection) gcc;
if( !( gc.getOtherSide( node ).getMachine() instanceof TileController ) )
2014-09-24 02:26:27 +02:00
{
final List<IPathItem> open = new LinkedList<IPathItem>();
2014-09-24 02:26:27 +02:00
closedList.add( gc );
open.add( gc );
gc.setControllerRoute( (GridNode) node, true );
2014-12-29 15:13:47 +01:00
this.active.add( new PathSegment( this, open, this.semiOpen, closedList ) );
2014-09-24 02:26:27 +02:00
}
}
}
}
}
if( !this.active.isEmpty() || this.ticksUntilReady > 0 )
2014-09-24 02:26:27 +02:00
{
final Iterator<PathSegment> i = this.active.iterator();
while( i.hasNext() )
2014-09-24 02:26:27 +02:00
{
final PathSegment pat = i.next();
if( pat.step() )
2014-09-24 02:26:27 +02:00
{
pat.setDead( true );
2014-09-24 02:26:27 +02:00
i.remove();
}
}
2014-12-29 15:13:47 +01:00
this.ticksUntilReady--;
2014-09-24 02:26:27 +02:00
if( this.active.isEmpty() && this.ticksUntilReady <= 0 )
2014-09-24 02:26:27 +02:00
{
if( this.controllerState == ControllerState.CONTROLLER_ONLINE )
2014-09-24 02:26:27 +02:00
{
final Iterator<TileController> controllerIterator = this.controllers.iterator();
if( controllerIterator.hasNext() )
2014-09-24 02:26:27 +02:00
{
final TileController controller = controllerIterator.next();
controller.getGridNode( ForgeDirection.UNKNOWN ).beginVisit( new ControllerChannelUpdater() );
2014-09-24 02:26:27 +02:00
}
}
// check for achievements
2014-12-29 15:13:47 +01:00
this.achievementPost();
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.booting = false;
this.setChannelPowerUsage( this.getChannelsByBlocks() / 128.0 );
2014-12-29 15:13:47 +01:00
this.myGrid.postEvent( new MENetworkBootingStatusChange() );
2014-09-24 02:26:27 +02:00
}
}
}
@Override
public void removeNode( final IGridNode gridNode, final IGridHost machine )
2014-09-24 02:26:27 +02:00
{
if( machine instanceof TileController )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.controllers.remove( machine );
this.recalculateControllerNextTick = true;
2014-09-24 02:26:27 +02:00
}
final EnumSet<GridFlags> flags = gridNode.getGridBlock().getFlags();
2014-09-24 02:26:27 +02:00
if( flags.contains( GridFlags.REQUIRE_CHANNEL ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.requireChannels.remove( gridNode );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( flags.contains( GridFlags.CANNOT_CARRY_COMPRESSED ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.blockDense.remove( gridNode );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.repath();
2014-09-24 02:26:27 +02:00
}
@Override
public void addNode( final IGridNode gridNode, final IGridHost machine )
2014-09-24 02:26:27 +02:00
{
if( machine instanceof TileController )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.controllers.add( (TileController) machine );
this.recalculateControllerNextTick = true;
2014-09-24 02:26:27 +02:00
}
final EnumSet<GridFlags> flags = gridNode.getGridBlock().getFlags();
2014-09-24 02:26:27 +02:00
if( flags.contains( GridFlags.REQUIRE_CHANNEL ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.requireChannels.add( gridNode );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( flags.contains( GridFlags.CANNOT_CARRY_COMPRESSED ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.blockDense.add( gridNode );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.repath();
2014-09-24 02:26:27 +02:00
}
@Override
public void onSplit( final IGridStorage storageB )
2014-09-24 02:26:27 +02:00
{
}
@Override
public void onJoin( final IGridStorage storageB )
{
}
@Override
public void populateGridStorage( final IGridStorage storage )
{
2014-09-24 02:26:27 +02:00
}
private void recalcController()
{
2014-12-29 15:13:47 +01:00
this.recalculateControllerNextTick = false;
final ControllerState old = this.controllerState;
2014-09-24 02:26:27 +02:00
if( this.controllers.isEmpty() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.controllerState = ControllerState.NO_CONTROLLER;
2014-09-24 02:26:27 +02:00
}
else
{
final IGridNode startingNode = this.controllers.iterator().next().getGridNode( ForgeDirection.UNKNOWN );
if( startingNode == null )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.controllerState = ControllerState.CONTROLLER_CONFLICT;
2014-09-24 02:26:27 +02:00
return;
}
final DimensionalCoord dc = startingNode.getGridBlock().getLocation();
final ControllerValidator cv = new ControllerValidator( dc.x, dc.y, dc.z );
2014-09-24 02:26:27 +02:00
startingNode.beginVisit( cv );
if( cv.isValid() && cv.getFound() == this.controllers.size() )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.controllerState = ControllerState.CONTROLLER_ONLINE;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.controllerState = ControllerState.CONTROLLER_CONFLICT;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
if( old != this.controllerState )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.myGrid.postEvent( new MENetworkControllerChange() );
2014-09-24 02:26:27 +02:00
}
}
private int calculateRequiredChannels()
2014-09-24 02:26:27 +02:00
{
this.semiOpen.clear();
2015-09-25 22:52:41 +02:00
int depth = 0;
for( final IGridNode nodes : this.requireChannels )
{
if( !this.semiOpen.contains( nodes ) )
{
final IGridBlock gb = nodes.getGridBlock();
final EnumSet<GridFlags> flags = gb.getFlags();
if( flags.contains( GridFlags.COMPRESSED_CHANNEL ) && !this.blockDense.isEmpty() )
2015-04-29 02:30:53 +02:00
{
return 9;
2015-04-29 02:30:53 +02:00
}
depth++;
if( flags.contains( GridFlags.MULTIBLOCK ) )
{
final IGridMultiblock gmb = (IGridMultiblock) gb;
final Iterator<IGridNode> i = gmb.getMultiblockNodes();
while( i.hasNext() )
2015-04-29 02:30:53 +02:00
{
this.semiOpen.add( (IPathItem) i.next() );
2015-04-29 02:30:53 +02:00
}
}
}
}
return depth;
2014-09-24 02:26:27 +02:00
}
private void achievementPost()
2014-09-24 02:26:27 +02:00
{
if( this.lastChannels != this.getChannelsInUse() && AEConfig.instance.isFeatureEnabled( AEFeature.Channels ) )
{
final Achievements currentBracket = this.getAchievementBracket( this.getChannelsInUse() );
final Achievements lastBracket = this.getAchievementBracket( this.lastChannels );
if( currentBracket != lastBracket && currentBracket != null )
{
final Set<Integer> players = new HashSet<Integer>();
for( final IGridNode n : this.requireChannels )
2015-04-29 02:30:53 +02:00
{
players.add( n.getPlayerID() );
2015-04-29 02:30:53 +02:00
}
for( final int id : players )
{
Platform.addStat( id, currentBracket.getAchievement() );
}
}
}
this.lastChannels = this.getChannelsInUse();
2014-09-24 02:26:27 +02:00
}
private Achievements getAchievementBracket( final int ch )
2014-09-24 02:26:27 +02:00
{
if( ch < 8 )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
if( ch < 128 )
2015-04-29 02:30:53 +02:00
{
return Achievements.Networking1;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( ch < 2048 )
2015-04-29 02:30:53 +02:00
{
return Achievements.Networking2;
2015-04-29 02:30:53 +02:00
}
return Achievements.Networking3;
2014-09-24 02:26:27 +02:00
}
@MENetworkEventSubscribe
void updateNodReq( final MENetworkChannelChanged ev )
2014-09-24 02:26:27 +02:00
{
final IGridNode gridNode = ev.node;
if( gridNode.getGridBlock().getFlags().contains( GridFlags.REQUIRE_CHANNEL ) )
2015-04-29 02:30:53 +02:00
{
this.requireChannels.add( gridNode );
2015-04-29 02:30:53 +02:00
}
else
2015-04-29 02:30:53 +02:00
{
this.requireChannels.remove( gridNode );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
this.repath();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isNetworkBooting()
2014-09-24 02:26:27 +02:00
{
return !this.active.isEmpty() && !this.booting;
}
2014-09-24 02:26:27 +02:00
@Override
public ControllerState getControllerState()
{
return this.controllerState;
2014-09-24 02:26:27 +02:00
}
@Override
public void repath()
{
// clean up...
this.active.clear();
this.setChannelsByBlocks( 0 );
this.updateNetwork = true;
}
double getChannelPowerUsage()
{
return this.channelPowerUsage;
}
private void setChannelPowerUsage( final double channelPowerUsage )
{
this.channelPowerUsage = channelPowerUsage;
}
public int getChannelsByBlocks()
{
return this.channelsByBlocks;
}
public void setChannelsByBlocks( final int channelsByBlocks )
{
this.channelsByBlocks = channelsByBlocks;
}
public int getChannelsInUse()
{
return this.channelsInUse;
}
public void setChannelsInUse( final int channelsInUse )
{
this.channelsInUse = channelsInUse;
}
2014-09-24 02:26:27 +02:00
}