Fixed generator not working

This commit is contained in:
Calclavia 2014-03-05 21:35:29 +08:00
parent f4fc1a7910
commit aa10031979
7 changed files with 48 additions and 33 deletions

View file

@ -43,9 +43,23 @@ public class TileGenerator extends TileElectrical implements IRotatable, IMechan
return gearRatio = (byte) ((gearRatio + 1) % 3); return gearRatio = (byte) ((gearRatio + 1) % 3);
} }
@Override
public void initiate()
{
node.reconstruct();
}
@Override
public void invalidate()
{
node.deconstruct();
}
@Override @Override
public void updateEntity() public void updateEntity()
{ {
super.updateEntity();
if (!isInversed) if (!isInversed)
{ {
receiveMechanical(); receiveMechanical();
@ -61,6 +75,7 @@ public class TileGenerator extends TileElectrical implements IRotatable, IMechan
public void receiveMechanical() public void receiveMechanical()
{ {
double power = node.getEnergy(); double power = node.getEnergy();
// System.out.println(power);
long receive = energy.receiveEnergy((long) power, true); long receive = energy.receiveEnergy((long) power, true);
if (receive > 0) if (receive > 0)

View file

@ -206,32 +206,31 @@ public class PartGear extends PartMechanical implements IMultiBlockStructure<Par
if (with instanceof MechanicalNode) if (with instanceof MechanicalNode)
{ {
IMechanicalNodeProvider source = ((MechanicalNode) with).parent; IMechanicalNodeProvider parent = ((MechanicalNode) with).parent;
/** /**
* Check for flat connections (gear face on gear face) to make sure it's * Check for flat connections (gear face on gear face) to make sure it's
* actually on * actually on this gear block.
* this gear block.
*/ */
if (from == placementSide.getOpposite()) if (from == placementSide.getOpposite())
{ {
if (source instanceof PartGear || source instanceof PartGearShaft) if (parent instanceof PartGear || parent instanceof PartGearShaft)
{ {
if (source instanceof PartGearShaft) if (parent instanceof PartGearShaft)
{ {
PartGearShaft shaft = (PartGearShaft) source; PartGearShaft shaft = (PartGearShaft) parent;
return shaft.tile().partMap(from.getOpposite().ordinal()) == PartGear.this && Math.abs(shaft.placementSide.offsetX) == Math.abs(placementSide.offsetX) && Math.abs(shaft.placementSide.offsetY) == Math.abs(placementSide.offsetY) && Math.abs(shaft.placementSide.offsetZ) == Math.abs(placementSide.offsetZ); return shaft.tile().partMap(from.getOpposite().ordinal()) == PartGear.this && Math.abs(shaft.placementSide.offsetX) == Math.abs(placementSide.offsetX) && Math.abs(shaft.placementSide.offsetY) == Math.abs(placementSide.offsetY) && Math.abs(shaft.placementSide.offsetZ) == Math.abs(placementSide.offsetZ);
} }
else if (source instanceof PartGear) else if (parent instanceof PartGear)
{ {
if (((PartGear) source).tile() == tile() && !getMultiBlock().isConstructed()) if (((PartGear) parent).tile() == tile() && !getMultiBlock().isConstructed())
{ {
return true; return true;
} }
if (((PartGear) source).placementSide != placementSide) if (((PartGear) parent).placementSide != placementSide)
{ {
TMultiPart part = tile().partMap(((PartGear) source).placementSide.ordinal()); TMultiPart part = tile().partMap(((PartGear) parent).placementSide.ordinal());
if (part instanceof PartGear) if (part instanceof PartGear)
{ {
@ -253,12 +252,12 @@ public class PartGear extends PartMechanical implements IMultiBlockStructure<Par
/** Small gear attempting to connect to large gear. */ /** Small gear attempting to connect to large gear. */
if (getMultiBlock().isConstructed()) if (getMultiBlock().isConstructed())
{ {
TMultiPart checkPart = ((PartGear) source).tile().partMap(placementSide.ordinal()); TMultiPart checkPart = ((PartGear) parent).tile().partMap(placementSide.ordinal());
if (checkPart instanceof PartGear) if (checkPart instanceof PartGear)
{ {
ForgeDirection requiredDirection = ((PartGear) checkPart).getPosition().subtract(position()).toForgeDirection(); ForgeDirection requiredDirection = ((PartGear) checkPart).getPosition().subtract(position()).toForgeDirection();
return ((PartGear) checkPart).isCenterMultiBlock() && ((PartGear) source).placementSide == requiredDirection; return ((PartGear) checkPart).isCenterMultiBlock() && ((PartGear) parent).placementSide == requiredDirection;
} }
} }
} }
@ -272,7 +271,7 @@ public class PartGear extends PartMechanical implements IMultiBlockStructure<Par
if (sourceTile instanceof IMechanicalNodeProvider) if (sourceTile instanceof IMechanicalNodeProvider)
{ {
MechanicalNode sourceInstance = ((IMechanicalNodeProvider) sourceTile).getNode(from); MechanicalNode sourceInstance = ((IMechanicalNodeProvider) sourceTile).getNode(from);
return sourceInstance == source; return sourceInstance == with;
} }
} }
else if (from == placementSide) else if (from == placementSide)
@ -283,7 +282,7 @@ public class PartGear extends PartMechanical implements IMultiBlockStructure<Par
if (sourceTile instanceof IMechanicalNodeProvider) if (sourceTile instanceof IMechanicalNodeProvider)
{ {
MechanicalNode sourceInstance = ((IMechanicalNodeProvider) sourceTile).getNode(from.getOpposite()); MechanicalNode sourceInstance = ((IMechanicalNodeProvider) sourceTile).getNode(from.getOpposite());
return sourceInstance == source; return sourceInstance == with;
} }
} }
else else

View file

@ -85,7 +85,7 @@ public class MechanicalNode extends EnergyNode
/** /**
* Energy loss * Energy loss
*/ */
double torqueLoss = Math.min(Math.abs(getAngularVelocity()), (Math.abs(getTorque() * getTorqueLoad()) + getTorqueLoad() / 10) * deltaTime); double torqueLoss = Math.min(Math.abs(getTorque()), (Math.abs(getTorque() * getTorqueLoad()) + getTorqueLoad() / 10) * deltaTime);
if (torque > 0) if (torque > 0)
torque -= torqueLoss; torque -= torqueLoss;
@ -231,7 +231,7 @@ public class MechanicalNode extends EnergyNode
@Override @Override
public double getEnergy() public double getEnergy()
{ {
return torque * angularVelocity; return getTorque() * getAngularVelocity();
} }
@Override @Override
@ -243,7 +243,7 @@ public class MechanicalNode extends EnergyNode
@Override @Override
public Grid newGrid() public Grid newGrid()
{ {
return new TickingGrid<MechanicalNode>(this); return new TickingGrid<MechanicalNode>(this, MechanicalNode.class);
} }
@Override @Override

View file

@ -64,12 +64,9 @@ public class TraitMechanical extends TileMultipart implements IMechanicalNodePro
part = partMap(PartMap.CENTER.ordinal()); part = partMap(PartMap.CENTER.ordinal());
} }
if (part != null) if (part instanceof IMechanicalNodeProvider)
{ {
if (part instanceof IMechanicalNodeProvider) return ((IMechanicalNodeProvider) part).getNode(from);
{
return ((IMechanicalNodeProvider) part).getNode(from);
}
} }
return null; return null;

View file

@ -33,6 +33,7 @@ public abstract class Node<G extends Grid, N>
} }
/** /**
* TODO: Try inject tile validate and invalidate events so this does not have to be called.
* This constructs the node. It should be called whenever the connections of the node are * 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. * updated OR when the node is first initiated and can access its connections.
*/ */

View file

@ -34,11 +34,10 @@ public abstract class NodeGrid<N extends Node> extends Grid<N>
if (isValidNode(connection) && connection instanceof Node) if (isValidNode(connection) && connection instanceof Node)
{ {
Node connectedNode = (Node) connection; Node connectedNode = (Node) connection;
if (connectedNode.getGrid() != this) if (connectedNode.getGrid() != this)
{ {
connectedNode.getGrid().getNodes().clear(); connectedNode.getGrid().getNodes().clear();
connectedNode.setGrid(this);
add((N) connectedNode); add((N) connectedNode);
reconstructNode((N) connectedNode); reconstructNode((N) connectedNode);
} }

View file

@ -13,24 +13,28 @@ public class TickingGrid<N extends Node> extends NodeGrid<N> implements IUpdate
NetworkTickHandler.addNetwork(this); NetworkTickHandler.addNetwork(this);
} }
public TickingGrid(N node)
{
this(node, node.getClass());
}
/** /**
* An grid update called only server side. * An grid update called only server side.
* TODO: Make actual ticker an independent thread.
*/ */
@Override @Override
public void update() public void update()
{ {
synchronized (nodes) new Thread()
{ {
for (Node node : nodes) @Override
public void run()
{ {
node.update(1 / 20f); synchronized (nodes)
{
for (Node node : nodes)
{
node.update(1 / 20f);
}
}
} }
} }.start();
} }
@Override @Override