Skip to content

Commit

Permalink
Change transformers to no longer be worse than accumulators (#5985)
Browse files Browse the repository at this point in the history
* First part of limiting transformer throughput

 - Limits transformer throughput to lower wire
 - Currently wastes excess power passed through
 - Intent is to make transformers competitive with accumulators, which make burn impossible

* Basic implementation of the idea

 - Output is limited
 - Currently, to fix current transformer we backsearch the connection to update all connections
 - There are almost certainly better ways to do this, don't use this way, this just proves I can do it

* Better implementation of the idea

 - No reverse searches, only two scans
 - Most of the association work is done at setup
 - Back-transfer now handled elegantly

* Simplify limit to return double

* Minor fix to change back

* Second minor reversion to previous code

* Third minor reversion of changed code

* Fix star import
  • Loading branch information
voidsong-dragonfly authored Jun 30, 2024
1 parent f08fe57 commit 090137a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
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;
}
}

0 comments on commit 090137a

Please sign in to comment.