From aa8e3b4a37949c48061677ea37df1406f8e27d96 Mon Sep 17 00:00:00 2001 From: Maxiron794 <56217284+Maxiron794@users.noreply.github.com> Date: Tue, 4 Feb 2025 19:32:52 +0100 Subject: [PATCH] fix of diode's strange behavior (#10454) --- .../world/blocks/power/PowerDiode.java | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/core/src/mindustry/world/blocks/power/PowerDiode.java b/core/src/mindustry/world/blocks/power/PowerDiode.java index 5fa2e1ba5087..1dc0f016c381 100644 --- a/core/src/mindustry/world/blocks/power/PowerDiode.java +++ b/core/src/mindustry/world/blocks/power/PowerDiode.java @@ -62,20 +62,23 @@ public void updateTile(){ PowerGraph frontGraph = front().power.graph; if(backGraph == frontGraph) return; - // 0f - 1f of battery capacity in use - float backStored = backGraph.getBatteryStored() / backGraph.getTotalBatteryCapacity(); - float frontStored = frontGraph.getBatteryStored() / frontGraph.getTotalBatteryCapacity(); - - // try to send if the back side has more % capacity stored than the front side - if(backStored > frontStored){ - // send half of the difference - float amount = backGraph.getBatteryStored() * (backStored - frontStored) / 2; - // prevent sending more than the front can handle - amount = Mathf.clamp(amount, 0, frontGraph.getTotalBatteryCapacity() * (1 - frontStored)); - - backGraph.transferPower(-amount); - frontGraph.transferPower(amount); - } + float backStored = backGraph.getBatteryStored(); + float backCapacity = backGraph.getTotalBatteryCapacity(); + float frontStored = frontGraph.getBatteryStored(); + float frontCapacity = frontGraph.getTotalBatteryCapacity(); + + if(backStored/backCapacity <= frontStored/frontCapacity) return; + + float targetPercentage = (frontStored + backStored) / (frontCapacity + backCapacity); + + // send half of the difference + float amount = (targetPercentage * frontCapacity - frontStored) / 2; + + // prevent sending more than the front can handle + amount = Mathf.clamp(amount, 0, frontCapacity - frontStored); + + backGraph.transferPower(-amount); + frontGraph.transferPower(amount); } } -} +} \ No newline at end of file