Rewrote structure of mechanical grid

This commit is contained in:
Calclavia 2014-03-04 20:37:55 +08:00
parent b369e77b9f
commit ff152d1009
17 changed files with 385 additions and 155 deletions

View file

@ -236,7 +236,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock
return true;
}
universalelectricity.api.vector.Vector3 primaryPos = getMultiBlock().getPrimary().getPosition();
universalelectricity.api.vector.Vector3 primaryPos = getMultiBlock().getPrimary().position();
if (primaryPos.intX() == x() && placementSide.offsetX == 0)
{
@ -360,7 +360,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock
{
if (source instanceof IMechanical)
{
universalelectricity.api.vector.Vector3 deltaPos = ((IMechanical) source).getPosition().subtract(getPosition());
universalelectricity.api.vector.Vector3 deltaPos = ((IMechanical) source).position().subtract(position());
boolean caseX = placementSide.offsetX != 0 && deltaPos.y == 0 && deltaPos.z == 0;
boolean caseY = placementSide.offsetY != 0 && deltaPos.x == 0 && deltaPos.z == 0;
@ -445,7 +445,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock
if (checkPart instanceof PartGear)
{
ForgeDirection requiredDirection = ((PartGear) checkPart).getPosition().subtract(getPosition()).toForgeDirection();
ForgeDirection requiredDirection = ((PartGear) checkPart).position().subtract(position()).toForgeDirection();
return ((PartGear) checkPart).isCenterMultiBlock() && ((PartGear) source).placementSide == requiredDirection;
}
}
@ -455,7 +455,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock
}
/** Face to face stick connection. */
TileEntity sourceTile = getPosition().translate(from.getOpposite()).getTileEntity(world());
TileEntity sourceTile = position().translate(from.getOpposite()).getTileEntity(world());
if (sourceTile instanceof IMechanical)
{
@ -466,7 +466,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock
else if (from == placementSide)
{
/** Face to face stick connection. */
TileEntity sourceTile = getPosition().translate(from).getTileEntity(world());
TileEntity sourceTile = position().translate(from).getTileEntity(world());
if (sourceTile instanceof IMechanical)
{
@ -476,7 +476,7 @@ public class PartGear extends PartMechanical implements IMechanical, IMultiBlock
}
else
{
TileEntity destinationTile = ((IMechanical) source).getPosition().translate(from.getOpposite()).getTileEntity(world());
TileEntity destinationTile = ((IMechanical) source).position().translate(from.getOpposite()).getTileEntity(world());
if (destinationTile instanceof IMechanical && destinationTile instanceof TileMultipart)
{

View file

@ -0,0 +1,8 @@
package resonantinduction.mechanical.energy.network;
import resonantinduction.core.grid.INode;
public abstract class EnergyNode implements INode
{
public abstract double getEnergy();
}

View file

@ -0,0 +1,8 @@
package resonantinduction.mechanical.energy.network;
import resonantinduction.core.grid.INodeProvider;
public interface IMechanicalNodeProvider extends INodeProvider<MechanicalNode>
{
}

View file

@ -2,15 +2,12 @@ package resonantinduction.mechanical.energy.network;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.WeakHashMap;
import resonantinduction.core.grid.Grid;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.api.mechanical.IMechanical;
import resonantinduction.api.mechanical.IMechanicalNetwork;
import universalelectricity.api.net.IUpdate;
import universalelectricity.api.vector.Vector3;
import universalelectricity.core.net.Network;
import universalelectricity.core.net.NetworkTickHandler;
/**
@ -29,102 +26,20 @@ import universalelectricity.core.net.NetworkTickHandler;
*
* @author Calclavia
*/
public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical> implements IMechanicalNetwork, IUpdate
public class MechanicalNetwork extends Grid<MechanicalNode>
{
public MechanicalNetwork()
{
super(IMechanical.class);
super(MechanicalNode.class);
}
public static final float ACCELERATION = 0.2f;
/** The current rotation of the network. Used by covneyor belts. */
private float rotation = 0;
private long lastRotateTime;
/**
* The cached connections of the mechanical network.
*/
private final WeakHashMap<IMechanical, WeakReference[]> connectionCache = new WeakHashMap<IMechanical, WeakReference[]>();
/**
* Only add the exact instance of the connector into the network. Multipart tiles allowed!
*/
@Override
public void addConnector(IMechanical connector)
public void add(MechanicalNode node)
{
super.addConnector(connector);
super.add(node);
NetworkTickHandler.addNetwork(this);
}
/**
* An network update called only server side.
*/
@Override
public void update()
{
synchronized (getConnectors())
{
/**
* Update all mechanical nodes.
*/
Iterator<IMechanical> it = getConnectors().iterator();
while (it.hasNext())
{
IMechanical mechanical = it.next();
WeakReference[] connections = connectionCache.get(mechanical);
if (connections != null)
{
for (int i = 0; i < connections.length; i++)
{
if (connections[i] != null)
{
ForgeDirection dir = ForgeDirection.getOrientation(i);
Object adjacent = connections[i].get();
if (adjacent instanceof IMechanical)
{
IMechanical adjacentMech = ((IMechanical) adjacent).getInstance(dir.getOpposite());
if (adjacentMech != null && adjacent != mechanical)
{
float ratio = adjacentMech.getRatio(dir.getOpposite(), mechanical) / mechanical.getRatio(dir, adjacentMech);
long torque = mechanical.getTorque();
boolean inverseRotation = mechanical.inverseRotation(dir, adjacentMech) && adjacentMech.inverseRotation(dir.getOpposite(), mechanical);
int inversion = inverseRotation ? -1 : 1;
if (Math.abs(torque + inversion * (adjacentMech.getTorque() / ratio * ACCELERATION)) < Math.abs(adjacentMech.getTorque() / ratio))
mechanical.setTorque((long) (torque + inversion * ((adjacentMech.getTorque() / ratio * ACCELERATION))));
float velocity = mechanical.getAngularVelocity();
if (Math.abs(velocity + inversion * (adjacentMech.getAngularVelocity() * ratio * ACCELERATION)) < Math.abs(adjacentMech.getAngularVelocity() * ratio))
mechanical.setAngularVelocity(velocity + (inversion * adjacentMech.getAngularVelocity() * ratio * ACCELERATION));
}
}
}
}
}
}
}
}
@Override
public boolean canUpdate()
{
return getConnectors().size() > 0;
}
@Override
public boolean continueUpdate()
{
return canUpdate();
}
@Override
public void reconstruct()
{
@ -226,10 +141,4 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanical>
{
return new MechanicalNetwork();
}
@Override
public Class getConnectorClass()
{
return IMechanical.class;
}
}

View file

@ -0,0 +1,175 @@
package resonantinduction.mechanical.energy.network;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.WeakHashMap;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.vector.Vector3;
import codechicken.multipart.TMultiPart;
public class MechanicalNode extends EnergyNode
{
protected final AbstractMap<MechanicalNode, ForgeDirection> connections = new WeakHashMap<MechanicalNode, ForgeDirection>();
protected final Object parent;
public double torque = 0;
public double angularVelocity = 0;
public float acceleration = 0.1f;
/**
* The current rotation of the mechanical node.
*/
public double angle = 0;
public MechanicalNode(Object parent)
{
this.parent = parent;
}
@Override
public void update()
{
/**
* Loss energy
*/
torque -= torque * torque * getLoad();
angularVelocity -= angularVelocity * angularVelocity * getLoad();
angle += angularVelocity / 20;
if (angle % (Math.PI * 2) != angle)
{
revolve();
angle = angle % (Math.PI * 2);
}
synchronized (connections)
{
Iterator<Entry<MechanicalNode, ForgeDirection>> it = connections.entrySet().iterator();
while (it.hasNext())
{
Entry<MechanicalNode, ForgeDirection> entry = it.next();
ForgeDirection dir = entry.getValue();
MechanicalNode adjacentMech = entry.getKey();
/**
* Calculate angular velocity and torque.
*/
float ratio = adjacentMech.getRatio(dir.getOpposite(), this) / getRatio(dir, adjacentMech);
boolean inverseRotation = inverseRotation(dir, adjacentMech) && adjacentMech.inverseRotation(dir.getOpposite(), this);
int inversion = inverseRotation ? -1 : 1;
if (Math.abs(torque + inversion * (adjacentMech.getTorque() / ratio * acceleration)) < Math.abs(adjacentMech.getTorque() / ratio))
torque = torque + inversion * (adjacentMech.getTorque() / ratio * acceleration);
if (Math.abs(angularVelocity + inversion * (adjacentMech.getAngularVelocity() * ratio * acceleration)) < Math.abs(adjacentMech.getAngularVelocity() * ratio))
angularVelocity = angularVelocity + (inversion * adjacentMech.getAngularVelocity() * ratio * acceleration);
/**
* Set all current rotations
*/
adjacentMech.angle = Math.abs(angle) * (adjacentMech.angle >= 0 ? 1 : -1);
}
}
}
/**
* Called when one revolution is made.
*/
protected void revolve()
{
}
public void apply(double torque, double angularVelocity)
{
this.torque += torque;
this.angularVelocity += angularVelocity;
}
public double getTorque()
{
return torque;
}
public double getAngularVelocity()
{
return angularVelocity;
}
public float getRatio(ForgeDirection dir, MechanicalNode with)
{
return 0.5f;
}
public boolean inverseRotation(ForgeDirection dir, MechanicalNode with)
{
return true;
}
public double getLoad()
{
return 0.01;
}
/**
* Recache the connections. This is the default connection implementation.
*/
public void recache()
{
synchronized (connections)
{
connections.clear();
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tile = position().translate(dir).getTileEntity(world());
if (tile instanceof IMechanicalNodeProvider)
{
MechanicalNode check = ((IMechanicalNodeProvider) tile).getNode(dir.getOpposite());
if (check != null && canConnect(dir, check) && check.canConnect(dir.getOpposite(), this))
{
connections.put(check, dir);
}
}
}
}
}
@Override
public AbstractMap<MechanicalNode, ForgeDirection> getConnections()
{
return connections;
}
public World world()
{
return parent instanceof TMultiPart ? ((TMultiPart) parent).world() : parent instanceof TileEntity ? ((TileEntity) parent).getWorldObj() : null;
}
public Vector3 position()
{
return parent instanceof TMultiPart ? new Vector3(((TMultiPart) parent).x(), ((TMultiPart) parent).y(), ((TMultiPart) parent).z()) : parent instanceof TileEntity ? new Vector3((TileEntity) parent) : null;
}
public boolean canConnect(ForgeDirection from, Object source)
{
return true;
}
@Override
public double getEnergy()
{
return torque * angularVelocity;
}
}

View file

@ -63,7 +63,7 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu
public void checkClientUpdate()
{
if (Math.abs(prevAngularVelocity - angularVelocity) > 0.05f)
if (Math.abs(prevAngularVelocity - angularVelocity) > 0.05f || (prevAngularVelocity != angularVelocity && (prevAngularVelocity == 0 || angularVelocity == 0)))
{
prevAngularVelocity = angularVelocity;
markPacketUpdate = true;
@ -251,7 +251,7 @@ public abstract class PartMechanical extends JCuboidPart implements JNormalOcclu
}
@Override
public universalelectricity.api.vector.Vector3 getPosition()
public universalelectricity.api.vector.Vector3 position()
{
return new universalelectricity.api.vector.Vector3(x(), y(), z());
}

View file

@ -14,7 +14,7 @@ import calclavia.lib.prefab.tile.TileAdvanced;
import com.google.common.io.ByteArrayDataInput;
public abstract class TileMechanical extends TileAdvanced implements IMechanical, IPacketReceiver
public abstract class TileMechanical extends TileAdvanced implements IMechanicalNodeProvider, IPacketReceiver
{
protected static final int PACKET_VELOCITY = Mechanical.contentRegistry.getNextPacketID();
@ -166,7 +166,7 @@ public abstract class TileMechanical extends TileAdvanced implements IMechanical
}
@Override
public Vector3 getPosition()
public Vector3 position()
{
return new Vector3(this);
}

View file

@ -83,7 +83,7 @@ public class TileMechanicalTurbine extends TileTurbine implements IMechanical
/**
* Face to face stick connection.
*/
TileEntity sourceTile = getPosition().translate(from).getTileEntity(getWorld());
TileEntity sourceTile = position().translate(from).getTileEntity(getWorld());
if (sourceTile instanceof IMechanical)
{

View file

@ -118,7 +118,7 @@ public class TileWaterTurbine extends TileMechanicalTurbine
/**
* Face to face stick connection.
*/
TileEntity sourceTile = getPosition().translate(from).getTileEntity(getWorld());
TileEntity sourceTile = position().translate(from).getTileEntity(getWorld());
if (sourceTile instanceof IMechanical)
{

View file

@ -9,7 +9,7 @@ import net.minecraft.entity.Entity;
*
* @Author DarkGuardsman
*/
public interface IBelt extends IMechanical
public interface IBelt
{
/**
* Used to get a list of entities the belt exerts an effect upon.

View file

@ -1,30 +0,0 @@
package resonantinduction.api.mechanical;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.net.IConnector;
import universalelectricity.api.vector.Vector3;
public interface IMechanical extends IConnector<IMechanicalNetwork>
{
/**
* The angular velocity.
*
* @return Can be negative.
*/
public float getAngularVelocity();
public void setAngularVelocity(float velocity);
public long getTorque();
public void setTorque(long torque);
public float getRatio(ForgeDirection dir, Object source);
public boolean inverseRotation(ForgeDirection dir, IMechanical with);
@Override
public IMechanical getInstance(ForgeDirection dir);
public Vector3 getPosition();
}

View file

@ -1,16 +0,0 @@
package resonantinduction.api.mechanical;
import universalelectricity.api.net.INetwork;
/**
* Mechanical network in interface form for interaction or extension
*
* @author DarkGuardsman
*/
public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechanical>
{
/**
* @return The current rotation value of the network. Used for syncing rotational values.
*/
public float getRotation(float velocity);
}

View file

@ -0,0 +1,139 @@
package resonantinduction.core.grid;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.WeakHashMap;
import universalelectricity.api.net.IUpdate;
import universalelectricity.core.net.ConnectionPathfinder;
/**
* A grid specifying a connection with a series of nodes.
*
* @author Calclavia
*
* @param <N> - The node type.
*/
public abstract class Grid<N extends INode> implements IGrid<N>, IUpdate
{
/**
* A set of connectors (e.g conductors).
*/
private final Set<N> nodes = Collections.newSetFromMap(new WeakHashMap<N, Boolean>());
private final Class<? extends N> nodeType;
public Grid(Class<? extends N> type)
{
nodeType = type;
}
public abstract N newInstance();
@Override
public void add(N node)
{
synchronized (nodes)
{
nodes.add(node);
}
}
@Override
public void remove(N node)
{
synchronized (nodes)
{
nodes.remove(node);
}
}
@Override
public Set<N> getNodes()
{
return nodes;
}
/**
* An grid update called only server side.
*/
@Override
public void update()
{
synchronized (nodes)
{
for (INode node : nodes)
{
node.update();
}
}
}
@Override
public boolean canUpdate()
{
return nodes.size() > 0;
}
@Override
public boolean continueUpdate()
{
return canUpdate();
}
/**
* A simple reconstruct class to rebuild the grid.
*/
@Override
public void reconstruct()
{
Iterator<N> it = new HashSet<N>(getNodes()).iterator();
while (it.hasNext())
{
N node = it.next();
if (isValidNode(node))
{
reconstructNode(node);
}
else
{
it.remove();
}
}
}
public boolean isValidNode(Object node)
{
return nodeType.isAssignableFrom(node.getClass());
}
/**
* Gets the first connector in the set.
*
* @return
*/
public N getFirstNode()
{
for (N node : getNodes())
{
return node;
}
return null;
}
protected void reconstructNode(N node)
{
}
@Override
public String toString()
{
return getClass().getSimpleName() + "[" + hashCode() + ", Connectors: " + nodes.size() + "]";
}
}

View file

@ -0,0 +1,14 @@
package resonantinduction.core.grid;
import java.util.Set;
public interface IGrid<N extends INode>
{
public void add(N node);
public void remove(N node);
public Set<N> getNodes();
public void reconstruct();
}

View file

@ -0,0 +1,13 @@
package resonantinduction.core.grid;
import java.util.AbstractMap;
public interface INode
{
public void update();
/**
* @return A map consisting of the connected object and a ForgeDirection.
*/
public AbstractMap getConnections();
}

View file

@ -0,0 +1,8 @@
package resonantinduction.core.grid;
import net.minecraftforge.common.ForgeDirection;
public interface INodeProvider<N extends INode>
{
public N getNode(ForgeDirection dir);
}

View file

@ -8,7 +8,9 @@ public class ItemHandCrank extends Item
public ItemHandCrank(int id)
{
super(id);
setMaxStackSize(1);
}
@Override
public boolean shouldPassSneakingClickToBlock(World world, int x, int y, int z)
{