Make EnergyNetwork capacity be a double (#5589)

* Support having capacity of network as a double and fix mean capacity for tube networks of multiple different tiers

* Less duplicate code for calculating capacities. Also less division then remultiplication

* Minor cleanup

* Reduce a small amount of duplicate code from universal cable
This commit is contained in:
Richard Freimer authored and GitHub committed 2019-08-04 20:01:27 -04:00
1 parent 9c76c68273
commit 0f5ef4e11e
10 files changed
+74 -92

No files matched your search

+1 -1
View File
@@ -18,7 +18,7 @@ public class MekanismAPI {
/**
* The version of the api classes - may not always match the mod's version
*/
public static final String API_VERSION = "9.7.8";
public static final String API_VERSION = "9.8.1";
/**
* Mekanism debug mode
@@ -32,6 +32,8 @@ public abstract class DynamicNetwork<ACCEPTOR, NETWORK extends DynamicNetwork<AC
protected Map<IGridTransmitter<ACCEPTOR, NETWORK, BUFFER>, EnumSet<EnumFacing>> changedAcceptors = new HashMap<>();
protected Range4D packetRange = null;
protected int capacity = 0;
protected double doubleCapacity = 0;
@Deprecated
protected double meanCapacity = 0;
protected boolean needsUpdate = false;
protected int updateDelay = 0;
@@ -70,11 +72,9 @@ public abstract class DynamicNetwork<ACCEPTOR, NETWORK extends DynamicNetwork<AC
if (!changedAcceptors.isEmpty()) {
for (Entry<IGridTransmitter<ACCEPTOR, NETWORK, BUFFER>, EnumSet<EnumFacing>> entry : changedAcceptors.entrySet()) {
IGridTransmitter<ACCEPTOR, NETWORK, BUFFER> transmitter = entry.getKey();
if (transmitter.isValid()) {
EnumSet<EnumFacing> directionsChanged = entry.getValue();
for (EnumFacing side : directionsChanged) {
//Update all the changed directions
for (EnumFacing side : entry.getValue()) {
updateTransmitterOnSide(transmitter, side);
}
}
@@ -153,7 +153,6 @@ public abstract class DynamicNetwork<ACCEPTOR, NETWORK extends DynamicNetwork<AC
} else {
changedAcceptors.put(transmitter, EnumSet.of(side));
}
TransmitterNetworkRegistry.registerChangedNetwork(this);
}
@@ -178,10 +177,7 @@ public abstract class DynamicNetwork<ACCEPTOR, NETWORK extends DynamicNetwork<AC
}
public Range4D getPacketRange() {
if (packetRange == null) {
return genPacketRange();
}
return packetRange;
return packetRange == null ? genPacketRange() : packetRange;
}
protected Range4D genPacketRange() {
@@ -202,23 +198,19 @@ public abstract class DynamicNetwork<ACCEPTOR, NETWORK extends DynamicNetwork<AC
for (IGridTransmitter transmitter : transmitters) {
Coord4D coord = transmitter.coord();
if (coord.x < minX) {
minX = coord.x;
} else if (coord.x > maxX) {
maxX = coord.x;
}
if (coord.y < minY) {
minY = coord.y;
} else if (coord.y > maxY) {
maxY = coord.y;
}
if (coord.z < minZ) {
minZ = coord.z;
}
if (coord.x > maxX) {
maxX = coord.x;
}
if (coord.y > maxY) {
maxY = coord.y;
}
if (coord.x > maxZ) {
} else if (coord.x > maxZ) {
maxZ = coord.z;
}
}
@@ -254,27 +246,28 @@ public abstract class DynamicNetwork<ACCEPTOR, NETWORK extends DynamicNetwork<AC
}
public synchronized void updateCapacity() {
doubleCapacity = transmitters.stream().mapToDouble(IGridTransmitter::getCapacity).sum();
capacity = doubleCapacity > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) doubleCapacity;
//TODO: Remove this at some point, but kept in for now in case something is using the API and requires the meanCapacity
updateMeanCapacity();
double newCapacity = meanCapacity * transmitters.size();
//TODO: Make EnergyNetwork use doubles for capacity rather than just integers
capacity = newCapacity > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) newCapacity;
}
/**
* Override this if things can have variable capacity along the network. An 'average' value of capacity. Calculate it how you will.
*/
@Deprecated
protected synchronized void updateMeanCapacity() {
if (transmitters.size() > 0) {
meanCapacity = transmitters.iterator().next().getCapacity();
} else {
meanCapacity = 0;
}
meanCapacity = transmitters.size() > 0 ? doubleCapacity / transmitters.size() : 0;
}
public int getCapacity() {
return capacity;
}
public double getCapacityAsDouble() {
return doubleCapacity;
}
public World getWorld() {
return world;
}
@@ -229,8 +229,7 @@ public class TransmitterNetworkRegistry {
transmitter.setOrphan(false);
for (EnumFacing direction : EnumFacing.VALUES) {
if (direction.getAxis().isHorizontal() && !transmitter.world()
.isBlockLoaded(from.getPos().offset(direction))) {
if (direction.getAxis().isHorizontal() && !transmitter.world().isBlockLoaded(from.getPos().offset(direction))) {
continue;
}
Coord4D directionCoord = transmitter.getAdjacentConnectableTransmitterCoord(direction);
@@ -77,7 +77,7 @@ public class TileEntityUniversalCable extends TileEntityTransmitter<EnergyAccept
List<EnumFacing> sides = getConnections(ConnectionType.PULL);
if (!sides.isEmpty()) {
TileEntity[] connectedOutputters = CableUtils.getConnectedOutputters(this, getPos(), getWorld());
double canDraw = tier.getCableCapacity();
double maxDraw = tier.getCableCapacity();
for (EnumFacing side : sides) {
TileEntity outputter = connectedOutputters[side.ordinal()];
if (outputter != null) {
@@ -85,42 +85,24 @@ public class TileEntityUniversalCable extends TileEntityTransmitter<EnergyAccept
IStrictEnergyStorage strictStorage;
ITeslaProducer teslaProducer;//do not assign anything to this here, or classloader issues may happen
IEnergyStorage forgeStorage;
if (CapabilityUtils.hasCapability(outputter, Capabilities.ENERGY_OUTPUTTER_CAPABILITY, side.getOpposite())
&& (strictStorage = CapabilityUtils.getCapability(outputter, Capabilities.ENERGY_STORAGE_CAPABILITY, side.getOpposite())) != null) {
double received = Math.min(strictStorage.getEnergy(), canDraw);
double toDraw = received;
if (received > 0) {
toDraw -= takeEnergy(received, true);
}
strictStorage.setEnergy(strictStorage.getEnergy() - toDraw);
if ((strictStorage = CapabilityUtils.getCapability(outputter, Capabilities.ENERGY_STORAGE_CAPABILITY, side.getOpposite())) != null) {
double received = draw(Math.min(strictStorage.getEnergy(), maxDraw));
strictStorage.setEnergy(strictStorage.getEnergy() - received);
} else if (MekanismUtils.useTesla() && (teslaProducer = CapabilityUtils.getCapability(outputter, Capabilities.TESLA_PRODUCER_CAPABILITY, side.getOpposite())) != null) {
double toDraw = TeslaIntegration.fromTesla(teslaProducer.takePower(TeslaIntegration.toTesla(canDraw), true));
if (toDraw > 0) {
toDraw -= takeEnergy(toDraw, true);
}
teslaProducer.takePower(TeslaIntegration.toTesla(toDraw), false);
double received = draw(TeslaIntegration.fromTesla(teslaProducer.takePower(TeslaIntegration.toTesla(maxDraw), true)));
teslaProducer.takePower(TeslaIntegration.toTesla(received), false);
} else if (MekanismUtils.useForge() && (forgeStorage = CapabilityUtils.getCapability(outputter, CapabilityEnergy.ENERGY, side.getOpposite())) != null) {
double toDraw = ForgeEnergyIntegration.fromForge(forgeStorage.extractEnergy(ForgeEnergyIntegration.toForge(canDraw), true));
if (toDraw > 0) {
toDraw -= takeEnergy(toDraw, true);
}
forgeStorage.extractEnergy(ForgeEnergyIntegration.toForge(toDraw), false);
double received = draw(ForgeEnergyIntegration.fromForge(forgeStorage.extractEnergy(ForgeEnergyIntegration.toForge(maxDraw), true)));
forgeStorage.extractEnergy(ForgeEnergyIntegration.toForge(received), false);
} else if (MekanismUtils.useRF() && outputter instanceof IEnergyProvider) {
IEnergyProvider rfProvider = (IEnergyProvider) outputter;
double toDraw = RFIntegration.fromRF(rfProvider.extractEnergy(side.getOpposite(), RFIntegration.toRF(canDraw), true));
if (toDraw > 0) {
toDraw -= takeEnergy(toDraw, true);
}
rfProvider.extractEnergy(side.getOpposite(), RFIntegration.toRF(toDraw), false);
double received = draw(RFIntegration.fromRF(rfProvider.extractEnergy(side.getOpposite(), RFIntegration.toRF(maxDraw), true)));
rfProvider.extractEnergy(side.getOpposite(), RFIntegration.toRF(received), false);
} else if (MekanismUtils.useIC2()) {
IEnergyTile tile = EnergyNet.instance.getSubTile(outputter.getWorld(), outputter.getPos());
if (tile instanceof IEnergySource) {
double received = Math.min(IC2Integration.fromEU(((IEnergySource) tile).getOfferedEnergy()), canDraw);
double toDraw = received;
if (received > 0) {
toDraw -= takeEnergy(received, true);
}
((IEnergySource) tile).drawEnergy(IC2Integration.toEU(toDraw));
double received = draw(Math.min(IC2Integration.fromEU(((IEnergySource) tile).getOfferedEnergy()), maxDraw));
((IEnergySource) tile).drawEnergy(IC2Integration.toEU(received));
}
}
}
@@ -130,6 +112,18 @@ public class TileEntityUniversalCable extends TileEntityTransmitter<EnergyAccept
super.update();
}
/**
* Takes a certain amount of energy and returns how much was actually taken
* @param toDraw Amount to take
* @return Amount actually taken
*/
private double draw(double toDraw) {
if (toDraw > 0) {
toDraw -= takeEnergy(toDraw, true);
}
return toDraw;
}
@Override
public void updateShare() {
if (getTransmitter().hasTransmitterNetwork() && getTransmitter().getTransmitterNetworkSize() > 0) {
@@ -259,7 +253,7 @@ public class TileEntityUniversalCable extends TileEntityTransmitter<EnergyAccept
@Override
public double getMaxEnergy() {
if (getTransmitter().hasTransmitterNetwork()) {
return getTransmitter().getTransmitterNetwork().getCapacity();
return getTransmitter().getTransmitterNetwork().getCapacityAsDouble();
}
return getCapacity();
}
@@ -281,6 +275,9 @@ public class TileEntityUniversalCable extends TileEntityTransmitter<EnergyAccept
}
}
/**
* @return Amount of left over energy
*/
public double takeEnergy(double energy, boolean doEmit) {
if (getTransmitter().hasTransmitterNetwork()) {
return getTransmitter().getTransmitterNetwork().emit(energy, doEmit);
@@ -62,7 +62,8 @@ public abstract class Transmitter<ACCEPTOR, NETWORK extends DynamicNetwork<ACCEP
@Override
public double getTransmitterNetworkCapacity() {
return hasTransmitterNetwork() ? getTransmitterNetwork().getCapacity() : getCapacity();
//This isn't *fully* accurate as the fluid and gas networks only actually support up to max int currently
return hasTransmitterNetwork() ? getTransmitterNetwork().getCapacityAsDouble() : getCapacity();
}
@Override
@@ -71,29 +71,19 @@ public class EnergyNetwork extends DynamicNetwork<EnergyAcceptorWrapper, EnergyN
@Override
public void clampBuffer() {
if (buffer.amount > getCapacity()) {
buffer.amount = getCapacity();
if (buffer.amount > getCapacityAsDouble()) {
buffer.amount = getCapacityAsDouble();
}
if (buffer.amount < 0) {
buffer.amount = 0;
}
}
@Override
protected void updateMeanCapacity() {
int numCables = transmitters.size();
double reciprocalSum = 0;
for (IGridTransmitter<EnergyAcceptorWrapper, EnergyNetwork, EnergyStack> cable : transmitters) {
reciprocalSum += 1.0 / (double) cable.getCapacity();
}
meanCapacity = (double) numCables / reciprocalSum;
}
public double getEnergyNeeded() {
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
return 0;
}
return getCapacity() - buffer.amount;
return getCapacityAsDouble() - buffer.amount;
}
private double tickEmit(double energyToSend) {
@@ -160,7 +150,7 @@ public class EnergyNetwork extends DynamicNetwork<EnergyAcceptorWrapper, EnergyN
}
public double getPowerScale() {
return Math.max(jouleBufferLastTick == 0 ? 0 : Math.min(Math.ceil(Math.log10(getPower()) * 2) / 10, 1), getCapacity() == 0 ? 0 : buffer.amount / getCapacity());
return Math.max(jouleBufferLastTick == 0 ? 0 : Math.min(Math.ceil(Math.log10(getPower()) * 2) / 10, 1), getCapacityAsDouble() == 0 ? 0 : buffer.amount / getCapacityAsDouble());
}
public void clearJoulesTransmitted() {
@@ -96,20 +96,10 @@ public class FluidNetwork extends DynamicNetwork<IFluidHandler, FluidNetwork, Fl
@Override
public void clampBuffer() {
if (buffer != null && buffer.amount > getCapacity()) {
buffer.amount = capacity;
buffer.amount = getCapacity();
}
}
@Override
protected void updateMeanCapacity() {
int numCables = transmitters.size();
double sum = 0;
for (IGridTransmitter<IFluidHandler, FluidNetwork, FluidStack> pipe : transmitters) {
sum += pipe.getCapacity();
}
meanCapacity = sum / (double) numCables;
}
public int getFluidNeeded() {
return getCapacity() - (buffer != null ? buffer.amount : 0);
}
@@ -112,7 +112,7 @@ public class GasNetwork extends DynamicNetwork<IGasHandler, GasNetwork, GasStack
@Override
public void clampBuffer() {
if (buffer != null && buffer.amount > getCapacity()) {
buffer.amount = capacity;
buffer.amount = getCapacity();
}
}
@@ -55,6 +55,11 @@ public class HeatNetwork extends DynamicNetwork<IHeatTransfer, HeatNetwork, Void
public void clampBuffer() {
}
@Override
public void updateCapacity() {
//The capacity is always zero so no point in doing calculations.
}
@Override
public void onUpdate() {
super.onUpdate();
@@ -65,12 +70,14 @@ public class HeatNetwork extends DynamicNetwork<IHeatTransfer, HeatNetwork, Void
if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
for (IGridTransmitter<IHeatTransfer, HeatNetwork, Void> transmitter : transmitters) {
if (transmitter instanceof TransmitterImpl && ((TransmitterImpl) transmitter).getTileEntity().hasCapability(Capabilities.HEAT_TRANSFER_CAPABILITY, null)) {
if (transmitter instanceof TransmitterImpl) {
IHeatTransfer heatTransmitter = (IHeatTransfer) ((TransmitterImpl) transmitter).getTileEntity().getCapability(Capabilities.HEAT_TRANSFER_CAPABILITY, null);
double[] d = heatTransmitter.simulateHeat();
newHeatTransferred += d[0];
newHeatLost += d[1];
newSumTemp += heatTransmitter.applyTemperatureChange();
if (heatTransmitter != null) {
double[] d = heatTransmitter.simulateHeat();
newHeatTransferred += d[0];
newHeatLost += d[1];
newSumTemp += heatTransmitter.applyTemperatureChange();
}
}
}
}
@@ -78,6 +78,11 @@ public class InventoryNetwork extends DynamicNetwork<TileEntity, InventoryNetwor
public void clampBuffer() {
}
@Override
public void updateCapacity() {
//The capacity is always zero so no point in doing calculations.
}
@Override
public String toString() {
return "[InventoryNetwork] " + transmitters.size() + " transmitters, " + possibleAcceptors.size() + " acceptors.";