Graph removal
This commit is contained in:
@@ -160,6 +160,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
|
|||||||
if(power != null){
|
if(power != null){
|
||||||
tile.block().powerGraphRemoved(tile);
|
tile.block().powerGraphRemoved(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
GridPoint2[] nearby = Edges.getEdges(tile.block().size);
|
GridPoint2[] nearby = Edges.getEdges(tile.block().size);
|
||||||
for(GridPoint2 point : nearby){
|
for(GridPoint2 point : nearby){
|
||||||
Tile other = world.tile(tile.x + point.x, tile.y + point.y);
|
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;
|
if(other == null || other.entity == null || other.getTeamID() != tile.getTeamID()) continue;
|
||||||
other = other.target();
|
other = other.target();
|
||||||
|
|
||||||
if(other.entity.power != null) other.block().updatePowerGraph(other);
|
if(other.block().hasPower) other.block().updatePowerGraph(other);
|
||||||
other.block().onProximityUpdate(other);
|
other.block().onProximityUpdate(other);
|
||||||
|
|
||||||
tmpTiles.add(other);
|
tmpTiles.add(other);
|
||||||
@@ -194,7 +195,6 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
|
|||||||
if(!other.entity.proximity.contains(tile, true)){
|
if(!other.entity.proximity.contains(tile, true)){
|
||||||
other.entity.proximity.add(tile);
|
other.entity.proximity.add(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//using a set to prevent duplicates
|
//using a set to prevent duplicates
|
||||||
@@ -202,7 +202,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
|
|||||||
proximity.add(tile);
|
proximity.add(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(power != null) tile.block().updatePowerGraph(tile);
|
if(tile.block().hasPower) tile.block().updatePowerGraph(tile);
|
||||||
tile.block().onProximityUpdate(tile);
|
tile.block().onProximityUpdate(tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -148,11 +148,12 @@ public class Block extends BaseBlock {
|
|||||||
other = other.target();
|
other = other.target();
|
||||||
if(other.entity.power != null){
|
if(other.entity.power != null){
|
||||||
entity.power.graph = other.entity.power.graph;
|
entity.power.graph = other.entity.power.graph;
|
||||||
|
entity.power.graph.add(tile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entity.power.graph = new PowerGraph(entity);
|
entity.power.graph = new PowerGraph();
|
||||||
entity.power.graph.add(tile);
|
entity.power.graph.add(tile);
|
||||||
}else{
|
}else{
|
||||||
//TODO
|
//TODO
|
||||||
|
|||||||
@@ -1,28 +1,22 @@
|
|||||||
package io.anuke.mindustry.world.blocks.power;
|
package io.anuke.mindustry.world.blocks.power;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.ObjectSet;
|
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 io.anuke.mindustry.world.Tile;
|
||||||
|
|
||||||
import static io.anuke.mindustry.Vars.threads;
|
import static io.anuke.mindustry.Vars.threads;
|
||||||
|
|
||||||
public class PowerGraph{
|
public class PowerGraph{
|
||||||
|
private final static Queue<Tile> queue = new Queue<>();
|
||||||
|
|
||||||
private final ObjectSet<Tile> producers = new ObjectSet<>();
|
private final ObjectSet<Tile> producers = new ObjectSet<>();
|
||||||
private final ObjectSet<Tile> consumers = new ObjectSet<>();
|
private final ObjectSet<Tile> consumers = new ObjectSet<>();
|
||||||
private final ObjectSet<Tile> all = new ObjectSet<>();
|
private final ObjectSet<Tile> all = new ObjectSet<>();
|
||||||
private final TileEntity seed;
|
|
||||||
|
|
||||||
private long lastFrameUpdated;
|
private long lastFrameUpdated;
|
||||||
|
|
||||||
public PowerGraph(TileEntity seed){
|
|
||||||
this.seed = seed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSeed(TileEntity entity){
|
|
||||||
return seed == entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -66,8 +60,29 @@ public class PowerGraph{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void remove(Tile tile){
|
public void remove(Tile tile){
|
||||||
|
for(Tile other : all){
|
||||||
|
other.entity.power.graph = null;
|
||||||
|
}
|
||||||
|
|
||||||
all.remove(tile);
|
all.remove(tile);
|
||||||
producers.remove(tile);
|
producers.remove(tile);
|
||||||
consumers.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user