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

View file

@ -32,6 +32,18 @@ public class BlockHotPlate extends BlockRI
this.setTickRandomly(true); this.setTickRandomly(true);
} }
@Override
public boolean renderAsNormalBlock()
{
return false;
}
@Override
public boolean isOpaqueCube()
{
return false;
}
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconReg) public void registerIcons(IconRegister iconReg)

View file

@ -15,7 +15,7 @@ import cpw.mods.fml.common.ModMetadata;
public class Settings public class Settings
{ {
/** IDs suggested by Jyzarc and Horfius */ /** 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() public static int getNextBlockID()
{ {

View file

@ -15,7 +15,7 @@ public class BlockRIRotatable extends BlockRotatable
{ {
public BlockRIRotatable(String name) public BlockRIRotatable(String name)
{ {
this(name, Settings.getNextItemID()); this(name, Settings.getNextBlockID());
} }
public BlockRIRotatable(String name, int id) public BlockRIRotatable(String name, int id)

View file

@ -21,9 +21,13 @@ public class BlockGenerator extends BlockRIRotatable
TileEntity tileEntity = world.getBlockTileEntity(x, y, z); TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
if (tileEntity instanceof TileGenerator) if (tileEntity instanceof TileGenerator)
{
if (!world.isRemote)
{ {
((TileGenerator) tileEntity).isInversed = !((TileGenerator) tileEntity).isInversed; ((TileGenerator) tileEntity).isInversed = !((TileGenerator) tileEntity).isInversed;
entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "electrical" : "mechanical") + " energy."); entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "electrical" : "mechanical") + " energy.");
}
return true; return true;
} }
return false; return false;

View file

@ -2,6 +2,8 @@ package resonantinduction.electrical.generator;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import resonantinduction.mechanical.network.IMechanical; import resonantinduction.mechanical.network.IMechanical;
import resonantinduction.mechanical.network.IMechanicalNetwork;
import resonantinduction.mechanical.network.MechanicalNetwork;
import universalelectricity.api.energy.EnergyStorageHandler; import universalelectricity.api.energy.EnergyStorageHandler;
import calclavia.lib.prefab.tile.TileElectrical; import calclavia.lib.prefab.tile.TileElectrical;
@ -46,26 +48,8 @@ public class TileGenerator extends TileElectrical implements IMechanical
} }
@Override @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 net.minecraft.tileentity.TileEntity;
import resonantinduction.api.IBelt; import resonantinduction.api.IBelt;
import resonantinduction.api.IBeltNetwork; import resonantinduction.api.IBeltNetwork;
import resonantinduction.mechanical.network.IMechanicalConnector;
import resonantinduction.mechanical.network.IMechanicalNetwork; import resonantinduction.mechanical.network.IMechanicalNetwork;
import resonantinduction.mechanical.network.MechanicalNetwork; import resonantinduction.mechanical.network.MechanicalNetwork;
import universalelectricity.api.net.IConnector; import universalelectricity.api.net.IConnector;
@ -126,10 +125,11 @@ public class BeltNetwork extends Network<IBeltNetwork, IBelt, TileEntity> implem
for (IConnector node : finder.closedSet) for (IConnector node : finder.closedSet)
{ {
if (node instanceof IMechanicalConnector) //TODO: Fix this
/* if (node instanceof IMechanicalConnector)
{ {
newNetwork.addConnector((IMechanicalConnector) node); newNetwork.addConnector((IMechanicalConnector) node);
} }*/
} }
newNetwork.reconstruct(); newNetwork.reconstruct();

View file

@ -40,8 +40,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{ {
public static Cuboid6[][] oBoxes = new Cuboid6[6][2]; public static Cuboid6[][] oBoxes = new Cuboid6[6][2];
private IMechanicalNetwork network;
static static
{ {
oBoxes[0][0] = new Cuboid6(1 / 8D, 0, 0, 7 / 8D, 1 / 8D, 1); 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); oBoxes[s][1] = oBoxes[0][1].copy().apply(t);
} }
} }
private IMechanicalNetwork network;
/** Side of the block this is placed on */ /** Side of the block this is placed on */
public ForgeDirection placementSide; public ForgeDirection placementSide;
/** Positive torque means it is spinning clockwise */
private long torque = 0;
private long force = 0;
/** The size of the gear */ /** The size of the gear */
private float radius = 0.5f; private float radius = 0.5f;
/** The angular velocity, radians per second. */ public boolean isClockwise = true;
private float angularVelocity = 0;
/** The current angle the gear is on. In radians per second. */ /** The current angle the gear is on. In radians per second. */
public float angle = 0; 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) public void preparePlacement(int side, int itemDamage)
{ {
this.placementSide = ForgeDirection.getOrientation((byte) (side ^ 1)); this.placementSide = ForgeDirection.getOrientation((byte) (side ^ 1));
} }
public long getTorque()
{
return (long) torque;// (force * radius);
}
@Override @Override
public void update() public void update()
{ {
if (manualCrankTime > 0)
if (angularVelocity < 0 || torque == 0)
{ {
angularVelocity = 0; onReceiveEnergy(null, 20, 0.3f);
torque = 0; 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. */ /** 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); 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) 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) else if (tile instanceof IMechanical)
{ {
torque = (long) (((IMechanical) tile).getPower() / angularVelocity); connections[this.placementSide.getOpposite().ordinal()] = tile;
((IMechanical) tile).setPower(torque, angularVelocity); getNetwork().reconstruct();
} }
/** Look for gears outside this block space, the relative UP, DOWN, LEFT, RIGHT */ /** 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) 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) */ /** 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 ForgeDirection checkDir = ForgeDirection.getOrientation(i);
if (i < 4) TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(checkDir).ordinal());
{
TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(ForgeDirection.getOrientation(i)).ordinal());
if (neighbor != this && neighbor instanceof PartGear) if (neighbor != this && neighbor instanceof PartGear)
{ {
equatePower((PartGear) neighbor, false); connections[checkDir.ordinal()] = neighbor;
} getNetwork().merge(((PartGear) neighbor).getNetwork());
equateRotation((PartGear) neighbor, false);
} }
} }
/** markRotationUpdate = false;
* Update angle rotation. }
*/
if (angularVelocity > 0 && torque != 0) @Override
public Object[] getConnections()
{ {
angle += angularVelocity / 20; return connections;
}
} }
public void equatePower(PartGear neighbor, boolean isPositive) public void equateRotation(PartGear neighbor, boolean isPositive)
{
if (!neighbor.markRotationUpdate)
{ {
if (isPositive) if (isPositive)
{ {
torque = (torque + ((PartGear) neighbor).torque) / 2; ((PartGear) neighbor).isClockwise = isClockwise;
((PartGear) neighbor).torque = torque;
} }
else else
{ {
torque = (torque - ((PartGear) neighbor).torque) / 2; ((PartGear) neighbor).isClockwise = !isClockwise;
((PartGear) neighbor).torque = -torque;
} }
angularVelocity = (angularVelocity + ((PartGear) neighbor).angularVelocity) / 2; neighbor.markRotationUpdate = true;
((PartGear) neighbor).angularVelocity = angularVelocity; }
} }
@Override @Override
public boolean activate(EntityPlayer player, MovingObjectPosition hit, ItemStack item) 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()) if (player.isSneaking())
{ {
this.torque += 10; this.manualCrankTime = 20;
this.angularVelocity += 0.2f;
} }
return false; 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. */ /** Packet Code. */
@Override @Override
public void readDesc(MCDataInput packet) public void readDesc(MCDataInput packet)
@ -199,6 +232,25 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
packet.writeByte(this.placementSide.ordinal()); 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 @Override
public int getSlotMask() public int getSlotMask()
{ {
@ -278,12 +330,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
return "resonant_induction_gear"; return "resonant_induction_gear";
} }
@Override
public Object[] getConnections()
{
return null;
}
@Override @Override
public IMechanicalNetwork getNetwork() 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; 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; 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); FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE);
MODEL.renderAll(); MODEL.renderAll();

View file

@ -3,20 +3,16 @@ package resonantinduction.mechanical.network;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.net.IConnectable; import universalelectricity.api.net.IConnectable;
/**
* Applied to machines that connect to a mech network
*
* @author Darkguardsman
*/
public interface IMechanical extends IConnectable public interface IMechanical extends IConnectable
{ {
public void setPower(long torque, float speed); /**
* Adds energy to a block. Returns the quantity of energy that was accepted. This should always
public long getPower(); * return 0 if the block cannot be externally charged.
*
/** Called by the network when its torque value changes. */ * @param from Orientation the energy is sent in from.
public void onTorqueChange(ForgeDirection side, int speed); * @param receive Maximum amount of energy (joules) to be sent into the block.
* @param doReceive If false, the charge will only be simulated.
/** Gets the force on the side, zero is ignored, neg is input force, pos is output force */ * @return Amount of energy that was accepted by the block.
public int getForceSide(ForgeDirection side); */
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity);
} }

View file

@ -1,11 +1,17 @@
package resonantinduction.mechanical.network; package resonantinduction.mechanical.network;
import universalelectricity.api.net.IConnectable;
import universalelectricity.api.net.IConnector; import universalelectricity.api.net.IConnector;
/** For the mechanical network. /**
* Applied to connectors in a mechanical network
* *
* @author Calclavia */ * @author Calclavia
public interface IMechanicalConnector extends IConnector<IMechanicalNetwork> */
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. * Gets the angular velocity of the network.
*
* @return In radians per second. * @return In radians per second.
*/ */
public int getAngularVelocity(); public float getAngularVelocity();
/** Called to rebuild the network */ /** Called to rebuild the network */
public void reconstruct(); public void reconstruct();
public void applyEnergy(long torque, float angularVelocity);
} }

View file

@ -1,7 +1,9 @@
package resonantinduction.mechanical.network; package resonantinduction.mechanical.network;
import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import net.minecraftforge.common.ForgeDirection; import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.net.IConnector; 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 * Torque = r (Radius) * F (Force) * sin0 (Direction/Angle of the force applied. 90 degrees if
* optimal.) * optimal.)
* *
* @author DarkGuardsman * @author Calclavia
*/ */
public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalConnector, IMechanical> implements IMechanicalNetwork public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalConnector, IMechanical> implements IMechanicalNetwork
{ {
private int torque = 0; public int torque = 0;
private int speed = 0; public float angularVelocity = 0;
private int resistance = 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 @Override
public void update() 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 @Override
@ -46,21 +103,17 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
{ {
// Reset all values related to wires // Reset all values related to wires
this.getNodes().clear(); this.getNodes().clear();
this.torqueMap.clear();
this.resistance = 0;
this.torque = 0;
this.speed = 0;
// Iterate threw list of wires // Iterate threw list of wires
Iterator<IMechanicalConnector> it = this.getConnectors().iterator(); Iterator<IMechanicalConnector> it = this.getConnectors().iterator();
while (it.hasNext()) while (it.hasNext())
{ {
IMechanicalConnector conductor = it.next(); IMechanicalConnector connector = it.next();
if (conductor != null) if (connector != null)
{ {
this.reconstructConductor(conductor); reconstructConnector(connector);
} }
else else
{ {
@ -76,16 +129,14 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
} }
/** Segmented out call so overriding can be done when conductors are reconstructed. */ /** 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. */ /** 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) if (obj instanceof IMechanical)
{ {
ForceWrapper[] set = this.torqueMap.get((IMechanical) obj); EnumSet<ForgeDirection> set = this.handlerDirectionMap.get(obj);
if (set == null) if (set == null)
{ {
set = new ForceWrapper[6]; set = EnumSet.noneOf(ForgeDirection.class);
} }
this.getNodes().add((IMechanical) obj); this.getNodes().add((IMechanical) obj);
set[side.ordinal()] = new ForceWrapper(((IMechanical) obj).getForceSide(side.getOpposite()), ((IMechanical) obj).getForceSide(side.getOpposite())); set.add(side);
this.torqueMap.put((IMechanical) obj, set); this.handlerDirectionMap.put(obj, set);
} }
} }
} }
@Override
public boolean canUpdate()
{
return true;
}
@Override
public boolean continueUpdate()
{
return true;
}
@Override @Override
public IMechanicalNetwork merge(IMechanicalNetwork network) public IMechanicalNetwork merge(IMechanicalNetwork network)
{ {
@ -127,7 +166,6 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
MechanicalNetwork newNetwork = new MechanicalNetwork(); MechanicalNetwork newNetwork = new MechanicalNetwork();
newNetwork.getConnectors().addAll(this.getConnectors()); newNetwork.getConnectors().addAll(this.getConnectors());
newNetwork.getConnectors().addAll(network.getConnectors()); newNetwork.getConnectors().addAll(network.getConnectors());
network.getConnectors().clear(); network.getConnectors().clear();
network.getNodes().clear(); network.getNodes().clear();
this.getConnectors().clear(); this.getConnectors().clear();
@ -225,35 +263,4 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
newNetwork.reconstruct(); 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();
}
} }