Finished initial system for mechanical network

This commit is contained in:
Calclavia 2014-01-18 01:33:37 +08:00
parent 018e3253a6
commit e8e8042241
13 changed files with 244 additions and 186 deletions

View file

@ -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

View file

@ -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)

View file

@ -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()
{

View file

@ -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)

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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<IBeltNetwork, IBelt, TileEntity> implem
for (IConnector node : finder.closedSet)
{
if (node instanceof IMechanicalConnector)
//TODO: Fix this
/* if (node instanceof IMechanicalConnector)
{
newNetwork.addConnector((IMechanicalConnector) node);
}
}*/
}
newNetwork.reconstruct();

View file

@ -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;
}
}

View file

@ -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();

View file

@ -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);
}

View file

@ -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<IMechanicalNetwork>
* @author Calclavia
*/
public interface IMechanicalConnector extends IMechanical, IConnector<IMechanicalNetwork>
{
public int getResistance();
/**
* An update called by the network.
*/
public void networkUpdate();
}

View file

@ -21,10 +21,13 @@ public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechan
/**
* Gets the angular velocity of the network.
*
* @return In radians per second.
*/
public int getAngularVelocity();
public float getAngularVelocity();
/** Called to rebuild the network */
public void reconstruct();
public void applyEnergy(long torque, float angularVelocity);
}

View file

@ -1,7 +1,9 @@
package resonantinduction.mechanical.network;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.net.IConnector;
@ -23,20 +25,75 @@ import universalelectricity.core.net.NetworkTickHandler;
* Torque = r (Radius) * F (Force) * sin0 (Direction/Angle of the force applied. 90 degrees if
* optimal.)
*
* @author DarkGuardsman
* @author Calclavia
*/
public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalConnector, IMechanical> implements IMechanicalNetwork
{
private int torque = 0;
private int speed = 0;
private int resistance = 0;
public int torque = 0;
public float angularVelocity = 0;
private HashMap<IMechanical, ForceWrapper[]> torqueMap = new HashMap<IMechanical, ForceWrapper[]>();
/** 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>>();
@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<IMechanicalNetwork, IMechanicalCo
{
// Reset all values related to wires
this.getNodes().clear();
this.torqueMap.clear();
this.resistance = 0;
this.torque = 0;
this.speed = 0;
// Iterate threw list of wires
Iterator<IMechanicalConnector> 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<IMechanicalNetwork, IMechanicalCo
}
/** Segmented out call so overriding can be done when conductors are reconstructed. */
protected void reconstructConductor(IMechanicalConnector conductor)
protected void reconstructConnector(IMechanicalConnector connector)
{
conductor.setNetwork(this);
connector.setNetwork(this);
for (int i = 0; i < conductor.getConnections().length; i++)
for (int i = 0; i < connector.getConnections().length; i++)
{
reconstructHandler(conductor.getConnections()[i], ForgeDirection.getOrientation(i).getOpposite());
reconstructHandler(connector.getConnections()[i], ForgeDirection.getOrientation(i).getOpposite());
}
this.resistance += conductor.getResistance();
}
/** Segmented out call so overriding can be done when machines are reconstructed. */
@ -95,30 +146,18 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
{
if (obj instanceof IMechanical)
{
ForceWrapper[] set = this.torqueMap.get((IMechanical) obj);
EnumSet<ForgeDirection> 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<IMechanicalNetwork, IMechanicalCo
MechanicalNetwork newNetwork = new MechanicalNetwork();
newNetwork.getConnectors().addAll(this.getConnectors());
newNetwork.getConnectors().addAll(network.getConnectors());
network.getConnectors().clear();
network.getNodes().clear();
this.getConnectors().clear();
@ -225,35 +263,4 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
newNetwork.reconstruct();
}
}
@Override
public int getTorque()
{
return this.torque;
}
@Override
public int getAngularVelocity()
{
return this.speed;
}
public static class ForceWrapper
{
public int force = 0;
public int speed = 0;
public ForceWrapper(int force, int speed)
{
this.force = force;
this.speed = speed;
}
}
@Override
public long getPower()
{
return this.getTorque() * this.getAngularVelocity();
}
}