From f789a3590197dd400420d112f7898dcf48fb3b4a Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Wed, 21 Nov 2018 20:07:05 +0100 Subject: [PATCH] Readded buffered and direct consumers as three classes --- .../world/consumers/ConsumePower.java | 48 +++++++++++++++++++ .../world/consumers/ConsumePowerBuffered.java | 43 +++++++++++++++++ .../world/consumers/ConsumePowerDirect.java | 43 +++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 core/src/io/anuke/mindustry/world/consumers/ConsumePower.java create mode 100644 core/src/io/anuke/mindustry/world/consumers/ConsumePowerBuffered.java create mode 100644 core/src/io/anuke/mindustry/world/consumers/ConsumePowerDirect.java diff --git a/core/src/io/anuke/mindustry/world/consumers/ConsumePower.java b/core/src/io/anuke/mindustry/world/consumers/ConsumePower.java new file mode 100644 index 0000000000..8f4e829d1d --- /dev/null +++ b/core/src/io/anuke/mindustry/world/consumers/ConsumePower.java @@ -0,0 +1,48 @@ +package io.anuke.mindustry.world.consumers; + +import io.anuke.ucore.scene.ui.layout.Table; + +import io.anuke.mindustry.entities.TileEntity; +import io.anuke.mindustry.world.Block; +import io.anuke.mindustry.world.meta.BlockStat; +import io.anuke.mindustry.world.meta.BlockStats; +import io.anuke.mindustry.world.meta.StatUnit; + +/** Consumer class for blocks which consume power while being connected to a power graph. */ +public abstract class ConsumePower extends Consume{ + /** The maximum amount of power which can be processed per tick. This might influence efficiency or load a buffer. */ + protected final float powerPerTick; + + public ConsumePower(float powerPerTick){ + this.powerPerTick = powerPerTick; + } + + @Override + public void buildTooltip(Table table){ + // No tooltip for power + } + + @Override + public String getIcon(){ + return "icon-power"; + } + + @Override + public void update(Block block, TileEntity entity){ + // Nothing to do since PowerGraph directly updates entity.power.satisfaction + } + + // valid(...) is implemented in subclass + // display(...) is implemented in subclass + + /** + * Retrieves the amount of power which is requested for the given block and entity. + * @param block The block which needs power. + * @param entity The entity which contains the power module. + * @return The amount of power which is requested per tick. + */ + public float requestedPower(Block block, TileEntity entity){ + // TODO Is it possible to make the block not consume power while items/liquids are missing? + return powerPerTick; + } +} diff --git a/core/src/io/anuke/mindustry/world/consumers/ConsumePowerBuffered.java b/core/src/io/anuke/mindustry/world/consumers/ConsumePowerBuffered.java new file mode 100644 index 0000000000..62eebdfb21 --- /dev/null +++ b/core/src/io/anuke/mindustry/world/consumers/ConsumePowerBuffered.java @@ -0,0 +1,43 @@ +package io.anuke.mindustry.world.consumers; + +import io.anuke.ucore.scene.ui.layout.Table; + +import io.anuke.mindustry.entities.TileEntity; +import io.anuke.mindustry.world.Block; +import io.anuke.mindustry.world.meta.BlockStat; +import io.anuke.mindustry.world.meta.BlockStats; +import io.anuke.mindustry.world.meta.StatUnit; + +/** Consumer class for blocks which directly consume power without buffering it. */ +public class ConsumePowerBuffered extends ConsumePower{ + /** The maximum power capacity in power units. */ + protected final float powerCapacity; + + /** + * Adds a power buffer to the owner which takes ticksToFill number of ticks to be filled. + * Note that this object does not remove power from the buffer. + * @param powerCapacity The maximum capacity in power units. + * @param ticksToFill The number of ticks it shall take to fill the buffer. + */ + public ConsumePowerBuffered(float powerCapacity, float ticksToFill){ + super(powerCapacity / ticksToFill); + this.powerCapacity = powerCapacity; + } + + @Override + public boolean valid(Block block, TileEntity entity){ + // TODO - Verify: It might be necessary to know about the power required per shot/event here. + return true; + } + + @Override + public void display(BlockStats stats){ + stats.add(BlockStat.powerCapacity, powerCapacity, StatUnit.powerSecond); + } + + @Override + public float requestedPower(Block block, TileEntity entity){ + // Only request power until the capacity is full + return Math.max(powerPerTick, powerCapacity * (1 - entity.power.satisfaction)); + } +} diff --git a/core/src/io/anuke/mindustry/world/consumers/ConsumePowerDirect.java b/core/src/io/anuke/mindustry/world/consumers/ConsumePowerDirect.java new file mode 100644 index 0000000000..0ce7d593a2 --- /dev/null +++ b/core/src/io/anuke/mindustry/world/consumers/ConsumePowerDirect.java @@ -0,0 +1,43 @@ +package io.anuke.mindustry.world.consumers; + +import io.anuke.ucore.scene.ui.layout.Table; + +import io.anuke.mindustry.entities.TileEntity; +import io.anuke.mindustry.world.Block; +import io.anuke.mindustry.world.meta.BlockStat; +import io.anuke.mindustry.world.meta.BlockStats; +import io.anuke.mindustry.world.meta.StatUnit; + +/** Consumer class for blocks which directly consume power without buffering it. */ +public class ConsumePowerDirect extends ConsumePower{ + /** The minimum power satisfaction (fraction of powerPerTick) which must be achieved before the module may work. */ + protected final float minimumSatisfaction; + + /** + * Makes the owner consume powerPerTick each tick and disables it unless 60% of that power is being supplied. + * @param powerPerTick The maximum amount of power which is required per tick for 100% efficiency. + */ + public ConsumePowerDirect(float powerPerTick){ + this(powerPerTick, 0.75f); + } + + /** + * Makes the owner consume powerPerTick each tick and disables it unless minimumSatisfaction (1.0 = 100%) of that power is being supplied. + * @param powerPerTick The maximum amount of power which is required per tick for 100% efficiency. + * @param minimumSatisfaction The percentage of powerPerTick which must be available for the module to work. + */ + public ConsumePowerDirect(float powerPerTick, float minimumSatisfaction){ + super(powerPerTick); + this.minimumSatisfaction = minimumSatisfaction; + } + + @Override + public boolean valid(Block block, TileEntity entity){ + return entity.power.satisfaction >= minimumSatisfaction; + } + + @Override + public void display(BlockStats stats){ + stats.add(BlockStat.powerUse, powerPerTick * 60f, StatUnit.powerSecond); + } +}