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.pathfinding;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
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;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class PathSegment
|
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final PathGridCache pgc;
|
|
|
|
private final Set<IPathItem> semiOpen;
|
|
|
|
private final Set<IPathItem> closed;
|
|
|
|
private boolean isDead;
|
|
|
|
private List<IPathItem> open;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public PathSegment( final PathGridCache myPGC, final List<IPathItem> open, final Set<IPathItem> semiOpen, final Set<IPathItem> closed )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
this.open = open;
|
2014-09-28 11:47:17 +02:00
|
|
|
this.semiOpen = semiOpen;
|
2014-09-24 02:26:27 +02:00
|
|
|
this.closed = closed;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.pgc = myPGC;
|
2015-10-08 15:42:42 +02:00
|
|
|
this.setDead( false );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean step()
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final List<IPathItem> oldOpen = this.open;
|
2014-12-29 15:13:47 +01:00
|
|
|
this.open = new LinkedList<IPathItem>();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final IPathItem i : oldOpen )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final IPathItem pi : i.getPossibleOptions() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final EnumSet<GridFlags> flags = pi.getFlags();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !this.closed.contains( pi ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
pi.setControllerRoute( i, true );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( flags.contains( GridFlags.REQUIRE_CHANNEL ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
// close the semi open.
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !this.semiOpen.contains( pi ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final boolean worked;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( flags.contains( GridFlags.COMPRESSED_CHANNEL ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
worked = this.useDenseChannel( pi );
|
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
|
|
|
worked = this.useChannel( pi );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( worked && flags.contains( GridFlags.MULTIBLOCK ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final Iterator<IGridNode> oni = ( (IGridMultiblock) ( (IGridNode) pi ).getGridBlock() ).getMultiblockNodes();
|
2015-04-03 08:54:31 +02:00
|
|
|
while( oni.hasNext() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final IGridNode otherNodes = oni.next();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( otherNodes != pi )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.semiOpen.add( (IPathItem) otherNodes );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pi.incrementChannelCount( 1 ); // give a channel.
|
2014-12-29 15:13:47 +01:00
|
|
|
this.semiOpen.remove( pi );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.closed.add( pi );
|
|
|
|
this.open.add( pi );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.open.isEmpty();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private boolean useDenseChannel( final IPathItem start )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
IPathItem pi = start;
|
2015-04-03 08:54:31 +02:00
|
|
|
while( pi != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !pi.canSupportMoreChannels() || pi.getFlags().contains( GridFlags.CANNOT_CARRY_COMPRESSED ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
pi = pi.getControllerRoute();
|
|
|
|
}
|
|
|
|
|
|
|
|
pi = start;
|
2015-04-03 08:54:31 +02:00
|
|
|
while( pi != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.pgc.setChannelsByBlocks( this.pgc.getChannelsByBlocks() + 1 );
|
2014-09-24 02:26:27 +02:00
|
|
|
pi.incrementChannelCount( 1 );
|
|
|
|
pi = pi.getControllerRoute();
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
this.pgc.setChannelsInUse( this.pgc.getChannelsInUse() + 1 );
|
2014-09-24 02:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private boolean useChannel( final IPathItem start )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
IPathItem pi = start;
|
2015-04-03 08:54:31 +02:00
|
|
|
while( pi != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !pi.canSupportMoreChannels() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
pi = pi.getControllerRoute();
|
|
|
|
}
|
|
|
|
|
|
|
|
pi = start;
|
2015-04-03 08:54:31 +02:00
|
|
|
while( pi != null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.pgc.setChannelsByBlocks( this.pgc.getChannelsByBlocks() + 1 );
|
2014-09-24 02:26:27 +02:00
|
|
|
pi.incrementChannelCount( 1 );
|
|
|
|
pi = pi.getControllerRoute();
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
this.pgc.setChannelsInUse( this.pgc.getChannelsInUse() + 1 );
|
2014-09-24 02:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-08 15:42:42 +02:00
|
|
|
|
|
|
|
public boolean isDead()
|
|
|
|
{
|
|
|
|
return this.isDead;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDead( final boolean isDead )
|
|
|
|
{
|
|
|
|
this.isDead = isDead;
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|