mirror of
https://github.com/Anuken/Mindustry.git
synced 2026-04-27 07:50:54 -07:00
Merged the three ConsumePower classes into a single one
This commit is contained in:
parent
405f153439
commit
d8ddb9dc82
7 changed files with 67 additions and 118 deletions
|
|
@ -20,7 +20,7 @@ import io.anuke.mindustry.input.CursorType;
|
|||
import io.anuke.mindustry.type.ContentType;
|
||||
import io.anuke.mindustry.type.Item;
|
||||
import io.anuke.mindustry.type.ItemStack;
|
||||
import io.anuke.mindustry.world.consumers.ConsumePowerBuffered;
|
||||
import io.anuke.mindustry.world.consumers.ConsumePower;
|
||||
import io.anuke.mindustry.world.meta.*;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.graphics.Draw;
|
||||
|
|
@ -338,7 +338,7 @@ public class Block extends BaseBlock {
|
|||
|
||||
//TODO make this easier to config.
|
||||
public void setBars(){
|
||||
if(consumes.has(ConsumePowerBuffered.class)){
|
||||
if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){
|
||||
bars.add(new BlockBar(BarType.power, true, tile -> tile.entity.power.satisfaction));
|
||||
}
|
||||
if(hasLiquids)
|
||||
|
|
@ -406,8 +406,8 @@ public class Block extends BaseBlock {
|
|||
explosiveness += tile.entity.liquids.sum((liquid, amount) -> liquid.flammability * amount / 2f);
|
||||
}
|
||||
|
||||
if(consumes.has(ConsumePowerBuffered.class)){
|
||||
power += tile.entity.power.satisfaction * consumes.get(ConsumePowerBuffered.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);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ import io.anuke.mindustry.graphics.Palette;
|
|||
import io.anuke.mindustry.type.Item;
|
||||
import io.anuke.mindustry.world.Block;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.consumers.ConsumePowerBuffered;
|
||||
import io.anuke.mindustry.world.consumers.ConsumePower;
|
||||
import io.anuke.mindustry.world.meta.BlockStat;
|
||||
import io.anuke.mindustry.world.meta.StatUnit;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
|
|
@ -131,10 +131,10 @@ public class MassDriver extends Block{
|
|||
public void setStats(){
|
||||
super.setStats();
|
||||
|
||||
if(!consumes.hasSubclassOf(ConsumePowerBuffered.class)){
|
||||
if(!consumes.has(ConsumePower.class) || !consumes.get(ConsumePower.class).isBuffered){
|
||||
throw new RuntimeException("Mass Driver did not have a buffered power consumer object attached.");
|
||||
}
|
||||
stats.add(BlockStat.powerShot, consumes.getFirstSubclassOf(ConsumePowerBuffered.class).powerCapacity, StatUnit.powerUnits);
|
||||
stats.add(BlockStat.powerShot, consumes.get(ConsumePower.class).powerCapacity, StatUnit.powerUnits);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -6,9 +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.ConsumePowerBuffered;
|
||||
import io.anuke.mindustry.world.consumers.Consumers;
|
||||
|
||||
import static io.anuke.mindustry.Vars.threads;
|
||||
|
|
@ -48,8 +46,8 @@ public class PowerGraph{
|
|||
float powerNeeded = 0f;
|
||||
for(Tile consumer : consumers){
|
||||
Consumers consumes = consumer.block().consumes;
|
||||
if(consumes.hasSubclassOf(ConsumePower.class)){
|
||||
powerNeeded += consumes.getFirstSubclassOf(ConsumePower.class).requestedPower(consumer.block(), consumer.entity);
|
||||
if(consumes.has(ConsumePower.class)){
|
||||
powerNeeded += consumes.get(ConsumePower.class).requestedPower(consumer.block(), consumer.entity);
|
||||
}
|
||||
}
|
||||
return powerNeeded;
|
||||
|
|
@ -59,8 +57,8 @@ public class PowerGraph{
|
|||
float totalAccumulator = 0f;
|
||||
for(Tile battery : batteries){
|
||||
Consumers consumes = battery.block().consumes;
|
||||
if(consumes.hasSubclassOf(ConsumePowerBuffered.class)){
|
||||
totalAccumulator += battery.entity.power.satisfaction * consumes.getFirstSubclassOf(ConsumePowerBuffered.class).powerCapacity;
|
||||
if(consumes.has(ConsumePower.class)){
|
||||
totalAccumulator += battery.entity.power.satisfaction * consumes.get(ConsumePower.class).powerCapacity;
|
||||
}
|
||||
}
|
||||
return totalAccumulator;
|
||||
|
|
@ -70,8 +68,8 @@ public class PowerGraph{
|
|||
float totalCapacity = 0f;
|
||||
for(Tile battery : batteries){
|
||||
Consumers consumes = battery.block().consumes;
|
||||
if(consumes.hasSubclassOf(ConsumePower.class)){
|
||||
totalCapacity += consumes.getFirstSubclassOf(ConsumePower.class).requestedPower(battery.block(), battery.entity);
|
||||
if(consumes.has(ConsumePower.class)){
|
||||
totalCapacity += consumes.get(ConsumePower.class).requestedPower(battery.block(), battery.entity);
|
||||
}
|
||||
}
|
||||
return totalCapacity;
|
||||
|
|
@ -105,7 +103,8 @@ public class PowerGraph{
|
|||
|
||||
float coverage = Math.min(1, produced / needed);
|
||||
for(Tile consumer : consumers){
|
||||
if(consumer.block().consumes.hasSubclassOf(ConsumePowerBuffered.class)){
|
||||
Consumers consumes = consumer.block().consumes;
|
||||
if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){
|
||||
consumer.entity.power.satisfaction += (1 - consumer.entity.power.satisfaction) * coverage;
|
||||
}else{
|
||||
consumer.entity.power.satisfaction = coverage;
|
||||
|
|
|
|||
|
|
@ -9,12 +9,40 @@ 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{
|
||||
public 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;
|
||||
/** The minimum power satisfaction (fraction of powerPerTick) which must be achieved before the module may work. */
|
||||
protected final float minimumSatisfaction;
|
||||
/** The maximum power capacity in power units. */
|
||||
public final float powerCapacity;
|
||||
/** True if the module can store power. */
|
||||
public final boolean isBuffered;
|
||||
|
||||
public ConsumePower(float powerPerTick){
|
||||
protected ConsumePower(float powerPerTick, float minimumSatisfaction, float powerCapacity, boolean isBuffered){
|
||||
this.powerPerTick = powerPerTick;
|
||||
this.minimumSatisfaction = minimumSatisfaction;
|
||||
this.powerCapacity = powerCapacity;
|
||||
this.isBuffered = isBuffered;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 static ConsumePower consumePowerBuffered(float powerPerTick, float minimumSatisfaction){
|
||||
return new ConsumePower(powerPerTick, minimumSatisfaction, 0.0f, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 static ConsumePower consumePowerDirect(float powerCapacity, float ticksToFill){
|
||||
return new ConsumePower(powerCapacity / ticksToFill, 0.0f, powerCapacity, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -32,8 +60,24 @@ public abstract class ConsumePower extends Consume{
|
|||
// Nothing to do since PowerGraph directly updates entity.power.satisfaction
|
||||
}
|
||||
|
||||
// valid(...) is implemented in subclass
|
||||
// display(...) is implemented in subclass
|
||||
@Override
|
||||
public boolean valid(Block block, TileEntity entity){
|
||||
if(isBuffered){
|
||||
// TODO - Verify: It might be necessary to know about the power required per shot/event here.
|
||||
return true;
|
||||
}else{
|
||||
return entity.power.satisfaction >= minimumSatisfaction;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void display(BlockStats stats){
|
||||
if(isBuffered){
|
||||
stats.add(BlockStat.powerCapacity, powerCapacity, StatUnit.powerSecond);
|
||||
}else{
|
||||
stats.add(BlockStat.powerUse, powerPerTick * 60f, StatUnit.powerSecond);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the amount of power which is requested for the given block and entity.
|
||||
|
|
@ -45,4 +89,6 @@ public abstract class ConsumePower extends Consume{
|
|||
// TODO Is it possible to make the block not consume power while items/liquids are missing?
|
||||
return powerPerTick;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
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. */
|
||||
public 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));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
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 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ public class Consumers{
|
|||
* @return the created consumer object.
|
||||
*/
|
||||
public ConsumePower powerDirect(float powerPerTick, float minimumSatisfaction){
|
||||
ConsumePower c = new ConsumePowerDirect(powerPerTick, minimumSatisfaction);
|
||||
ConsumePower c = ConsumePower.consumePowerDirect(powerPerTick, minimumSatisfaction);
|
||||
add(c);
|
||||
return c;
|
||||
}
|
||||
|
|
@ -72,7 +72,7 @@ public class Consumers{
|
|||
* @param ticksToFill The number of ticks it shall take to fill the buffer.
|
||||
*/
|
||||
public ConsumePower powerBuffered(float powerCapacity, float ticksToFill){
|
||||
ConsumePower c = new ConsumePowerBuffered(powerCapacity, ticksToFill);
|
||||
ConsumePower c = ConsumePower.consumePowerBuffered(powerCapacity, ticksToFill);
|
||||
add(c);
|
||||
return c;
|
||||
}
|
||||
|
|
@ -122,15 +122,6 @@ public class Consumers{
|
|||
return map.containsKey(type);
|
||||
}
|
||||
|
||||
public boolean hasSubclassOf(Class<? extends Consume> type){
|
||||
for(Consume consume : all()){
|
||||
if(type.isAssignableFrom(consume.getClass())){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public <T extends Consume> T get(Class<T> type){
|
||||
if(!map.containsKey(type)){
|
||||
throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "'!");
|
||||
|
|
@ -138,15 +129,6 @@ public class Consumers{
|
|||
return (T) map.get(type);
|
||||
}
|
||||
|
||||
public <T extends Consume> T getFirstSubclassOf(Class<T> type){
|
||||
for(Consume consume : all()){
|
||||
if(type.isAssignableFrom(consume.getClass())){
|
||||
return (T)consume;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "' (subclasses included)!");
|
||||
}
|
||||
|
||||
public Iterable<Consume> all(){
|
||||
return map.values();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue