This commit is contained in:
Patrick 'Quezler' Mounier 2019-10-31 20:41:48 +01:00
parent 4c5e30dbcc
commit 0c14854519
No known key found for this signature in database
GPG Key ID: 0D6CA7326C76D8EA
2 changed files with 30 additions and 23 deletions

View File

@ -31,28 +31,30 @@ public class PowerDiode extends Block{
if(!back.block().hasPower || !front.block().hasPower) return;
PowerGraph backGraph = back.entity.power.graph;
PowerGraph frontGraph = front.entity.power.graph;
if(backGraph == frontGraph) return;
// skip if the receiving graph is already full
if(frontGraph.getBatteryStored() == frontGraph.getTotalBatteryCapacity()) return;
// half the difference
float send = Mathf.clamp((backGraph.getBatteryStored() - frontGraph.getBatteryStored()) / 2, 0f, Integer.MAX_VALUE);
// send all overflow if all batteries of the sending graph are full
if(backGraph.getBatteryStored() == backGraph.getTotalBatteryCapacity()){
send += backGraph.getLastPowerProduced() - backGraph.getPowerNeeded();
}
// limit offering to space available
send = Mathf.clamp(send, 0f, frontGraph.getTotalBatteryCapacity() - frontGraph.getBatteryStored());
// limit to sendable power
send = Mathf.clamp(send, 0f, backGraph.getBatteryStored());
if (send == 0f) return;
backGraph.useBatteries(send);
frontGraph.chargeBatteries(send);
// backGraph
// if(backGraph == frontGraph) return;
//
// // skip if the receiving graph is already full
// if(frontGraph.getBatteryStored() == frontGraph.getTotalBatteryCapacity()) return;
//
// // half the difference
// float send = Mathf.clamp((backGraph.getBatteryStored() - frontGraph.getBatteryStored()) / 2, 0f, Integer.MAX_VALUE);
//
// // send all overflow if all batteries of the sending graph are full
// if(backGraph.getBatteryStored() == backGraph.getTotalBatteryCapacity()){
// send += backGraph.getLastPowerProduced() - backGraph.getPowerNeeded();
// }
//
// // limit offering to space available
// send = Mathf.clamp(send, 0f, frontGraph.getTotalBatteryCapacity() - frontGraph.getBatteryStored());
//
// // limit to sendable power
// send = Mathf.clamp(send, 0f, backGraph.getBatteryStored());
//
// if (send == 0f) return;
// backGraph.useBatteries(send);
// frontGraph.chargeBatteries(send);
}
@Override
@ -82,4 +84,6 @@ public class PowerDiode extends Block{
if(rotation == 3) return world.ltile(x, y - 1);
return null;
}
}

View File

@ -26,6 +26,9 @@ public class PowerGraph{
private final int graphID;
private static int lastGraphID;
public float diodeTo = 0f;
public float diodeFrom = 0f;
{
graphID = lastGraphID++;
}
@ -56,7 +59,7 @@ public class PowerGraph{
}
public float getPowerProduced(){
float powerProduced = 0f;
float powerProduced = diodeTo;
for(Tile producer : producers){
if(producer.entity == null) continue;
powerProduced += producer.block().getPowerProduction(producer) * producer.entity.delta();
@ -65,7 +68,7 @@ public class PowerGraph{
}
public float getPowerNeeded(){
float powerNeeded = 0f;
float powerNeeded = diodeFrom;
for(Tile consumer : consumers){
Consumers consumes = consumer.block().consumes;
if(consumes.hasPower()){