From 828661e9dedf4b27166ba40cd6b1e17e77ff6781 Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Sat, 15 Dec 2018 23:49:46 +0100 Subject: [PATCH] More efficient way of retrieving ConsumePower subclasses --- core/src/io/anuke/mindustry/world/Block.java | 6 ++--- .../world/blocks/distribution/MassDriver.java | 2 +- .../world/blocks/power/PowerGraph.java | 26 +++++++++---------- .../mindustry/world/consumers/Consumers.java | 23 ++-------------- tests/src/test/java/power/PowerTests.java | 4 +-- 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index 534ee13a52..839d0af6bd 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -345,7 +345,7 @@ public class Block extends BaseBlock { } public void setBars(){ - if(consumes.hasSubtypeOf(ConsumePower.class) && consumes.getSubtypeOf(ConsumePower.class).isBuffered){ + if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){ bars.add(new BlockBar(BarType.power, true, tile -> tile.entity.power.satisfaction)); } if(hasLiquids) bars.add(new BlockBar(BarType.liquid, true, tile -> tile.entity.liquids.total() / liquidCapacity)); @@ -412,8 +412,8 @@ public class Block extends BaseBlock { explosiveness += tile.entity.liquids.sum((liquid, amount) -> liquid.flammability * amount / 2f); } - if(consumes.hasSubtypeOf(ConsumePower.class) && consumes.getSubtypeOf(ConsumePower.class).isBuffered){ - power += tile.entity.power.satisfaction * consumes.getSubtypeOf(ConsumePower.class).powerCapacity; + if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){ + power += tile.entity.power.satisfaction * consumes.get(ConsumePower.class).powerCapacity; } tempColor.mul(1f / units); diff --git a/core/src/io/anuke/mindustry/world/blocks/distribution/MassDriver.java b/core/src/io/anuke/mindustry/world/blocks/distribution/MassDriver.java index 80eea77ef8..390f13f46b 100644 --- a/core/src/io/anuke/mindustry/world/blocks/distribution/MassDriver.java +++ b/core/src/io/anuke/mindustry/world/blocks/distribution/MassDriver.java @@ -131,7 +131,7 @@ public class MassDriver extends Block{ public void setStats(){ super.setStats(); - stats.add(BlockStat.powerShot, consumes.getSubtypeOf(ConsumePower.class).powerCapacity * powerPercentageUsed, StatUnit.powerUnits); + stats.add(BlockStat.powerShot, consumes.get(ConsumePower.class).powerCapacity * powerPercentageUsed, StatUnit.powerUnits); } @Override 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 3cbf21c096..b9d36c2894 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java @@ -48,8 +48,8 @@ public class PowerGraph{ float powerNeeded = 0f; for(Tile consumer : consumers){ Consumers consumes = consumer.block().consumes; - if(consumes.hasSubtypeOf(ConsumePower.class)){ - ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class); + if(consumes.has(ConsumePower.class)){ + ConsumePower consumePower = consumes.get(ConsumePower.class); if(otherConsumersAreValid(consumer, consumePower)){ powerNeeded += consumePower.requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta(); } @@ -62,8 +62,8 @@ public class PowerGraph{ float totalAccumulator = 0f; for(Tile battery : batteries){ Consumers consumes = battery.block().consumes; - if(consumes.hasSubtypeOf(ConsumePower.class)){ - totalAccumulator += battery.entity.power.satisfaction * consumes.getSubtypeOf(ConsumePower.class).powerCapacity; + if(consumes.has(ConsumePower.class)){ + totalAccumulator += battery.entity.power.satisfaction * consumes.get(ConsumePower.class).powerCapacity; } } return totalAccumulator; @@ -73,8 +73,8 @@ public class PowerGraph{ float totalCapacity = 0f; for(Tile battery : batteries){ Consumers consumes = battery.block().consumes; - if(consumes.hasSubtypeOf(ConsumePower.class)){ - totalCapacity += consumes.getSubtypeOf(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta(); + if(consumes.has(ConsumePower.class)){ + totalCapacity += consumes.get(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta(); } } return totalCapacity; @@ -88,8 +88,8 @@ public class PowerGraph{ float consumedPowerPercentage = Math.min(1.0f, needed / stored); for(Tile battery : batteries){ Consumers consumes = battery.block().consumes; - if(consumes.hasSubtypeOf(ConsumePower.class)){ - ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class); + if(consumes.has(ConsumePower.class)){ + ConsumePower consumePower = consumes.get(ConsumePower.class); if(consumePower.powerCapacity > 0f){ battery.entity.power.satisfaction = Math.max(0.0f, battery.entity.power.satisfaction - consumedPowerPercentage); } @@ -104,8 +104,8 @@ public class PowerGraph{ for(Tile battery : batteries){ Consumers consumes = battery.block().consumes; - if(consumes.hasSubtypeOf(ConsumePower.class)){ - ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class); + if(consumes.has(ConsumePower.class)){ + ConsumePower consumePower = consumes.get(ConsumePower.class); if(consumePower.powerCapacity > 0f){ float additionalPowerPercentage = Math.min(1.0f, excess / consumePower.powerCapacity); battery.entity.power.satisfaction = Math.min(1.0f, battery.entity.power.satisfaction + additionalPowerPercentage); @@ -121,8 +121,8 @@ public class PowerGraph{ float coverage = Math.min(1, produced / needed); for(Tile consumer : consumers){ Consumers consumes = consumer.block().consumes; - if(consumes.hasSubtypeOf(ConsumePower.class)){ - ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class); + if(consumes.has(ConsumePower.class)){ + ConsumePower consumePower = consumes.get(ConsumePower.class); if(!otherConsumersAreValid(consumer, consumePower)){ consumer.entity.power.satisfaction = 0.0f; // Only supply power if the consumer would get valid that way }else{ @@ -181,7 +181,7 @@ public class PowerGraph{ public void clear(){ for(Tile other : all){ if(other.entity != null && other.entity.power != null){ - if(other.block().consumes.hasSubtypeOf(ConsumePower.class) && !other.block().consumes.getSubtypeOf(ConsumePower.class).isBuffered){ + if(other.block().consumes.has(ConsumePower.class) && !other.block().consumes.get(ConsumePower.class).isBuffered){ // Reset satisfaction to zero in case of direct consumer. There is no reason to clear power from buffered consumers. other.entity.power.satisfaction = 0.0f; } diff --git a/core/src/io/anuke/mindustry/world/consumers/Consumers.java b/core/src/io/anuke/mindustry/world/consumers/Consumers.java index c647b233e0..9391503750 100644 --- a/core/src/io/anuke/mindustry/world/consumers/Consumers.java +++ b/core/src/io/anuke/mindustry/world/consumers/Consumers.java @@ -109,7 +109,7 @@ public class Consumers{ } public Consume add(Consume consume){ - map.put(consume.getClass(), consume); + map.put((consume instanceof ConsumePower ? ConsumePower.class : consume.getClass()), consume); return consume; } @@ -121,32 +121,13 @@ public class Consumers{ return map.containsKey(type); } - public boolean hasSubtypeOf(Class type){ - for(Consume consume : all()){ - if(type.isAssignableFrom(consume.getClass())){ - return true; - } - } - return false; - } - @SuppressWarnings("unchecked") public T get(Class type){ if(!map.containsKey(type)){ throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "'!"); } return (T) map.get(type); - } - - @SuppressWarnings("unchecked") - public T getSubtypeOf(Class type){ - for(Consume consume : all()){ - if(type.isAssignableFrom(consume.getClass())){ - return (T)consume; - } - } - throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "' or any of its subtypes!"); - } + }< public Iterable all(){ return map.values(); diff --git a/tests/src/test/java/power/PowerTests.java b/tests/src/test/java/power/PowerTests.java index 64bfa2e451..a79089ab91 100644 --- a/tests/src/test/java/power/PowerTests.java +++ b/tests/src/test/java/power/PowerTests.java @@ -174,8 +174,8 @@ public class PowerTests extends PowerTestFixture{ powerGraph.update(); assertEquals(0.0f, consumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR); - if(consumerTile.block().consumes.hasSubtypeOf(ConsumePower.class)){ - ConsumePower consumePower = consumerTile.block().consumes.getSubtypeOf(ConsumePower.class); + if(consumerTile.block().consumes.has(ConsumePower.class)){ + ConsumePower consumePower = consumerTile.block().consumes.get(ConsumePower.class); assertFalse(consumePower.valid(consumerTile.block(), consumerTile.entity())); } }