More efficient way of retrieving ConsumePower subclasses

This commit is contained in:
Timmeey86 2018-12-15 23:49:46 +01:00
parent 52f78ee4ed
commit 828661e9de
5 changed files with 21 additions and 40 deletions

View File

@ -345,7 +345,7 @@ public class Block extends BaseBlock {
}
public void setBars(){
if(consumes.hasSubtypeOf(ConsumePower.class) && consumes.getSubtypeOf(ConsumePower.class).isBuffered){
if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){
bars.add(new BlockBar(BarType.power, true, tile -> tile.entity.power.satisfaction));
}
if(hasLiquids) bars.add(new BlockBar(BarType.liquid, true, tile -> tile.entity.liquids.total() / liquidCapacity));
@ -412,8 +412,8 @@ public class Block extends BaseBlock {
explosiveness += tile.entity.liquids.sum((liquid, amount) -> liquid.flammability * amount / 2f);
}
if(consumes.hasSubtypeOf(ConsumePower.class) && consumes.getSubtypeOf(ConsumePower.class).isBuffered){
power += tile.entity.power.satisfaction * consumes.getSubtypeOf(ConsumePower.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);

View File

@ -131,7 +131,7 @@ public class MassDriver extends Block{
public void setStats(){
super.setStats();
stats.add(BlockStat.powerShot, consumes.getSubtypeOf(ConsumePower.class).powerCapacity * powerPercentageUsed, StatUnit.powerUnits);
stats.add(BlockStat.powerShot, consumes.get(ConsumePower.class).powerCapacity * powerPercentageUsed, StatUnit.powerUnits);
}
@Override

View File

@ -48,8 +48,8 @@ public class PowerGraph{
float powerNeeded = 0f;
for(Tile consumer : consumers){
Consumers consumes = consumer.block().consumes;
if(consumes.hasSubtypeOf(ConsumePower.class)){
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
if(consumes.has(ConsumePower.class)){
ConsumePower consumePower = consumes.get(ConsumePower.class);
if(otherConsumersAreValid(consumer, consumePower)){
powerNeeded += consumePower.requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta();
}
@ -62,8 +62,8 @@ public class PowerGraph{
float totalAccumulator = 0f;
for(Tile battery : batteries){
Consumers consumes = battery.block().consumes;
if(consumes.hasSubtypeOf(ConsumePower.class)){
totalAccumulator += battery.entity.power.satisfaction * consumes.getSubtypeOf(ConsumePower.class).powerCapacity;
if(consumes.has(ConsumePower.class)){
totalAccumulator += battery.entity.power.satisfaction * consumes.get(ConsumePower.class).powerCapacity;
}
}
return totalAccumulator;
@ -73,8 +73,8 @@ public class PowerGraph{
float totalCapacity = 0f;
for(Tile battery : batteries){
Consumers consumes = battery.block().consumes;
if(consumes.hasSubtypeOf(ConsumePower.class)){
totalCapacity += consumes.getSubtypeOf(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta();
if(consumes.has(ConsumePower.class)){
totalCapacity += consumes.get(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta();
}
}
return totalCapacity;
@ -88,8 +88,8 @@ public class PowerGraph{
float consumedPowerPercentage = Math.min(1.0f, needed / stored);
for(Tile battery : batteries){
Consumers consumes = battery.block().consumes;
if(consumes.hasSubtypeOf(ConsumePower.class)){
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
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);
}
@ -104,8 +104,8 @@ public class PowerGraph{
for(Tile battery : batteries){
Consumers consumes = battery.block().consumes;
if(consumes.hasSubtypeOf(ConsumePower.class)){
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
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);
@ -121,8 +121,8 @@ public class PowerGraph{
float coverage = Math.min(1, produced / needed);
for(Tile consumer : consumers){
Consumers consumes = consumer.block().consumes;
if(consumes.hasSubtypeOf(ConsumePower.class)){
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
if(consumes.has(ConsumePower.class)){
ConsumePower consumePower = consumes.get(ConsumePower.class);
if(!otherConsumersAreValid(consumer, consumePower)){
consumer.entity.power.satisfaction = 0.0f; // Only supply power if the consumer would get valid that way
}else{
@ -181,7 +181,7 @@ public class PowerGraph{
public void clear(){
for(Tile other : all){
if(other.entity != null && other.entity.power != null){
if(other.block().consumes.hasSubtypeOf(ConsumePower.class) && !other.block().consumes.getSubtypeOf(ConsumePower.class).isBuffered){
if(other.block().consumes.has(ConsumePower.class) && !other.block().consumes.get(ConsumePower.class).isBuffered){
// Reset satisfaction to zero in case of direct consumer. There is no reason to clear power from buffered consumers.
other.entity.power.satisfaction = 0.0f;
}

View File

@ -109,7 +109,7 @@ public class Consumers{
}
public Consume add(Consume consume){
map.put(consume.getClass(), consume);
map.put((consume instanceof ConsumePower ? ConsumePower.class : consume.getClass()), consume);
return consume;
}
@ -121,32 +121,13 @@ public class Consumers{
return map.containsKey(type);
}
public boolean hasSubtypeOf(Class<? extends Consume> type){
for(Consume consume : all()){
if(type.isAssignableFrom(consume.getClass())){
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public <T extends Consume> T get(Class<T> type){
if(!map.containsKey(type)){
throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "'!");
}
return (T) map.get(type);
}
@SuppressWarnings("unchecked")
public <T extends Consume> T getSubtypeOf(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 + "' or any of its subtypes!");
}
}<
public Iterable<Consume> all(){
return map.values();

View File

@ -174,8 +174,8 @@ public class PowerTests extends PowerTestFixture{
powerGraph.update();
assertEquals(0.0f, consumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR);
if(consumerTile.block().consumes.hasSubtypeOf(ConsumePower.class)){
ConsumePower consumePower = consumerTile.block().consumes.getSubtypeOf(ConsumePower.class);
if(consumerTile.block().consumes.has(ConsumePower.class)){
ConsumePower consumePower = consumerTile.block().consumes.get(ConsumePower.class);
assertFalse(consumePower.valid(consumerTile.block(), consumerTile.entity()));
}
}