Fixed some issues with battery box

This commit is contained in:
Robert S 2014-04-13 00:46:55 -04:00
parent ea86928124
commit 99cfdc7fdb
3 changed files with 216 additions and 219 deletions

View file

@ -7,106 +7,104 @@ import java.util.Set;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.net.Network;
/** Energy network designed to allow several tiles to act as if they share the same energy
* level */
public class EnergyDistributionNetwork extends Network<EnergyDistributionNetwork, TileEnergyDistribution>
{
public long totalEnergy = 0;
public long totalCapacity = 0;
public long totalEnergy = 0;
public long totalCapacity = 0;
public EnergyDistributionNetwork()
{
super(TileEnergyDistribution.class);
}
public EnergyDistributionNetwork()
{
super(TileEnergyDistribution.class);
}
public void redistribute(TileEnergyDistribution... exclusion)
{
int lowestY = 255, highestY = 0;
public void redistribute(TileEnergyDistribution... exclusion)
{
int lowestY = 255, highestY = 0;
totalEnergy = 0;
totalCapacity = 0;
totalEnergy = 0;
totalCapacity = 0;
for (TileEnergyDistribution connector : this.getConnectors())
{
totalEnergy += connector.getEnergy(ForgeDirection.UNKNOWN);
totalCapacity += connector.getEnergyCapacity(ForgeDirection.UNKNOWN);
for (TileEnergyDistribution connector : this.getConnectors())
{
totalEnergy += connector.getEnergyHandler().getEnergy();
totalCapacity += connector.getEnergyHandler().getEnergyCapacity();
lowestY = Math.min(connector.yCoord, lowestY);
highestY = Math.max(connector.yCoord, highestY);
lowestY = Math.min(connector.yCoord, lowestY);
highestY = Math.max(connector.yCoord, highestY);
connector.renderEnergyAmount = 0;
}
connector.renderEnergyAmount = 0;
}
/**
* Apply render
*/
long remainingRenderEnergy = totalEnergy;
/** Apply render */
long remainingRenderEnergy = totalEnergy;
for (int y = lowestY; y <= highestY; y++)
{
Set<TileEnergyDistribution> connectorsInlevel = new LinkedHashSet<TileEnergyDistribution>();
for (int y = lowestY; y <= highestY; y++)
{
Set<TileEnergyDistribution> connectorsInlevel = new LinkedHashSet<TileEnergyDistribution>();
for (TileEnergyDistribution connector : this.getConnectors())
{
if (connector.yCoord == y)
{
connectorsInlevel.add(connector);
}
}
for (TileEnergyDistribution connector : this.getConnectors())
{
if (connector.yCoord == y)
{
connectorsInlevel.add(connector);
}
}
int levelSize = connectorsInlevel.size();
long used = 0;
int levelSize = connectorsInlevel.size();
long used = 0;
for (TileEnergyDistribution connector : connectorsInlevel)
{
long tryInject = Math.min(remainingRenderEnergy / levelSize, connector.getEnergyCapacity(ForgeDirection.UNKNOWN));
connector.renderEnergyAmount = tryInject;
used += tryInject;
}
for (TileEnergyDistribution connector : connectorsInlevel)
{
long tryInject = Math.min(remainingRenderEnergy / levelSize, connector.getEnergyHandler().getEnergyCapacity());
connector.renderEnergyAmount = tryInject;
used += tryInject;
}
remainingRenderEnergy -= used;
remainingRenderEnergy -= used;
if (remainingRenderEnergy <= 0)
break;
}
if (remainingRenderEnergy <= 0)
break;
}
/**
* Apply energy loss.
*/
double percentageLoss = 0;// Math.max(0, (1 - (getConnectors().size() * 6 / 100d)));
long energyLoss = (long) (percentageLoss * 100);
totalEnergy -= energyLoss;
/** Apply energy loss. */
double percentageLoss = 0;// Math.max(0, (1 - (getConnectors().size() * 6 / 100d)));
long energyLoss = (long) (percentageLoss * 100);
totalEnergy -= energyLoss;
int amountOfNodes = this.getConnectors().size() - exclusion.length;
int amountOfNodes = this.getConnectors().size() - exclusion.length;
if (totalEnergy > 0 && amountOfNodes > 0)
{
long remainingEnergy = totalEnergy;
if (totalEnergy > 0 && amountOfNodes > 0)
{
long remainingEnergy = totalEnergy;
TileEnergyDistribution firstNode = this.getFirstConnector();
TileEnergyDistribution firstNode = this.getFirstConnector();
for (TileEnergyDistribution node : this.getConnectors())
{
if (node != firstNode && !Arrays.asList(exclusion).contains(node))
{
double percentage = ((double) node.getEnergyCapacity(ForgeDirection.UNKNOWN) / (double) totalCapacity);
long energyForBattery = Math.max(Math.round(totalEnergy * percentage), 0);
node.setEnergy(ForgeDirection.UNKNOWN, energyForBattery);
remainingEnergy -= energyForBattery;
}
}
for (TileEnergyDistribution node : this.getConnectors())
{
if (node != firstNode && !Arrays.asList(exclusion).contains(node))
{
double percentage = ((double) node.getEnergyHandler().getEnergyCapacity() / (double) totalCapacity);
long energyForBattery = Math.max(Math.round(totalEnergy * percentage), 0);
node.getEnergyHandler().setEnergy(energyForBattery);
remainingEnergy -= energyForBattery;
}
}
firstNode.setEnergy(ForgeDirection.UNKNOWN, Math.max(remainingEnergy, 0));
}
}
firstNode.getEnergyHandler().setEnergy(Math.max(remainingEnergy, 0));
}
}
@Override
protected void reconstructConnector(TileEnergyDistribution node)
{
node.setNetwork(this);
}
@Override
protected void reconstructConnector(TileEnergyDistribution node)
{
node.setNetwork(this);
}
@Override
public EnergyDistributionNetwork newInstance()
{
return new EnergyDistributionNetwork();
}
@Override
public EnergyDistributionNetwork newInstance()
{
return new EnergyDistributionNetwork();
}
}

View file

@ -18,7 +18,7 @@ import calclavia.lib.network.IPacketSender;
import com.google.common.io.ByteArrayDataInput;
/**
* A modular battery.
* A modular battery box that allows shared connections with boxes next to it.
*
* @author Calclavia
*/
@ -34,7 +34,7 @@ public class TileBattery extends TileEnergyDistribution implements IVoltageInput
public TileBattery()
{
this.energy = new EnergyStorageHandler(0);
this.setEnergyHandler(new EnergyStorageHandler(0));
this.ioMap = 0;
this.saveIOMap = true;
}
@ -52,7 +52,7 @@ public class TileBattery extends TileEnergyDistribution implements IVoltageInput
public void initiate()
{
super.initiate();
energy.setCapacity(getEnergyForTier(getBlockMetadata()));
getEnergyHandler().setCapacity(getEnergyForTier(getBlockMetadata()));
}
@Override
@ -62,7 +62,7 @@ public class TileBattery extends TileEnergyDistribution implements IVoltageInput
{
// energy.setMaxTransfer((long) Math.min(Math.pow(10000,
// this.getNetwork().getConnectors().size()), energy.getEnergyCapacity()));
energy.setMaxTransfer(energy.getEnergyCapacity());
getEnergyHandler().setMaxTransfer(getEnergyHandler().getEnergyCapacity());
markDistributionUpdate |= produce() > 0;
}
@ -78,7 +78,7 @@ public class TileBattery extends TileEnergyDistribution implements IVoltageInput
@Override
public void onReceivePacket(ByteArrayDataInput data, EntityPlayer player, Object... extra)
{
energy.setEnergy(data.readLong());
getEnergyHandler().setEnergy(data.readLong());
ioMap = data.readShort();
}

View file

@ -7,165 +7,164 @@ import net.minecraftforge.common.ForgeDirection;
import universalelectricity.api.net.IConnector;
import universalelectricity.api.vector.Vector3;
public class TileEnergyDistribution extends TileElectrical
implements IConnector<EnergyDistributionNetwork>
public class TileEnergyDistribution extends TileElectrical implements IConnector<EnergyDistributionNetwork>
{
public boolean markClientUpdate = false;
public boolean markDistributionUpdate = false;
public long renderEnergyAmount = 0;
private EnergyDistributionNetwork network;
public boolean markClientUpdate = false;
public boolean markDistributionUpdate = false;
public long renderEnergyAmount = 0;
private EnergyDistributionNetwork network;
public TileEnergyDistribution()
{
super(null);
}
public TileEnergyDistribution()
{
super(null);
}
public TileEnergyDistribution(Material material)
{
super(material);
}
public TileEnergyDistribution(Material material)
{
super(material);
}
@Override
public void initiate()
{
super.initiate();
this.updateStructure();
}
@Override
public void initiate()
{
super.initiate();
this.updateStructure();
}
@Override
public void onAdded()
{
if (!world().isRemote)
{
updateStructure();
}
}
@Override
public void onAdded()
{
if (!world().isRemote)
{
updateStructure();
}
}
@Override
public void onNeighborChanged()
{
if (!world().isRemote)
{
updateStructure();
}
}
@Override
public void onNeighborChanged()
{
if (!world().isRemote)
{
updateStructure();
}
}
@Override
public void updateEntity()
{
super.updateEntity();
@Override
public void updateEntity()
{
super.updateEntity();
if (!this.worldObj.isRemote)
{
if (markDistributionUpdate && ticks % 5 == 0)
{
getNetwork().redistribute();
markDistributionUpdate = false;
}
if (!this.worldObj.isRemote)
{
if (markDistributionUpdate && ticks % 5 == 0)
{
getNetwork().redistribute();
markDistributionUpdate = false;
}
if (markClientUpdate && ticks % 5 == 0)
{
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
}
if (markClientUpdate && ticks % 5 == 0)
{
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
}
@Override
public long getEnergy(ForgeDirection from)
{
return getNetwork().totalEnergy;
}
@Override
public long getEnergy(ForgeDirection from)
{
return getNetwork().totalEnergy;
}
@Override
public long getEnergyCapacity(ForgeDirection from)
{
return getNetwork().totalCapacity;
@Override
public long getEnergyCapacity(ForgeDirection from)
{
return getNetwork().totalCapacity;
}
}
@Override
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{
long returnValue = super.onReceiveEnergy(from, receive, doReceive);
markDistributionUpdate = true;
markClientUpdate = true;
return returnValue;
}
@Override
public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive)
{
long returnValue = super.onReceiveEnergy(from, receive, doReceive);
markDistributionUpdate = true;
markClientUpdate = true;
return returnValue;
}
@Override
public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract)
{
long returnValue = super.onExtractEnergy(from, extract, doExtract);
markDistributionUpdate = true;
markClientUpdate = true;
return returnValue;
}
@Override
public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract)
{
long returnValue = super.onExtractEnergy(from, extract, doExtract);
markDistributionUpdate = true;
markClientUpdate = true;
return returnValue;
}
@Override
public EnergyDistributionNetwork getNetwork()
{
if (this.network == null)
{
this.network = new EnergyDistributionNetwork();
this.network.addConnector(this);
}
@Override
public EnergyDistributionNetwork getNetwork()
{
if (this.network == null)
{
this.network = new EnergyDistributionNetwork();
this.network.addConnector(this);
}
return this.network;
}
return this.network;
}
@Override
public void setNetwork(EnergyDistributionNetwork structure)
{
this.network = structure;
}
@Override
public void setNetwork(EnergyDistributionNetwork structure)
{
this.network = structure;
}
public void updateStructure()
{
if (!this.worldObj.isRemote)
{
for (Object obj : getConnections())
{
if (obj != null)
{
this.getNetwork().merge(((TileEnergyDistribution) obj).getNetwork());
}
}
public void updateStructure()
{
if (!this.worldObj.isRemote)
{
for (Object obj : getConnections())
{
if (obj != null)
{
this.getNetwork().merge(((TileEnergyDistribution) obj).getNetwork());
}
}
markDistributionUpdate = true;
markClientUpdate = true;
}
}
markDistributionUpdate = true;
markClientUpdate = true;
}
}
@Override
public Object[] getConnections()
{
Object[] connections = new Object[6];
@Override
public Object[] getConnections()
{
Object[] connections = new Object[6];
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tile = new Vector3(this).translate(dir).getTileEntity(this.worldObj);
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS)
{
TileEntity tile = new Vector3(this).translate(dir).getTileEntity(this.worldObj);
if (tile != null && tile.getClass() == this.getClass())
{
connections[dir.ordinal()] = tile;
}
}
if (tile != null && tile.getClass() == this.getClass())
{
connections[dir.ordinal()] = tile;
}
}
return connections;
}
return connections;
}
@Override
public void invalidate()
{
this.getNetwork().redistribute(this);
this.getNetwork().split(this);
super.invalidate();
}
@Override
public void invalidate()
{
this.getNetwork().redistribute(this);
this.getNetwork().split(this);
super.invalidate();
}
@Override
public IConnector<EnergyDistributionNetwork> getInstance(ForgeDirection from)
{
return this;
}
@Override
public IConnector<EnergyDistributionNetwork> getInstance(ForgeDirection from)
{
return this;
}
}