Merge pull request #16 from Timmeey86/baltitenger
More efficient way of retrieving ConsumePower subclasses
This commit is contained in:
@@ -345,7 +345,7 @@ public class Block extends BaseBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setBars(){
|
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));
|
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));
|
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);
|
explosiveness += tile.entity.liquids.sum((liquid, amount) -> liquid.flammability * amount / 2f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class) && consumes.getSubtypeOf(ConsumePower.class).isBuffered){
|
if(consumes.has(ConsumePower.class) && consumes.get(ConsumePower.class).isBuffered){
|
||||||
power += tile.entity.power.satisfaction * consumes.getSubtypeOf(ConsumePower.class).powerCapacity;
|
power += tile.entity.power.satisfaction * consumes.get(ConsumePower.class).powerCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
tempColor.mul(1f / units);
|
tempColor.mul(1f / units);
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ public class MassDriver extends Block{
|
|||||||
public void setStats(){
|
public void setStats(){
|
||||||
super.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
|
@Override
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ public class PowerGraph{
|
|||||||
float powerNeeded = 0f;
|
float powerNeeded = 0f;
|
||||||
for(Tile consumer : consumers){
|
for(Tile consumer : consumers){
|
||||||
Consumers consumes = consumer.block().consumes;
|
Consumers consumes = consumer.block().consumes;
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
|
ConsumePower consumePower = consumes.get(ConsumePower.class);
|
||||||
if(otherConsumersAreValid(consumer, consumePower)){
|
if(otherConsumersAreValid(consumer, consumePower)){
|
||||||
powerNeeded += consumePower.requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta();
|
powerNeeded += consumePower.requestedPower(consumer.block(), consumer.entity) * consumer.entity.delta();
|
||||||
}
|
}
|
||||||
@@ -62,8 +62,8 @@ public class PowerGraph{
|
|||||||
float totalAccumulator = 0f;
|
float totalAccumulator = 0f;
|
||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
Consumers consumes = battery.block().consumes;
|
Consumers consumes = battery.block().consumes;
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
totalAccumulator += battery.entity.power.satisfaction * consumes.getSubtypeOf(ConsumePower.class).powerCapacity;
|
totalAccumulator += battery.entity.power.satisfaction * consumes.get(ConsumePower.class).powerCapacity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return totalAccumulator;
|
return totalAccumulator;
|
||||||
@@ -73,8 +73,8 @@ public class PowerGraph{
|
|||||||
float totalCapacity = 0f;
|
float totalCapacity = 0f;
|
||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
Consumers consumes = battery.block().consumes;
|
Consumers consumes = battery.block().consumes;
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
totalCapacity += consumes.getSubtypeOf(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta();
|
totalCapacity += consumes.get(ConsumePower.class).requestedPower(battery.block(), battery.entity) * battery.entity.delta();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return totalCapacity;
|
return totalCapacity;
|
||||||
@@ -88,8 +88,8 @@ public class PowerGraph{
|
|||||||
float consumedPowerPercentage = Math.min(1.0f, needed / stored);
|
float consumedPowerPercentage = Math.min(1.0f, needed / stored);
|
||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
Consumers consumes = battery.block().consumes;
|
Consumers consumes = battery.block().consumes;
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
|
ConsumePower consumePower = consumes.get(ConsumePower.class);
|
||||||
if(consumePower.powerCapacity > 0f){
|
if(consumePower.powerCapacity > 0f){
|
||||||
battery.entity.power.satisfaction = Math.max(0.0f, battery.entity.power.satisfaction - consumedPowerPercentage);
|
battery.entity.power.satisfaction = Math.max(0.0f, battery.entity.power.satisfaction - consumedPowerPercentage);
|
||||||
}
|
}
|
||||||
@@ -104,8 +104,8 @@ public class PowerGraph{
|
|||||||
|
|
||||||
for(Tile battery : batteries){
|
for(Tile battery : batteries){
|
||||||
Consumers consumes = battery.block().consumes;
|
Consumers consumes = battery.block().consumes;
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
|
ConsumePower consumePower = consumes.get(ConsumePower.class);
|
||||||
if(consumePower.powerCapacity > 0f){
|
if(consumePower.powerCapacity > 0f){
|
||||||
float additionalPowerPercentage = Math.min(1.0f, excess / consumePower.powerCapacity);
|
float additionalPowerPercentage = Math.min(1.0f, excess / consumePower.powerCapacity);
|
||||||
battery.entity.power.satisfaction = Math.min(1.0f, battery.entity.power.satisfaction + additionalPowerPercentage);
|
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);
|
float coverage = Math.min(1, produced / needed);
|
||||||
for(Tile consumer : consumers){
|
for(Tile consumer : consumers){
|
||||||
Consumers consumes = consumer.block().consumes;
|
Consumers consumes = consumer.block().consumes;
|
||||||
if(consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumes.has(ConsumePower.class)){
|
||||||
ConsumePower consumePower = consumes.getSubtypeOf(ConsumePower.class);
|
ConsumePower consumePower = consumes.get(ConsumePower.class);
|
||||||
if(!otherConsumersAreValid(consumer, consumePower)){
|
if(!otherConsumersAreValid(consumer, consumePower)){
|
||||||
consumer.entity.power.satisfaction = 0.0f; // Only supply power if the consumer would get valid that way
|
consumer.entity.power.satisfaction = 0.0f; // Only supply power if the consumer would get valid that way
|
||||||
}else{
|
}else{
|
||||||
@@ -181,7 +181,7 @@ public class PowerGraph{
|
|||||||
public void clear(){
|
public void clear(){
|
||||||
for(Tile other : all){
|
for(Tile other : all){
|
||||||
if(other.entity != null && other.entity.power != null){
|
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.
|
// 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;
|
other.entity.power.satisfaction = 0.0f;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ public class Consumers{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Consume add(Consume consume){
|
public Consume add(Consume consume){
|
||||||
map.put(consume.getClass(), consume);
|
map.put((consume instanceof ConsumePower ? ConsumePower.class : consume.getClass()), consume);
|
||||||
return consume;
|
return consume;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,32 +121,13 @@ public class Consumers{
|
|||||||
return map.containsKey(type);
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends Consume> T get(Class<T> type){
|
public <T extends Consume> T get(Class<T> type){
|
||||||
if(!map.containsKey(type)){
|
if(!map.containsKey(type)){
|
||||||
throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "'!");
|
throw new IllegalArgumentException("Block does not contain consumer of type '" + type + "'!");
|
||||||
}
|
}
|
||||||
return (T) map.get(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(){
|
public Iterable<Consume> all(){
|
||||||
return map.values();
|
return map.values();
|
||||||
|
|||||||
@@ -174,8 +174,8 @@ public class PowerTests extends PowerTestFixture{
|
|||||||
powerGraph.update();
|
powerGraph.update();
|
||||||
|
|
||||||
assertEquals(0.0f, consumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR);
|
assertEquals(0.0f, consumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR);
|
||||||
if(consumerTile.block().consumes.hasSubtypeOf(ConsumePower.class)){
|
if(consumerTile.block().consumes.has(ConsumePower.class)){
|
||||||
ConsumePower consumePower = consumerTile.block().consumes.getSubtypeOf(ConsumePower.class);
|
ConsumePower consumePower = consumerTile.block().consumes.get(ConsumePower.class);
|
||||||
assertFalse(consumePower.valid(consumerTile.block(), consumerTile.entity()));
|
assertFalse(consumePower.valid(consumerTile.block(), consumerTile.entity()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user