From 28e9a9a9efc4a9c1cd682235796a9a5b2d06c801 Mon Sep 17 00:00:00 2001 From: Calclavia Date: Fri, 24 Jan 2014 17:08:50 +0800 Subject: [PATCH] Generator is now working again --- .../electrical/generator/TileGenerator.java | 39 ++++++-- .../mechanical/belt/TileConveyorBelt.java | 12 ++- .../network/IMechanicalNetwork.java | 30 ------ .../mechanical/network/MechanicalNetwork.java | 92 +------------------ .../mechanical/network/PartMechanical.java | 18 +++- .../mechanical/process/BlockGrinderWheel.java | 4 +- .../process/RenderGrinderWheel.java | 2 +- .../mechanical/process/TileGrinderWheel.java | 6 +- .../mechanical/trait/TraitMechanical.java | 34 +++---- 9 files changed, 82 insertions(+), 155 deletions(-) diff --git a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java index bf287efcf..16bda264b 100644 --- a/src/main/java/resonantinduction/electrical/generator/TileGenerator.java +++ b/src/main/java/resonantinduction/electrical/generator/TileGenerator.java @@ -42,7 +42,8 @@ public class TileGenerator extends TileElectrical implements IRotatable if (!isInversed) { - // energy.receiveEnergy(getNetwork().getPower(), true); + receiveMechanical(this.getDirection()); + receiveMechanical(this.getDirection().getOpposite()); produce(); } else @@ -53,6 +54,24 @@ public class TileGenerator extends TileElectrical implements IRotatable } + public void receiveMechanical(ForgeDirection inputDir) + { + Vector3 inputVector = new Vector3(this).translate(inputDir); + TileEntity tile = inputVector.getTileEntity(worldObj); + + if (tile instanceof IMechanical) + { + IMechanical mech = ((IMechanical) tile).getInstance(inputDir.getOpposite()); + long receive = energy.receiveEnergy((long) Math.abs(mech.getTorque() * mech.getAngularVelocity()), true); + + if (receive > 0) + { + mech.setTorque((long) (mech.getTorque() * 0.5)); + mech.setAngularVelocity(mech.getAngularVelocity() * 0.5f); + } + } + } + public void produceMechanical(ForgeDirection outputDir) { Vector3 outputVector = new Vector3(this).translate(outputDir); @@ -67,13 +86,21 @@ public class TileGenerator extends TileElectrical implements IRotatable { final float maxAngularVelocity = energy.getEnergyCapacity() / (float) torqueRatio; final long maxTorque = (long) ((double) energy.getEnergyCapacity() / maxAngularVelocity); - float addAngularVelocity = extract / (float) torqueRatio; - long addTorque = (long) (((double) extract) / addAngularVelocity); - long setTorque = Math.min(mech.getTorque() + addTorque, maxTorque); - float setAngularVelocity = Math.min(mech.getAngularVelocity() + addAngularVelocity, maxAngularVelocity); + float setAngularVelocity = extract / (float) torqueRatio; + long setTorque = (long) (((double) extract) / setAngularVelocity); + + long currentTorque = Math.abs(mech.getTorque()); + + if (currentTorque != 0) + setTorque = Math.min(+setTorque, maxTorque) * (mech.getTorque() / currentTorque); + + float currentVelo = Math.abs(mech.getAngularVelocity()); + if (currentVelo != 0) + setAngularVelocity = Math.min(+setAngularVelocity, maxAngularVelocity) * (mech.getAngularVelocity() / currentVelo); + mech.setTorque(setTorque); mech.setAngularVelocity(setAngularVelocity); - energy.extractEnergy((long) (setTorque * setAngularVelocity), true); + energy.extractEnergy((long) Math.abs(setTorque * setAngularVelocity), true); } } } diff --git a/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java b/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java index 2686b483e..81d211e5a 100644 --- a/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java +++ b/src/main/java/resonantinduction/mechanical/belt/TileConveyorBelt.java @@ -79,12 +79,13 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl this.worldObj.playSound(this.xCoord, this.yCoord, this.zCoord, "mods.assemblyline.conveyor", 0.5f, 0.7f, true); } - double wheelRotPct = getNetwork().getRotation() / Math.PI; + angle += Math.abs(angularVelocity / 20); + double beltPercentage = (angle % Math.PI) / Math.PI; // Sync the animation. Slant belts are slower. if (this.getSlant() == SlantType.NONE || this.getSlant() == SlantType.TOP) { - this.animationFrame = (int) (wheelRotPct * MAX_FRAME); + this.animationFrame = (int) (beltPercentage * MAX_FRAME); if (this.animationFrame < 0) this.animationFrame = 0; if (this.animationFrame > MAX_FRAME) @@ -92,7 +93,7 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl } else { - this.animationFrame = (int) (wheelRotPct * MAX_SLANT_FRAME); + this.animationFrame = (int) (beltPercentage * MAX_SLANT_FRAME); if (this.animationFrame < 0) this.animationFrame = 0; if (this.animationFrame > MAX_SLANT_FRAME) @@ -104,6 +105,7 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl if (markRefresh) { sendRefreshPacket(); + markRefresh = false; } } @@ -216,7 +218,7 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl @Override public boolean canConnect(ForgeDirection direction) { - return direction == ForgeDirection.DOWN; + return direction != getDirection() || direction != getDirection().getOpposite(); } public void refresh() @@ -293,6 +295,6 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl public float getMoveVelocity() { - return Math.max(getNetwork().getAngularVelocity(), getNetwork().getPrevAngularVelocity()); + return Math.abs(angularVelocity); } } diff --git a/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java b/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java index 3dc1b45d4..d4afdff50 100644 --- a/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java +++ b/src/main/java/resonantinduction/mechanical/network/IMechanicalNetwork.java @@ -9,38 +9,8 @@ import universalelectricity.api.net.INetwork; */ public interface IMechanicalNetwork extends INetwork { - /** - * Gets the power of the network. - * - * @return Power in Watts. - */ - public long getPower(); - - public void setPower(long torque, float angularVelocity); - - /** Torque applied by the network at the given speed */ - public long getTorque(); - - /** - * Gets the angular velocity of the network. - * - * @return In radians per second. - */ - public float getAngularVelocity(); - - public long getPrevTorque(); - - public float getPrevAngularVelocity(); - - public long onReceiveEnergy(IMechanical source, long torque, float angularVelocity); - /** * @return The current rotation value of the network. */ public float getRotation(); - - /** - * Disables the network due to rotation issues. - */ - public void setDisabled(); } diff --git a/src/main/java/resonantinduction/mechanical/network/MechanicalNetwork.java b/src/main/java/resonantinduction/mechanical/network/MechanicalNetwork.java index 7be8ad724..337636519 100644 --- a/src/main/java/resonantinduction/mechanical/network/MechanicalNetwork.java +++ b/src/main/java/resonantinduction/mechanical/network/MechanicalNetwork.java @@ -31,15 +31,6 @@ import universalelectricity.core.net.NetworkTickHandler; */ public class MechanicalNetwork extends Network implements IMechanicalNetwork, IUpdate { - private long prevTorque = 0; - private float prevAngularVelocity = 0; - - private long torque = 0; - private float angularVelocity = 0; - - /** The cached resistance caused by all connectors */ - private float load = 0; - /** The current rotation of the network */ private float rotation = 0; private long lastRotateTime; @@ -47,10 +38,6 @@ public class MechanicalNetwork extends Network /** The direction in which a conductor is placed relative to a specific conductor. */ protected final HashMap> handlerDirectionMap = new LinkedHashMap>(); - private Set prevGenerators = new LinkedHashSet(); - private Set generators = new LinkedHashSet(); - private boolean disabled; - private boolean markUpdateRotation = true; /** @@ -74,7 +61,7 @@ public class MechanicalNetwork extends Network * Update all mechanical nodes. */ Iterator it = new LinkedHashSet(getConnectors()).iterator(); - + while (it.hasNext()) { IMechanical mechanical = it.next(); @@ -115,7 +102,7 @@ public class MechanicalNetwork extends Network @Override public boolean canUpdate() { - return !disabled && getConnectors().size() > 0; + return getConnectors().size() > 0; } @Override @@ -124,68 +111,9 @@ public class MechanicalNetwork extends Network return canUpdate(); } - /** - * Applies energy to the mechanical network this tick. - * Note: Server side only. - */ - @Override - public long onReceiveEnergy(IMechanical source, long torque, float angularVelocity) - { - this.torque += torque; - this.angularVelocity += angularVelocity; - this.generators.add(source); - NetworkTickHandler.addNetwork(this); - return (long) (torque * angularVelocity); - } - - @Override - public long getPrevTorque() - { - return prevTorque; - } - - @Override - public float getPrevAngularVelocity() - { - return prevAngularVelocity; - } - - @Override - public long getTorque() - { - return torque; - } - - @Override - public float getAngularVelocity() - { - return angularVelocity; - } - - @Override - public long getPower() - { - return (long) (getTorque() * getAngularVelocity()); - } - - @Override - public void setPower(long torque, float angularVelocity) - { - prevTorque = this.torque; - prevAngularVelocity = this.angularVelocity; - this.torque = torque; - this.angularVelocity = angularVelocity; - } - @Override public void reconstruct() { - disabled = false; - // Reset - prevTorque = torque = 0; - prevAngularVelocity = angularVelocity = 0; - load = 1; - super.reconstruct(); NetworkTickHandler.addNetwork(this); } @@ -222,20 +150,13 @@ public class MechanicalNetwork extends Network if (deltaTime > 1) { - rotation = (float) (((angularVelocity) * (deltaTime / 1000f) + rotation) % (2 * Math.PI)); + rotation = (float) (((0) * (deltaTime / 1000f) + rotation) % (2 * Math.PI)); lastRotateTime = System.currentTimeMillis(); } return rotation; } - @Override - public void setDisabled() - { - System.out.println("NETWORK DISABLED"); - disabled = true; - } - @Override public IMechanicalNetwork newInstance() { @@ -247,11 +168,4 @@ public class MechanicalNetwork extends Network { return IMechanical.class; } - - @Override - public String toString() - { - return this.getClass().getSimpleName() + "[" + this.hashCode() + ", Handlers: " + getConnectors().size() + ", Connectors: " + getConnectors().size() + ", Power:" + getPower() + "]"; - } - } diff --git a/src/main/java/resonantinduction/mechanical/network/PartMechanical.java b/src/main/java/resonantinduction/mechanical/network/PartMechanical.java index 4570404ea..02ed122f7 100644 --- a/src/main/java/resonantinduction/mechanical/network/PartMechanical.java +++ b/src/main/java/resonantinduction/mechanical/network/PartMechanical.java @@ -48,10 +48,16 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu /** The mechanical connections this connector has made */ protected Object[] connections = new Object[6]; - protected float angularVelocity; + protected float prevAngularVelocity, angularVelocity; protected long torque; + /** + * Packets + */ + int ticks = 0; + boolean markPacketUpdate = false; + /** Side of the block this is placed on */ public ForgeDirection placementSide = ForgeDirection.UNKNOWN; @@ -66,11 +72,19 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu @Override public void update() { + ticks++; angle += angularVelocity / 20; - if (!world().isRemote) + if (prevAngularVelocity != angularVelocity) + { + prevAngularVelocity = angularVelocity; + markPacketUpdate = true; + } + + if (!world().isRemote && markPacketUpdate && ticks % 5 == 0) { sendRotationPacket(); + markPacketUpdate = false; } super.update(); diff --git a/src/main/java/resonantinduction/mechanical/process/BlockGrinderWheel.java b/src/main/java/resonantinduction/mechanical/process/BlockGrinderWheel.java index bfbcd80ab..80d5def26 100644 --- a/src/main/java/resonantinduction/mechanical/process/BlockGrinderWheel.java +++ b/src/main/java/resonantinduction/mechanical/process/BlockGrinderWheel.java @@ -70,11 +70,11 @@ public class BlockGrinderWheel extends BlockRIRotatable implements ITileEntityPr } - if (tile.getNetwork().getAngularVelocity() > 0) + if (tile.getAngularVelocity() != 0) { // Move entity based on the direction of the block. ForgeDirection dir = this.getDirection(world, x, y, z); - dir = ForgeDirection.getOrientation(!(dir.ordinal() % 2 == 0) ? dir.ordinal() - 1 : dir.ordinal()); + dir = ForgeDirection.getOrientation(!(dir.ordinal() % 2 == 0) ? dir.ordinal() - 1 : dir.ordinal()).getOpposite(); float speed = tile.getAngularVelocity() / 20; entity.addVelocity(dir.offsetX * speed, Math.random() * speed, dir.offsetZ * speed); } diff --git a/src/main/java/resonantinduction/mechanical/process/RenderGrinderWheel.java b/src/main/java/resonantinduction/mechanical/process/RenderGrinderWheel.java index 2769e5cd5..bafb62015 100644 --- a/src/main/java/resonantinduction/mechanical/process/RenderGrinderWheel.java +++ b/src/main/java/resonantinduction/mechanical/process/RenderGrinderWheel.java @@ -34,7 +34,7 @@ public class RenderGrinderWheel extends TileEntitySpecialRenderer TileGrinderWheel tile = (TileGrinderWheel) t; glPushMatrix(); glTranslatef((float) x + 0.5F, (float) y + 0.5f, (float) z + 0.5F); - glScalef(0.51f, 0.51f, 0.51f); + glScalef(0.51f, 0.5f, 0.5f); RenderUtility.rotateBlockBasedOnDirection(tile.getDirection()); glRotatef((float) Math.toDegrees(tile.angle), 0, 0, 1); FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE); diff --git a/src/main/java/resonantinduction/mechanical/process/TileGrinderWheel.java b/src/main/java/resonantinduction/mechanical/process/TileGrinderWheel.java index 0782c8d97..aafae13b6 100644 --- a/src/main/java/resonantinduction/mechanical/process/TileGrinderWheel.java +++ b/src/main/java/resonantinduction/mechanical/process/TileGrinderWheel.java @@ -33,7 +33,7 @@ public class TileGrinderWheel extends TileMechanical implements IRotatable public EntityItem grindingItem = null; - private final long requiredTorque = 10000; + private final long requiredTorque = 1000; private long counter = 0; @Override @@ -51,7 +51,7 @@ public class TileGrinderWheel extends TileMechanical implements IRotatable */ public boolean canWork() { - return counter > requiredTorque; + return counter >= requiredTorque; } public void doWork() @@ -110,7 +110,7 @@ public class TileGrinderWheel extends TileMechanical implements IRotatable if (didWork) { - if (this.ticks % 20 == 0) + if (this.ticks % 8 == 0) { this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, Reference.PREFIX + "grinder", 0.5f, 1); } diff --git a/src/main/java/resonantinduction/mechanical/trait/TraitMechanical.java b/src/main/java/resonantinduction/mechanical/trait/TraitMechanical.java index 158901a2a..d432a9ae2 100644 --- a/src/main/java/resonantinduction/mechanical/trait/TraitMechanical.java +++ b/src/main/java/resonantinduction/mechanical/trait/TraitMechanical.java @@ -69,18 +69,6 @@ public class TraitMechanical extends TileMultipart implements IMechanical return false; } - @Override - public boolean isClockwise() - { - return false; - } - - @Override - public void setClockwise(boolean isClockwise) - { - - } - @Override public Object[] getConnections() { @@ -117,20 +105,32 @@ public class TraitMechanical extends TileMultipart implements IMechanical } @Override - public float getResistance() + public float getAngularVelocity() { return 0; } @Override - public boolean isRotationInversed() + public void setAngularVelocity(float velocity) { - return false; + } @Override - public int[] getLocation() + public long getTorque() { - return null; + return 0; + } + + @Override + public void setTorque(long torque) + { + + } + + @Override + public float getRatio() + { + return 0; } }