Merge pull request #5 from Timmeey86/baltitenger
Fixed batteries / Added Tests / Fixed delta
This commit is contained in:
@@ -338,7 +338,7 @@ public class Block extends BaseBlock {
|
|||||||
|
|
||||||
//TODO make this easier to config.
|
//TODO make this easier to config.
|
||||||
public void setBars(){
|
public void setBars(){
|
||||||
if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){
|
if(consumes.has(ConsumePower.class)){
|
||||||
bars.add(new BlockBar(BarType.power, true, tile -> tile.entity.power.satisfaction));
|
bars.add(new BlockBar(BarType.power, true, tile -> tile.entity.power.satisfaction));
|
||||||
}
|
}
|
||||||
if(hasLiquids)
|
if(hasLiquids)
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ public class PowerGenerator extends PowerDistributor{
|
|||||||
@Override
|
@Override
|
||||||
public void setStats(){
|
public void setStats(){
|
||||||
super.setStats();
|
super.setStats();
|
||||||
stats.add(generationType, powerProduction, StatUnit.powerSecond);
|
stats.add(generationType, powerProduction * 60.0f, StatUnit.powerSecond);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getPowerProduction(Tile tile){
|
public float getPowerProduction(Tile tile){
|
||||||
return powerProduction * tile.<GeneratorEntity>entity().productionEfficiency * tile.entity.delta();
|
return powerProduction * tile.<GeneratorEntity>entity().productionEfficiency;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public class PowerGraph{
|
|||||||
public float getPowerProduced(){
|
public float getPowerProduced(){
|
||||||
float powerProduced = 0f;
|
float powerProduced = 0f;
|
||||||
for(Tile producer : producers){
|
for(Tile producer : producers){
|
||||||
powerProduced += producer.block().getPowerProduction(producer);
|
powerProduced += producer.block().getPowerProduction(producer) * producer.entity.delta();
|
||||||
}
|
}
|
||||||
return powerProduced;
|
return powerProduced;
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,7 @@ public class PowerGraph{
|
|||||||
for(Tile consumer : consumers){
|
for(Tile consumer : consumers){
|
||||||
Consumers consumes = consumer.block().consumes;
|
Consumers consumes = consumer.block().consumes;
|
||||||
if(consumes.has(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
powerNeeded += consumes.get(ConsumePower.class).requestedPower(consumer.block(), consumer.entity);
|
powerNeeded += consumes.get(ConsumePower.class).requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return powerNeeded;
|
return powerNeeded;
|
||||||
@@ -69,7 +69,7 @@ public class PowerGraph{
|
|||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
Consumers consumes = battery.block().consumes;
|
Consumers consumes = battery.block().consumes;
|
||||||
if(consumes.has(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
totalCapacity += consumes.get(ConsumePower.class).requestedPower(battery.block(), battery.entity);
|
totalCapacity += consumes.get(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return totalCapacity;
|
return totalCapacity;
|
||||||
@@ -80,9 +80,15 @@ public class PowerGraph{
|
|||||||
if(MathUtils.isEqual(stored, 0f)){ return 0f; }
|
if(MathUtils.isEqual(stored, 0f)){ return 0f; }
|
||||||
|
|
||||||
float used = Math.min(stored, needed);
|
float used = Math.min(stored, needed);
|
||||||
float percentageRemaining = 1f - (used / stored);
|
float consumedPowerPercentage = Math.min(1.0f, needed / stored);
|
||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
battery.entity.power.satisfaction *= percentageRemaining;
|
Consumers consumes = battery.block().consumes;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return used;
|
return used;
|
||||||
}
|
}
|
||||||
@@ -91,9 +97,15 @@ public class PowerGraph{
|
|||||||
float capacity = getBatteryCapacity();
|
float capacity = getBatteryCapacity();
|
||||||
if(MathUtils.isEqual(capacity, 0f)){ return 0f; }
|
if(MathUtils.isEqual(capacity, 0f)){ return 0f; }
|
||||||
|
|
||||||
float thing = Math.min(1, excess / capacity);
|
|
||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
battery.entity.power.satisfaction += (1 - battery.entity.power.satisfaction) * thing;
|
Consumers consumes = battery.block().consumes;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Math.min(excess, capacity);
|
return Math.min(excess, capacity);
|
||||||
}
|
}
|
||||||
@@ -120,7 +132,7 @@ public class PowerGraph{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update(){
|
public void update(){
|
||||||
if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 || producers.size == 0){
|
if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 && producers.size == 0 && batteries.size == 0){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,6 +218,8 @@ public class PowerGraph{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Update the graph once so direct consumers without any connected producer lose their power
|
||||||
|
graph.update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,5 +101,47 @@ public class PowerTests extends PowerTestFixture{
|
|||||||
powerGraph.update();
|
powerGraph.update();
|
||||||
assertEquals(expectedSatisfaction, bufferedConsumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Satisfaction of buffered consumer did not match");
|
assertEquals(expectedSatisfaction, bufferedConsumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Satisfaction of buffered consumer did not match");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Tests the satisfaction of a single direct consumer after a single update of the power graph which contains a single producer and a single battery.
|
||||||
|
* The used battery is created with a maximum capacity of 100 and receives ten power per tick.
|
||||||
|
*/
|
||||||
|
@TestFactory
|
||||||
|
DynamicTest[] testDirectConsumptionWithBattery(){
|
||||||
|
return new DynamicTest[]{
|
||||||
|
dynamicTest("01", () -> test_directConsumptionWithBattery(10.0f, 0.0f, 0.0f, 10.0f, 0.0f, "Empty battery, no consumer")),
|
||||||
|
dynamicTest("02", () -> test_directConsumptionWithBattery(10.0f, 0.0f, 90.0f, 100.0f, 0.0f, "Battery full after update, no consumer")),
|
||||||
|
dynamicTest("03", () -> test_directConsumptionWithBattery(10.0f, 0.0f, 100.0f, 100.0f, 0.0f, "Full battery, no consumer")),
|
||||||
|
dynamicTest("04", () -> test_directConsumptionWithBattery(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, "No producer, no consumer, empty battery")),
|
||||||
|
dynamicTest("05", () -> test_directConsumptionWithBattery(0.0f, 0.0f, 100.0f, 100.0f, 0.0f, "No producer, no consumer, full battery")),
|
||||||
|
dynamicTest("06", () -> test_directConsumptionWithBattery(0.0f, 10.0f, 0.0f, 0.0f, 0.0f, "No producer, empty battery")),
|
||||||
|
dynamicTest("07", () -> test_directConsumptionWithBattery(0.0f, 10.0f, 100.0f, 90.0f, 1.0f, "No producer, full battery")),
|
||||||
|
dynamicTest("08", () -> test_directConsumptionWithBattery(0.0f, 10.0f, 5.0f, 0.0f, 0.5f, "No producer, low battery")),
|
||||||
|
dynamicTest("09", () -> test_directConsumptionWithBattery(5.0f, 10.0f, 5.0f, 0.0f, 1.0f, "Producer + Battery = Consumed")),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
void test_directConsumptionWithBattery(float producedPower, float requestedPower, float initialBatteryCapacity, float expectedBatteryCapacity, float expectedSatisfaction, String parameterDescription){
|
||||||
|
PowerGraph powerGraph = new PowerGraph();
|
||||||
|
|
||||||
|
if(producedPower > 0.0f){
|
||||||
|
Tile producerTile = createFakeTile(0, 0, createFakeProducerBlock(producedPower));
|
||||||
|
powerGraph.add(producerTile);
|
||||||
|
}
|
||||||
|
Tile directConsumerTile = null;
|
||||||
|
if(requestedPower > 0.0f){
|
||||||
|
directConsumerTile = createFakeTile(0, 1, createFakeDirectConsumer(requestedPower, 0.6f));
|
||||||
|
powerGraph.add(directConsumerTile);
|
||||||
|
}
|
||||||
|
float maxCapacity = 100f;
|
||||||
|
Tile batteryTile = createFakeTile(0, 2, createFakeBattery(maxCapacity, 10 ));
|
||||||
|
batteryTile.entity.power.satisfaction = initialBatteryCapacity / maxCapacity;
|
||||||
|
|
||||||
|
powerGraph.add(batteryTile);
|
||||||
|
|
||||||
|
powerGraph.update();
|
||||||
|
assertEquals(expectedBatteryCapacity, batteryTile.entity.power.satisfaction * maxCapacity, MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Expected battery capacity did not match");
|
||||||
|
if(directConsumerTile != null){
|
||||||
|
assertEquals(expectedSatisfaction, directConsumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Satisfaction of direct consumer did not match");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user