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

135 lines
2.7 KiB
Java
Raw Normal View History

2014-09-24 02:26:27 +02:00
package appeng.me.pathfinding;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGridMultiblock;
import appeng.api.networking.IGridNode;
import appeng.me.cache.PathGridCache;
public class PathSegment
{
public boolean isDead;
final PathGridCache pgc;
2014-09-24 02:26:27 +02:00
2014-09-28 22:20:14 +02:00
public PathSegment(PathGridCache myPGC, List<IPathItem> open, Set<IPathItem> semiOpen, Set<IPathItem> closed)
2014-09-24 02:26:27 +02:00
{
this.open = open;
this.semiOpen = semiOpen;
2014-09-24 02:26:27 +02:00
this.closed = closed;
pgc = myPGC;
isDead = false;
}
List<IPathItem> open;
final Set<IPathItem> semiOpen;
final Set<IPathItem> closed;
2014-09-24 02:26:27 +02:00
public boolean step()
{
List<IPathItem> oldOpen = open;
2014-09-28 22:20:14 +02:00
open = new LinkedList<IPathItem>();
2014-09-24 02:26:27 +02:00
for (IPathItem i : oldOpen)
{
for (IPathItem pi : i.getPossibleOptions())
{
EnumSet<GridFlags> flags = pi.getFlags();
if ( !closed.contains( pi ) )
{
pi.setControllerRoute( i, true );
if ( flags.contains( GridFlags.REQUIRE_CHANNEL ) )
{
// close the semi open.
if ( !semiOpen.contains( pi ) )
2014-09-24 02:26:27 +02:00
{
boolean worked;
2014-09-24 02:26:27 +02:00
if ( flags.contains( GridFlags.COMPRESSED_CHANNEL ) )
worked = useDenseChannel( pi );
else
worked = useChannel( pi );
if ( worked && flags.contains( GridFlags.MULTIBLOCK ) )
{
Iterator<IGridNode> oni = ((IGridMultiblock) ((IGridNode) pi).getGridBlock()).getMultiblockNodes();
while (oni.hasNext())
{
IGridNode otherNodes = oni.next();
if ( otherNodes != pi )
semiOpen.add( (IPathItem) otherNodes );
2014-09-24 02:26:27 +02:00
}
}
}
else
{
pi.incrementChannelCount( 1 ); // give a channel.
semiOpen.remove( pi );
2014-09-24 02:26:27 +02:00
}
}
closed.add( pi );
open.add( pi );
}
}
}
return open.isEmpty();
}
private boolean useChannel(IPathItem start)
{
IPathItem pi = start;
while (pi != null)
{
if ( !pi.canSupportMoreChannels() )
return false;
pi = pi.getControllerRoute();
}
pi = start;
while (pi != null)
{
pgc.channelsByBlocks++;
pi.incrementChannelCount( 1 );
pi = pi.getControllerRoute();
}
pgc.channelsInUse++;
return true;
}
private boolean useDenseChannel(IPathItem start)
{
IPathItem pi = start;
while (pi != null)
{
if ( !pi.canSupportMoreChannels() || pi.getFlags().contains( GridFlags.CANNOT_CARRY_COMPRESSED ) )
return false;
pi = pi.getControllerRoute();
}
pi = start;
while (pi != null)
{
pgc.channelsByBlocks++;
pi.incrementChannelCount( 1 );
pi = pi.getControllerRoute();
}
pgc.channelsInUse++;
return true;
}
}