Revert to the old buffer handling, reopens #18

This commit is contained in:
malte0811 2017-12-08 19:55:02 +01:00
parent 37233621bf
commit 8ff35f0ea4
2 changed files with 61 additions and 20 deletions

View file

@ -7,6 +7,8 @@ public final class DummyTEs {
TileEntityIC2ConnectorTin newTe = getRaw();
newTe.inBuffer = inBuffer;
newTe.outBuffer = outBuffer;
newTe.maxToNet = maxToNet;
newTe.maxToMachine = maxToMachine;
newTe.relay = relay;
newTe.facing = facing;
world.setTileEntity(pos, newTe);

View file

@ -26,6 +26,7 @@ import blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Conn
import blusunrize.immersiveengineering.api.energy.wires.TileEntityImmersiveConnectable;
import blusunrize.immersiveengineering.api.energy.wires.WireType;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IDirectionalTile;
import blusunrize.immersiveengineering.common.util.Utils;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergyAcceptor;
@ -38,6 +39,7 @@ import malte0811.industrialWires.blocks.IBlockBoundsIW;
import malte0811.industrialWires.wires.IC2Wiretype;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.AxisAlignedBB;
@ -46,6 +48,7 @@ import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Optional;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import reborncore.api.power.IEnergyInterfaceTile;
import javax.annotation.Nonnull;
import java.util.HashMap;
@ -59,14 +62,15 @@ import java.util.Set;
})
public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable implements IEnergySource, IEnergySink, IDirectionalTile,
ITickable, IIC2Connector, IBlockBoundsIW {
private static final double MIN_ENERGY = 1e-5;
EnumFacing facing = EnumFacing.NORTH;
boolean relay;
private boolean first = true;
//IC2 net to IE net buffer
double inBuffer = 0;
double maxToNet = 0;
//IE net to IC2 net buffer
double outBuffer = 0;
double maxToMachine = 0;
double maxStored = IC2Wiretype.IC2_TYPES[0].getTransferRate() / 8;
int tier = 1;
@ -85,44 +89,54 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
first = false;
}
if (!world.isRemote) {
if (inBuffer > 2*MIN_ENERGY) {
if (inBuffer > .1) {
transferPower();
}
if (outBuffer>.1&&IndustrialWires.hasTechReborn) {
TileEntity output = Utils.getExistingTileEntity(world, pos.offset(facing));
if (output instanceof IEnergyInterfaceTile) {
IEnergyInterfaceTile out = (IEnergyInterfaceTile) output;
if (out.canAcceptEnergy(facing.getOpposite())) {
outBuffer -= out.addEnergy(Math.min(outBuffer, maxToMachine));
}
}
}
}
}
private void transferPower() {
Set<AbstractConnection> conns = new HashSet<>(ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(pos, world));
Map<AbstractConnection, Pair<IIC2Connector, Double>> maxOutputs = new HashMap<>();
double outputMax = Math.min(inBuffer, maxToNet);
double sum = 0;
for (AbstractConnection c : conns) {
IImmersiveConnectable iic = ApiUtils.toIIC(c.end, world);
if (iic instanceof IIC2Connector) {
double tmp = inBuffer - ((IIC2Connector) iic).insertEnergy(inBuffer, true);
if (tmp > MIN_ENERGY) {
double tmp = outputMax - ((IIC2Connector) iic).insertEnergy(outputMax, true);
if (tmp > .00000001) {
maxOutputs.put(c, new ImmutablePair<>((IIC2Connector) iic, tmp));
sum += tmp;
}
}
}
if (sum < MIN_ENERGY) {
if (sum < .0001) {
return;
}
final double oldInBuf = inBuffer;
final double oldInBuf = outputMax;
HashMap<Connection, Integer> transferedPerConn = ImmersiveNetHandler.INSTANCE.getTransferedRates(world.provider.getDimension());
for (AbstractConnection c : maxOutputs.keySet()) {
Pair<IIC2Connector, Double> p = maxOutputs.get(c);
double out = oldInBuf * p.getRight() / Math.max(sum, oldInBuf);
double out = oldInBuf * p.getRight() / sum;
double loss = getAverageLossRate(c);
double inserted = out - p.getLeft().insertEnergy(out - loss, false);
this.inBuffer -= inserted;
inBuffer -= inserted;
float intermediaryLoss = 0;
HashSet<IImmersiveConnectable> passedConnectors = new HashSet<>();
double energyAtConn = inserted + loss;
for (Connection sub : c.subConnections) {
int transferredPerCon = transferedPerConn.getOrDefault(sub, 0);
energyAtConn -= sub.cableType.getLossRatio() * sub.length;
transferedPerConn.put(sub, (int) (transferredPerCon + energyAtConn));
ImmersiveNetHandler.INSTANCE.getTransferedRates(world.provider.getDimension()).put(sub, (int) (transferredPerCon + energyAtConn));
IImmersiveConnectable subStart = ApiUtils.toIIC(sub.start, world);
IImmersiveConnectable subEnd = ApiUtils.toIIC(sub.end, world);
if (subStart != null && passedConnectors.add(subStart))
@ -141,10 +155,18 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
return f;
}
//Input through the IE net
//Input through the net
@Override
public double insertEnergy(double eu, boolean simulate) {
final double insert = simulate?Math.min(Math.max(0, eu-outBuffer), maxStored-outBuffer):eu;
final double insert = Math.min(maxStored - outBuffer, eu);
if (insert > 0) {
if (outBuffer < maxToMachine) {
maxToMachine = outBuffer;
}
if (eu > maxToMachine) {
maxToMachine = eu;
}
}
if (!simulate) {
outBuffer += insert;
}
@ -219,8 +241,8 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
@Override
@Optional.Method(modid="ic2")
public double getDemandedEnergy() {
double ret = maxStored;
if (ret-inBuffer < MIN_ENERGY)
double ret = maxStored + .5 - inBuffer;
if (ret < .1)
ret = 0;
return ret;
}
@ -234,17 +256,18 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
@Override
@Optional.Method(modid="ic2")
public double injectEnergy(EnumFacing directionFrom, double amount, double voltage) {
double consume = Math.min(Math.max(0, amount-inBuffer), maxStored-inBuffer);
if (consume>0) {
inBuffer += consume;
if (inBuffer < maxStored) {
addToIn(amount);
markDirty();
return 0;
}
return amount-consume-amount*1e-9;
return amount;
}
@Override
@Optional.Method(modid="ic2")
public double getOfferedEnergy() {
return outBuffer;
return Math.min(maxToMachine, outBuffer);
}
@Override
@ -260,6 +283,16 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
return tier;
}
private void addToIn(double amount) {
if (inBuffer < maxToNet) {
maxToNet = inBuffer;
}
inBuffer += amount;
if (amount > maxToNet) {
maxToNet = amount;
}
}
@Override
public void readCustomNBT(NBTTagCompound nbt, boolean descPacket) {
super.readCustomNBT(nbt, descPacket);
@ -268,10 +301,14 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
inBuffer = nbt.getDouble("inBuffer");
outBuffer = nbt.getDouble("outBuffer");
if (nbt.hasKey("maxToNet")) {
inBuffer = Math.min(nbt.getDouble("maxToNet"), inBuffer);
maxToNet = nbt.getDouble("maxToNet");
} else {
maxToNet = inBuffer;
}
if (nbt.hasKey("maxToMachine")) {
outBuffer = Math.min(nbt.getDouble("maxToMachine"), outBuffer);
maxToMachine = nbt.getDouble("maxToMachine");
} else {
maxToMachine = outBuffer;
}
}
@ -282,6 +319,8 @@ public class TileEntityIC2ConnectorTin extends TileEntityImmersiveConnectable im
nbt.setBoolean("relay", relay);
nbt.setDouble("inBuffer", inBuffer);
nbt.setDouble("outBuffer", outBuffer);
nbt.setDouble("maxToNet", maxToNet);
nbt.setDouble("maxToMachine", maxToMachine);
}
@Nonnull