diff --git a/src/api/java/blusunrize/immersiveengineering/api/wires/localhandlers/EnergyTransferHandler.java b/src/api/java/blusunrize/immersiveengineering/api/wires/localhandlers/EnergyTransferHandler.java index 1577acbb23..747fece03a 100644 --- a/src/api/java/blusunrize/immersiveengineering/api/wires/localhandlers/EnergyTransferHandler.java +++ b/src/api/java/blusunrize/immersiveengineering/api/wires/localhandlers/EnergyTransferHandler.java @@ -41,7 +41,7 @@ public class EnergyTransferHandler extends LocalNetworkHandler implements IWorld private final Map sinks = new HashMap<>(); private final List transferPaths = new ArrayList<>(); private boolean sourceSinkMapInitialized = true; - Object2DoubleOpenHashMap connectionsToLimits = new Object2DoubleOpenHashMap<>(); + HashMap> limits = new HashMap<>(); public EnergyTransferHandler(LocalWireNetwork net, GlobalWireNetwork global) { @@ -120,7 +120,7 @@ private void reset() sources.clear(); transferPaths.clear(); sourceSinkMapInitialized = false; - connectionsToLimits.clear();; + limits.clear();; } public Map getSources() @@ -165,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((double)limiting.getPowerLimit(), (double)limiting.getPowerLimit())); } } for(Entry source : sources.entrySet()) @@ -222,9 +225,9 @@ private void runDijkstraWithSource(ConnectionPoint source, Consumer output private void transferPower() { updateSourcesAndSinks(); + resetLimits(); for(SinkPathsFromSource sourceData : transferPaths) { - connectionsToLimits.clear(); ConnectionPoint sourceCp = sourceData.sourceCP(); EnergyConnector source = sourceData.sourceConnector(); int available = source.getAvailableEnergy(); @@ -237,11 +240,21 @@ record OutputData(double amount, Path path, EnergyConnector output) List 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; } @@ -264,35 +277,9 @@ record OutputData(double amount, Path path, EnergyConnector output) IImmersiveConnectable iic = localNet.getConnector(currentPoint); if(!currentPoint.equals(path.end) && iic instanceof EnergyConnector connector) connector.onEnergyPassedThrough(availableRecent); - // This is so transformers can limit total power running through them - if (iic instanceof LimitingEnergyConnector limiting && transferredNextTick != null) - { - // Get the limit and update the connection - double limit = (Math.max(limiting.getPowerLimit()-transferredNextTick.getDouble(c), 0)/availableFactor); - if ((int)limit<(int)availableRecent) - { - double factor = 1; - connectionsToLimits.put(c, limit); - for(int back=0;back Arrays.asList(limits.get(p).get(0), limits.get(p).get(0))); + } + public static class Path { public final Connection[] conns; diff --git a/src/main/java/blusunrize/immersiveengineering/common/blocks/metal/AbstractTransformerBlockEntity.java b/src/main/java/blusunrize/immersiveengineering/common/blocks/metal/AbstractTransformerBlockEntity.java index 820f493372..313171e182 100644 --- a/src/main/java/blusunrize/immersiveengineering/common/blocks/metal/AbstractTransformerBlockEntity.java +++ b/src/main/java/blusunrize/immersiveengineering/common/blocks/metal/AbstractTransformerBlockEntity.java @@ -76,6 +76,7 @@ public String getHigherWiretype() @Override public int getPowerLimit() { + if (leftType==null||rightType==null) return 0; return Math.min(((IEnergyWire)leftType).getTransferRate(), ((IEnergyWire)rightType).getTransferRate()); }