diff --git a/core/src/io/anuke/mindustry/entities/TileEntity.java b/core/src/io/anuke/mindustry/entities/TileEntity.java index 7fbbab0ac4..7c870d0865 100644 --- a/core/src/io/anuke/mindustry/entities/TileEntity.java +++ b/core/src/io/anuke/mindustry/entities/TileEntity.java @@ -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); } diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index 20422dceeb..6bfb196960 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -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 diff --git a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java index 536c4430c5..4022a3c60e 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java @@ -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 queue = new Queue<>(); + private final ObjectSet producers = new ObjectSet<>(); private final ObjectSet consumers = new ObjectSet<>(); private final ObjectSet 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); + } + } + } + } } }