diff --git a/core/src/io/anuke/mindustry/world/BaseBlock.java b/core/src/io/anuke/mindustry/world/BaseBlock.java index 6a729435ce..6111e5a197 100644 --- a/core/src/io/anuke/mindustry/world/BaseBlock.java +++ b/core/src/io/anuke/mindustry/world/BaseBlock.java @@ -25,8 +25,8 @@ public abstract class BaseBlock extends MappableContent{ public boolean outputsLiquid = false; public boolean singleLiquid = true; - public boolean consumesPower; - public boolean outputsPower; + public boolean consumesPower = true; + public boolean outputsPower = false; public boolean bufferedPowerConsumer = false; /** In case of unbuffered consumers, this stores the amount of power which is required per tick in order to work at maximum efficiency. 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 68afdec034..9045ffb284 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java @@ -109,10 +109,12 @@ public class PowerGraph{ float powerNeeded = getPowerNeeded(); float powerProduced = getPowerProduced(); - if(powerNeeded > powerProduced){ - powerProduced += useBatteries(powerNeeded - powerProduced); - }else if(powerProduced > powerNeeded){ - powerProduced -= chargeBatteries(powerProduced - powerNeeded); + if(Math.abs(powerNeeded - powerProduced) > 0.0001f){ + if(powerNeeded > powerProduced){ + powerProduced += useBatteries(powerNeeded-powerProduced); + }else if(powerProduced > powerNeeded){ + powerProduced -= chargeBatteries(powerProduced-powerNeeded); + } } distributePower(powerNeeded, powerProduced); diff --git a/tests/src/test/java/PowerTests.java b/tests/src/test/java/PowerTests.java index d0ea1e218c..07f7e6e2ba 100644 --- a/tests/src/test/java/PowerTests.java +++ b/tests/src/test/java/PowerTests.java @@ -16,6 +16,7 @@ import java.util.LinkedList; import java.util.List; import static io.anuke.mindustry.Vars.threads; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; public class PowerTests{ @@ -60,32 +61,31 @@ public class PowerTests{ void test_balancedPower(){ PowerGraph powerGraph = new PowerGraph(); - // Create one water extractor (5.4 power consumed) + // Create one water extractor (5.4 power/Second = 0.09/tick) Tile waterExtractorTile = createFakeTile(0, 0, (Floor)Blocks.sand, ProductionBlocks.waterExtractor); powerGraph.add(waterExtractorTile); - // Create 20 small solar panels (20*0.27=5.4 power produced) + // Create 20 small solar panels (20*0.27=5.4 power/second = 0.09/tick) List solarPanelTiles = new LinkedList<>(); - float producedPowerSum = 0.0f; for(int counter = 0; counter < 20; counter++){ Tile solarPanelTile = createFakeTile( 2 + counter / 2, counter % 2, (Floor)Blocks.sand, PowerBlocks.solarPanel); powerGraph.add(solarPanelTile); solarPanelTiles.add(solarPanelTile); } + float powerNeeded = powerGraph.getPowerNeeded(); + float powerProduced = powerGraph.getPowerProduced(); + // If these lines fail, you probably changed power production/consumption and need to adapt this test + // OR their implementation is corrupt. // TODO: Create fake blocks which are independent of such changes - powerGraph.update(); + float epsilon = 0.00001f; + assertEquals(powerNeeded, 0.09f, epsilon); + assertEquals(powerProduced, 0.09f, epsilon); + // Note: The assertions above induce that powerNeeded = powerProduced (with floating point inaccuracy) - powerGraph.getPowerNeeded(); - powerGraph.getPowerProduced(); - -/* if(powerNeeded > powerProduced){ - powerProduced += useBatteries(powerNeeded - powerProduced); - }else if(powerProduced > powerNeeded){ - powerProduced -= chargeBatteries(powerProduced - powerNeeded); - } - - distributePower(powerNeeded, powerProduced);*/ + // Distribute power and make sure the water extractor is powered + powerGraph.distributePower(powerNeeded, powerProduced); + assertEquals(waterExtractorTile.entity.power.satisfaction, 1.0f, epsilon); } }