From cb69eb7c0f8f401ca22d336476fb7cd5b3ce1a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baltaz=C3=A1r=20Radics?= Date: Tue, 23 Oct 2018 04:47:44 +0200 Subject: [PATCH] Simplified power logic (#261) * Simplified power logic * Made the requested changes --- .../world/blocks/power/PowerGraph.java | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java index 957d5ee250..1cd1cf785a 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java @@ -5,7 +5,6 @@ import com.badlogic.gdx.utils.IntSet; import com.badlogic.gdx.utils.ObjectSet; import com.badlogic.gdx.utils.Queue; import io.anuke.mindustry.world.Tile; -import io.anuke.ucore.core.Timers; import static io.anuke.mindustry.Vars.threads; @@ -39,37 +38,27 @@ public class PowerGraph{ lastFrameUpdated = threads.getFrameID(); float totalInput = 0f; - for(Tile producer : producers){ totalInput += producer.entity.power.amount; } + float maxOutput = 0f; + for(Tile consumer : consumers){ + maxOutput += consumer.block().powerCapacity - consumer.entity.power.amount; + } + + if (totalInput <= 0.0001f || maxOutput <= 0.0001f) { + return; + } + + float inputUsed = Math.min(maxOutput / totalInput, 1f); for(Tile producer : producers){ - float accumulator = producer.entity.power.amount; + producer.entity.power.amount -= producer.entity.power.amount * inputUsed; + } - if(accumulator <= 0.0001f) continue; - - float toEach = accumulator / consumers.size; - float outputs = 0f; - - for(Tile tile : consumers){ - outputs += Math.min(tile.block().powerCapacity - tile.entity.power.amount, toEach) / toEach; - } - - float finalEach = toEach / outputs * Timers.delta(); - float buffer = 0f; - - if(Float.isNaN(finalEach) || Float.isInfinite(finalEach)){ - continue; - } - - for(Tile tile : consumers){ - float used = Math.min(tile.block().powerCapacity - tile.entity.power.amount, finalEach) * accumulator / totalInput; - buffer += used; - tile.entity.power.amount += used; - } - - producer.entity.power.amount -= buffer; + float outputSatisfied = Math.min(totalInput / maxOutput, 1f); + for(Tile consumer : consumers){ + consumer.entity.power.amount += (consumer.block().powerCapacity - consumer.entity.power.amount) * outputSatisfied; } }