From 61aec43d440ba2bafaa8abe76c840fa62897c66f Mon Sep 17 00:00:00 2001 From: Calclavia Date: Wed, 5 Mar 2014 19:37:24 +0800 Subject: [PATCH] Finished new node grid --- .../mechanical/belt/BlockConveyorBelt.java | 12 --- .../energy/network/MechanicalNetwork.java | 30 -------- .../energy/network/MechanicalNode.java | 37 +++++---- .../energy/network/PartMechanical.java | 14 +--- .../energy/turbine/TileMechanicalTurbine.java | 2 +- .../resonantinduction/core/grid/Grid.java | 39 ++++++---- .../resonantinduction/core/grid/IGrid.java | 14 ---- .../resonantinduction/core/grid/INode.java | 29 ------- .../core/grid/INodeProvider.java | 2 +- .../resonantinduction/core/grid/Node.java | 77 ++++++++++++++++--- .../resonantinduction/core/grid/NodeGrid.java | 60 +++++++-------- .../core/grid/TickingGrid.java | 47 +++++++++++ 12 files changed, 192 insertions(+), 171 deletions(-) delete mode 100644 mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNetwork.java delete mode 100644 src/main/java/resonantinduction/core/grid/IGrid.java delete mode 100644 src/main/java/resonantinduction/core/grid/INode.java create mode 100644 src/main/java/resonantinduction/core/grid/TickingGrid.java diff --git a/mechanical/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java b/mechanical/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java index 89c53a556..3c228b9d0 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java @@ -47,18 +47,6 @@ public class BlockConveyorBelt extends BlockTile } } - @Override - public void onNeighborBlockChange(World world, int x, int y, int z, int par5) - { - TileEntity t = world.getBlockTileEntity(x, y, z); - - if (t != null && t instanceof TileConveyorBelt) - { - TileConveyorBelt tileEntity = (TileConveyorBelt) t; - tileEntity.mechanicalNode.reconstruct(); - } - } - @Override public void setBlockBoundsBasedOnState(IBlockAccess world, int x, int y, int z) { diff --git a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNetwork.java b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNetwork.java deleted file mode 100644 index a6a3286cb..000000000 --- a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNetwork.java +++ /dev/null @@ -1,30 +0,0 @@ -package resonantinduction.mechanical.energy.network; - -import resonantinduction.core.grid.NodeGrid; -import universalelectricity.core.net.NetworkTickHandler; - -/** - * A mechanical network for translate speed and force using mechanical rotations. - * - * Useful Formula: - * - * Power is the work per unit time. - * Power (W) = Torque (Strength of the rotation, Newton Meters) x Speed (Angular Velocity, RADIAN - * PER SECOND). - * *OR* - * Power = Torque / Time - * - * Torque = r (Radius) * F (Force) * sin0 (Direction/Angle of the force applied. 90 degrees if - * optimal.) - * - * @author Calclavia - */ -public class MechanicalNetwork extends NodeGrid -{ - public MechanicalNetwork(MechanicalNode node) - { - super(MechanicalNode.class); - add(node); - NetworkTickHandler.addNetwork(this); - } -} diff --git a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java index c4b03f7ec..c651ce5c8 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/MechanicalNode.java @@ -1,22 +1,35 @@ package resonantinduction.mechanical.energy.network; -import java.util.AbstractMap; import java.util.Iterator; import java.util.Map.Entry; -import java.util.WeakHashMap; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; -import resonantinduction.core.grid.IGrid; +import resonantinduction.core.grid.Grid; +import resonantinduction.core.grid.TickingGrid; import universalelectricity.api.vector.Vector3; import codechicken.multipart.TMultiPart; +/** + * A mechanical node for mechanical energy. + * + * Useful Formula: + * + * Power is the work per unit time. + * Power (W) = Torque (Strength of the rotation, Newton Meters) x Speed (Angular Velocity, RADIAN + * PER SECOND). + * *OR* + * Power = Torque / Time + * + * Torque = r (Radius) * F (Force) * sin0 (Direction/Angle of the force applied. 90 degrees if + * optimal.) + * + * @author Calclavia + */ public class MechanicalNode extends EnergyNode { - protected final AbstractMap connections = new WeakHashMap(); - public final IMechanicalNodeProvider parent; public double torque = 0; @@ -54,7 +67,7 @@ public class MechanicalNode extends EnergyNode public void update(float deltaTime) { power = getEnergy() / deltaTime; - + prevAngularVelocity = angularVelocity; onUpdate(); @@ -71,7 +84,7 @@ public class MechanicalNode extends EnergyNode if (world() != null && !world().isRemote) { double acceleration = this.acceleration * deltaTime; -System.out.println("UPDATED"); + /** * Loss energy */ @@ -190,12 +203,6 @@ System.out.println("UPDATED"); } } - @Override - public AbstractMap getConnections() - { - return connections; - } - public World world() { return parent instanceof TMultiPart ? ((TMultiPart) parent).world() : parent instanceof TileEntity ? ((TileEntity) parent).getWorldObj() : null; @@ -224,9 +231,9 @@ System.out.println("UPDATED"); } @Override - public IGrid newGrid() + public Grid newGrid() { - return new MechanicalNetwork(this); + return new TickingGrid(this); } @Override diff --git a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/PartMechanical.java b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/PartMechanical.java index 668c956d1..966fb98ab 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/energy/network/PartMechanical.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/energy/network/PartMechanical.java @@ -79,22 +79,10 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu node.reconstruct(); } - @Override - public void onNeighborChanged() - { - node.reconstruct(); - } - - @Override - public void onPartChanged(TMultiPart part) - { - node.reconstruct(); - } - @Override public void onWorldSeparate() { - node.split(); + node.deconstruct(); } /** Packet Code. */ diff --git a/mechanical/src/main/java/resonantinduction/mechanical/energy/turbine/TileMechanicalTurbine.java b/mechanical/src/main/java/resonantinduction/mechanical/energy/turbine/TileMechanicalTurbine.java index da881563f..7699d31d5 100644 --- a/mechanical/src/main/java/resonantinduction/mechanical/energy/turbine/TileMechanicalTurbine.java +++ b/mechanical/src/main/java/resonantinduction/mechanical/energy/turbine/TileMechanicalTurbine.java @@ -57,7 +57,7 @@ public class TileMechanicalTurbine extends TileTurbine implements IMechanicalNod @Override public void invalidate() { - node.split(); + node.deconstruct(); super.invalidate(); } diff --git a/src/main/java/resonantinduction/core/grid/Grid.java b/src/main/java/resonantinduction/core/grid/Grid.java index 23682ca2e..146e29751 100644 --- a/src/main/java/resonantinduction/core/grid/Grid.java +++ b/src/main/java/resonantinduction/core/grid/Grid.java @@ -16,7 +16,7 @@ import universalelectricity.core.net.ConnectionPathfinder; * * @param - The node type. */ -public abstract class Grid implements IGrid +public abstract class Grid { /** * A set of connectors (e.g conductors). @@ -29,7 +29,6 @@ public abstract class Grid implements IGrid nodeType = type; } - @Override public void add(N node) { synchronized (nodes) @@ -38,7 +37,6 @@ public abstract class Grid implements IGrid } } - @Override public void remove(N node) { synchronized (nodes) @@ -47,21 +45,20 @@ public abstract class Grid implements IGrid } } - @Override public Set getNodes() { return nodes; } /** - * A simple reconstruct class to rebuild the grid. + * A simple reconstruct class to rebuild the grid. The set "nodes" is copied due to the fact + * that this method will allow the modification of nodes while looping. */ - @Override public void reconstruct() { synchronized (nodes) { - Iterator it = nodes.iterator(); + Iterator it = new HashSet(nodes).iterator(); while (it.hasNext()) { @@ -84,6 +81,19 @@ public abstract class Grid implements IGrid return nodeType.isAssignableFrom(node.getClass()); } + protected void reconstructNode(N node) + { + + } + + public void deconstruct() + { + synchronized (nodes) + { + nodes.clear(); + } + } + /** * Gets the first connector in the set. * @@ -91,22 +101,19 @@ public abstract class Grid implements IGrid */ public N getFirstNode() { - for (N node : getNodes()) + synchronized (nodes) { - return node; + for (N node : nodes) + { + return node; + } } - return null; } - protected void reconstructNode(N node) - { - - } - @Override public String toString() { - return getClass().getSimpleName() + "[" + hashCode() + ", Connectors: " + nodes.size() + "]"; + return getClass().getSimpleName() + "[" + hashCode() + ", Nodes: " + nodes.size() + "]"; } } diff --git a/src/main/java/resonantinduction/core/grid/IGrid.java b/src/main/java/resonantinduction/core/grid/IGrid.java deleted file mode 100644 index 336ff1940..000000000 --- a/src/main/java/resonantinduction/core/grid/IGrid.java +++ /dev/null @@ -1,14 +0,0 @@ -package resonantinduction.core.grid; - -import java.util.Set; - -public interface IGrid -{ - public void add(N node); - - public void remove(N node); - - public Set getNodes(); - - public void reconstruct(); -} diff --git a/src/main/java/resonantinduction/core/grid/INode.java b/src/main/java/resonantinduction/core/grid/INode.java deleted file mode 100644 index 071670423..000000000 --- a/src/main/java/resonantinduction/core/grid/INode.java +++ /dev/null @@ -1,29 +0,0 @@ -package resonantinduction.core.grid; - -import java.util.AbstractMap; - -import net.minecraftforge.common.ForgeDirection; - -public interface INode -{ - /** - * Updates the node. This may be called on a different thread. - * - * @param deltaTime - The time in seconds that has passed between the successive updates. - */ - void update(float deltaTime); - - /** - * @return A map consisting of the connected object and a ForgeDirection. - */ - AbstractMap getConnections(); - - G getGrid(); - - void setGrid(G grid); - - /** - * Called whenever the node changes to update its cached connections and network. - */ - void reconstruct(); -} diff --git a/src/main/java/resonantinduction/core/grid/INodeProvider.java b/src/main/java/resonantinduction/core/grid/INodeProvider.java index cf828b14e..c3599a4c7 100644 --- a/src/main/java/resonantinduction/core/grid/INodeProvider.java +++ b/src/main/java/resonantinduction/core/grid/INodeProvider.java @@ -2,7 +2,7 @@ package resonantinduction.core.grid; import net.minecraftforge.common.ForgeDirection; -public interface INodeProvider +public interface INodeProvider { public N getNode(ForgeDirection from); } diff --git a/src/main/java/resonantinduction/core/grid/Node.java b/src/main/java/resonantinduction/core/grid/Node.java index d021568a8..fd44f181f 100644 --- a/src/main/java/resonantinduction/core/grid/Node.java +++ b/src/main/java/resonantinduction/core/grid/Node.java @@ -1,13 +1,17 @@ package resonantinduction.core.grid; -import resonantinduction.mechanical.energy.network.PartMechanical; -import net.minecraft.nbt.NBTTagCompound; +import java.util.AbstractMap; +import java.util.WeakHashMap; -public abstract class Node implements INode +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.ForgeDirection; + +public abstract class Node { + protected final AbstractMap connections = new WeakHashMap(); + public G grid = null; - @Override public final G getGrid() { if (grid == null) @@ -18,37 +22,90 @@ public abstract class Node implements INode protected abstract G newGrid(); - @Override public final void setGrid(G grid) { this.grid = grid; } - @Override + public void update(float deltaTime) + { + + } + + /** + * This constructs the node. It should be called whenever the connections of the node are + * updated OR when the node is first initiated and can access its connections. + */ public void reconstruct() { - recache(); - getGrid().reconstruct(); + synchronized (connections) + { + recache(); + getGrid().add(this); + getGrid().reconstruct(); + } } - // TODO: Fix this. - public void split() + /** + * This destroys the node, removing it from the grid and also destroying all references to it. + */ + public void deconstruct() { + synchronized (connections) + { + /** + * Remove self from all connections. + */ + for (N connection : connections.keySet()) + { + if (getGrid().isValidNode(connection)) + { + ((Node) connection).getConnections().remove(this); + } + } + getGrid().remove(this); + getGrid().deconstruct(); + } } + /** + * Called for a node to recache all its connections. + */ public void recache() { } + /** + * Returns all the connections in this node. + * + * @return + */ + public AbstractMap getConnections() + { + return connections; + } + + /** + * Must be called to load the node's data. + */ public void load(NBTTagCompound nbt) { } + /** + * Must be called to save the node's data. + */ public void save(NBTTagCompound nbt) { } + + @Override + public String toString() + { + return getClass().getSimpleName() + "[" + hashCode() + ", Connections: " + connections.size() + ", Grid:" + getGrid() + "]"; + } } diff --git a/src/main/java/resonantinduction/core/grid/NodeGrid.java b/src/main/java/resonantinduction/core/grid/NodeGrid.java index 1ed28fe68..fac12e5be 100644 --- a/src/main/java/resonantinduction/core/grid/NodeGrid.java +++ b/src/main/java/resonantinduction/core/grid/NodeGrid.java @@ -1,57 +1,39 @@ package resonantinduction.core.grid; import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import net.minecraftforge.common.ForgeDirection; import resonantinduction.mechanical.energy.network.MechanicalNode; +import universalelectricity.api.net.IConnectable; import universalelectricity.api.net.IUpdate; -public class NodeGrid extends Grid implements IUpdate +public abstract class NodeGrid extends Grid { public NodeGrid(Class type) { super(type); } - /** - * An grid update called only server side. - */ - @Override - public void update() - { - synchronized (nodes) - { - for (INode node : nodes) - { - node.update(1 / 20f); - } - } - } - - @Override - public boolean canUpdate() - { - return nodes.size() > 0; - } - - @Override - public boolean continueUpdate() - { - return canUpdate(); - } - @Override protected void reconstructNode(N node) { + node.recache(); node.setGrid(this); AbstractMap connections = node.getConnections(); for (Object connection : connections.keySet()) { - if (isValidNode(connection) && connection instanceof INode) + if (isValidNode(connection) && connection instanceof Node) { - INode connectedNode = (INode) connection; + Node connectedNode = (Node) connection; if (connectedNode.getGrid() != this) { @@ -63,4 +45,22 @@ public class NodeGrid extends Grid implements IUpdate } } } + + @Override + public void deconstruct() + { + synchronized (nodes) + { + Iterator it = new HashSet(nodes).iterator(); + + while (it.hasNext()) + { + N node = it.next(); + node.setGrid(null); + node.reconstruct(); + } + + nodes.clear(); + } + } } diff --git a/src/main/java/resonantinduction/core/grid/TickingGrid.java b/src/main/java/resonantinduction/core/grid/TickingGrid.java new file mode 100644 index 000000000..cb8c2828c --- /dev/null +++ b/src/main/java/resonantinduction/core/grid/TickingGrid.java @@ -0,0 +1,47 @@ +package resonantinduction.core.grid; + +import resonantinduction.mechanical.energy.network.MechanicalNode; +import universalelectricity.api.net.IUpdate; +import universalelectricity.core.net.NetworkTickHandler; + +public class TickingGrid extends NodeGrid implements IUpdate +{ + public TickingGrid(N node, Class type) + { + super(type); + add(node); + NetworkTickHandler.addNetwork(this); + } + + public TickingGrid(N node) + { + this(node, node.getClass()); + } + + /** + * An grid update called only server side. + */ + @Override + public void update() + { + synchronized (nodes) + { + for (Node node : nodes) + { + node.update(1 / 20f); + } + } + } + + @Override + public boolean canUpdate() + { + return nodes.size() > 0; + } + + @Override + public boolean continueUpdate() + { + return canUpdate(); + } +}