Graph removal

This commit is contained in:
Anuken
2018-09-18 20:48:48 -04:00
parent 33418397f8
commit c87eaaa928
3 changed files with 30 additions and 14 deletions

View File

@@ -160,6 +160,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
if(power != null){
tile.block().powerGraphRemoved(tile);
}
GridPoint2[] nearby = Edges.getEdges(tile.block().size);
for(GridPoint2 point : nearby){
Tile other = world.tile(tile.x + point.x, tile.y + point.y);
@@ -185,7 +186,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
if(other == null || other.entity == null || other.getTeamID() != tile.getTeamID()) continue;
other = other.target();
if(other.entity.power != null) other.block().updatePowerGraph(other);
if(other.block().hasPower) other.block().updatePowerGraph(other);
other.block().onProximityUpdate(other);
tmpTiles.add(other);
@@ -194,7 +195,6 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
if(!other.entity.proximity.contains(tile, true)){
other.entity.proximity.add(tile);
}
}
//using a set to prevent duplicates
@@ -202,7 +202,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
proximity.add(tile);
}
if(power != null) tile.block().updatePowerGraph(tile);
if(tile.block().hasPower) tile.block().updatePowerGraph(tile);
tile.block().onProximityUpdate(tile);
}

View File

@@ -148,11 +148,12 @@ public class Block extends BaseBlock {
other = other.target();
if(other.entity.power != null){
entity.power.graph = other.entity.power.graph;
entity.power.graph.add(tile);
return;
}
}
entity.power.graph = new PowerGraph(entity);
entity.power.graph = new PowerGraph();
entity.power.graph.add(tile);
}else{
//TODO

View File

@@ -1,28 +1,22 @@
package io.anuke.mindustry.world.blocks.power;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.entities.TileEntity;
import com.badlogic.gdx.utils.Queue;
import io.anuke.mindustry.world.Tile;
import static io.anuke.mindustry.Vars.threads;
public class PowerGraph{
private final static Queue<Tile> queue = new Queue<>();
private final ObjectSet<Tile> producers = new ObjectSet<>();
private final ObjectSet<Tile> consumers = new ObjectSet<>();
private final ObjectSet<Tile> all = new ObjectSet<>();
private final TileEntity seed;
private long lastFrameUpdated;
public PowerGraph(TileEntity seed){
this.seed = seed;
}
public boolean isSeed(TileEntity entity){
return seed == entity;
}
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;
}
@@ -66,8 +60,29 @@ public class PowerGraph{
}
public void remove(Tile tile){
for(Tile other : all){
other.entity.power.graph = null;
}
all.remove(tile);
producers.remove(tile);
consumers.remove(tile);
for(Tile other : tile.entity.proximity()){
if(other.entity.power.graph != null) continue;
PowerGraph graph = new PowerGraph();
queue.clear();
queue.addLast(other);
while(queue.size > 0){
Tile child = queue.removeFirst();
child.entity.power.graph = graph;
add(child);
for(Tile next : child.entity.proximity()){
if(next != tile && next.entity.power != null && next.entity.power.graph == null){
queue.addLast(next);
}
}
}
}
}
}