Made mechanical network packets more efficient

This commit is contained in:
Calclavia 2014-01-18 12:36:31 +08:00
parent 5c3601585d
commit 883930a1bf
7 changed files with 215 additions and 80 deletions

View file

@ -3,6 +3,7 @@ package resonantinduction.electrical.generator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import resonantinduction.core.Reference;
import resonantinduction.core.prefab.block.BlockRIRotatable;
import resonantinduction.core.render.RIBlockRenderingHandler;
import cpw.mods.fml.relauncher.Side;
@ -13,6 +14,7 @@ public class BlockGenerator extends BlockRIRotatable
public BlockGenerator()
{
super("generator");
setTextureName(Reference.PREFIX + "material_steel");
}
@Override

View file

@ -2,6 +2,7 @@ package resonantinduction.electrical.generator;
import java.util.EnumSet;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.mechanical.network.IMechanical;
import universalelectricity.api.energy.EnergyStorageHandler;
@ -15,8 +16,6 @@ import calclavia.lib.prefab.tile.TileElectrical;
*/
public class TileGenerator extends TileElectrical implements IMechanical
{
private long power;
/** Generator turns KE -> EE. Inverted one will turn EE -> KE. */
public boolean isInversed = false;
@ -33,7 +32,6 @@ public class TileGenerator extends TileElectrical implements IMechanical
{
if (!isInversed)
{
this.power -= this.energy.receiveEnergy(power, true);
this.produce();
}
else
@ -88,4 +86,18 @@ public class TileGenerator extends TileElectrical implements IMechanical
{
energy.receiveEnergy((long) (torque * angularVelocity), true);
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
isInversed = nbt.getBoolean("isInversed");
}
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setBoolean("isInversed", isInversed);
}
}

View file

@ -256,12 +256,6 @@ public class PartFramedWire extends PartAdvancedWire implements TSlottedPart, JN
IconHitEffects.addDestroyEffects(this, effectRenderer, false);
}
@Override
public void onPartChanged(TMultiPart part)
{
refresh();
}
public boolean isBlockedOnSide(ForgeDirection side)
{
TMultiPart blocker = tile().partMap(side.ordinal());
@ -481,6 +475,12 @@ public class PartFramedWire extends PartAdvancedWire implements TSlottedPart, JN
refresh();
}
@Override
public void onPartChanged(TMultiPart part)
{
refresh();
}
/**
* Packets
*/

View file

@ -81,39 +81,134 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
@Override
public void update()
{
if (manualCrankTime > 0)
if (!this.world().isRemote)
{
onReceiveEnergy(null, 20, 0.3f);
manualCrankTime--;
}
if (!world().isRemote)
{
//TODO: Save packets.
if (markRotationUpdate || this.getNetwork().getPrevTorque() != this.getNetwork().getTorque() || this.getNetwork().getPrevAngularVelocity() != this.getNetwork().getAngularVelocity())
if (manualCrankTime > 0)
{
this.sendRotationUpdate(this.getNetwork().getTorque(), this.getNetwork().getAngularVelocity());
onReceiveEnergy(null, 20, 0.3f);
manualCrankTime--;
}
}
if (markRotationUpdate)
else
{
refresh();
if (markRotationUpdate)
{
updateRotations();
}
/**
* Update angle rotation.
*/
if (isClockwise)
angle += this.getNetwork().getAngularVelocity() / 20f;
else
angle -= this.getNetwork().getAngularVelocity() / 20f;
}
}
@Override
public void networkUpdate()
public void onNetworkChanged()
{
/**
* Update angle rotation.
*/
if (isClockwise)
angle += this.getNetwork().getAngularVelocity() / 20;
else
angle -= this.getNetwork().getAngularVelocity() / 20;
if (world() != null)
{
if (markRotationUpdate)
{
updateRotations();
}
}
}
/**
* Updates rotations of all nearby gears.
*/
public void updateRotations()
{
/** 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);
TileEntity tile = vec.getTileEntity(world());
if (tile instanceof TileMultipart)
{
TMultiPart neighbor = ((TileMultipart) tile).partMap(this.placementSide.getOpposite().ordinal());
if (neighbor instanceof PartGear)
{
equateRotation((PartGear) neighbor, false);
}
}
/** Look for gears outside this block space, the relative UP, DOWN, LEFT, RIGHT */
for (int i = 0; i < 4; i++)
{
ForgeDirection checkDir = ForgeDirection.getOrientation(Rotation.rotateSide(this.placementSide.ordinal(), i));
universalelectricity.api.vector.Vector3 checkVec = new universalelectricity.api.vector.Vector3(tile()).modifyPositionFromSide(checkDir);
TileEntity checkTile = checkVec.getTileEntity(world());
if (checkTile instanceof TileMultipart)
{
TMultiPart neighbor = ((TileMultipart) checkTile).partMap(this.placementSide.ordinal());
if (neighbor != this && neighbor instanceof PartGear)
{
equateRotation((PartGear) neighbor, false);
}
}
}
/** Look for gears that are internal and adjacent to this gear. (The 2 sides) */
for (int i = 0; i < 4; i++)
{
ForgeDirection checkDir = ForgeDirection.getOrientation(i);
TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(checkDir).ordinal());
if (neighbor != this && neighbor instanceof PartGear)
{
equateRotation((PartGear) neighbor, false);
}
}
markRotationUpdate = false;
}
@Override
public void onAdded()
{
super.onAdded();
refresh();
}
@Override
public void onMoved()
{
this.refresh();
}
@Override
public void onChunkLoad()
{
super.onChunkLoad();
refresh();
}
@Override
public void onNeighborChanged()
{
super.onNeighborChanged();
refresh();
}
@Override
public void onPartChanged(TMultiPart part)
{
refresh();
}
/**
* Refresh should be called sparingly.
*/
public void refresh()
{
/** Look for gears that are back-to-back with this gear. Equate torque. */
@ -129,13 +224,11 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{
connections[this.placementSide.getOpposite().ordinal()] = neighbor;
getNetwork().merge(((PartGear) neighbor).getNetwork());
equateRotation((PartGear) neighbor, false);
}
}
else if (tile instanceof IMechanical)
{
connections[this.placementSide.getOpposite().ordinal()] = tile;
getNetwork().reconstruct();
}
/** Look for gears outside this block space, the relative UP, DOWN, LEFT, RIGHT */
@ -154,7 +247,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{
connections[checkDir.ordinal()] = neighbor;
getNetwork().merge(((PartGear) neighbor).getNetwork());
equateRotation((PartGear) neighbor, false);
}
}
}
@ -169,11 +261,10 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{
connections[checkDir.ordinal()] = neighbor;
getNetwork().merge(((PartGear) neighbor).getNetwork());
equateRotation((PartGear) neighbor, false);
}
}
markRotationUpdate = false;
getNetwork().reconstruct();
}
@Override
@ -213,17 +304,17 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity)
{
getNetwork().applyEnergy(torque, angularVelocity);
markRotationUpdate = true;
if (!world().isRemote)
{
getNetwork().applyEnergy(torque, angularVelocity);
markRotationUpdate = true;
}
}
@Override
public void preRemove()
{
if (!world().isRemote)
{
this.getNetwork().split(this);
}
this.getNetwork().split(this);
}
/** Packet Code. */
@ -245,19 +336,22 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
read(packet, packet.readUByte());
}
@Override
public void sendNetworkPacket(long torque, float angularVelocity)
{System.out.println("SEND");
tile().getWriteStream(this).writeByte(0).writeLong(torque).writeFloat(angularVelocity).writeBoolean(isClockwise);
}
public void read(MCDataInput packet, int packetID)
{
if (packetID == 0)
{
onReceiveEnergy(null, packet.readLong(), packet.readFloat());
getNetwork().setPower(packet.readLong(), packet.readFloat());
isClockwise = packet.readBoolean();
markRotationUpdate = true;
}
}
public void sendRotationUpdate(long torque, float angularVelocity)
{
tile().getWriteStream(this).writeByte(0).writeLong(torque).writeFloat(angularVelocity);
}
@Override
public int getSlotMask()
{

View file

@ -13,5 +13,10 @@ public interface IMechanicalConnector extends IMechanical, IConnector<IMechanica
/**
* An update called by the network.
*/
public void networkUpdate();
public void onNetworkChanged();
/**
* Uses this connector to send a packet to the client.
*/
public void sendNetworkPacket(long torque, float angularVelocity);
}

View file

@ -17,7 +17,7 @@ public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechan
public long getPower();
/** Torque applied by the network at the given speed */
public int getTorque();
public long getTorque();
/**
* Gets the angular velocity of the network.
@ -26,7 +26,7 @@ public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechan
*/
public float getAngularVelocity();
public int getPrevTorque();
public long getPrevTorque();
public float getPrevAngularVelocity();
@ -34,4 +34,6 @@ public interface IMechanicalNetwork extends INetwork<IMechanicalNetwork, IMechan
public void reconstruct();
public void applyEnergy(long torque, float angularVelocity);
public void setPower(long readLong, float readFloat);
}

View file

@ -29,24 +29,35 @@ import universalelectricity.core.net.NetworkTickHandler;
*/
public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalConnector, IMechanical> implements IMechanicalNetwork
{
public int prevTorque = 0;
public float prevAngularVelocity = 0;
private long prevTorque = 0;
private float prevAngularVelocity = 0;
public int torque = 0;
public float angularVelocity = 0;
private long torque = 0;
private float angularVelocity = 0;
/** 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>>();
/**
* An network update called only server side.
*/
@Override
public void update()
{
prevTorque = torque;
prevAngularVelocity = angularVelocity;
for (IMechanicalConnector connector : this.getConnectors())
if (getPrevTorque() != getTorque() || getPrevAngularVelocity() != getAngularVelocity())
{
connector.networkUpdate();
boolean isFirst = true;
for (IMechanicalConnector connector : this.getConnectors())
{
if (isFirst)
{
connector.sendNetworkPacket(torque, angularVelocity);
isFirst = false;
}
connector.onNetworkChanged();
}
}
for (IMechanical node : this.getNodes())
@ -56,12 +67,16 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
node.onReceiveEnergy(dir, torque, angularVelocity);
}
}
prevTorque = torque;
prevAngularVelocity = angularVelocity;
torque = 0;
angularVelocity = 0;
}
/**
* Applies energy to the mechanical network this tick.
* Note: Server side only.
*/
@Override
public void applyEnergy(long torque, float angularVelocity)
@ -72,21 +87,42 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
}
@Override
public int getTorque()
public long getPrevTorque()
{
return this.torque;
return prevTorque;
}
@Override
public float getPrevAngularVelocity()
{
return prevAngularVelocity;
}
@Override
public long getTorque()
{
return torque;
}
@Override
public float getAngularVelocity()
{
return this.angularVelocity;
return angularVelocity;
}
@Override
public long getPower()
{
return (long) (this.getTorque() * this.getAngularVelocity());
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
@ -98,7 +134,7 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
@Override
public boolean continueUpdate()
{
return false;
return true;
}
@Override
@ -125,11 +161,6 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
it.remove();
}
}
if (this.getNodes().size() > 0)
{
NetworkTickHandler.addNetwork(this);
}
}
}
@ -269,15 +300,4 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
}
}
@Override
public int getPrevTorque()
{
return prevTorque;
}
@Override
public float getPrevAngularVelocity()
{
return prevAngularVelocity;
}
}