This commit is contained in:
Anuken
2022-08-04 20:20:46 -04:00
parent 62d0d62d22
commit 11d3b1d973
3 changed files with 25 additions and 8 deletions

View File

@ -152,6 +152,13 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
return self();
}
@Override
public void add(){
if(power != null){
power.graph.checkAdd();
}
}
@Override
@Replace
public int tileX(){

View File

@ -38,7 +38,7 @@ abstract class PayloadComp implements Posc, Rotc, Hitboxc, Unitc{
//update power graph first, resolve everything
for(Payload pay : payloads){
if(pay instanceof BuildPayload pb && pb.build.power != null){
if(payloadPower == null) payloadPower = new PowerGraph();
if(payloadPower == null) payloadPower = new PowerGraph(false);
//pb.build.team = team;
pb.build.power.graph = null;

View File

@ -18,7 +18,7 @@ public class PowerGraph{
public final Seq<Building> batteries = new Seq<>(false, 16, Building.class);
public final Seq<Building> all = new Seq<>(false, 16, Building.class);
private final PowerGraphUpdater entity;
private final @Nullable PowerGraphUpdater entity;
private final WindowedMean powerBalance = new WindowedMean(60);
private float lastPowerProduced, lastPowerNeeded, lastPowerStored;
private float lastScaledPowerIn, lastScaledPowerOut, lastCapacity;
@ -34,6 +34,11 @@ public class PowerGraph{
graphID = lastGraphID++;
}
public PowerGraph(boolean noEntity){
entity = null;
graphID = lastGraphID++;
}
public int getID(){
return graphID;
}
@ -251,11 +256,12 @@ public class PowerGraph{
public void addGraph(PowerGraph graph){
if(graph == this) return;
//other entity should be removed as the graph was merged
graph.entity.remove();
if(graph.entity != null) graph.entity.remove();
for(Building tile : graph.all){
add(tile);
}
checkAdd();
}
public void add(Building build){
@ -264,14 +270,12 @@ public class PowerGraph{
if(build.power.graph != this || !build.power.init){
//any old graph that is added here MUST be invalid, remove it
if(build.power.graph != null && build.power.graph != this){
build.power.graph.entity.remove();
if( build.power.graph.entity != null) build.power.graph.entity.remove();
}
build.power.graph = this;
build.power.init = true;
all.add(build);
//there's something to update, add the entity
entity.add();
if(build.block.outputsPower && build.block.consumesPower && !build.block.consPower.buffered){
producers.add(build);
@ -286,13 +290,17 @@ public class PowerGraph{
}
}
public void checkAdd(){
if(entity != null) entity.add();
}
public void clear(){
all.clear();
producers.clear();
consumers.clear();
batteries.clear();
//nothing left
entity.remove();
if(entity != null) entity.remove();
}
public void reflow(Building tile){
@ -302,6 +310,7 @@ public class PowerGraph{
while(queue.size > 0){
Building child = queue.removeFirst();
add(child);
checkAdd();
for(Building next : child.getPowerConnections(outArray2)){
if(closedSet.add(next.pos())){
queue.addLast(next);
@ -329,6 +338,7 @@ public class PowerGraph{
//create graph for this branch
PowerGraph graph = new PowerGraph();
graph.checkAdd();
graph.add(other);
//add to queue for BFS
queue.clear();
@ -353,7 +363,7 @@ public class PowerGraph{
}
//implied empty graph here
entity.remove();
if(entity != null) entity.remove();
}
@Deprecated