- Fixed double power production

- Buffered consumers no longer request power when full
This commit is contained in:
Timmeey86 2018-12-01 14:31:01 +01:00
parent b4aba3d263
commit 982c9bf964
4 changed files with 23 additions and 16 deletions

View File

@ -15,13 +15,8 @@ import java.io.DataOutput;
import java.io.IOException;
public class PowerGenerator extends PowerDistributor{
/** The amount of power produced per tick. */
/** The amount of power produced per tick in case of an efficiency of 1.0, which currently represents 200%. */
protected float powerProduction;
/** The maximum possible efficiency for this generator. Supply values larger than 1.0f if more than 100% is possible.
* This could be the case when e.g. an item with 100% flammability is the reference point, but a more effective liquid
* can be supplied as an alternative.
*/
protected float maxEfficiency = 1.0f;
public BlockStat generationType = BlockStat.basePowerGeneration;
public PowerGenerator(String name){
@ -39,8 +34,9 @@ public class PowerGenerator extends PowerDistributor{
@Override
public float getPowerProduction(Tile tile){
// Multiply all efficiencies by two since 0.5 = 100% efficiency
return powerProduction * tile.<GeneratorEntity>entity().productionEfficiency * 2.0f;
// While 0.5 efficiency currently reflects 100%, we do not need to multiply by any factor since powerProduction states the
// power which would be produced at 1.0 efficiency
return powerProduction * tile.<GeneratorEntity>entity().productionEfficiency;
}
@Override
@ -57,12 +53,13 @@ public class PowerGenerator extends PowerDistributor{
public void setBars(){
super.setBars();
if(hasPower){
bars.add(new BlockBar(BarType.power, true, tile -> tile.<GeneratorEntity>entity().productionEfficiency / maxEfficiency));
bars.add(new BlockBar(BarType.power, true, tile -> tile.<GeneratorEntity>entity().productionEfficiency));
}
}
public static class GeneratorEntity extends TileEntity{
public float generateTime;
/** The efficiency of the producer. Currently, an efficiency of 0.5 means 100% */
public float productionEfficiency = 0.0f;
@Override

View File

@ -1,5 +1,6 @@
package io.anuke.mindustry.world.consumers;
import com.badlogic.gdx.math.MathUtils;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.mindustry.entities.TileEntity;
@ -86,10 +87,12 @@ public class ConsumePower extends Consume{
* @return The amount of power which is requested per tick.
*/
public float requestedPower(Block block, TileEntity entity){
// TODO Make the block not consume power on the following conditions, either here or in PowerGraph:
// - Other consumers are not valid, e.g. additional input items/liquids are missing
// - Buffer is full
return powerPerTick;
if(isBuffered){
// Stop requesting power once the buffer is full.
return MathUtils.isEqual(entity.power.satisfaction, 1.0f) ? 0.0f : powerPerTick;
}else{
return powerPerTick;
}
}

View File

@ -42,8 +42,9 @@ public class PowerTestFixture{
}
protected static PowerGenerator createFakeProducerBlock(float producedPower){
// Multiply produced power by 2 since production efficiency is defined to be 0.5 = 100%
return new PowerGenerator("fakegen"){{
powerProduction = producedPower;
powerProduction = producedPower * 2.0f;
}};
}

View File

@ -98,7 +98,13 @@ public class PowerTests extends PowerTestFixture{
powerGraph.add(bufferedConsumerTile);
assertEquals(producedPower * FakeThreadHandler.fakeDelta, powerGraph.getPowerProduced(), MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Produced power did not match");
assertEquals(Math.min(maxBuffer, powerConsumedPerTick * FakeThreadHandler.fakeDelta), powerGraph.getPowerNeeded(), MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": ConsumedPower did not match");
float expectedPowerUsage;
if(initialSatisfaction == 1.0f){
expectedPowerUsage = 0f;
}else{
expectedPowerUsage = Math.min(maxBuffer, powerConsumedPerTick * FakeThreadHandler.fakeDelta);
}
assertEquals(expectedPowerUsage, powerGraph.getPowerNeeded(), MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Consumed power did not match");
// Update and check for the expected power satisfaction of the consumer
powerGraph.update();
@ -128,7 +134,7 @@ public class PowerTests extends PowerTestFixture{
if(producedPower > 0.0f){
Tile producerTile = createFakeTile(0, 0, createFakeProducerBlock(producedPower));
producerTile.<PowerGenerator.GeneratorEntity>entity().productionEfficiency = 0.5f; // Currently, 0.5f = 100%
producerTile.<PowerGenerator.GeneratorEntity>entity().productionEfficiency = 0.5f;
powerGraph.add(producerTile);
}
Tile directConsumerTile = null;