From 1d57568322cbf7d1bb0a84215949b50ffd907f4d Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Thu, 29 Nov 2018 23:35:56 +0100 Subject: [PATCH 1/2] All power distributors now update power graph Replaced manual power graph update by base implementation call so future changes in base class get applied to all subclasses automatically. Added the call where missing. --- .../io/anuke/mindustry/world/blocks/power/FusionReactor.java | 2 +- .../io/anuke/mindustry/world/blocks/power/ItemGenerator.java | 2 ++ .../anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java | 2 ++ .../io/anuke/mindustry/world/blocks/power/LiquidGenerator.java | 2 +- .../io/anuke/mindustry/world/blocks/power/NuclearReactor.java | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/core/src/io/anuke/mindustry/world/blocks/power/FusionReactor.java b/core/src/io/anuke/mindustry/world/blocks/power/FusionReactor.java index 0194cfb81d..047825375c 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/FusionReactor.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/FusionReactor.java @@ -44,7 +44,7 @@ public class FusionReactor extends PowerGenerator{ float efficiencyAdded = Mathf.pow(entity.warmup, 4f) * Timers.delta(); entity.productionEfficiency = Mathf.clamp(entity.productionEfficiency + efficiencyAdded * increaseOrDecrease); - tile.entity.power.graph.update(); + super.update(tile); } @Override diff --git a/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java index a8d8da9699..3a16d3df04 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java @@ -93,6 +93,8 @@ public abstract class ItemGenerator extends PowerGenerator{ Effects.effect(explodeEffect, tile.worldx() + Mathf.range(size * tilesize / 2f), tile.worldy() + Mathf.range(size * tilesize / 2f)); } } + + super.update(tile); } protected abstract float getItemEfficiency(Item item); 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 c41f8d1c39..974ade4db5 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java @@ -89,6 +89,8 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{ entity.productionEfficiency = 0.0f; } } + + super.update(tile); } @Override diff --git a/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java index 7314c56a0d..fd909bae76 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java @@ -66,7 +66,7 @@ public abstract class LiquidGenerator extends PowerGenerator{ } } - tile.entity.power.graph.update(); + super.update(tile); } @Override diff --git a/core/src/io/anuke/mindustry/world/blocks/power/NuclearReactor.java b/core/src/io/anuke/mindustry/world/blocks/power/NuclearReactor.java index 2edeaa6f09..f2abdbc304 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/NuclearReactor.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/NuclearReactor.java @@ -128,7 +128,7 @@ public class NuclearReactor extends PowerGenerator{ if(entity.heat >= 0.999f){ entity.kill(); }else{ - tile.entity.power.graph.update(); + super.update(tile); } } From bcb9cfd2fe2808e6907644f06bcab47710ddf232 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Fri, 30 Nov 2018 00:03:53 +0100 Subject: [PATCH 2/2] Synchronized Item/Liquid generator code --- .../world/blocks/power/ItemGenerator.java | 7 +++++ .../blocks/power/ItemLiquidGenerator.java | 29 ++++--------------- .../world/blocks/power/LiquidGenerator.java | 19 +++++++----- .../mindustry/world/blocks/units/MechPad.java | 7 ----- .../java/power/ItemLiquidGeneratorTests.java | 1 + 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java index 3a16d3df04..b18092c111 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ItemGenerator.java @@ -76,6 +76,11 @@ public abstract class ItemGenerator extends PowerGenerator{ public void update(Tile tile){ ItemGeneratorEntity entity = tile.entity(); + if(!entity.cons.valid()){ + entity.productionEfficiency = 0.0f; + return; + } + if(entity.generateTime <= 0f && entity.items.total() > 0){ Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f)); Item item = entity.items.take(); @@ -92,6 +97,8 @@ public abstract class ItemGenerator extends PowerGenerator{ 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; } super.update(tile); 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 974ade4db5..c8af36a3ff 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/ItemLiquidGenerator.java @@ -35,8 +35,6 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{ public void update(Tile tile){ ItemGeneratorEntity entity = tile.entity(); - entity.power.graph.update(); - Liquid liquid = null; for(Liquid other : content.liquids()){ if(entity.liquids.get(other) >= 0.001f && getLiquidEfficiency(other) >= minLiquidEfficiency){ @@ -67,30 +65,13 @@ 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)); } + + // Update the graph manually instead of letting the base class do it since that would consume items + entity.power.graph.update(); }else{ - - if(entity.generateTime <= 0f && entity.items.total() > 0){ - Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f)); - Item item = entity.items.take(); - entity.productionEfficiency = getItemEfficiency(item); - entity.explosiveness = item.explosiveness; - entity.generateTime = 1f; - } - - if(entity.generateTime > 0f){ - entity.generateTime -= 1f / itemDuration * entity.delta(); - entity.generateTime = Mathf.clamp(entity.generateTime); - - if(Mathf.chance(entity.delta() * 0.06 * Mathf.clamp(entity.explosiveness - 0.25f))){ - 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; - } + // No liquids accepted, act like a default ItemGenerator + super.update(tile); } - - super.update(tile); } @Override diff --git a/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java b/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java index fd909bae76..67701ec58d 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/LiquidGenerator.java @@ -48,18 +48,21 @@ public abstract class LiquidGenerator extends PowerGenerator{ @Override public void update(Tile tile){ - TileEntity entity = tile.entity(); + ItemGeneratorEntity entity = tile.entity(); + + // 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(); - // TODO Code duplication with ItemLiquidGenerator if(entity.liquids.get(entity.liquids.current()) >= 0.001f){ - //float powerPerLiquid = getEfficiency(entity.liquids.current()) * this.powerPerLiquid; - float used = Math.min(entity.liquids.currentAmount(), maxLiquidGenerate * entity.delta()); - // TODO Adapt to new power system - //used = Math.min(used, (powerCapacity - entity.power.amount) / powerPerLiquid); + float baseLiquidEfficiency = getEfficiency(entity.liquids.current()) * this.liquidPowerMultiplier; + float maximumPossible = maxLiquidGenerate * calculationDelta; + float used = Math.min(entity.liquids.currentAmount() * calculationDelta, maximumPossible); entity.liquids.remove(entity.liquids.current(), used); - // TODO Adapt to new power system - //entity.power.amount += used * powerPerLiquid; + + // Note: 1 Item with 100% Flammability = 100% efficiency. This means 100% is not max but rather a reference point for this generator. + entity.productionEfficiency = baseLiquidEfficiency * used / maximumPossible; if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){ Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f)); diff --git a/core/src/io/anuke/mindustry/world/blocks/units/MechPad.java b/core/src/io/anuke/mindustry/world/blocks/units/MechPad.java index a1fa070367..6eafeb2a1d 100644 --- a/core/src/io/anuke/mindustry/world/blocks/units/MechPad.java +++ b/core/src/io/anuke/mindustry/world/blocks/units/MechPad.java @@ -52,13 +52,6 @@ public class MechPad extends Block{ super.init(); } - @Override - public void setStats(){ - super.setStats(); - // TODO Verify for new power system - //stats.remove(BlockStat.powerUse); - } - @Override public boolean shouldConsume(Tile tile){ return false; diff --git a/tests/src/test/java/power/ItemLiquidGeneratorTests.java b/tests/src/test/java/power/ItemLiquidGeneratorTests.java index 9511059fda..aae193543d 100644 --- a/tests/src/test/java/power/ItemLiquidGeneratorTests.java +++ b/tests/src/test/java/power/ItemLiquidGeneratorTests.java @@ -22,6 +22,7 @@ import static org.junit.jupiter.api.DynamicTest.dynamicTest; * Additionally, each PowerGraph::update() call will have its own thread frame, i.e. the method will never be called twice within the same frame. * Both of these constraints are handled by FakeThreadHandler within PowerTestFixture. * Any power amount (produced, consumed, buffered) should be affected by FakeThreadHandler.fakeDelta but satisfaction should not! + * TODO Find a way to reuse these tests for LiquidGenerator and ItemGenerator */ public class ItemLiquidGeneratorTests extends PowerTestFixture{