cleaned up PowerGraph
This commit is contained in:
@@ -31,18 +31,15 @@ public class PowerGraph{
|
||||
return graphID;
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 || producers.size == 0){
|
||||
return;
|
||||
}
|
||||
|
||||
lastFrameUpdated = threads.getFrameID();
|
||||
|
||||
public float getPowerProduced(){
|
||||
float powerProduced = 0f;
|
||||
for(Tile producer : producers){
|
||||
totalInput += producer.block().getPowerProduction(producer);
|
||||
}
|
||||
return powerProduced;
|
||||
}
|
||||
|
||||
public float getPowrerNeeded(){
|
||||
float powerNeeded = 0f;
|
||||
for(Tile consumer : consumers){
|
||||
if(consumer.block().bufferedPowerConsumer){
|
||||
@@ -51,39 +48,70 @@ public class PowerGraph{
|
||||
powerNeeded += consumer.block().basePowerUse + consumer.entity.power.extraUse;
|
||||
}
|
||||
}
|
||||
return powerNeeded;
|
||||
}
|
||||
|
||||
public float getBatteryStored(){
|
||||
float totalAccumulator = 0f;
|
||||
float totalCapacity = 0f;
|
||||
for(Tile battery : batteries){
|
||||
totalAccumulator += battery.entity.power.satisfaction * battery.block().basePowerUse;
|
||||
}
|
||||
}
|
||||
|
||||
public float getBatteryCapacity(){
|
||||
float totalCapacity = 0f;
|
||||
for(Tile battery : batteries){
|
||||
totalCapacity += (1f - battery.entity.power.satisfaction) * battery.block().basePowerUse;
|
||||
}
|
||||
}
|
||||
|
||||
public float useBatteries(float needed){
|
||||
float stored = getBatteryStored();
|
||||
float used = Math.min(stored, needed);
|
||||
float thing = 1f - (used / stored);
|
||||
for(Tile battery : batteries){
|
||||
battery.entity.power.satisfaction *= thing;
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
public float chargeBatteries(float excess){
|
||||
float capacity = getBatteryCapacity();
|
||||
float thing = Math.min(1, excess / capacity);
|
||||
for(tile battery : batteries){
|
||||
battery.power.satisfaction += (1 - battery.power.satisfaction) * thing;
|
||||
}
|
||||
return Math.min(excess, capacity);
|
||||
}
|
||||
|
||||
public void distributePower(float needed, float produced){
|
||||
float satisfaction = Math.min(1, produced / needed);
|
||||
for(Tile consumer : consumers){
|
||||
if(consumer.block().bufferedPowerConsumer){
|
||||
consumer.power.satisfaction += (1 - consumer.power.satisfaction) * satisfaction;
|
||||
}else{
|
||||
consumer.power.satisfaction = satisfaction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void update(){
|
||||
if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 || producers.size == 0){
|
||||
return;
|
||||
}
|
||||
|
||||
lastFrameUpdated = threads.getFrameID();
|
||||
|
||||
float powerNeeded = getPowrerNeeded();
|
||||
float powerProduced = getPowerProduced();
|
||||
|
||||
if(powerNeeded > powerProduced){
|
||||
float accumulatorUsed = Math.min(totalAccumulator, powerNeeded - powerProduced);
|
||||
float thing = 1f - (accumulatorUsed / totalAccumulator);
|
||||
for(Tile battery : batteries){
|
||||
battery.entity.power.satisfaction *= thing;
|
||||
}
|
||||
powerProduced += accumulatorUsed;
|
||||
powerProduced += useBatteries(powerNeeded - powerProduced);
|
||||
}else if(powerProduced > powerNeeded){
|
||||
powerProduced -= chargeBatteries(powerProduced - powerNeeded);
|
||||
}
|
||||
|
||||
float powerSatisfaction = Math.min(1, powerProduced / powerNeeded);
|
||||
for(Tile consumer : producers){
|
||||
if(consumer.block().bufferedPowerConsumer){
|
||||
consumer.power.satisfaction += (1 - consumer.power.satisfaction) * powerSatisfaction;
|
||||
}else{
|
||||
consumer.power.satisfaction = powerSatisfaction;
|
||||
}
|
||||
}
|
||||
|
||||
if(powerProduced > powerNeeded){
|
||||
powerProduced -= powerNeeded;
|
||||
float thing = Math.min(1, powerProduced / totalCapacity);
|
||||
for(tile battery : batteries){
|
||||
battery.power.satisfaction += (1 - battery.power.satisfaction) * thing;
|
||||
}
|
||||
}
|
||||
distributePower(powerNeeded, powerProduced);
|
||||
}
|
||||
|
||||
public void add(PowerGraph graph){
|
||||
|
||||
Reference in New Issue
Block a user