Power graph initial commit

This commit is contained in:
Anuken
2018-09-12 18:35:29 -04:00
parent 7b4e6128ce
commit abd87793e4
8 changed files with 69 additions and 64 deletions

View File

@@ -75,12 +75,12 @@ public class ProductionBlocks extends BlockList implements ContentList{
waterextractor = new SolidPump("water-extractor"){{
result = Liquids.water;
pumpAmount = 0.1f;
pumpAmount = 0.065f;
size = 2;
liquidCapacity = 30f;
rotateSpeed = 1.4f;
consumes.power(0.15f);
consumes.power(0.13f);
}};
oilextractor = new Fracker("oil-extractor"){{

View File

@@ -179,19 +179,19 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
for(GridPoint2 point : nearby){
Tile other = world.tile(tile.x + point.x, tile.y + point.y);
if(other != null){
other.block().onProximityUpdate(other);
other = other.target();
if(other == null || other.entity == null || other.getTeamID() != tile.getTeamID()) continue;
other = other.target();
if(other.entity.power != null) other.block().updatePowerGraph(other);
other.block().onProximityUpdate(other);
tmpTiles.add(other);
//add this tile to proximity of nearby tiles
if(!other.entity.proximity.contains(tile, true)){
other.entity.proximity.add(tile);
}
if(other != null && other.entity != null){
tmpTiles.add(other);
//add this tile to proximity of nearby tiles
if(!other.entity.proximity.contains(tile, true)){
other.entity.proximity.add(tile);
}
}
}
//using a set to prevent duplicates
@@ -199,6 +199,7 @@ public class TileEntity extends BaseEntity implements TargetTrait, HealthTrait{
proximity.add(tile);
}
if(power != null) tile.block().updatePowerGraph(tile);
tile.block().onProximityUpdate(tile);
}

View File

@@ -25,6 +25,7 @@ public abstract class BaseBlock extends MappableContent{
public boolean outputsLiquid = false;
public boolean singleLiquid = true;
public boolean outputsPower = false;
public int itemCapacity;
public float liquidCapacity = 10f;

View File

@@ -19,6 +19,7 @@ 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;
@@ -138,6 +139,26 @@ public class Block extends BaseBlock {
return drops != null && drops.item == item;
}
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;
return;
}
}
entity.power.graph = new PowerGraph();
entity.power.graph.add(tile);
}else{
//TODO
}
}
public boolean isLayer(Tile tile){
return true;
}

View File

@@ -1,66 +1,17 @@
package io.anuke.mindustry.world.blocks.power;
import com.badlogic.gdx.math.GridPoint2;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.world.Edges;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.PowerBlock;
import io.anuke.ucore.core.Timers;
public class PowerDistributor extends PowerBlock{
public PowerDistributor(String name){
super(name);
}
protected void distributePower(Tile tile){
TileEntity entity = tile.entity;
int sources = 0;
if(entity == null) return;
if(Float.isNaN(entity.power.amount)){
entity.power.amount = 0f;
}
for(GridPoint2 point : Edges.getEdges(size)){
Tile target = tile.getNearby(point);
if(target != null && target.block().hasPower &&
shouldDistribute(tile, target)) sources++;
}
if(sources == 0) return;
float result = entity.power.amount / sources;
for(GridPoint2 point : Edges.getEdges(size)){
Tile target = tile.getNearby(point);
if(target == null) continue;
target = target.target();
if(target.block().hasPower && shouldDistribute(tile, target)){
float diff = (tile.entity.power.amount / powerCapacity - target.entity.power.amount / target.block().powerCapacity) / 1.4f;
float transmit = Math.min(result * Timers.delta(), diff * powerCapacity);
if(target.block().acceptPower(target, tile, transmit)){
float transferred = target.block().addPower(target, transmit);
entity.power.amount -= transferred;
}
}
}
}
protected boolean shouldDistribute(Tile tile, Tile other){
other = other.target();
//only generators can distribute to other generators
return other.getTeamID() == tile.getTeamID() && (!(other.block() instanceof PowerGenerator) || tile.block() instanceof PowerGenerator)
&& other.entity != null
&& other.block().hasPower
&& other.entity.power.amount / other.block().powerCapacity < tile.entity.power.amount / powerCapacity;
outputsPower = true;
}
@Override
public void update(Tile tile){
distributePower(tile);
tile.entity.power.graph.distribute(tile);
}
}

View File

@@ -0,0 +1,27 @@
package io.anuke.mindustry.world.blocks.power;
import com.badlogic.gdx.utils.ObjectSet;
import io.anuke.mindustry.world.Tile;
public class PowerGraph{
private ObjectSet<Tile> producers = new ObjectSet<>();
private ObjectSet<Tile> consumers = new ObjectSet<>();
private ObjectSet<Tile> all = new ObjectSet<>();
public void distribute(Tile tile){
}
public void add(Tile tile){
all.add(tile);
if(tile.block().outputsPower){
producers.add(tile);
}
}
public void remove(Tile tile){
all.add(tile);
producers.remove(tile);
consumers.remove(tile);
}
}

View File

@@ -49,6 +49,7 @@ public class PowerNode extends PowerBlock{
layer = Layer.power;
powerCapacity = 5f;
configurable = true;
outputsPower = true;
}
@Remote(targets = Loc.both, called = Loc.server, forward = true)

View File

@@ -1,5 +1,7 @@
package io.anuke.mindustry.world.modules;
import io.anuke.mindustry.world.blocks.power.PowerGraph;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
@@ -8,6 +10,7 @@ public class PowerModule extends BlockModule{
public float amount;
public float capacity = 10f;
public float voltage = 0.0001f;
public PowerGraph graph;
public boolean acceptsPower(){
return amount + 0.001f <= capacity;