Fixed handling of item duration

This commit is contained in:
Timmeey86 2018-11-28 11:59:58 +01:00
parent 1f8751054c
commit cf3d2c3def
2 changed files with 33 additions and 5 deletions

View File

@ -45,13 +45,16 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{
}
}
entity.productionEfficiency = 0.0f;
// Note: Do not use this delta when calculating the amount of power or the power efficiency, but use it for resource consumption if necessary.
// Power amount is delta'd by PowerGraph class already.
float calculationDelta = entity.delta();
if(!entity.cons.valid()){
entity.productionEfficiency = 0.0f;
return;
}
//liquid takes priority over solids
if(liquid != null && entity.liquids.get(liquid) >= 0.001f && entity.cons.valid()){
if(liquid != null && entity.liquids.get(liquid) >= 0.001f){
float baseLiquidEfficiency = getLiquidEfficiency(liquid) * this.liquidPowerMultiplier;
float maximumPossible = maxLiquidGenerate * calculationDelta;
float used = Math.min(entity.liquids.get(liquid) * calculationDelta, maximumPossible);
@ -64,7 +67,7 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{
if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){
Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f));
}
}else if(entity.cons.valid()){
}else{
if(entity.generateTime <= 0f && entity.items.total() > 0){
Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f));
@ -82,6 +85,8 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{
entity.damage(Mathf.random(8f));
Effects.effect(explodeEffect, tile.worldx() + Mathf.range(size * tilesize / 2f), tile.worldy() + Mathf.range(size * tilesize / 2f));
}
}else{
entity.productionEfficiency = 0.0f;
}
}
}

View File

@ -29,7 +29,7 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
private Tile tile;
private ItemGenerator.ItemGeneratorEntity entity;
private final float fakeLiquidPowerMultiplier = 2.0f;
private final float fakeItemDuration = 0.5f;
private final float fakeItemDuration = 60f; // 60 ticks
private final float maximumLiquidUsage = 0.5f;
@BeforeEach
@ -102,8 +102,11 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
assertTrue(generator.acceptItem(item, tile, null), parameterDescription + ": Items which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
// Reset items since BeforeEach will not be called between dynamic tests
// Clean up manually since BeforeEach will not be called between dynamic tests
entity.items.clear();
entity.generateTime = 0.0f;
entity.productionEfficiency = 0.0f;
if(amount > 0){
entity.items.add(item, amount);
}
@ -116,4 +119,24 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
assertEquals(expectedRemainingItemAmount, entity.items.get(item), parameterDescription + ": Remaining item amount mismatch.");
assertEquals(expectedEfficiency, entity.productionEfficiency, parameterDescription + ": Efficiency mismatch.");
}
/** Makes sure the efficiency stays equal during the item duration. */
@Test
void test_efficiencyConstantDuringItemDuration(){
// Burn a single coal and test for the duration
entity.items.add(Items.coal, 1);
entity.cons.update(tile.entity);
generator.update(tile);
float expectedEfficiency = entity.productionEfficiency;
float currentDuration = 0.0f;
while((currentDuration += FakeThreadHandler.fakeDelta) <= fakeItemDuration){
generator.update(tile);
assertEquals(expectedEfficiency, entity.productionEfficiency, "Duration: " + String.valueOf(currentDuration));
}
generator.update(tile);
assertEquals(0.0f, entity.productionEfficiency, "Duration: " + String.valueOf(currentDuration));
}
}