From 95ce03ee487a5d0fe5fa700c1d3f26cfdb2d7476 Mon Sep 17 00:00:00 2001 From: DarkGuardsman Date: Tue, 14 Jan 2014 15:13:39 -0500 Subject: [PATCH] More work on mechanical networks --- .../java/resonantinduction/api/IBelt.java | 26 +- .../resonantinduction/api/IBeltNetwork.java | 15 + .../electrical/generator/TileGenerator.java | 10 +- .../mechanical/belt/BeltNetwork.java | 160 +++++++ .../mechanical/belt/BlockConveyorBelt.java | 2 +- .../mechanical/belt/TileConveyorBelt.java | 416 +++++++++--------- .../mechanical/gear/PartGear.java | 19 +- .../mechanical/network/IMechConnector.java | 11 +- .../mechanical/network/IMechMachine.java | 6 +- .../mechanical/network/IMechNetwork.java | 11 +- .../mechanical/network/MechNetwork.java | 250 +++++++++-- 11 files changed, 633 insertions(+), 293 deletions(-) create mode 100644 src/main/java/resonantinduction/api/IBeltNetwork.java create mode 100644 src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java diff --git a/src/main/java/resonantinduction/api/IBelt.java b/src/main/java/resonantinduction/api/IBelt.java index fcce4986d..7b32bb5fd 100644 --- a/src/main/java/resonantinduction/api/IBelt.java +++ b/src/main/java/resonantinduction/api/IBelt.java @@ -3,21 +3,19 @@ package resonantinduction.api; import java.util.List; import net.minecraft.entity.Entity; +import universalelectricity.api.net.IConnector; -/** An interface applied to the tile entity of a conveyor belt */ -public interface IBelt +/** An interface applied to the tile entity of a conveyor belt + * @Author DarkGuardsman */ +public interface IBelt extends IConnector { - /** - * Used to get a list of entities the belt exerts an effect upon. - * - * @return list of entities in the belts are of effect - */ - public List getAffectedEntities(); + /** Used to get a list of entities the belt exerts an effect upon. + * + * @return list of entities in the belts are of effect */ + public List getAffectedEntities(); - /** - * Adds and entity to the ignore list so its not moved has to be done every 20 ticks - * - * @param entity - */ - public void ignoreEntity(Entity entity); + /** Adds and entity to the ignore list so its not moved has to be done every 20 ticks + * + * @param entity */ + public void ignoreEntity(Entity entity); } diff --git a/src/main/java/resonantinduction/api/IBeltNetwork.java b/src/main/java/resonantinduction/api/IBeltNetwork.java new file mode 100644 index 000000000..9c9531f7a --- /dev/null +++ b/src/main/java/resonantinduction/api/IBeltNetwork.java @@ -0,0 +1,15 @@ +package resonantinduction.api; + +import net.minecraft.tileentity.TileEntity; +import universalelectricity.api.net.INetwork; + +public interface IBeltNetwork extends INetwork +{ + /** Frame of animation the belts all share */ + public int frame(); + + /** Speed to apply to each entity */ + public float speed(); + + public void reconstruct(); +} diff --git a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java index ce85596c6..d9238dae1 100644 --- a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java +++ b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java @@ -11,6 +11,7 @@ import calclavia.lib.prefab.tile.TileElectrical; * @author Calclavia */ public class TileGenerator extends TileElectrical implements IMechMachine { + //P = \tau \times 2 \pi \times \omega private long power; /** Generator turns KE -> EE. Inverted one will turn EE -> KE. */ @@ -40,7 +41,7 @@ public class TileGenerator extends TileElectrical implements IMechMachine private boolean isFunctioning() { - return true; + return this.worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord); } @Override @@ -48,4 +49,11 @@ public class TileGenerator extends TileElectrical implements IMechMachine { // TODO Auto-generated method stub } + + @Override + public int getForceSide(ForgeDirection side) + { + // TODO Auto-generated method stub + return 0; + } } diff --git a/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java b/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java new file mode 100644 index 000000000..e8ae64296 --- /dev/null +++ b/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java @@ -0,0 +1,160 @@ +package resonantinduction.mechanical.belt; + +import net.minecraft.tileentity.TileEntity; +import resonantinduction.api.IBelt; +import resonantinduction.api.IBeltNetwork; +import resonantinduction.mechanical.network.IMechConnector; +import resonantinduction.mechanical.network.IMechNetwork; +import resonantinduction.mechanical.network.MechNetwork; +import universalelectricity.api.net.IConnector; +import universalelectricity.core.net.ConnectionPathfinder; +import universalelectricity.core.net.Network; + +/** Network used to update belts in a uniform way + * + * @author DarkGuardsman */ +public class BeltNetwork extends Network implements IBeltNetwork +{ + + @Override + public boolean canUpdate() + { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean continueUpdate() + { + // TODO Auto-generated method stub + return false; + } + + @Override + public void update() + { + // TODO Auto-generated method stub + + } + + @Override + public IBeltNetwork merge(IBeltNetwork network) + { + if (network.getClass().isAssignableFrom(this.getClass()) && network != this) + { + BeltNetwork newNetwork = new BeltNetwork(); + newNetwork.getConnectors().addAll(this.getConnectors()); + newNetwork.getConnectors().addAll(network.getConnectors()); + + network.getConnectors().clear(); + network.getNodes().clear(); + this.getConnectors().clear(); + this.getNodes().clear(); + + newNetwork.reconstruct(); + return newNetwork; + } + + return null; + } + + @Override + public void split(IBelt splitPoint) + { + this.removeConnector(splitPoint); + this.reconstruct(); + + /** Loop through the connected blocks and attempt to see if there are connections between the + * two points elsewhere. */ + Object[] connectedBlocks = splitPoint.getConnections(); + + for (int i = 0; i < connectedBlocks.length; i++) + { + Object connectedBlockA = connectedBlocks[i]; + + if (connectedBlockA instanceof IBelt) + { + for (int ii = 0; ii < connectedBlocks.length; ii++) + { + final Object connectedBlockB = connectedBlocks[ii]; + + if (connectedBlockA != connectedBlockB && connectedBlockB instanceof IBelt) + { + ConnectionPathfinder finder = new ConnectionPathfinder((IConnector) connectedBlockB, splitPoint); + finder.findNodes((IConnector) connectedBlockA); + + if (finder.results.size() <= 0) + { + try + { + /** The connections A and B are not connected anymore. Give them both + * a new common network. */ + IBeltNetwork newNetwork = new BeltNetwork(); + for (IConnector node : finder.closedSet) + { + if (node != splitPoint && node instanceof IBelt) + { + newNetwork.addConnector((IBelt) node); + } + } + newNetwork.reconstruct(); + } + catch (Exception e) + { + e.printStackTrace(); + } + + } + } + } + } + } + } + + @Override + public void split(IBelt connectorA, IBelt connectorB) + { + /** Check if connectorA connects with connectorB. */ + ConnectionPathfinder finder = new ConnectionPathfinder(connectorB); + finder.findNodes(connectorA); + + if (finder.results.size() <= 0) + { + /** The connections A and B are not connected anymore. Give them both a new common + * network. */ + IMechNetwork newNetwork = new MechNetwork(); + + for (IConnector node : finder.closedSet) + { + if (node instanceof IMechConnector) + { + newNetwork.addConnector((IMechConnector) node); + } + } + + newNetwork.reconstruct(); + } + } + + @Override + public int frame() + { + // TODO Auto-generated method stub + return 0; + } + + @Override + public float speed() + { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void reconstruct() + { + // TODO Auto-generated method stub + + } + +} diff --git a/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java b/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java index 2cf3a7ae9..1d36305bf 100644 --- a/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java +++ b/src/main/java/resonantinduction/mechanical/belt/BlockConveyorBelt.java @@ -250,7 +250,7 @@ public class BlockConveyorBelt extends BlockRI { return; } - if (tile.isFunctioning() && !world.isBlockIndirectlyGettingPowered(x, y, z)) + if (!world.isBlockIndirectlyGettingPowered(x, y, z)) { float acceleration = tile.acceleration; float maxSpeed = tile.maxSpeed; diff --git a/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java b/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java index 77ed09f04..dc0c267a2 100644 --- a/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java +++ b/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java @@ -5,250 +5,252 @@ import java.util.Iterator; import java.util.List; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.packet.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.ForgeDirection; import resonantinduction.api.IBelt; +import resonantinduction.api.IBeltNetwork; import resonantinduction.core.ResonantInduction; -import resonantinduction.core.prefab.tile.TileAssembly; import resonantinduction.mechanical.Mechanical; import universalelectricity.api.vector.Vector3; +import calclavia.lib.network.IPacketReceiverWithID; import calclavia.lib.prefab.tile.IRotatable; +import calclavia.lib.prefab.tile.TileAdvanced; import com.google.common.io.ByteArrayDataInput; -import cpw.mods.fml.common.network.Player; - -/** - * Conveyer belt TileEntity that allows entities of all kinds to be moved +/** Conveyer belt TileEntity that allows entities of all kinds to be moved * - * @author DarkGuardsman - */ -public class TileConveyorBelt extends TileAssembly implements IBelt, IRotatable + * @author DarkGuardsman */ +public class TileConveyorBelt extends TileAdvanced implements IBelt, IRotatable, IPacketReceiverWithID { - public enum SlantType - { - NONE, UP, DOWN, TOP - } + public enum SlantType + { + NONE, + UP, + DOWN, + TOP + } - public static final int MAX_FRAME = 13; - public static final int MAX_SLANT_FRAME = 23; - /** Packet name to ID the packet containing info on the angle of the belt */ - public static final String slantPacketID = "slantPacket"; - /** Acceleration of entities on the belt */ - public final float acceleration = 0.01f; - /** max speed of entities on the belt */ - public final float maxSpeed = 0.1f; - /** Current rotation of the model wheels */ - public float wheelRotation = 0; - /** Frame count for texture animation from 0 - maxFrame */ - private int animFrame = 0; - private SlantType slantType = SlantType.NONE; - /** Entities that are ignored allowing for other tiles to interact with them */ - public List IgnoreList = new ArrayList(); + public static final int MAX_FRAME = 13; + public static final int MAX_SLANT_FRAME = 23; + /** Packet name to ID the packet containing info on the angle of the belt */ + public static final String slantPacketID = "slantPacket"; + /** Acceleration of entities on the belt */ + public final float acceleration = 0.01f; + /** max speed of entities on the belt */ + public final float maxSpeed = 0.1f; + /** Current rotation of the model wheels */ + public float wheelRotation = 0; + /** Frame count for texture animation from 0 - maxFrame */ + private int animFrame = 0; + private SlantType slantType = SlantType.NONE; + /** Entities that are ignored allowing for other tiles to interact with them */ + public List IgnoreList = new ArrayList(); + private boolean functioning; - public TileConveyorBelt() - { - super(1); - } + @Override + public void updateEntity() + { + super.updateEntity(); + /* PROCESSES IGNORE LIST AND REMOVES UNNEED ENTRIES */ + Iterator it = this.IgnoreList.iterator(); + while (it.hasNext()) + { + if (!this.getAffectedEntities().contains(it.next())) + { + it.remove(); + } + } + if (this.worldObj.isRemote) + { + if (this.ticks % 10 == 0 && this.worldObj.isRemote && this.worldObj.getBlockId(this.xCoord - 1, this.yCoord, this.zCoord) != Mechanical.blockConveyorBelt.blockID && this.worldObj.getBlockId(xCoord, yCoord, zCoord - 1) != Mechanical.blockConveyorBelt.blockID) + { + this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 0.5f, 0.7f, true); + } + this.wheelRotation = (40 + this.wheelRotation) % 360; - @Override - public void updateEntity() - { - super.updateEntity(); - /* PROCESSES IGNORE LIST AND REMOVES UNNEED ENTRIES */ - Iterator it = this.IgnoreList.iterator(); - while (it.hasNext()) - { - if (!this.getAffectedEntities().contains(it.next())) - { - it.remove(); - } - } - if (this.worldObj.isRemote && this.isFunctioning()) - { - if (this.ticks % 10 == 0 && this.worldObj.isRemote && this.worldObj.getBlockId(this.xCoord - 1, this.yCoord, this.zCoord) != Mechanical.blockConveyorBelt.blockID && this.worldObj.getBlockId(xCoord, yCoord, zCoord - 1) != Mechanical.blockConveyorBelt.blockID) - { - this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 0.5f, 0.7f, true); - } - this.wheelRotation = (40 + this.wheelRotation) % 360; + float wheelRotPct = wheelRotation / 360f; - float wheelRotPct = wheelRotation / 360f; + // Sync the animation. Slant belts are slower. + if (this.getSlant() == SlantType.NONE || this.getSlant() == SlantType.TOP) + { + this.animFrame = (int) (wheelRotPct * MAX_FRAME); + if (this.animFrame < 0) + this.animFrame = 0; + if (this.animFrame > MAX_FRAME) + this.animFrame = MAX_FRAME; + } + else + { + this.animFrame = (int) (wheelRotPct * MAX_SLANT_FRAME); + if (this.animFrame < 0) + this.animFrame = 0; + if (this.animFrame > MAX_SLANT_FRAME) + this.animFrame = MAX_SLANT_FRAME; + } + } - // Sync the animation. Slant belts are slower. - if (this.getSlant() == SlantType.NONE || this.getSlant() == SlantType.TOP) - { - this.animFrame = (int) (wheelRotPct * MAX_FRAME); - if (this.animFrame < 0) - this.animFrame = 0; - if (this.animFrame > MAX_FRAME) - this.animFrame = MAX_FRAME; - } - else - { - this.animFrame = (int) (wheelRotPct * MAX_SLANT_FRAME); - if (this.animFrame < 0) - this.animFrame = 0; - if (this.animFrame > MAX_SLANT_FRAME) - this.animFrame = MAX_SLANT_FRAME; - } - } + } - } + @Override + public Packet getDescriptionPacket() + { + if (this.slantType != SlantType.NONE) + { + return ResonantInduction.PACKET_TILE.getPacket(this, slantPacketID, true, this.slantType.ordinal()); + } + return super.getDescriptionPacket(); + } - @Override - public boolean canFunction() - { - return super.canFunction() && !this.worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord); - } + @Override + public boolean onReceivePacket(int id, ByteArrayDataInput data, EntityPlayer player, Object... extra) + { + if (this.worldObj.isRemote) + { + try + { + if (id == 0) + { + this.functioning = data.readBoolean(); + this.slantType = SlantType.values()[data.readInt()]; + return true; + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + return false; + } - @Override - public Packet getDescriptionPacket() - { - if (this.slantType != SlantType.NONE) - { - return ResonantInduction.PACKET_TILE.getPacket(this, slantPacketID, this.isFunctioning(), this.slantType.ordinal()); - } - return super.getDescriptionPacket(); - } + public SlantType getSlant() + { + return slantType; + } - @Override - public boolean simplePacket(String id, ByteArrayDataInput dis, Player player) - { - if (!super.simplePacket(id, dis, player) && this.worldObj.isRemote) - { - try - { - if (id.equalsIgnoreCase(slantPacketID)) - { - this.functioning = dis.readBoolean(); - this.slantType = SlantType.values()[dis.readInt()]; - return true; - } - } - catch (Exception e) - { - e.printStackTrace(); - } - } - return false; - } + public void setSlant(SlantType slantType) + { + if (slantType == null) + { + slantType = SlantType.NONE; + } + this.slantType = slantType; + this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); + } - public SlantType getSlant() - { - return slantType; - } + @Override + public void setDirection(ForgeDirection facingDirection) + { + this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, facingDirection.ordinal(), 3); + } - public void setSlant(SlantType slantType) - { - if (slantType == null) - { - slantType = SlantType.NONE; - } - this.slantType = slantType; - this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord); - } + @Override + public ForgeDirection getDirection() + { + return ForgeDirection.getOrientation(this.getBlockMetadata()); + } - @Override - public void setDirection(ForgeDirection facingDirection) - { - this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, facingDirection.ordinal(), 3); - } + @Override + public List getAffectedEntities() + { + return worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 1, this.zCoord + 1)); + } - @Override - public ForgeDirection getDirection() - { - return ForgeDirection.getOrientation(this.getBlockMetadata()); - } + public int getAnimationFrame() + { + return this.animFrame; + } - @Override - public List getAffectedEntities() - { - return worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 1, this.zCoord + 1)); - } + /** NBT Data */ + @Override + public void readFromNBT(NBTTagCompound nbt) + { + super.readFromNBT(nbt); + this.slantType = SlantType.values()[nbt.getByte("slant")]; + } - public int getAnimationFrame() - { - return this.animFrame; - } + /** Writes a tile entity to NBT. */ + @Override + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + nbt.setByte("slant", (byte) this.slantType.ordinal()); + } - /** NBT Data */ - @Override - public void readFromNBT(NBTTagCompound nbt) - { - super.readFromNBT(nbt); - this.slantType = SlantType.values()[nbt.getByte("slant")]; - } + @Override + public void ignoreEntity(Entity entity) + { + if (!this.IgnoreList.contains(entity)) + { + this.IgnoreList.add(entity); + } + } - /** Writes a tile entity to NBT. */ - @Override - public void writeToNBT(NBTTagCompound nbt) - { - super.writeToNBT(nbt); - nbt.setByte("slant", (byte) this.slantType.ordinal()); - } + @Override + public boolean canConnect(ForgeDirection direction) + { + return direction == ForgeDirection.DOWN; + } - @Override - public void ignoreEntity(Entity entity) - { - if (!this.IgnoreList.contains(entity)) - { - this.IgnoreList.add(entity); - } - } + public void refresh() + { + if (this.worldObj != null && !this.worldObj.isRemote) + { + Vector3 face = new Vector3(this).modifyPositionFromSide(this.getDirection()); + Vector3 back = new Vector3(this).modifyPositionFromSide(this.getDirection().getOpposite()); + TileEntity front, rear; + if (this.slantType == SlantType.DOWN) + { + face.translate(new Vector3(0, -1, 0)); + back.translate(new Vector3(0, 1, 0)); + } + else if (this.slantType == SlantType.UP) + { + face.translate(new Vector3(0, 1, 0)); + back.translate(new Vector3(0, -1, 0)); + } + else + { + return; + } + front = face.getTileEntity(this.worldObj); + rear = back.getTileEntity(this.worldObj); + if (front instanceof IBelt) + { + this.getNetwork().merge(((IBelt) front).getNetwork()); + } + if (rear instanceof IBelt) + { + this.getNetwork().merge(((IBelt) rear).getNetwork()); + } - @Override - public boolean canConnect(ForgeDirection direction) - { - return direction == ForgeDirection.DOWN; - } + } + } - @Override - public void refresh() - { - super.refresh(); - if (this.worldObj != null && !this.worldObj.isRemote) - { - Vector3 face = new Vector3(this).modifyPositionFromSide(this.getDirection()); - Vector3 back = new Vector3(this).modifyPositionFromSide(this.getDirection().getOpposite()); - TileEntity front, rear; - if (this.slantType == SlantType.DOWN) - { - face.translate(new Vector3(0, -1, 0)); - back.translate(new Vector3(0, 1, 0)); - } - else if (this.slantType == SlantType.UP) - { - face.translate(new Vector3(0, 1, 0)); - back.translate(new Vector3(0, -1, 0)); - } - else - { - return; - } - front = face.getTileEntity(this.worldObj); - rear = back.getTileEntity(this.worldObj); - if (front instanceof TileAssembly) - { - this.getTileNetwork().mergeNetwork(((TileAssembly) front).getTileNetwork(), this); - this.connectedTiles.add(front); - } - if (rear instanceof TileAssembly) - { - this.getTileNetwork().mergeNetwork(((TileAssembly) rear).getTileNetwork(), this); - this.connectedTiles.add(rear); - } + @Override + public Object[] getConnections() + { + // TODO Auto-generated method stub + return null; + } - } - } + @Override + public IBeltNetwork getNetwork() + { + // TODO Auto-generated method stub + return null; + } - @Override - public int getWattLoad() - { - return 5; - } + @Override + public void setNetwork(IBeltNetwork network) + { + // TODO Auto-generated method stub + } } diff --git a/src/main/java/resonantinduction/mechanical/gear/PartGear.java b/src/main/java/resonantinduction/mechanical/gear/PartGear.java index 0f362b59a..e8c49eb78 100644 --- a/src/main/java/resonantinduction/mechanical/gear/PartGear.java +++ b/src/main/java/resonantinduction/mechanical/gear/PartGear.java @@ -217,18 +217,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart return "resonant_induction_gear"; } - @Override - public long getTorque() - { - return this.torque; - } - - @Override - public void setTorque(long torque) - { - this.torque = torque; - } - @Override public Object[] getConnections() { @@ -258,4 +246,11 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart return new universalelectricity.api.vector.Vector3(this.x() + direction.offsetX, this.y() + direction.offsetY, this.z() + direction.offsetZ).getTileEntity(this.world()) instanceof IMechConnector; } + @Override + public int getResistance() + { + // TODO Auto-generated method stub + return 0; + } + } \ No newline at end of file diff --git a/src/main/java/resonantinduction/mechanical/network/IMechConnector.java b/src/main/java/resonantinduction/mechanical/network/IMechConnector.java index 45f747413..300d39cc5 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechConnector.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechConnector.java @@ -2,15 +2,10 @@ package resonantinduction.mechanical.network; import universalelectricity.api.net.IConnector; -/** - * For the mechanical network. +/** For the mechanical network. * - * @author Calclavia - * - */ + * @author Calclavia */ public interface IMechConnector extends IConnector { - public long getTorque(); - - public void setTorque(long torque); + public int getResistance(); } diff --git a/src/main/java/resonantinduction/mechanical/network/IMechMachine.java b/src/main/java/resonantinduction/mechanical/network/IMechMachine.java index da448ec03..f144869f9 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechMachine.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechMachine.java @@ -8,7 +8,9 @@ import universalelectricity.api.net.IConnectable; * @author Darkguardsman */ public interface IMechMachine extends IConnectable { - /** Called by the network when its torque value changes. Force is not given since the network - * operates on a can move or can't move setup. Speed is given as a represent */ + /** Called by the network when its torque value changes. */ public void onTorqueChange(ForgeDirection side, int speed); + + /** Gets the force on the side, zero is ignored, neg is input force, pos is output force */ + public int getForceSide(ForgeDirection side); } diff --git a/src/main/java/resonantinduction/mechanical/network/IMechNetwork.java b/src/main/java/resonantinduction/mechanical/network/IMechNetwork.java index 840512abc..2202cc2a9 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechNetwork.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechNetwork.java @@ -7,9 +7,12 @@ import universalelectricity.api.net.INetwork; * @author DarkGuardsman */ public interface IMechNetwork extends INetwork { - public int getForce(); - - public int getRotSpeed(); - + /** Power applied by the network at the given speed */ public int getTorque(); + + /** Rotation of the the network in a single update */ + public int getRotationPerTick(); + + /** Called to rebuild the network */ + public void reconstruct(); } diff --git a/src/main/java/resonantinduction/mechanical/network/MechNetwork.java b/src/main/java/resonantinduction/mechanical/network/MechNetwork.java index 238ddea2f..bb8827fd6 100644 --- a/src/main/java/resonantinduction/mechanical/network/MechNetwork.java +++ b/src/main/java/resonantinduction/mechanical/network/MechNetwork.java @@ -1,76 +1,238 @@ package resonantinduction.mechanical.network; -import universalelectricity.core.net.Network; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; -/** - * @author Calclavia +import net.minecraftforge.common.ForgeDirection; +import universalelectricity.api.CompatibilityModule; +import universalelectricity.api.energy.IConductor; +import universalelectricity.api.net.IConnector; +import universalelectricity.core.net.ConnectionPathfinder; +import universalelectricity.core.net.Network; +import universalelectricity.core.net.NetworkTickHandler; + +/** Simple network to translate speed and force using mechanical rotation * - */ + * @author DarkGuardsman */ public class MechNetwork extends Network implements IMechNetwork { - private long energyBuffer; + private int force = 0; + private int speed = 0; + private int resistance = 0; - @Override - public void update() - { - - } + private HashMap forceMap = new HashMap(); - @Override - public boolean canUpdate() - { - return true; - } + @Override + public void update() + { - @Override - public boolean continueUpdate() - { - return true; - } - - public void addTorque(long energy) - { - this.energyBuffer += energy; - } + } - @Override - public void split(IMechConnector connection) - { - - } + @Override + public void reconstruct() + { + if (this.getConnectors().size() > 0) + { + // Reset all values related to wires + this.getNodes().clear(); + this.forceMap.clear(); + this.resistance = 0; + this.force = 0; + this.speed = 0; - @Override - public void split(IMechConnector connectorA, IMechConnector connectorB) - { - - } + // Iterate threw list of wires + Iterator it = this.getConnectors().iterator(); + + while (it.hasNext()) + { + IMechConnector conductor = it.next(); + + if (conductor != null) + { + this.reconstructConductor(conductor); + } + else + { + it.remove(); + } + } + + if (this.getNodes().size() > 0) + { + NetworkTickHandler.addNetwork(this); + } + } + } + + /** Segmented out call so overriding can be done when conductors are reconstructed. */ + protected void reconstructConductor(IMechConnector conductor) + { + conductor.setNetwork(this); + + for (int i = 0; i < conductor.getConnections().length; i++) + { + reconstructHandler(conductor.getConnections()[i], ForgeDirection.getOrientation(i).getOpposite()); + } + + this.resistance += conductor.getResistance(); + } + + /** Segmented out call so overriding can be done when machines are reconstructed. */ + protected void reconstructHandler(Object obj, ForgeDirection side) + { + if (obj != null && !(obj instanceof IMechConnector)) + { + if (obj instanceof IMechMachine) + { + ForceWrapper[] set = this.forceMap.get((IMechMachine)obj); + if (set == null) + { + set = new ForceWrapper[6]; + } + this.getNodes().add((IMechMachine) obj); + set[side.ordinal()] = new ForceWrapper(((IMechMachine) obj).getForceSide(side.getOpposite()),((IMechMachine) obj).getForceSide(side.getOpposite())); + this.forceMap.put((IMechMachine) obj, set); + } + } + } + + @Override + public boolean canUpdate() + { + return true; + } + + @Override + public boolean continueUpdate() + { + return true; + } @Override public IMechNetwork merge(IMechNetwork network) { - // TODO Auto-generated method stub + if (network.getClass().isAssignableFrom(this.getClass()) && network != this) + { + MechNetwork newNetwork = new MechNetwork(); + newNetwork.getConnectors().addAll(this.getConnectors()); + newNetwork.getConnectors().addAll(network.getConnectors()); + + network.getConnectors().clear(); + network.getNodes().clear(); + this.getConnectors().clear(); + this.getNodes().clear(); + + newNetwork.reconstruct(); + return newNetwork; + } + return null; } @Override - public int getForce() + public void split(IMechConnector splitPoint) { - // TODO Auto-generated method stub - return 0; + this.removeConnector(splitPoint); + this.reconstruct(); + + /** Loop through the connected blocks and attempt to see if there are connections between the + * two points elsewhere. */ + Object[] connectedBlocks = splitPoint.getConnections(); + + for (int i = 0; i < connectedBlocks.length; i++) + { + Object connectedBlockA = connectedBlocks[i]; + + if (connectedBlockA instanceof IMechConnector) + { + for (int ii = 0; ii < connectedBlocks.length; ii++) + { + final Object connectedBlockB = connectedBlocks[ii]; + + if (connectedBlockA != connectedBlockB && connectedBlockB instanceof IMechConnector) + { + ConnectionPathfinder finder = new ConnectionPathfinder((IConnector) connectedBlockB, splitPoint); + finder.findNodes((IConnector) connectedBlockA); + + if (finder.results.size() <= 0) + { + try + { + /** The connections A and B are not connected anymore. Give them both + * a new common network. */ + IMechNetwork newNetwork = new MechNetwork(); + for (IConnector node : finder.closedSet) + { + if (node != splitPoint && node instanceof IMechConnector) + { + newNetwork.addConnector((IMechConnector) node); + } + } + newNetwork.reconstruct(); + } + catch (Exception e) + { + e.printStackTrace(); + } + + } + } + } + } + } } @Override - public int getRotSpeed() + public void split(IMechConnector connectorA, IMechConnector connectorB) { - // TODO Auto-generated method stub - return 0; + this.reconstruct(); + + /** Check if connectorA connects with connectorB. */ + ConnectionPathfinder finder = new ConnectionPathfinder(connectorB); + finder.findNodes(connectorA); + + if (finder.results.size() <= 0) + { + /** The connections A and B are not connected anymore. Give them both a new common + * network. */ + IMechNetwork newNetwork = new MechNetwork(); + + for (IConnector node : finder.closedSet) + { + if (node instanceof IMechConnector) + { + newNetwork.addConnector((IMechConnector) node); + } + } + + newNetwork.reconstruct(); + } } @Override public int getTorque() { - // TODO Auto-generated method stub - return 0; + return this.force; + } + + @Override + public int getRotationPerTick() + { + return this.speed; + } + + public static class ForceWrapper + { + public int force = 0; + public int speed = 0; + + public ForceWrapper(int force, int speed) + { + this.force = force; + this.speed = speed; + } } }