Generator is now working again

This commit is contained in:
Calclavia 2014-01-24 17:08:50 +08:00
parent 7c3adc4c2b
commit 28e9a9a9ef
9 changed files with 82 additions and 155 deletions

View file

@ -42,7 +42,8 @@ public class TileGenerator extends TileElectrical implements IRotatable
if (!isInversed) if (!isInversed)
{ {
// energy.receiveEnergy(getNetwork().getPower(), true); receiveMechanical(this.getDirection());
receiveMechanical(this.getDirection().getOpposite());
produce(); produce();
} }
else 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) public void produceMechanical(ForgeDirection outputDir)
{ {
Vector3 outputVector = new Vector3(this).translate(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 float maxAngularVelocity = energy.getEnergyCapacity() / (float) torqueRatio;
final long maxTorque = (long) ((double) energy.getEnergyCapacity() / maxAngularVelocity); final long maxTorque = (long) ((double) energy.getEnergyCapacity() / maxAngularVelocity);
float addAngularVelocity = extract / (float) torqueRatio; float setAngularVelocity = extract / (float) torqueRatio;
long addTorque = (long) (((double) extract) / addAngularVelocity); long setTorque = (long) (((double) extract) / setAngularVelocity);
long setTorque = Math.min(mech.getTorque() + addTorque, maxTorque);
float setAngularVelocity = Math.min(mech.getAngularVelocity() + addAngularVelocity, maxAngularVelocity); 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.setTorque(setTorque);
mech.setAngularVelocity(setAngularVelocity); mech.setAngularVelocity(setAngularVelocity);
energy.extractEnergy((long) (setTorque * setAngularVelocity), true); energy.extractEnergy((long) Math.abs(setTorque * setAngularVelocity), true);
} }
} }
} }

View file

@ -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); 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. // Sync the animation. Slant belts are slower.
if (this.getSlant() == SlantType.NONE || this.getSlant() == SlantType.TOP) 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) if (this.animationFrame < 0)
this.animationFrame = 0; this.animationFrame = 0;
if (this.animationFrame > MAX_FRAME) if (this.animationFrame > MAX_FRAME)
@ -92,7 +93,7 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl
} }
else else
{ {
this.animationFrame = (int) (wheelRotPct * MAX_SLANT_FRAME); this.animationFrame = (int) (beltPercentage * MAX_SLANT_FRAME);
if (this.animationFrame < 0) if (this.animationFrame < 0)
this.animationFrame = 0; this.animationFrame = 0;
if (this.animationFrame > MAX_SLANT_FRAME) if (this.animationFrame > MAX_SLANT_FRAME)
@ -104,6 +105,7 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl
if (markRefresh) if (markRefresh)
{ {
sendRefreshPacket(); sendRefreshPacket();
markRefresh = false;
} }
} }
@ -216,7 +218,7 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl
@Override @Override
public boolean canConnect(ForgeDirection direction) public boolean canConnect(ForgeDirection direction)
{ {
return direction == ForgeDirection.DOWN; return direction != getDirection() || direction != getDirection().getOpposite();
} }
public void refresh() public void refresh()
@ -293,6 +295,6 @@ public class TileConveyorBelt extends TileMechanical implements IBelt, IRotatabl
public float getMoveVelocity() public float getMoveVelocity()
{ {
return Math.max(getNetwork().getAngularVelocity(), getNetwork().getPrevAngularVelocity()); return Math.abs(angularVelocity);
} }
} }

View file

@ -9,38 +9,8 @@ import universalelectricity.api.net.INetwork;
*/ */
public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechanical> public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechanical>
{ {
/**
* 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. * @return The current rotation value of the network.
*/ */
public float getRotation(); public float getRotation();
/**
* Disables the network due to rotation issues.
*/
public void setDisabled();
} }

View file

@ -31,15 +31,6 @@ import universalelectricity.core.net.NetworkTickHandler;
*/ */
public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical> implements IMechanicalNetwork, IUpdate public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical> 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 */ /** The current rotation of the network */
private float rotation = 0; private float rotation = 0;
private long lastRotateTime; private long lastRotateTime;
@ -47,10 +38,6 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
/** The direction in which a conductor is placed relative to a specific conductor. */ /** The direction in which a conductor is placed relative to a specific conductor. */
protected final HashMap<Object, EnumSet<ForgeDirection>> handlerDirectionMap = new LinkedHashMap<Object, EnumSet<ForgeDirection>>(); protected final HashMap<Object, EnumSet<ForgeDirection>> handlerDirectionMap = new LinkedHashMap<Object, EnumSet<ForgeDirection>>();
private Set<IMechanical> prevGenerators = new LinkedHashSet<IMechanical>();
private Set<IMechanical> generators = new LinkedHashSet<IMechanical>();
private boolean disabled;
private boolean markUpdateRotation = true; private boolean markUpdateRotation = true;
/** /**
@ -74,7 +61,7 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
* Update all mechanical nodes. * Update all mechanical nodes.
*/ */
Iterator<IMechanical> it = new LinkedHashSet<IMechanical>(getConnectors()).iterator(); Iterator<IMechanical> it = new LinkedHashSet<IMechanical>(getConnectors()).iterator();
while (it.hasNext()) while (it.hasNext())
{ {
IMechanical mechanical = it.next(); IMechanical mechanical = it.next();
@ -115,7 +102,7 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
@Override @Override
public boolean canUpdate() public boolean canUpdate()
{ {
return !disabled && getConnectors().size() > 0; return getConnectors().size() > 0;
} }
@Override @Override
@ -124,68 +111,9 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
return canUpdate(); 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 @Override
public void reconstruct() public void reconstruct()
{ {
disabled = false;
// Reset
prevTorque = torque = 0;
prevAngularVelocity = angularVelocity = 0;
load = 1;
super.reconstruct(); super.reconstruct();
NetworkTickHandler.addNetwork(this); NetworkTickHandler.addNetwork(this);
} }
@ -222,20 +150,13 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
if (deltaTime > 1) if (deltaTime > 1)
{ {
rotation = (float) (((angularVelocity) * (deltaTime / 1000f) + rotation) % (2 * Math.PI)); rotation = (float) (((0) * (deltaTime / 1000f) + rotation) % (2 * Math.PI));
lastRotateTime = System.currentTimeMillis(); lastRotateTime = System.currentTimeMillis();
} }
return rotation; return rotation;
} }
@Override
public void setDisabled()
{
System.out.println("NETWORK DISABLED");
disabled = true;
}
@Override @Override
public IMechanicalNetwork newInstance() public IMechanicalNetwork newInstance()
{ {
@ -247,11 +168,4 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
{ {
return IMechanical.class; return IMechanical.class;
} }
@Override
public String toString()
{
return this.getClass().getSimpleName() + "[" + this.hashCode() + ", Handlers: " + getConnectors().size() + ", Connectors: " + getConnectors().size() + ", Power:" + getPower() + "]";
}
} }

View file

@ -48,10 +48,16 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu
/** The mechanical connections this connector has made */ /** The mechanical connections this connector has made */
protected Object[] connections = new Object[6]; protected Object[] connections = new Object[6];
protected float angularVelocity; protected float prevAngularVelocity, angularVelocity;
protected long torque; protected long torque;
/**
* Packets
*/
int ticks = 0;
boolean markPacketUpdate = false;
/** Side of the block this is placed on */ /** Side of the block this is placed on */
public ForgeDirection placementSide = ForgeDirection.UNKNOWN; public ForgeDirection placementSide = ForgeDirection.UNKNOWN;
@ -66,11 +72,19 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu
@Override @Override
public void update() public void update()
{ {
ticks++;
angle += angularVelocity / 20; angle += angularVelocity / 20;
if (!world().isRemote) if (prevAngularVelocity != angularVelocity)
{
prevAngularVelocity = angularVelocity;
markPacketUpdate = true;
}
if (!world().isRemote && markPacketUpdate && ticks % 5 == 0)
{ {
sendRotationPacket(); sendRotationPacket();
markPacketUpdate = false;
} }
super.update(); super.update();

View file

@ -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. // Move entity based on the direction of the block.
ForgeDirection dir = this.getDirection(world, x, y, z); 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; float speed = tile.getAngularVelocity() / 20;
entity.addVelocity(dir.offsetX * speed, Math.random() * speed, dir.offsetZ * speed); entity.addVelocity(dir.offsetX * speed, Math.random() * speed, dir.offsetZ * speed);
} }

View file

@ -34,7 +34,7 @@ public class RenderGrinderWheel extends TileEntitySpecialRenderer
TileGrinderWheel tile = (TileGrinderWheel) t; TileGrinderWheel tile = (TileGrinderWheel) t;
glPushMatrix(); glPushMatrix();
glTranslatef((float) x + 0.5F, (float) y + 0.5f, (float) z + 0.5F); 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()); RenderUtility.rotateBlockBasedOnDirection(tile.getDirection());
glRotatef((float) Math.toDegrees(tile.angle), 0, 0, 1); glRotatef((float) Math.toDegrees(tile.angle), 0, 0, 1);
FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE); FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE);

View file

@ -33,7 +33,7 @@ public class TileGrinderWheel extends TileMechanical implements IRotatable
public EntityItem grindingItem = null; public EntityItem grindingItem = null;
private final long requiredTorque = 10000; private final long requiredTorque = 1000;
private long counter = 0; private long counter = 0;
@Override @Override
@ -51,7 +51,7 @@ public class TileGrinderWheel extends TileMechanical implements IRotatable
*/ */
public boolean canWork() public boolean canWork()
{ {
return counter > requiredTorque; return counter >= requiredTorque;
} }
public void doWork() public void doWork()
@ -110,7 +110,7 @@ public class TileGrinderWheel extends TileMechanical implements IRotatable
if (didWork) 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); this.worldObj.playSoundEffect(this.xCoord + 0.5, this.yCoord + 0.5, this.zCoord + 0.5, Reference.PREFIX + "grinder", 0.5f, 1);
} }

View file

@ -69,18 +69,6 @@ public class TraitMechanical extends TileMultipart implements IMechanical
return false; return false;
} }
@Override
public boolean isClockwise()
{
return false;
}
@Override
public void setClockwise(boolean isClockwise)
{
}
@Override @Override
public Object[] getConnections() public Object[] getConnections()
{ {
@ -117,20 +105,32 @@ public class TraitMechanical extends TileMultipart implements IMechanical
} }
@Override @Override
public float getResistance() public float getAngularVelocity()
{ {
return 0; return 0;
} }
@Override @Override
public boolean isRotationInversed() public void setAngularVelocity(float velocity)
{ {
return false;
} }
@Override @Override
public int[] getLocation() public long getTorque()
{ {
return null; return 0;
}
@Override
public void setTorque(long torque)
{
}
@Override
public float getRatio()
{
return 0;
} }
} }