From e5dabc63c26efc733ca0850c1dbc01dcfbba8975 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Wed, 5 Dec 2018 18:03:50 +0100 Subject: [PATCH 1/3] Removed power bars from producers --- .../world/blocks/power/PowerGenerator.java | 8 -- .../test/java/power/PowerBalancingTests.java | 94 +++++++++++++++++++ 2 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 tests/src/test/java/power/PowerBalancingTests.java diff --git a/core/src/io/anuke/mindustry/world/blocks/power/PowerGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/PowerGenerator.java index d165925c07..5045f83c5f 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGenerator.java @@ -49,14 +49,6 @@ public class PowerGenerator extends PowerDistributor{ return new GeneratorEntity(); } - @Override - public void setBars(){ - super.setBars(); - if(hasPower){ - bars.add(new BlockBar(BarType.power, true, tile -> tile.entity().productionEfficiency)); - } - } - public static class GeneratorEntity extends TileEntity{ public float generateTime; /** The efficiency of the producer. Currently, an efficiency of 0.5 means 100% */ diff --git a/tests/src/test/java/power/PowerBalancingTests.java b/tests/src/test/java/power/PowerBalancingTests.java new file mode 100644 index 0000000000..2555285985 --- /dev/null +++ b/tests/src/test/java/power/PowerBalancingTests.java @@ -0,0 +1,94 @@ +package power; + +import com.badlogic.gdx.math.MathUtils; +import io.anuke.mindustry.content.Items; +import io.anuke.mindustry.content.Liquids; +import io.anuke.mindustry.content.blocks.PowerBlocks; +import io.anuke.mindustry.type.Item; +import io.anuke.mindustry.type.Liquid; +import io.anuke.mindustry.world.Block; +import io.anuke.mindustry.world.Tile; +import io.anuke.mindustry.world.blocks.power.BurnerGenerator; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** Sets expectations to specific production blocks using specific inputs. */ + +public class PowerBalancingTests extends PowerTestFixture{ +// Last updated to values of: v63 + + /** + * Tests the produced power of a power block with a full stack of items or liquids. + * @apiNote Tests only a single tick with a fixed delta and interpolates that to match one second. + * @param powerBlock The block to be tested. + * @param inputItem The item to be supplied (may be null). + * @param inputLiquid The liquid to be supplied (may be null). + * @param expectedPowerPerSecond The amount of power which should be produced per second. + */ + public void testPowerGenerator(Block powerBlock, Item inputItem, Liquid inputLiquid, float expectedPowerPerSecond){ + Tile fakeTile = createFakeTile(0, 0, powerBlock); + if(inputItem != null){ + fakeTile.entity.items.add(inputItem, powerBlock.itemCapacity); + } + if(inputLiquid != null){ + fakeTile.entity.liquids.add(inputLiquid, powerBlock.liquidCapacity); + } + fakeTile.entity.cons.update(fakeTile.entity); + powerBlock.update(fakeTile); + + assertEquals(expectedPowerPerSecond, fakeTile.entity.power.graph.getPowerProduced() * 60f / FakeThreadHandler.fakeDelta, MathUtils.FLOAT_ROUNDING_ERROR * 10.0f); + } + + @Test + public void testCombustionWithCoal(){ + testPowerGenerator(PowerBlocks.combustionGenerator, Items.coal, null, 2.7f); // 100% flammability + } + + @Test + public void testCombustionWithOil(){ + testPowerGenerator(PowerBlocks.combustionGenerator, null, Liquids.oil, 2.7f * 1.2f); // 120% flammability + } + + @Test + public void testCombustionWithBlastCompound(){ + testPowerGenerator(PowerBlocks.combustionGenerator, Items.blastCompound, null, 2.7f * 0.4f); // 40% flammability + } + + @Test + public void testTurbineWithCoal(){ + testPowerGenerator(PowerBlocks.turbineGenerator, Items.coal, Liquids.water, 8.4f); // 100% flammability + } + + @Test + public void testTurbineWithBiomatter(){ + testPowerGenerator(PowerBlocks.turbineGenerator, Items.biomatter, Liquids.water, 8.4f * 0.8f); // 100% flammability + } + + @Test + public void testThermalWithLava(){ + testPowerGenerator(PowerBlocks.thermalGenerator, null, Liquids.lava, 36f); // 100% flammability + } + + @Test + public void testSolarPanel(){ + testPowerGenerator(PowerBlocks.solarPanel, null, null, 0.27f); + } + + @Test + public void testLargeSolarPanel(){ + testPowerGenerator(PowerBlocks.largeSolarPanel, null, null, 3.3f); + } + + @Test + public void testThoriumReactor(){ + testPowerGenerator(PowerBlocks.thoriumReactor, Items.thorium, Liquids.cryofluid, 33.0f); + } + + @Test + public void testRadioIsotopeGenerator(){ + testPowerGenerator(PowerBlocks.rtgGenerator, Items.thorium, null, 9.0f); + } +} From 329dac77eefe5ffb8704e2e0a168200ae9e1b8c2 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Wed, 5 Dec 2018 18:56:07 +0100 Subject: [PATCH 2/3] Consumers only draw power when all other required inputs are valid --- .../world/blocks/power/PowerGraph.java | 29 ++++++++-- .../test/java/power/DirectConsumerTests.java | 57 +++++++++++++++++++ .../src/test/java/power/PowerTestFixture.java | 3 +- 3 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 tests/src/test/java/power/DirectConsumerTests.java 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 ea2e8abb26..f9e5efdadd 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java @@ -6,6 +6,7 @@ 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.mindustry.world.consumers.Consume; import io.anuke.mindustry.world.consumers.ConsumePower; import io.anuke.mindustry.world.consumers.Consumers; import io.anuke.ucore.util.Mathf; @@ -48,7 +49,10 @@ public class PowerGraph{ for(Tile consumer : consumers){ Consumers consumes = consumer.block().consumes; if(consumes.has(ConsumePower.class)){ - powerNeeded += consumes.get(ConsumePower.class).requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta(); + ConsumePower consumePower = consumes.get(ConsumePower.class); + if(otherConsumersAreValid(consumer, consumePower)){ + powerNeeded += consumePower.requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta(); + } } } return powerNeeded; @@ -119,12 +123,16 @@ public class PowerGraph{ Consumers consumes = consumer.block().consumes; if(consumes.has(ConsumePower.class)){ ConsumePower consumePower = consumes.get(ConsumePower.class); - if(consumePower.isBuffered){ - // Add an equal percentage of power to all buffers, based on the global power coverage in this graph - float maximumRate = consumePower.requestedPower(consumer.block(), consumer.entity()) * coverage * consumer.entity.delta(); - consumer.entity.power.satisfaction = Mathf.clamp(consumer.entity.power.satisfaction + maximumRate / consumePower.powerCapacity); + if(!otherConsumersAreValid(consumer, consumePower)){ + consumer.entity.power.satisfaction = 0.0f; // Only supply power if the consumer would get valid that way }else{ - consumer.entity.power.satisfaction = coverage; + if(consumePower.isBuffered){ + // Add an equal percentage of power to all buffers, based on the global power coverage in this graph + float maximumRate = consumePower.requestedPower(consumer.block(), consumer.entity()) * coverage * consumer.entity.delta(); + consumer.entity.power.satisfaction = Mathf.clamp(consumer.entity.power.satisfaction + maximumRate / consumePower.powerCapacity); + }else{ + consumer.entity.power.satisfaction = coverage; + } } } } @@ -222,6 +230,15 @@ public class PowerGraph{ } } + private boolean otherConsumersAreValid(Tile tile, Consume consumePower){ + for(Consume cons : tile.block().consumes.all()){ + if(cons != consumePower && !cons.isOptional() && !cons.valid(tile.block(), tile.entity())){ + return false; + } + } + return true; + } + @Override public String toString(){ return "PowerGraph{" + diff --git a/tests/src/test/java/power/DirectConsumerTests.java b/tests/src/test/java/power/DirectConsumerTests.java new file mode 100644 index 0000000000..92812f8079 --- /dev/null +++ b/tests/src/test/java/power/DirectConsumerTests.java @@ -0,0 +1,57 @@ +package power; + +import io.anuke.mindustry.content.Items; +import io.anuke.mindustry.content.UnitTypes; +import io.anuke.mindustry.type.ItemStack; +import io.anuke.mindustry.world.Tile; +import io.anuke.mindustry.world.blocks.power.PowerGenerator; +import io.anuke.mindustry.world.blocks.power.PowerGraph; +import io.anuke.mindustry.world.blocks.units.UnitFactory; +import org.junit.jupiter.api.Test; +import sun.nio.cs.Surrogate; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** Tests for direct power consumers. */ +public class DirectConsumerTests extends PowerTestFixture{ + + @Test + void noPowerRequestedWithNoItems(){ + testUnitFactory(0, 0, 0.08f, 0.08f, 0.0f); + } + + @Test + void noPowerRequestedWithInsufficientItems(){ + testUnitFactory(30, 0, 0.08f, 0.08f, 0.0f); + testUnitFactory(0, 30, 0.08f, 0.08f, 0.0f); + } + + @Test + void powerRequestedWithSufficientItems(){ + testUnitFactory(30, 30, 0.08f, 0.08f, 1.0f); + } + + void testUnitFactory(int siliconAmount, int leadAmount, float producedPower, float requestedPower, float expectedSatisfaction){ + Tile consumerTile = createFakeTile(0, 0, new UnitFactory("fakefactory"){{ + type = UnitTypes.spirit; + produceTime = 60; + consumes.powerDirect(requestedPower); + consumes.items(new ItemStack(Items.silicon, 30), new ItemStack(Items.lead, 30)); + }}); + consumerTile.entity.items.add(Items.silicon, siliconAmount); + consumerTile.entity.items.add(Items.lead, leadAmount); + + Tile producerTile = createFakeTile(2, 0, createFakeProducerBlock(producedPower)); + producerTile.entity().productionEfficiency = 0.5f; // 100% + + PowerGraph graph = new PowerGraph(); + graph.add(producerTile); + graph.add(consumerTile); + + consumerTile.entity.update(); + graph.update(); + + assertEquals(expectedSatisfaction, consumerTile.entity.power.satisfaction); + } +} diff --git a/tests/src/test/java/power/PowerTestFixture.java b/tests/src/test/java/power/PowerTestFixture.java index 5728f6bc8e..92d195399b 100644 --- a/tests/src/test/java/power/PowerTestFixture.java +++ b/tests/src/test/java/power/PowerTestFixture.java @@ -99,7 +99,8 @@ public class PowerTestFixture{ } // Assign incredibly high health so the block does not get destroyed on e.g. burning Blast Compound - block.health *= 100.0f; + block.health = 100000; + tile.entity.health = 100000.0f; return tile; }catch(Exception ex){ From 57bebd3fb6800e54d8d870b1da2f960f6ee5dea9 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Wed, 5 Dec 2018 18:57:52 +0100 Subject: [PATCH 3/3] Removed file which was not supposed to be commited --- .../test/java/power/PowerBalancingTests.java | 94 ------------------- 1 file changed, 94 deletions(-) delete mode 100644 tests/src/test/java/power/PowerBalancingTests.java diff --git a/tests/src/test/java/power/PowerBalancingTests.java b/tests/src/test/java/power/PowerBalancingTests.java deleted file mode 100644 index 2555285985..0000000000 --- a/tests/src/test/java/power/PowerBalancingTests.java +++ /dev/null @@ -1,94 +0,0 @@ -package power; - -import com.badlogic.gdx.math.MathUtils; -import io.anuke.mindustry.content.Items; -import io.anuke.mindustry.content.Liquids; -import io.anuke.mindustry.content.blocks.PowerBlocks; -import io.anuke.mindustry.type.Item; -import io.anuke.mindustry.type.Liquid; -import io.anuke.mindustry.world.Block; -import io.anuke.mindustry.world.Tile; -import io.anuke.mindustry.world.blocks.power.BurnerGenerator; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** Sets expectations to specific production blocks using specific inputs. */ - -public class PowerBalancingTests extends PowerTestFixture{ -// Last updated to values of: v63 - - /** - * Tests the produced power of a power block with a full stack of items or liquids. - * @apiNote Tests only a single tick with a fixed delta and interpolates that to match one second. - * @param powerBlock The block to be tested. - * @param inputItem The item to be supplied (may be null). - * @param inputLiquid The liquid to be supplied (may be null). - * @param expectedPowerPerSecond The amount of power which should be produced per second. - */ - public void testPowerGenerator(Block powerBlock, Item inputItem, Liquid inputLiquid, float expectedPowerPerSecond){ - Tile fakeTile = createFakeTile(0, 0, powerBlock); - if(inputItem != null){ - fakeTile.entity.items.add(inputItem, powerBlock.itemCapacity); - } - if(inputLiquid != null){ - fakeTile.entity.liquids.add(inputLiquid, powerBlock.liquidCapacity); - } - fakeTile.entity.cons.update(fakeTile.entity); - powerBlock.update(fakeTile); - - assertEquals(expectedPowerPerSecond, fakeTile.entity.power.graph.getPowerProduced() * 60f / FakeThreadHandler.fakeDelta, MathUtils.FLOAT_ROUNDING_ERROR * 10.0f); - } - - @Test - public void testCombustionWithCoal(){ - testPowerGenerator(PowerBlocks.combustionGenerator, Items.coal, null, 2.7f); // 100% flammability - } - - @Test - public void testCombustionWithOil(){ - testPowerGenerator(PowerBlocks.combustionGenerator, null, Liquids.oil, 2.7f * 1.2f); // 120% flammability - } - - @Test - public void testCombustionWithBlastCompound(){ - testPowerGenerator(PowerBlocks.combustionGenerator, Items.blastCompound, null, 2.7f * 0.4f); // 40% flammability - } - - @Test - public void testTurbineWithCoal(){ - testPowerGenerator(PowerBlocks.turbineGenerator, Items.coal, Liquids.water, 8.4f); // 100% flammability - } - - @Test - public void testTurbineWithBiomatter(){ - testPowerGenerator(PowerBlocks.turbineGenerator, Items.biomatter, Liquids.water, 8.4f * 0.8f); // 100% flammability - } - - @Test - public void testThermalWithLava(){ - testPowerGenerator(PowerBlocks.thermalGenerator, null, Liquids.lava, 36f); // 100% flammability - } - - @Test - public void testSolarPanel(){ - testPowerGenerator(PowerBlocks.solarPanel, null, null, 0.27f); - } - - @Test - public void testLargeSolarPanel(){ - testPowerGenerator(PowerBlocks.largeSolarPanel, null, null, 3.3f); - } - - @Test - public void testThoriumReactor(){ - testPowerGenerator(PowerBlocks.thoriumReactor, Items.thorium, Liquids.cryofluid, 33.0f); - } - - @Test - public void testRadioIsotopeGenerator(){ - testPowerGenerator(PowerBlocks.rtgGenerator, Items.thorium, null, 9.0f); - } -}