Power graph loading / merging

This commit is contained in:
Anuken 2018-09-19 09:41:30 -04:00
parent c87eaaa928
commit af5b579a2f
6 changed files with 41 additions and 24 deletions

View File

@ -216,6 +216,10 @@ public class World extends Module{
Events.fire(new WorldLoadEvent()); Events.fire(new WorldLoadEvent());
} }
public boolean isGenerating(){
return generating;
}
/**Loads up a sector map. This does not call play(), but calls reset().*/ /**Loads up a sector map. This does not call play(), but calls reset().*/
public void loadSector(Sector sector){ public void loadSector(Sector sector){
currentSector = sector; currentSector = sector;

View File

@ -186,7 +186,6 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
if(other == null || other.entity == null || other.getTeamID() != tile.getTeamID()) continue; if(other == null || other.entity == null || other.getTeamID() != tile.getTeamID()) continue;
other = other.target(); other = other.target();
if(other.block().hasPower) other.block().updatePowerGraph(other);
other.block().onProximityUpdate(other); other.block().onProximityUpdate(other);
tmpTiles.add(other); tmpTiles.add(other);

View File

@ -19,7 +19,6 @@ import io.anuke.mindustry.input.CursorType;
import io.anuke.mindustry.type.ContentType; import io.anuke.mindustry.type.ContentType;
import io.anuke.mindustry.type.Item; import io.anuke.mindustry.type.Item;
import io.anuke.mindustry.type.ItemStack; import io.anuke.mindustry.type.ItemStack;
import io.anuke.mindustry.world.blocks.power.PowerGraph;
import io.anuke.mindustry.world.meta.*; import io.anuke.mindustry.world.meta.*;
import io.anuke.ucore.core.Timers; import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw; import io.anuke.ucore.graphics.Draw;
@ -142,22 +141,11 @@ public class Block extends BaseBlock {
public void updatePowerGraph(Tile tile){ public void updatePowerGraph(Tile tile){
TileEntity entity = tile.entity(); TileEntity entity = tile.entity();
if(entity.power.graph == null){
for(Tile other : entity.proximity()){ for(Tile other : entity.proximity()){
other = other.target();
if(other.entity.power != null){ if(other.entity.power != null){
entity.power.graph = other.entity.power.graph; other.entity.power.graph.add(entity.power.graph);
entity.power.graph.add(tile);
return;
} }
} }
entity.power.graph = new PowerGraph();
entity.power.graph.add(tile);
}else{
//TODO
}
} }
public void powerGraphRemoved(Tile tile){ public void powerGraphRemoved(Tile tile){
@ -504,7 +492,8 @@ public class Block extends BaseBlock {
"entity.x", tile.entity.x, "entity.x", tile.entity.x,
"entity.y", tile.entity.y, "entity.y", tile.entity.y,
"entity.id", tile.entity.id, "entity.id", tile.entity.id,
"entity.items.total", hasItems ? tile.entity.items.total() : null "entity.items.total", hasItems ? tile.entity.items.total() : null,
"entity.graph", tile.entity.power != null && tile.entity.power.graph != null ? tile.entity.power.graph.getID() : null
); );
} }
} }

View File

@ -420,8 +420,13 @@ public class Tile implements PosTrait, TargetTrait{
entity.cons = new ConsumeModule(); entity.cons = new ConsumeModule();
if(block.hasItems) entity.items = new InventoryModule(); if(block.hasItems) entity.items = new InventoryModule();
if(block.hasLiquids) entity.liquids = new LiquidModule(); if(block.hasLiquids) entity.liquids = new LiquidModule();
if(block.hasPower) entity.power = new PowerModule(); if(block.hasPower){
entity.power = new PowerModule();
entity.power.graph.add(this);
}
if(!world.isGenerating()){
entity.updateProximity(); entity.updateProximity();
}
}else if(!(block instanceof BlockPart)){ }else if(!(block instanceof BlockPart)){
//since the entity won't update proximity for us, update proximity for all nearby tiles manually //since the entity won't update proximity for us, update proximity for all nearby tiles manually
for(GridPoint2 p : Geometry.d4){ for(GridPoint2 p : Geometry.d4){

View File

@ -3,6 +3,7 @@ package io.anuke.mindustry.world.blocks.power;
import com.badlogic.gdx.utils.ObjectSet; import com.badlogic.gdx.utils.ObjectSet;
import com.badlogic.gdx.utils.Queue; import com.badlogic.gdx.utils.Queue;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.util.Log;
import static io.anuke.mindustry.Vars.threads; import static io.anuke.mindustry.Vars.threads;
@ -14,9 +15,18 @@ public class PowerGraph{
private final ObjectSet<Tile> all = new ObjectSet<>(); private final ObjectSet<Tile> all = new ObjectSet<>();
private long lastFrameUpdated; private long lastFrameUpdated;
private final int graphID;
private static int lastGraphID;
{
graphID = lastGraphID++;
}
public int getID(){
return graphID;
}
public void update(){ public void update(){
//Log.info("producers {0}\nconsumers {1}\nall {2}", producers, consumers, all);
if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 || producers.size == 0){ if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 || producers.size == 0){
return; return;
} }
@ -26,6 +36,8 @@ public class PowerGraph{
for(Tile producer : producers){ for(Tile producer : producers){
float accumulator = producer.entity.power.amount; float accumulator = producer.entity.power.amount;
if(accumulator <= 0.0001f) continue;
float toEach = accumulator / consumers.size; float toEach = accumulator / consumers.size;
float outputs = 0f; float outputs = 0f;
@ -36,8 +48,8 @@ public class PowerGraph{
float finalEach = toEach / outputs; float finalEach = toEach / outputs;
float buffer = 0f; float buffer = 0f;
if(Float.isNaN(finalEach)){ if(Float.isNaN(finalEach) || Float.isInfinite(finalEach)){
return; continue;
} }
for(Tile tile : consumers){ for(Tile tile : consumers){
@ -50,13 +62,21 @@ public class PowerGraph{
} }
} }
public void add(PowerGraph graph){
for(Tile tile : graph.all){
add(tile);
}
}
public void add(Tile tile){ public void add(Tile tile){
tile.entity.power.graph = this;
all.add(tile); all.add(tile);
if(tile.block().outputsPower){ if(tile.block().outputsPower){
producers.add(tile); producers.add(tile);
}else{ }else{
consumers.add(tile); consumers.add(tile);
} }
Log.info("New graph: {0} produce {1} consume {2} total", producers.size, consumers.size, all.size);
} }
public void remove(Tile tile){ public void remove(Tile tile){
@ -69,7 +89,7 @@ public class PowerGraph{
consumers.remove(tile); consumers.remove(tile);
for(Tile other : tile.entity.proximity()){ for(Tile other : tile.entity.proximity()){
if(other.entity.power.graph != null) continue; if(other.entity.power == null || (other.entity.power != null && other.entity.power.graph != null)) continue;
PowerGraph graph = new PowerGraph(); PowerGraph graph = new PowerGraph();
queue.clear(); queue.clear();
queue.addLast(other); queue.addLast(other);

View File

@ -10,7 +10,7 @@ public class PowerModule extends BlockModule{
public float amount; public float amount;
public float capacity = 10f; public float capacity = 10f;
public float voltage = 0.0001f; public float voltage = 0.0001f;
public PowerGraph graph; public PowerGraph graph = new PowerGraph();
public boolean acceptsPower(){ public boolean acceptsPower(){
return amount + 0.001f <= capacity; return amount + 0.001f <= capacity;