Implemented conservative generators / Closes #804

This commit is contained in:
Anuken 2019-12-12 16:28:02 -05:00
parent 1ae20553ff
commit 01e3912827
2 changed files with 15 additions and 4 deletions

View File

@ -120,7 +120,7 @@ public class ItemLiquidGenerator extends PowerGenerator{
float maximumPossible = maxLiquidGenerate * calculationDelta;
float used = Math.min(entity.liquids.get(liquid) * calculationDelta, maximumPossible);
entity.liquids.remove(liquid, used);
entity.liquids.remove(liquid, used * entity.power.graph.getUsageFraction());
entity.productionEfficiency = baseLiquidEfficiency * used / maximumPossible;
if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){
@ -137,7 +137,7 @@ public class ItemLiquidGenerator extends PowerGenerator{
}
if(entity.generateTime > 0f){
entity.generateTime -= Math.min(1f / itemDuration * entity.delta(), entity.generateTime);
entity.generateTime -= Math.min(1f / itemDuration * entity.delta() * entity.power.graph.getUsageFraction(), entity.generateTime);
if(randomlyExplode && state.rules.reactorExplosions && Mathf.chance(entity.delta() * 0.06 * Mathf.clamp(entity.explosiveness - 0.5f))){
//this block is run last so that in the event of a block destruction, no code relies on the block type

View File

@ -19,7 +19,7 @@ public class PowerGraph{
private final ObjectSet<Tile> all = new ObjectSet<>();
private final WindowedMean powerBalance = new WindowedMean(60);
private float lastPowerProduced, lastPowerNeeded;
private float lastPowerProduced, lastPowerNeeded, lastUsageFraction;
private long lastFrameUpdated = -1;
private final int graphID;
@ -54,6 +54,10 @@ public class PowerGraph{
return Mathf.clamp(lastPowerProduced / lastPowerNeeded);
}
public float getUsageFraction(){
return lastUsageFraction;
}
public float getPowerProduced(){
float powerProduced = 0f;
for(Tile producer : producers){
@ -180,7 +184,7 @@ public class PowerGraph{
tile.entity.power.status = 1f;
}
lastPowerNeeded = lastPowerProduced = 1f;
lastPowerNeeded = lastPowerProduced = lastUsageFraction = 1f;
return;
}
@ -188,6 +192,7 @@ public class PowerGraph{
float powerNeeded = getPowerNeeded();
float powerProduced = getPowerProduced();
float rawProduced = powerProduced;
lastPowerNeeded = powerNeeded;
lastPowerProduced = powerProduced;
@ -208,6 +213,12 @@ public class PowerGraph{
}
powerBalance.addValue((lastPowerProduced - lastPowerNeeded) / Time.delta());
//overproducing: 10 / 20 = 0.5
//underproducing: 20 / 10 = 2 -> clamp -> 1.0
//nothing being produced: 20 / 0 -> 1.0
//nothing being consumed: 0 / 20 -> 0.0
lastUsageFraction = Mathf.zero(rawProduced) ? 1f : Mathf.clamp(powerNeeded / rawProduced);
}
public void add(PowerGraph graph){