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());
}
public boolean isGenerating(){
return generating;
}
/**Loads up a sector map. This does not call play(), but calls reset().*/
public void loadSector(Sector 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;
other = other.target();
if(other.block().hasPower) other.block().updatePowerGraph(other);
other.block().onProximityUpdate(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.Item;
import io.anuke.mindustry.type.ItemStack;
import io.anuke.mindustry.world.blocks.power.PowerGraph;
import io.anuke.mindustry.world.meta.*;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.graphics.Draw;
@ -142,21 +141,10 @@ public class Block extends BaseBlock {
public void updatePowerGraph(Tile tile){
TileEntity entity = tile.entity();
if(entity.power.graph == null){
for(Tile other : entity.proximity()){
other = other.target();
if(other.entity.power != null){
entity.power.graph = other.entity.power.graph;
entity.power.graph.add(tile);
return;
}
for(Tile other : entity.proximity()){
if(other.entity.power != null){
other.entity.power.graph.add(entity.power.graph);
}
entity.power.graph = new PowerGraph();
entity.power.graph.add(tile);
}else{
//TODO
}
}
@ -504,7 +492,8 @@ public class Block extends BaseBlock {
"entity.x", tile.entity.x,
"entity.y", tile.entity.y,
"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();
if(block.hasItems) entity.items = new InventoryModule();
if(block.hasLiquids) entity.liquids = new LiquidModule();
if(block.hasPower) entity.power = new PowerModule();
entity.updateProximity();
if(block.hasPower){
entity.power = new PowerModule();
entity.power.graph.add(this);
}
if(!world.isGenerating()){
entity.updateProximity();
}
}else if(!(block instanceof BlockPart)){
//since the entity won't update proximity for us, update proximity for all nearby tiles manually
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.Queue;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.util.Log;
import static io.anuke.mindustry.Vars.threads;
@ -14,9 +15,18 @@ public class PowerGraph{
private final ObjectSet<Tile> all = new ObjectSet<>();
private long lastFrameUpdated;
private final int graphID;
private static int lastGraphID;
{
graphID = lastGraphID++;
}
public int getID(){
return graphID;
}
public void update(){
//Log.info("producers {0}\nconsumers {1}\nall {2}", producers, consumers, all);
if(threads.getFrameID() == lastFrameUpdated || consumers.size == 0 || producers.size == 0){
return;
}
@ -26,6 +36,8 @@ public class PowerGraph{
for(Tile producer : producers){
float accumulator = producer.entity.power.amount;
if(accumulator <= 0.0001f) continue;
float toEach = accumulator / consumers.size;
float outputs = 0f;
@ -36,8 +48,8 @@ public class PowerGraph{
float finalEach = toEach / outputs;
float buffer = 0f;
if(Float.isNaN(finalEach)){
return;
if(Float.isNaN(finalEach) || Float.isInfinite(finalEach)){
continue;
}
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){
tile.entity.power.graph = this;
all.add(tile);
if(tile.block().outputsPower){
producers.add(tile);
}else{
consumers.add(tile);
}
Log.info("New graph: {0} produce {1} consume {2} total", producers.size, consumers.size, all.size);
}
public void remove(Tile tile){
@ -69,7 +89,7 @@ public class PowerGraph{
consumers.remove(tile);
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();
queue.clear();
queue.addLast(other);

View File

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