fix of diode's strange behavior (#10454)
This commit is contained in:
@@ -62,20 +62,23 @@ public class PowerDiode extends Block{
|
|||||||
PowerGraph frontGraph = front().power.graph;
|
PowerGraph frontGraph = front().power.graph;
|
||||||
if(backGraph == frontGraph) return;
|
if(backGraph == frontGraph) return;
|
||||||
|
|
||||||
// 0f - 1f of battery capacity in use
|
float backStored = backGraph.getBatteryStored();
|
||||||
float backStored = backGraph.getBatteryStored() / backGraph.getTotalBatteryCapacity();
|
float backCapacity = backGraph.getTotalBatteryCapacity();
|
||||||
float frontStored = frontGraph.getBatteryStored() / frontGraph.getTotalBatteryCapacity();
|
float frontStored = frontGraph.getBatteryStored();
|
||||||
|
float frontCapacity = frontGraph.getTotalBatteryCapacity();
|
||||||
|
|
||||||
// try to send if the back side has more % capacity stored than the front side
|
if(backStored/backCapacity <= frontStored/frontCapacity) return;
|
||||||
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);
|
float targetPercentage = (frontStored + backStored) / (frontCapacity + backCapacity);
|
||||||
frontGraph.transferPower(amount);
|
|
||||||
}
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user