Skip to content

Commit

Permalink
fix of diode's strange behavior (#10454)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxiron794 authored Feb 4, 2025
1 parent 5f48ba8 commit aa8e3b4
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions core/src/mindustry/world/blocks/power/PowerDiode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit aa8e3b4

Please sign in to comment.