From af5b579a2fac9a1cab8a88d320b3119ad9dfe6e0 Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 19 Sep 2018 09:41:30 -0400 Subject: [PATCH] Power graph loading / merging --- core/src/io/anuke/mindustry/core/World.java | 4 +++ .../anuke/mindustry/entities/TileEntity.java | 1 - core/src/io/anuke/mindustry/world/Block.java | 21 ++++---------- core/src/io/anuke/mindustry/world/Tile.java | 9 ++++-- .../world/blocks/power/PowerGraph.java | 28 ++++++++++++++++--- .../mindustry/world/modules/PowerModule.java | 2 +- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/World.java b/core/src/io/anuke/mindustry/core/World.java index 6f0a3dd43d..16fd6f710e 100644 --- a/core/src/io/anuke/mindustry/core/World.java +++ b/core/src/io/anuke/mindustry/core/World.java @@ -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; diff --git a/core/src/io/anuke/mindustry/entities/TileEntity.java b/core/src/io/anuke/mindustry/entities/TileEntity.java index 7c870d0865..8e409ff572 100644 --- a/core/src/io/anuke/mindustry/entities/TileEntity.java +++ b/core/src/io/anuke/mindustry/entities/TileEntity.java @@ -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); diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index 6bfb196960..97cc9fbe95 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -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 ); } } \ No newline at end of file diff --git a/core/src/io/anuke/mindustry/world/Tile.java b/core/src/io/anuke/mindustry/world/Tile.java index 2ea97939b5..1b585bc7aa 100644 --- a/core/src/io/anuke/mindustry/world/Tile.java +++ b/core/src/io/anuke/mindustry/world/Tile.java @@ -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){ 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 4022a3c60e..ef25574e5f 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerGraph.java @@ -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 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); diff --git a/core/src/io/anuke/mindustry/world/modules/PowerModule.java b/core/src/io/anuke/mindustry/world/modules/PowerModule.java index b75a85fc76..ca250a93c3 100644 --- a/core/src/io/anuke/mindustry/world/modules/PowerModule.java +++ b/core/src/io/anuke/mindustry/world/modules/PowerModule.java @@ -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;