diff --git a/src/main/java/resonantinduction/archaic/firebox/BlockFirebox.java b/src/main/java/resonantinduction/archaic/firebox/BlockFirebox.java index cd977cb1..e48fe7a6 100644 --- a/src/main/java/resonantinduction/archaic/firebox/BlockFirebox.java +++ b/src/main/java/resonantinduction/archaic/firebox/BlockFirebox.java @@ -127,9 +127,16 @@ public class BlockFirebox extends BlockRI } @Override - public boolean isControlDown(EntityPlayer player) + public float getBlockBrightness(IBlockAccess access, int x, int y, int z) { - return ControlKeyModifer.isControlDown(player); + TileEntity tileEntity = access.getBlockTileEntity(x, y, z); + + if (((TileFirebox) tileEntity).isBurning()) + { + return 1; + } + + return 0; } @Override diff --git a/src/main/java/resonantinduction/archaic/firebox/BlockHotPlate.java b/src/main/java/resonantinduction/archaic/firebox/BlockHotPlate.java index 5441b44a..a6fbfaa7 100644 --- a/src/main/java/resonantinduction/archaic/firebox/BlockHotPlate.java +++ b/src/main/java/resonantinduction/archaic/firebox/BlockHotPlate.java @@ -32,6 +32,18 @@ public class BlockHotPlate extends BlockRI this.setTickRandomly(true); } + @Override + public boolean renderAsNormalBlock() + { + return false; + } + + @Override + public boolean isOpaqueCube() + { + return false; + } + @Override @SideOnly(Side.CLIENT) public void registerIcons(IconRegister iconReg) diff --git a/src/main/java/resonantinduction/core/Settings.java b/src/main/java/resonantinduction/core/Settings.java index 16fd994c..ee24b79b 100644 --- a/src/main/java/resonantinduction/core/Settings.java +++ b/src/main/java/resonantinduction/core/Settings.java @@ -15,7 +15,7 @@ import cpw.mods.fml.common.ModMetadata; public class Settings { /** IDs suggested by Jyzarc and Horfius */ - public static final IDManager idManager = new IDManager(3200, 20150); + public static final IDManager idManager = new IDManager(1200, 20150); public static int getNextBlockID() { diff --git a/src/main/java/resonantinduction/core/prefab/block/BlockRIRotatable.java b/src/main/java/resonantinduction/core/prefab/block/BlockRIRotatable.java index 1fbbbca4..5731eff2 100644 --- a/src/main/java/resonantinduction/core/prefab/block/BlockRIRotatable.java +++ b/src/main/java/resonantinduction/core/prefab/block/BlockRIRotatable.java @@ -15,7 +15,7 @@ public class BlockRIRotatable extends BlockRotatable { public BlockRIRotatable(String name) { - this(name, Settings.getNextItemID()); + this(name, Settings.getNextBlockID()); } public BlockRIRotatable(String name, int id) diff --git a/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java b/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java index a3106bb4..b408c46b 100644 --- a/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java +++ b/src/main/java/resonantinduction/electrical/generator/BlockGenerator.java @@ -22,8 +22,12 @@ public class BlockGenerator extends BlockRIRotatable if (tileEntity instanceof TileGenerator) { - ((TileGenerator) tileEntity).isInversed = !((TileGenerator) tileEntity).isInversed; - entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "electrical" : "mechanical") + " energy."); + if (!world.isRemote) + { + ((TileGenerator) tileEntity).isInversed = !((TileGenerator) tileEntity).isInversed; + entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "electrical" : "mechanical") + " energy."); + } + return true; } return false; diff --git a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java index 0979d4ec..17602f66 100644 --- a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java +++ b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java @@ -2,6 +2,8 @@ package resonantinduction.electrical.generator; import net.minecraftforge.common.ForgeDirection; import resonantinduction.mechanical.network.IMechanical; +import resonantinduction.mechanical.network.IMechanicalNetwork; +import resonantinduction.mechanical.network.MechanicalNetwork; import universalelectricity.api.energy.EnergyStorageHandler; import calclavia.lib.prefab.tile.TileElectrical; @@ -46,26 +48,8 @@ public class TileGenerator extends TileElectrical implements IMechanical } @Override - public void setPower(long torque, float speed) + public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity) { - this.power = (long) Math.abs(torque * speed); + energy.receiveEnergy((long) (torque * angularVelocity), true); } - - @Override - public long getPower() - { - return this.power; - } - - @Override - public void onTorqueChange(ForgeDirection side, int speed) - { - } - - @Override - public int getForceSide(ForgeDirection side) - { - return 0; - } - } diff --git a/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java b/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java index 7f42f08d..25ea88ba 100644 --- a/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java +++ b/src/main/java/resonantinduction/mechanical/belt/BeltNetwork.java @@ -3,7 +3,6 @@ package resonantinduction.mechanical.belt; import net.minecraft.tileentity.TileEntity; import resonantinduction.api.IBelt; import resonantinduction.api.IBeltNetwork; -import resonantinduction.mechanical.network.IMechanicalConnector; import resonantinduction.mechanical.network.IMechanicalNetwork; import resonantinduction.mechanical.network.MechanicalNetwork; import universalelectricity.api.net.IConnector; @@ -126,10 +125,11 @@ public class BeltNetwork extends Network implem for (IConnector node : finder.closedSet) { - if (node instanceof IMechanicalConnector) + //TODO: Fix this + /* if (node instanceof IMechanicalConnector) { newNetwork.addConnector((IMechanicalConnector) node); - } + }*/ } newNetwork.reconstruct(); diff --git a/src/main/java/resonantinduction/mechanical/gear/PartGear.java b/src/main/java/resonantinduction/mechanical/gear/PartGear.java index e401e68f..7ad1f13d 100644 --- a/src/main/java/resonantinduction/mechanical/gear/PartGear.java +++ b/src/main/java/resonantinduction/mechanical/gear/PartGear.java @@ -40,8 +40,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart { public static Cuboid6[][] oBoxes = new Cuboid6[6][2]; - private IMechanicalNetwork network; - static { oBoxes[0][0] = new Cuboid6(1 / 8D, 0, 0, 7 / 8D, 1 / 8D, 1); @@ -53,44 +51,64 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart oBoxes[s][1] = oBoxes[0][1].copy().apply(t); } } + + private IMechanicalNetwork network; + /** Side of the block this is placed on */ public ForgeDirection placementSide; - /** Positive torque means it is spinning clockwise */ - private long torque = 0; - - private long force = 0; - /** The size of the gear */ private float radius = 0.5f; - /** The angular velocity, radians per second. */ - private float angularVelocity = 0; + public boolean isClockwise = true; /** The current angle the gear is on. In radians per second. */ public float angle = 0; + /** When true, it will start marking nearby gears for update */ + public boolean markRotationUpdate = true; + + /** The mechanical connections this gear has made */ + protected Object[] connections = new Object[6]; + + private int manualCrankTime = 0; + public void preparePlacement(int side, int itemDamage) { this.placementSide = ForgeDirection.getOrientation((byte) (side ^ 1)); } - public long getTorque() - { - return (long) torque;// (force * radius); - } - @Override public void update() { - - if (angularVelocity < 0 || torque == 0) + if (manualCrankTime > 0) { - angularVelocity = 0; - torque = 0; + onReceiveEnergy(null, 20, 0.3f); + manualCrankTime--; } - // TODO: Should we average the torque? + if (markRotationUpdate) + { + refresh(); + } + + } + + @Override + public void networkUpdate() + { + /** + * Update angle rotation. + */ + if (isClockwise) + angle += this.getNetwork().getAngularVelocity() / 20; + else + angle -= this.getNetwork().getAngularVelocity() / 20; + // this.sendRotationUpdate(); + } + + public void refresh() + { /** Look for gears that are back-to-back with this gear. Equate torque. */ universalelectricity.api.vector.Vector3 vec = new universalelectricity.api.vector.Vector3(tile()).modifyPositionFromSide(placementSide); @@ -98,17 +116,19 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart if (tile instanceof TileMultipart) { - TMultiPart part = ((TileMultipart) tile).partMap(this.placementSide.getOpposite().ordinal()); + TMultiPart neighbor = ((TileMultipart) tile).partMap(this.placementSide.getOpposite().ordinal()); - if (part instanceof PartGear) + if (neighbor instanceof PartGear) { - equatePower((PartGear) part, false); + connections[this.placementSide.getOpposite().ordinal()] = neighbor; + getNetwork().merge(((PartGear) neighbor).getNetwork()); + equateRotation((PartGear) neighbor, false); } } else if (tile instanceof IMechanical) { - torque = (long) (((IMechanical) tile).getPower() / angularVelocity); - ((IMechanical) tile).setPower(torque, angularVelocity); + connections[this.placementSide.getOpposite().ordinal()] = tile; + getNetwork().reconstruct(); } /** Look for gears outside this block space, the relative UP, DOWN, LEFT, RIGHT */ @@ -125,67 +145,80 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart if (neighbor != this && neighbor instanceof PartGear) { - equatePower((PartGear) neighbor, false); + connections[checkDir.ordinal()] = neighbor; + getNetwork().merge(((PartGear) neighbor).getNetwork()); + equateRotation((PartGear) neighbor, false); } } } /** Look for gears that are internal and adjacent to this gear. (The 2 sides) */ - for (int i = 0; i < 6; i++) + for (int i = 0; i < 4; i++) { - // TODO: Make it work with UP-DOWN - if (i < 4) - { - TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(ForgeDirection.getOrientation(i)).ordinal()); + ForgeDirection checkDir = ForgeDirection.getOrientation(i); + TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(checkDir).ordinal()); - if (neighbor != this && neighbor instanceof PartGear) - { - equatePower((PartGear) neighbor, false); - } + if (neighbor != this && neighbor instanceof PartGear) + { + connections[checkDir.ordinal()] = neighbor; + getNetwork().merge(((PartGear) neighbor).getNetwork()); + equateRotation((PartGear) neighbor, false); } } - - /** - * Update angle rotation. - */ - if (angularVelocity > 0 && torque != 0) - { - angle += angularVelocity / 20; - } + + markRotationUpdate = false; } - public void equatePower(PartGear neighbor, boolean isPositive) + @Override + public Object[] getConnections() { - if (isPositive) - { - torque = (torque + ((PartGear) neighbor).torque) / 2; - ((PartGear) neighbor).torque = torque; + return connections; + } - } - else + public void equateRotation(PartGear neighbor, boolean isPositive) + { + if (!neighbor.markRotationUpdate) { - torque = (torque - ((PartGear) neighbor).torque) / 2; - ((PartGear) neighbor).torque = -torque; - } + if (isPositive) + { + ((PartGear) neighbor).isClockwise = isClockwise; + } + else + { + ((PartGear) neighbor).isClockwise = !isClockwise; + } - angularVelocity = (angularVelocity + ((PartGear) neighbor).angularVelocity) / 2; - ((PartGear) neighbor).angularVelocity = angularVelocity; + neighbor.markRotationUpdate = true; + } } @Override public boolean activate(EntityPlayer player, MovingObjectPosition hit, ItemStack item) { - System.out.println("Torque" + torque + " Angular Velocity" + angularVelocity); - + System.out.println(this.getNetwork().getAngularVelocity()); if (player.isSneaking()) { - this.torque += 10; - this.angularVelocity += 0.2f; + this.manualCrankTime = 20; } return false; } + public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity) + { + getNetwork().applyEnergy(torque, angularVelocity); + markRotationUpdate = true; + } + + @Override + public void preRemove() + { + if (!world().isRemote) + { + this.getNetwork().split(this); + } + } + /** Packet Code. */ @Override public void readDesc(MCDataInput packet) @@ -199,6 +232,25 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart packet.writeByte(this.placementSide.ordinal()); } + @Override + public void read(MCDataInput packet) + { + read(packet, packet.readUByte()); + } + + public void read(MCDataInput packet, int packetID) + { + if (packetID == 0) + { + ((MechanicalNetwork) this.getNetwork()).angularVelocity = packet.readFloat(); + } + } + + public void sendRotationUpdate() + { + tile().getWriteStream(this).writeByte(0).writeFloat(this.getNetwork().getAngularVelocity()); + } + @Override public int getSlotMask() { @@ -278,12 +330,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart return "resonant_induction_gear"; } - @Override - public Object[] getConnections() - { - return null; - } - @Override public IMechanicalNetwork getNetwork() { @@ -307,11 +353,4 @@ 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 IMechanicalConnector; } - @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/gear/RenderGear.java b/src/main/java/resonantinduction/mechanical/gear/RenderGear.java index fd262567..4114c427 100644 --- a/src/main/java/resonantinduction/mechanical/gear/RenderGear.java +++ b/src/main/java/resonantinduction/mechanical/gear/RenderGear.java @@ -63,7 +63,7 @@ public class RenderGear break; } - GL11.glRotatef((float) Math.toDegrees(part.angle) * (part.getTorque() > 0 ? 1 : -1), 0, 1, 0); + GL11.glRotatef((float) Math.toDegrees(part.angle), 0, 1, 0); FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE); MODEL.renderAll(); diff --git a/src/main/java/resonantinduction/mechanical/network/IMechanical.java b/src/main/java/resonantinduction/mechanical/network/IMechanical.java index 750ceb1f..c6d3539b 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechanical.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechanical.java @@ -3,20 +3,16 @@ package resonantinduction.mechanical.network; import net.minecraftforge.common.ForgeDirection; import universalelectricity.api.net.IConnectable; -/** - * Applied to machines that connect to a mech network - * - * @author Darkguardsman - */ public interface IMechanical extends IConnectable { - public void setPower(long torque, float speed); - - public long getPower(); - - /** 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); + /** + * Adds energy to a block. Returns the quantity of energy that was accepted. This should always + * return 0 if the block cannot be externally charged. + * + * @param from Orientation the energy is sent in from. + * @param receive Maximum amount of energy (joules) to be sent into the block. + * @param doReceive If false, the charge will only be simulated. + * @return Amount of energy that was accepted by the block. + */ + public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity); } diff --git a/src/main/java/resonantinduction/mechanical/network/IMechanicalConnector.java b/src/main/java/resonantinduction/mechanical/network/IMechanicalConnector.java index 30ffa637..bccbae07 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechanicalConnector.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechanicalConnector.java @@ -1,11 +1,17 @@ package resonantinduction.mechanical.network; +import universalelectricity.api.net.IConnectable; import universalelectricity.api.net.IConnector; -/** For the mechanical network. +/** + * Applied to connectors in a mechanical network * - * @author Calclavia */ -public interface IMechanicalConnector extends IConnector + * @author Calclavia + */ +public interface IMechanicalConnector extends IMechanical, IConnector { - public int getResistance(); + /** + * An update called by the network. + */ + public void networkUpdate(); } diff --git a/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java b/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java index 89bba276..07514e02 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java @@ -21,10 +21,13 @@ public interface IMechanicalNetwork extends INetwork implements IMechanicalNetwork { - private int torque = 0; - private int speed = 0; - private int resistance = 0; + public int torque = 0; + public float angularVelocity = 0; - private HashMap torqueMap = new HashMap(); + /** The direction in which a conductor is placed relative to a specific conductor. */ + protected final HashMap> handlerDirectionMap = new LinkedHashMap>(); @Override public void update() { + for (IMechanicalConnector connector : this.getConnectors()) + { + connector.networkUpdate(); + } + for (IMechanical node : this.getNodes()) + { + for (ForgeDirection dir : handlerDirectionMap.get(node)) + { + node.onReceiveEnergy(dir, torque, angularVelocity); + } + } + + torque = 0; + angularVelocity = 0; + } + + /** + * Applies energy to the mechanical network this tick. + */ + @Override + public void applyEnergy(long torque, float angularVelocity) + { + this.torque += Math.abs(torque); + this.angularVelocity += Math.abs(angularVelocity); + NetworkTickHandler.addNetwork(this); + } + + @Override + public int getTorque() + { + return this.torque; + } + + @Override + public float getAngularVelocity() + { + return this.angularVelocity; + } + + @Override + public long getPower() + { + return (long) (this.getTorque() * this.getAngularVelocity()); + } + + @Override + public boolean canUpdate() + { + return true; + } + + @Override + public boolean continueUpdate() + { + return false; } @Override @@ -46,21 +103,17 @@ public class MechanicalNetwork extends Network it = this.getConnectors().iterator(); while (it.hasNext()) { - IMechanicalConnector conductor = it.next(); + IMechanicalConnector connector = it.next(); - if (conductor != null) + if (connector != null) { - this.reconstructConductor(conductor); + reconstructConnector(connector); } else { @@ -76,16 +129,14 @@ public class MechanicalNetwork extends Network set = this.handlerDirectionMap.get(obj); if (set == null) { - set = new ForceWrapper[6]; + set = EnumSet.noneOf(ForgeDirection.class); } this.getNodes().add((IMechanical) obj); - set[side.ordinal()] = new ForceWrapper(((IMechanical) obj).getForceSide(side.getOpposite()), ((IMechanical) obj).getForceSide(side.getOpposite())); - this.torqueMap.put((IMechanical) obj, set); + set.add(side); + this.handlerDirectionMap.put(obj, set); } } } - @Override - public boolean canUpdate() - { - return true; - } - - @Override - public boolean continueUpdate() - { - return true; - } - @Override public IMechanicalNetwork merge(IMechanicalNetwork network) { @@ -127,7 +166,6 @@ public class MechanicalNetwork extends Network