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