diff --git a/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/GearNetwork.java b/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/GearNetwork.java index 546922259..3b5cf608f 100644 --- a/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/GearNetwork.java +++ b/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/GearNetwork.java @@ -1,9 +1,11 @@ package resonantinduction.mechanical.gear.dev; import java.util.Collections; +import java.util.Map.Entry; import java.util.Set; import java.util.WeakHashMap; +import net.minecraftforge.common.ForgeDirection; import universalelectricity.api.net.IUpdate; import universalelectricity.core.net.ConnectionPathfinder; @@ -20,14 +22,33 @@ public class GearNetwork implements IUpdate private boolean doRemap = false; private boolean isDead = false; + public GearNetwork() + { + doRemap = true; + } + + public GearNetwork(NodeGear nodeGear) + { + this(); + onAdded(nodeGear); + } + /** Called by a gear when its added to the world */ public void onAdded(NodeGear gear) + { + onAdded(gear, true); + } + + public void onAdded(NodeGear gear, boolean checkMerge) { if (gear instanceof NodeGenerator) { if (!generators.contains(gear)) { + //TODO set gear's network generators.add((NodeGenerator) gear); + if (checkMerge) + checkForMerge(gear); doRemap = true; } } @@ -36,16 +57,25 @@ public class GearNetwork implements IUpdate if (!nodes.contains(gear)) { nodes.add(gear); + if (checkMerge) + checkForMerge(gear); doRemap = true; } } } + public void checkForMerge(NodeGear gear) + { + + } + /** Called by a gear when its removed from the world */ - public void onRemoved(NodeGear gear) + public void onRemoved(NodeGear gear, boolean doSplit) { if (generators.remove(gear) || nodes.remove(gear)) { + if (doSplit) + this.split(gear); doRemap = true; } } @@ -101,27 +131,29 @@ public class GearNetwork implements IUpdate return null; } - public void split(NodeGear splitPoint) + /** Called to do a path finding check too see if the network needs to be split into two or more + * networks. Does up to 6 path finding checks, and stops when it finds a connection back to one + * of the sub networks. */ + protected void split(NodeGear splitPoint) { /** Loop through the connected blocks and attempt to see if there are connections between the * two points elsewhere. */ - Object[] connectedBlocks = getConnectionsFor(splitPoint); - removeConnector(splitPoint); + WeakHashMap connectedBlocks = splitPoint.connections; - for (int i = 0; i < connectedBlocks.length; i++) + for (Entry entry : connectedBlocks.entrySet()) { - Object connectedA = connectedBlocks[i]; + final NodeGear connectedA = entry.getKey(); - if (connectedA != null && isValidConnector(connectedA)) + if (connectedA != null) { - for (int ii = 0; ii < connectedBlocks.length; ii++) + for (Entry entry2 : connectedBlocks.entrySet()) { - final Object connectedB = connectedBlocks[ii]; + final NodeGear connectedB = entry2.getKey(); - if (connectedB != null && connectedA != connectedB && isValidConnector(connectedB)) + if (connectedB != null && connectedA != connectedB) { - ConnectionPathfinder finder = new ConnectionPathfinder(getConnectorClass(), (C) connectedB, splitPoint); - finder.findNodes((C) connectedA); + ConnectionPathfinder finder = new ConnectionPathfinder(NodeGear.class, connectedB, splitPoint); + finder.findNodes(connectedA); if (finder.results.size() <= 0) { @@ -129,15 +161,14 @@ public class GearNetwork implements IUpdate { /** The connections A and B are not connected anymore. Give them both * a new common network. */ - N newNetwork = newInstance(); + GearNetwork newNetwork = new GearNetwork(); - for (C node : finder.closedSet) + for (NodeGear node : finder.closedSet) { if (node != splitPoint) { - newNetwork.addConnector(node); - removeConnector(node); - onSplit(newNetwork); + newNetwork.onAdded(node, false); + onRemoved(node, false); } } newNetwork.reconstruct(); @@ -159,7 +190,7 @@ public class GearNetwork implements IUpdate /** Called to rebuild the network */ protected void reconstruct() { - + //TODO path find from each generator to each gear } /** Called to destroy or rather clean up the network. Make sure to do your cleanup in this diff --git a/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/NodeGear.java b/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/NodeGear.java index 11f55df0e..93ca412c4 100644 --- a/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/NodeGear.java +++ b/mechanical/src/main/scala/resonantinduction/mechanical/gear/dev/NodeGear.java @@ -1,24 +1,31 @@ package resonantinduction.mechanical.gear.dev; import java.util.HashMap; +import java.util.WeakHashMap; +import net.minecraftforge.common.ForgeDirection; import resonant.api.grid.INode; +import resonant.api.grid.INodeProvider; import resonant.lib.type.Pair; +import universalelectricity.api.net.IConnector; /** Applied to any device that acts as part of a gear network using or supplying rotational force * * @author Darkguardsman */ -public class NodeGear implements INode +public class NodeGear implements INode, IConnector { /** Used by the gear network to track the rotation effect each generator has on this gear */ public HashMap> rotationEffectMap = new HashMap>(); - + protected WeakHashMap connections = new WeakHashMap(); + /** Speed by which this gear rotates */ protected float rotationSpeed = 0; /** Force carried by this gear */ protected float force = 0; /** Flag if the gear is rotating clockwise */ protected boolean clockwise = false; + /** Network this gear is part of */ + protected GearNetwork network; @Override public void update(float deltaTime) @@ -48,4 +55,39 @@ public class NodeGear implements INode } + @Override + public GearNetwork getNetwork() + { + if (network == null) + { + network = new GearNetwork(this); + } + return network; + } + + @Override + public void setNetwork(GearNetwork network) + { + // TODO Auto-generated method stub + + } + + @Override + public boolean canConnect(ForgeDirection from, Object source) + { + return source instanceof NodeGear || source instanceof INodeProvider && ((INodeProvider) source).getNode(NodeGear.class, from) != null; + } + + @Override + public Object[] getConnections() + { + return null; + } + + @Override + public IConnector getInstance(ForgeDirection dir) + { + return this; + } + }