From 29d55d3aa88bde6b9070083abc5e4cd6240df93a Mon Sep 17 00:00:00 2001 From: Calclavia Date: Mon, 20 Jan 2014 14:29:43 +0800 Subject: [PATCH] Now adding individual multiparts into network instead of the tile() --- .../prefab/part/PartFramedConnection.java | 12 +- .../mechanical/network/IMechanical.java | 3 + .../network/PathfinderRotationManager.java | 109 ++++++++++++++++++ 3 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 src/main/java/resonantinduction/mechanical/network/PathfinderRotationManager.java diff --git a/src/main/java/resonantinduction/core/prefab/part/PartFramedConnection.java b/src/main/java/resonantinduction/core/prefab/part/PartFramedConnection.java index cba1596cd..f037277fd 100644 --- a/src/main/java/resonantinduction/core/prefab/part/PartFramedConnection.java +++ b/src/main/java/resonantinduction/core/prefab/part/PartFramedConnection.java @@ -198,11 +198,11 @@ public abstract class PartFramedConnection closedSet = new LinkedHashSet(); + + /** The resulted path found by the pathfinder. Could be null if no path was found. */ + public final Set results = new LinkedHashSet(); + + private final IConnector targetConnector; + private final List ignoreConnector; + + public PathfinderRotationManager(IConnector targetConnector, IConnector... ignoreConnector) + { + this.targetConnector = targetConnector; + if (ignoreConnector != null) + { + this.ignoreConnector = Arrays.asList(ignoreConnector); + } + else + { + this.ignoreConnector = new ArrayList(); + } + } + + /** + * 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 getConnectedNodes(IConnector currentNode) + { + Set connectedNodes = new HashSet(); + + 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(); + } +}