Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Transformers Being Worse Than Accumulators #5985

Merged
merged 8 commits into from
Jun 30, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class EnergyTransferHandler extends LocalNetworkHandler implements IWorld
private final Map<ConnectionPoint, EnergyConnector> sinks = new HashMap<>();
private final List<SinkPathsFromSource> transferPaths = new ArrayList<>();
private boolean sourceSinkMapInitialized = true;
HashMap<Connection, List<Double>> limits = new HashMap<>();

public EnergyTransferHandler(LocalWireNetwork net, GlobalWireNetwork global)
{
Expand Down Expand Up @@ -119,6 +120,7 @@ private void reset()
sources.clear();
transferPaths.clear();
sourceSinkMapInitialized = false;
limits.clear();;
}

public Map<ConnectionPoint, EnergyConnector> getSources()
Expand Down Expand Up @@ -163,6 +165,9 @@ private void updateSourcesAndSinks()
sinks.put(cp, energyIIC);
if(energyIIC.isSource(cp))
sources.put(cp, energyIIC);
if(energyIIC instanceof LimitingEnergyConnector limiting)
for(Connection c : localNet.getConnections(cp))
limits.put(c, Arrays.asList(limiting.getPowerLimit(), limiting.getPowerLimit()));
}
}
for(Entry<ConnectionPoint, EnergyConnector> source : sources.entrySet())
Expand Down Expand Up @@ -220,6 +225,7 @@ private void runDijkstraWithSource(ConnectionPoint source, Consumer<Path> output
private void transferPower()
{
updateSourcesAndSinks();
resetLimits();
for(SinkPathsFromSource sourceData : transferPaths)
{
ConnectionPoint sourceCp = sourceData.sourceCP();
Expand All @@ -234,11 +240,21 @@ record OutputData(double amount, Path path, EnergyConnector output)
List<OutputData> maxOut = new ArrayList<>(sourceData.paths().size());
for(SinkPath sinkEntry : sourceData.paths())
{
double limit = Double.MAX_VALUE;
Connection conn = null;
for(Connection c : sinkEntry.pathTo().conns)
if (limits.containsKey(c))
{
limit = limits.get(c).get(1);
conn = c;
}
EnergyConnector sink = sinkEntry.sinkConnector();
int requested = sink.getRequestedEnergy();
int requested = (int)Math.min(sink.getRequestedEnergy(), limit*(1-sinkEntry.pathTo().loss));
if(requested <= 0)
continue;
double requiredAtSource = Math.min(requested/(1-sinkEntry.pathTo().loss), available);
if(conn!=null)
limits.put(conn, Arrays.asList(limits.get(conn).get(0), limit-requiredAtSource));
maxOut.add(new OutputData(requiredAtSource, sinkEntry.pathTo(), sink));
maxSum += requiredAtSource;
}
Expand Down Expand Up @@ -304,6 +320,12 @@ else if(c.type instanceof IEnergyWire energyWire)
return Double.POSITIVE_INFINITY;
}

private void resetLimits()
{
if (limits.isEmpty()) return;
limits.replaceAll((p, v) -> Arrays.asList(limits.get(p).get(0), limits.get(p).get(0)));
}

public static class Path
{
public final Connection[] conns;
Expand Down Expand Up @@ -410,6 +432,11 @@ default void onEnergyPassedThrough(double amount)
}
}

public interface LimitingEnergyConnector extends EnergyConnector
{
double getPowerLimit();
}

private record SinkPath(ConnectionPoint sinkCP, EnergyConnector sinkConnector, Path pathTo)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import blusunrize.immersiveengineering.api.wires.ConnectionPoint;
import blusunrize.immersiveengineering.api.wires.IImmersiveConnectable;
import blusunrize.immersiveengineering.api.wires.WireType;
import blusunrize.immersiveengineering.api.wires.localhandlers.EnergyTransferHandler.IEnergyWire;
import blusunrize.immersiveengineering.api.wires.localhandlers.EnergyTransferHandler.LimitingEnergyConnector;
import blusunrize.immersiveengineering.api.wires.utils.WireUtils;
import blusunrize.immersiveengineering.common.blocks.IEBlockInterfaces.IStateBasedDirectional;
import blusunrize.immersiveengineering.common.blocks.generic.ImmersiveConnectableBlockEntity;
Expand All @@ -36,7 +38,7 @@
import static blusunrize.immersiveengineering.api.wires.WireType.MV_CATEGORY;

public abstract class AbstractTransformerBlockEntity extends ImmersiveConnectableBlockEntity
implements IStateBasedDirectional
implements IStateBasedDirectional, LimitingEnergyConnector
{
protected static final int RIGHT_INDEX = 0;
protected static final int LEFT_INDEX = 1;
Expand Down Expand Up @@ -75,6 +77,12 @@ public String getHigherWiretype()
return MV_CATEGORY;
}

@Override
public double getPowerLimit() {
if (leftType==null||rightType==null) return 0;
return Math.min(((IEnergyWire)leftType).getTransferRate(), ((IEnergyWire)rightType).getTransferRate());
}

@Override
public Collection<ConnectionPoint> getConnectionPoints()
{
Expand Down Expand Up @@ -177,4 +185,16 @@ public boolean canConnect()
protected void updateMirrorState()
{
}

@Override
public boolean isSource(ConnectionPoint cp)
{
return false;
}

@Override
public boolean isSink(ConnectionPoint cp)
{
return false;
}
}
Loading