From cf3d2c3def279031e104b48953c24c2d19c4d262 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Wed, 28 Nov 2018 11:59:58 +0100 Subject: [PATCH] Fixed handling of item duration --- .../blocks/power/ItemLiquidGenerator.java | 11 +++++--- .../java/power/ItemLiquidGeneratorTests.java | 27 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java index c3e19143e2..c41f8d1c39 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java @@ -45,13 +45,16 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{ } } - entity.productionEfficiency = 0.0f; // Note: Do not use this delta when calculating the amount of power or the power efficiency, but use it for resource consumption if necessary. // Power amount is delta'd by PowerGraph class already. float calculationDelta = entity.delta(); + if(!entity.cons.valid()){ + entity.productionEfficiency = 0.0f; + return; + } //liquid takes priority over solids - if(liquid != null && entity.liquids.get(liquid) >= 0.001f && entity.cons.valid()){ + if(liquid != null && entity.liquids.get(liquid) >= 0.001f){ float baseLiquidEfficiency = getLiquidEfficiency(liquid) * this.liquidPowerMultiplier; float maximumPossible = maxLiquidGenerate * calculationDelta; float used = Math.min(entity.liquids.get(liquid) * calculationDelta, maximumPossible); @@ -64,7 +67,7 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{ if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){ Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f)); } - }else if(entity.cons.valid()){ + }else{ if(entity.generateTime <= 0f && entity.items.total() > 0){ Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f)); @@ -82,6 +85,8 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{ entity.damage(Mathf.random(8f)); Effects.effect(explodeEffect, tile.worldx() + Mathf.range(size * tilesize / 2f), tile.worldy() + Mathf.range(size * tilesize / 2f)); } + }else{ + entity.productionEfficiency = 0.0f; } } } diff --git a/tests/src/test/java/power/ItemLiquidGeneratorTests.java b/tests/src/test/java/power/ItemLiquidGeneratorTests.java index 471b59bdf6..c1b1ff6bf8 100644 --- a/tests/src/test/java/power/ItemLiquidGeneratorTests.java +++ b/tests/src/test/java/power/ItemLiquidGeneratorTests.java @@ -29,7 +29,7 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{ private Tile tile; private ItemGenerator.ItemGeneratorEntity entity; private final float fakeLiquidPowerMultiplier = 2.0f; - private final float fakeItemDuration = 0.5f; + private final float fakeItemDuration = 60f; // 60 ticks private final float maximumLiquidUsage = 0.5f; @BeforeEach @@ -102,8 +102,11 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{ assertTrue(generator.acceptItem(item, tile, null), parameterDescription + ": Items which will be declined by the generator don't need to be tested - The code won't be called for those cases."); - // Reset items since BeforeEach will not be called between dynamic tests + // Clean up manually since BeforeEach will not be called between dynamic tests entity.items.clear(); + entity.generateTime = 0.0f; + entity.productionEfficiency = 0.0f; + if(amount > 0){ entity.items.add(item, amount); } @@ -116,4 +119,24 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{ assertEquals(expectedRemainingItemAmount, entity.items.get(item), parameterDescription + ": Remaining item amount mismatch."); assertEquals(expectedEfficiency, entity.productionEfficiency, parameterDescription + ": Efficiency mismatch."); } + + /** Makes sure the efficiency stays equal during the item duration. */ + @Test + void test_efficiencyConstantDuringItemDuration(){ + + // Burn a single coal and test for the duration + entity.items.add(Items.coal, 1); + entity.cons.update(tile.entity); + generator.update(tile); + + float expectedEfficiency = entity.productionEfficiency; + + float currentDuration = 0.0f; + while((currentDuration += FakeThreadHandler.fakeDelta) <= fakeItemDuration){ + generator.update(tile); + assertEquals(expectedEfficiency, entity.productionEfficiency, "Duration: " + String.valueOf(currentDuration)); + } + generator.update(tile); + assertEquals(0.0f, entity.productionEfficiency, "Duration: " + String.valueOf(currentDuration)); + } }