Now adding individual multiparts into network instead of the tile()

This commit is contained in:
Calclavia 2014-01-20 14:29:43 +08:00
parent c0937c8cdb
commit 29d55d3aa8
3 changed files with 118 additions and 6 deletions

View file

@ -198,11 +198,11 @@ public abstract class PartFramedConnection<M extends Enum, C extends IConnector<
@Override
public void bind(TileMultipart t)
{
if (tile() != null && this.getNetwork() != null)
if ( this.getNetwork() != null)
{
getNetwork().getConnectors().remove(tile());
getNetwork().getConnectors().remove(this);
super.bind(t);
getNetwork().getConnectors().add(getConnector(tile()));
getNetwork().getConnectors().add(this);
}
else
{
@ -290,7 +290,7 @@ public abstract class PartFramedConnection<M extends Enum, C extends IConnector<
if (or != possibleWireConnections)
{
this.getNetwork().removeConnector(this);
this.getNetwork().split(getConnector(tile()));
this.getNetwork().split(this);
setNetwork(null);
}
@ -327,7 +327,7 @@ public abstract class PartFramedConnection<M extends Enum, C extends IConnector<
{
TileEntity[] connections = new TileEntity[6];
if (world() != null && tile() != null)
if (world() != null)
{
for (byte i = 0; i < 6; i++)
{
@ -360,7 +360,7 @@ public abstract class PartFramedConnection<M extends Enum, C extends IConnector<
return false;
}
Vector3 connectPos = new Vector3(tile()).modifyPositionFromSide(direction);
Vector3 connectPos = new Vector3(tile()).translate(direction);
TileEntity connectTile = connectPos.getTileEntity(world());
return !isConnectionPrevented(connectTile, direction);
}

View file

@ -16,6 +16,9 @@ public interface IMechanical extends IConnectable
*/
public long onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity, boolean doReceive);
/**
* @return Is the mechanical machine going clockwise currently?
*/
public boolean isClockwise();
public void setClockwise(boolean isClockwise);

View file

@ -0,0 +1,109 @@
package resonantinduction.mechanical.network;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import universalelectricity.api.net.IConnector;
/**
* Sets proper rotations on all connected units in the mechanical network.
*
* The pathfinder will find the first point and rotate all adjacent blocks next to it to be the
* opposite of the original.
*
* @author Calclavia
*
*/
public class PathfinderRotationManager
{
/** A list of nodes that the pathfinder already went through. */
public final Set<IConnector> closedSet = new LinkedHashSet<IConnector>();
/** The resulted path found by the pathfinder. Could be null if no path was found. */
public final Set<IConnector> results = new LinkedHashSet<IConnector>();
private final IConnector targetConnector;
private final List<IConnector> ignoreConnector;
public PathfinderRotationManager(IConnector targetConnector, IConnector... ignoreConnector)
{
this.targetConnector = targetConnector;
if (ignoreConnector != null)
{
this.ignoreConnector = Arrays.asList(ignoreConnector);
}
else
{
this.ignoreConnector = new ArrayList<IConnector>();
}
}
/**
* A recursive function to find all connectors.
*
* @return True on success finding, false on failure.
*/
public boolean findNodes(IConnector currentNode)
{
this.closedSet.add(currentNode);
if (this.onSearch(currentNode))
{
return false;
}
for (IConnector node : this.getConnectedNodes(currentNode))
{
if (!this.closedSet.contains(node))
{
if (this.findNodes(node))
{
return true;
}
}
}
return false;
}
public Set<IConnector> getConnectedNodes(IConnector currentNode)
{
Set<IConnector> connectedNodes = new HashSet<IConnector>();
if (currentNode != null)
{
for (int i = 0; i < currentNode.getConnections().length; i++)
{
Object obj = currentNode.getConnections()[i];
if (obj instanceof IConnector && !this.ignoreConnector.contains(obj))
{
connectedNodes.add((IConnector) obj);
}
}
}
return connectedNodes;
}
public boolean onSearch(IConnector node)
{
if (node == this.targetConnector)
{
this.results.add(node);
return true;
}
return false;
}
public void reset()
{
this.results.clear();
this.closedSet.clear();
}
}