mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-03-13 19:39:04 +07:00
Power graph loading / merging
This commit is contained in:
parent
c87eaaa928
commit
af5b579a2f
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
@ -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){
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user