use of gregtech.api.pipenet.Node in project GregTech by GregTechCE.
the class EnergyNet method computePatches.
public List<RoutePath> computePatches(BlockPos startPos) {
ArrayList<RoutePath> readyPaths = new ArrayList<>();
RoutePath currentPath = new RoutePath();
Node<WireProperties> firstNode = getNodeAt(startPos);
currentPath.path.put(startPos, firstNode.data);
readyPaths.add(currentPath.cloneAndCompute(startPos));
HashSet<BlockPos> observedSet = new HashSet<>();
observedSet.add(startPos);
MutableBlockPos currentPos = new MutableBlockPos(startPos);
Stack<EnumFacing> moveStack = new Stack<>();
main: while (true) {
for (EnumFacing facing : EnumFacing.VALUES) {
currentPos.move(facing);
Node<WireProperties> secondNode = getNodeAt(currentPos);
if (secondNode != null && canNodesConnect(firstNode, facing, secondNode, this) && !observedSet.contains(currentPos)) {
BlockPos immutablePos = currentPos.toImmutable();
observedSet.add(immutablePos);
firstNode = secondNode;
moveStack.push(facing.getOpposite());
currentPath.path.put(immutablePos, getNodeAt(immutablePos).data);
if (secondNode.isActive) {
// if we are on active node, this is end of our path
RoutePath finalizedPath = currentPath.cloneAndCompute(immutablePos);
readyPaths.add(finalizedPath);
}
continue main;
} else {
currentPos.move(facing.getOpposite());
}
}
if (!moveStack.isEmpty()) {
currentPos.move(moveStack.pop());
// also remove already visited block from path
currentPath.path.remove(currentPos);
} else
break;
}
return readyPaths;
}
Aggregations