diff --git a/src/minecraft/dark/assembly/common/machine/NetworkAssembly.java b/src/minecraft/dark/assembly/common/machine/NetworkAssembly.java index 069ff85c..f711ad8e 100644 --- a/src/minecraft/dark/assembly/common/machine/NetworkAssembly.java +++ b/src/minecraft/dark/assembly/common/machine/NetworkAssembly.java @@ -8,11 +8,11 @@ import java.util.Set; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.ForgeDirection; import universalelectricity.core.block.IElectrical; +import universalelectricity.core.block.IElectricalStorage; import universalelectricity.core.vector.Vector3; import dark.api.INetworkEnergyPart; import dark.api.INetworkPart; import dark.core.tile.network.NetworkSharedPower; -import dark.core.tile.network.NetworkTileEntities; public class NetworkAssembly extends NetworkSharedPower { @@ -56,13 +56,39 @@ public class NetworkAssembly extends NetworkSharedPower float currentDemand = 0; long lastTime = lastDemandCalcTime; long time = date.getTime(); - if(lastTime == 0) + if (lastTime == 0) { lastTime = time; } + currentDemand += getNetworkPartsDemand(); + Iterator it = this.powerLoads.iterator(); + while (it.hasNext()) + { + currentDemand += this.getDemandTile(it.next()); + } - + /* Average calculations */ + this.lastDemandCalcTime = time; + this.lastNetDemand = currentDemand; + //TODO calculate the averages over time to produce a better number + if(this.minDemand == 0) + { + this.minDemand = currentDemand; + } + if(this.averageDemand == 0) + { + this.averageDemand = currentDemand; + } + if(currentDemand > this.maxDemand) + { + this.maxDemand = currentDemand; + } + if(currentDemand < this.minDemand) + { + this.minDemand = currentDemand; + } + this.averageDemand = (averageDemand + currentDemand) / 2; return currentDemand; } @@ -73,14 +99,14 @@ public class NetworkAssembly extends NetworkSharedPower float currentDemand = 0; long lastTime = lastDemandPCalcTime; long time = date.getTime(); - if(lastTime == 0) + if (lastTime == 0) { lastTime = time; } - for(INetworkPart part : this.getNetworkMemebers()) + for (INetworkPart part : this.getNetworkMemebers()) { - if(part instanceof TileEntityAssembly) + if (part instanceof TileEntityAssembly) { currentDemand += ((TileEntityAssembly) part).getWattLoad(); } @@ -92,6 +118,32 @@ public class NetworkAssembly extends NetworkSharedPower return currentDemand; } + public float getDemandTile(TileEntity te) + { + float demand = 0; + if (te instanceof IElectrical) + { + Vector3 vec = new Vector3(te); + for (int side = 0; side < 6; side++) + { + ForgeDirection dir = ForgeDirection.getOrientation(side); + TileEntity ent = vec.clone().modifyPositionFromSide(dir).getTileEntity(te.worldObj); + if (ent instanceof INetworkEnergyPart && ((INetworkEnergyPart) ent).getTileNetwork().equals(this)) + { + if (((IElectrical) te).canConnect(dir.getOpposite())) + { + demand += (((IElectrical) te).getRequest(dir.getOpposite())); + } + } + } + if (te instanceof IElectricalStorage) + { + demand = Math.min(((IElectricalStorage) te).getMaxEnergyStored(), Math.min(((IElectricalStorage) te).getMaxEnergyStored() - ((IElectricalStorage) te).getEnergyStored(), demand)); + } + } + return demand; + } + /** Called when the network gets more power then its parts need. Also called after all parts in * the network get there power and is time to start suppling connections */ public void supplyPower(float power) diff --git a/src/minecraft/dark/assembly/common/machine/TileEntityAssembly.java b/src/minecraft/dark/assembly/common/machine/TileEntityAssembly.java index ea9d5ea6..d20c270d 100644 --- a/src/minecraft/dark/assembly/common/machine/TileEntityAssembly.java +++ b/src/minecraft/dark/assembly/common/machine/TileEntityAssembly.java @@ -7,6 +7,7 @@ import java.util.Random; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraftforge.common.ForgeDirection; +import universalelectricity.core.electricity.ElectricityPack; import universalelectricity.core.vector.Vector3; import universalelectricity.prefab.network.IPacketReceiver; import dark.api.INetworkEnergyPart; @@ -102,7 +103,6 @@ public abstract class TileEntityAssembly extends TileEntityMachine implements IP return this.running; } - @Override public boolean canTileConnect(TileEntity entity, ForgeDirection dir) { @@ -160,6 +160,17 @@ public abstract class TileEntityAssembly extends TileEntityMachine implements IP return ((NetworkSharedPower) this.getTileNetwork()).drainPower(this, watts, doDrain); } + @Override + public float receiveElectricity(ForgeDirection from, ElectricityPack receive, boolean doReceive) + { + if (this.getInputDirections().contains(from)) + { + return this.getTileNetwork().dumpPower(this, receive.getWatts(), doReceive); + } + + return 0; + } + /** Amount of energy this tile runs on per tick */ public double getWattLoad() { @@ -169,7 +180,7 @@ public abstract class TileEntityAssembly extends TileEntityMachine implements IP @Override public float getRequest(ForgeDirection direction) { - return this.getTileNetwork().getEnergySpace(); + return this.getTileNetwork().getNetworkDemand(); } @Override