Synchronized Item/Liquid generator code

This commit is contained in:
Timmeey86
2018-11-30 00:03:53 +01:00
parent 1d57568322
commit bcb9cfd2fe
5 changed files with 24 additions and 39 deletions

View File

@@ -76,6 +76,11 @@ public abstract class ItemGenerator extends PowerGenerator{
public void update(Tile tile){ public void update(Tile tile){
ItemGeneratorEntity entity = tile.entity(); ItemGeneratorEntity entity = tile.entity();
if(!entity.cons.valid()){
entity.productionEfficiency = 0.0f;
return;
}
if(entity.generateTime <= 0f && entity.items.total() > 0){ if(entity.generateTime <= 0f && entity.items.total() > 0){
Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f)); Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f));
Item item = entity.items.take(); Item item = entity.items.take();
@@ -92,6 +97,8 @@ public abstract class ItemGenerator extends PowerGenerator{
entity.damage(Mathf.random(8f)); entity.damage(Mathf.random(8f));
Effects.effect(explodeEffect, tile.worldx() + Mathf.range(size * tilesize / 2f), tile.worldy() + Mathf.range(size * tilesize / 2f)); Effects.effect(explodeEffect, tile.worldx() + Mathf.range(size * tilesize / 2f), tile.worldy() + Mathf.range(size * tilesize / 2f));
} }
}else{
entity.productionEfficiency = 0.0f;
} }
super.update(tile); super.update(tile);

View File

@@ -35,8 +35,6 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{
public void update(Tile tile){ public void update(Tile tile){
ItemGeneratorEntity entity = tile.entity(); ItemGeneratorEntity entity = tile.entity();
entity.power.graph.update();
Liquid liquid = null; Liquid liquid = null;
for(Liquid other : content.liquids()){ for(Liquid other : content.liquids()){
if(entity.liquids.get(other) >= 0.001f && getLiquidEfficiency(other) >= minLiquidEfficiency){ if(entity.liquids.get(other) >= 0.001f && getLiquidEfficiency(other) >= minLiquidEfficiency){
@@ -67,31 +65,14 @@ public abstract class ItemLiquidGenerator extends ItemGenerator{
if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){ if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){
Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f)); Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f));
} }
// Update the graph manually instead of letting the base class do it since that would consume items
entity.power.graph.update();
}else{ }else{
// No liquids accepted, act like a default ItemGenerator
if(entity.generateTime <= 0f && entity.items.total() > 0){
Effects.effect(generateEffect, tile.worldx() + Mathf.range(3f), tile.worldy() + Mathf.range(3f));
Item item = entity.items.take();
entity.productionEfficiency = getItemEfficiency(item);
entity.explosiveness = item.explosiveness;
entity.generateTime = 1f;
}
if(entity.generateTime > 0f){
entity.generateTime -= 1f / itemDuration * entity.delta();
entity.generateTime = Mathf.clamp(entity.generateTime);
if(Mathf.chance(entity.delta() * 0.06 * Mathf.clamp(entity.explosiveness - 0.25f))){
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;
}
}
super.update(tile); super.update(tile);
} }
}
@Override @Override
public void draw(Tile tile){ public void draw(Tile tile){

View File

@@ -48,18 +48,21 @@ public abstract class LiquidGenerator extends PowerGenerator{
@Override @Override
public void update(Tile tile){ public void update(Tile tile){
TileEntity entity = tile.entity(); ItemGeneratorEntity entity = tile.entity();
// 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();
// TODO Code duplication with ItemLiquidGenerator
if(entity.liquids.get(entity.liquids.current()) >= 0.001f){ if(entity.liquids.get(entity.liquids.current()) >= 0.001f){
//float powerPerLiquid = getEfficiency(entity.liquids.current()) * this.powerPerLiquid; float baseLiquidEfficiency = getEfficiency(entity.liquids.current()) * this.liquidPowerMultiplier;
float used = Math.min(entity.liquids.currentAmount(), maxLiquidGenerate * entity.delta()); float maximumPossible = maxLiquidGenerate * calculationDelta;
// TODO Adapt to new power system float used = Math.min(entity.liquids.currentAmount() * calculationDelta, maximumPossible);
//used = Math.min(used, (powerCapacity - entity.power.amount) / powerPerLiquid);
entity.liquids.remove(entity.liquids.current(), used); entity.liquids.remove(entity.liquids.current(), used);
// TODO Adapt to new power system
//entity.power.amount += used * powerPerLiquid; // Note: 1 Item with 100% Flammability = 100% efficiency. This means 100% is not max but rather a reference point for this generator.
entity.productionEfficiency = baseLiquidEfficiency * used / maximumPossible;
if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){ if(used > 0.001f && Mathf.chance(0.05 * entity.delta())){
Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f)); Effects.effect(generateEffect, tile.drawx() + Mathf.range(3f), tile.drawy() + Mathf.range(3f));

View File

@@ -52,13 +52,6 @@ public class MechPad extends Block{
super.init(); super.init();
} }
@Override
public void setStats(){
super.setStats();
// TODO Verify for new power system
//stats.remove(BlockStat.powerUse);
}
@Override @Override
public boolean shouldConsume(Tile tile){ public boolean shouldConsume(Tile tile){
return false; return false;

View File

@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.DynamicTest.dynamicTest;
* Additionally, each PowerGraph::update() call will have its own thread frame, i.e. the method will never be called twice within the same frame. * Additionally, each PowerGraph::update() call will have its own thread frame, i.e. the method will never be called twice within the same frame.
* Both of these constraints are handled by FakeThreadHandler within PowerTestFixture. * Both of these constraints are handled by FakeThreadHandler within PowerTestFixture.
* Any power amount (produced, consumed, buffered) should be affected by FakeThreadHandler.fakeDelta but satisfaction should not! * Any power amount (produced, consumed, buffered) should be affected by FakeThreadHandler.fakeDelta but satisfaction should not!
* TODO Find a way to reuse these tests for LiquidGenerator and ItemGenerator
*/ */
public class ItemLiquidGeneratorTests extends PowerTestFixture{ public class ItemLiquidGeneratorTests extends PowerTestFixture{