Building loop cleanup

This commit is contained in:
Anuken
2022-02-13 19:33:04 -05:00
parent 03d99cb05a
commit f85aaed323
12 changed files with 55 additions and 254 deletions

View File

@@ -308,6 +308,7 @@ public class World{
}
state.rules.cloudColor = sector.planet.landCloudColor;
state.rules.environment = sector.planet.defaultEnv;
state.rules.hiddenBuildItems.clear();
state.rules.hiddenBuildItems.addAll(sector.planet.hiddenItems);
sector.info.resources = content.toSeq();

View File

@@ -109,7 +109,7 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
set(tile.drawx(), tile.drawy());
if(shouldAdd){
if(shouldAdd && shouldUpdate()){
add();
}
@@ -524,7 +524,7 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
/** Call when this entity is updating. This wakes it up. */
public void noSleep(){
sleepTime = 0f;
if(sleeping){
if(sleeping && shouldUpdate()){
add();
sleeping = false;
sleepingEntities--;
@@ -1508,6 +1508,10 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
this.team = next;
indexer.addIndex(tile);
Events.fire(teamChangeEvent.set(last, self()));
if(!shouldUpdate()){
remove();
}
}
public boolean canPickup(){
@@ -1522,6 +1526,7 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
/** Called right after this building is picked up. */
public void afterPickedUp(){
if(power != null){
//TODO can lead to ghost graphs?
power.graph = new PowerGraph();
power.links.clear();
if(block.consumes.hasPower() && !block.consumes.getPower().buffered){
@@ -1773,9 +1778,8 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
//TODO should just avoid updating buildings instead
if(state.isEditor()) return;
//TODO refactor to timestamp-based system
timeScaleDuration -= Time.delta;
if(timeScaleDuration <= 0f || !block.canOverdrive){
//TODO refactor to timestamp-based system?
if((timeScaleDuration -= Time.delta) <= 0f || !block.canOverdrive){
timeScale = 1f;
}
@@ -1789,11 +1793,6 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
}
}
//TODO this check should not be here, just remove unsupported buildings instead
if(team == Team.derelict || !block.supportsEnv(state.rules.environment)){
enabled = false;
}
//TODO separate system for sound? AudioSource, etc
if(!headless){
if(sound != null){
@@ -1805,18 +1804,13 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
}
}
//TODO consume module is not necessary for every building, e.g. conveyors should not have it - perhaps it should be nullable?
//TODO updating consumption is not necessary for every building, e.g. conveyors should not have it
updateConsumption();
//TODO just handle per-block instead
if(enabled || !block.noUpdateDisabled){
updateTile();
}
//TODO power graph should be separate entity
if(power != null){
power.graph.update();
}
}
@Override

View File

@@ -0,0 +1,16 @@
package mindustry.entities.comp;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
import mindustry.world.blocks.power.*;
@EntityDef(value = PowerGraphUpdaterc.class, serialize = false, genio = false)
@Component
abstract class PowerGraphUpdaterComp implements Entityc{
public transient PowerGraph graph;
@Override
public void update(){
graph.update();
}
}

View File

@@ -206,7 +206,7 @@ public class OverlayRenderer{
if(build != null && build.team == player.team()){
build.drawSelect();
if(!build.enabled && build.block.drawDisabled){
if((!build.enabled && build.block.drawDisabled) || !build.shouldUpdate()){
build.drawDisabled();
}

View File

@@ -6,8 +6,6 @@ import arc.util.*;
import mindustry.gen.*;
import mindustry.world.consumers.*;
import static mindustry.Vars.*;
public class PowerGraph{
private static final Queue<Building> queue = new Queue<>();
private static final Seq<Building> outArray1 = new Seq<>();
@@ -20,17 +18,19 @@ public class PowerGraph{
public final Seq<Building> batteries = new Seq<>(false);
public final Seq<Building> all = new Seq<>(false);
private final PowerGraphUpdater entity;
private final WindowedMean powerBalance = new WindowedMean(60);
private float lastPowerProduced, lastPowerNeeded, lastPowerStored;
private float lastScaledPowerIn, lastScaledPowerOut, lastCapacity;
//diodes workaround for correct energy production info
private float energyDelta = 0f;
private long lastFrameUpdated = -1;
private final int graphID;
private static int lastGraphID;
{
public PowerGraph(){
entity = PowerGraphUpdater.create();
entity.graph = this;
graphID = lastGraphID++;
}
@@ -207,9 +207,7 @@ public class PowerGraph{
}
public void update(){
if(state.updateId == lastFrameUpdated){
return;
}else if(!consumers.isEmpty() && consumers.first().cheating()){
if(!consumers.isEmpty() && consumers.first().cheating()){
//when cheating, just set status to 1
for(Building tile : consumers){
tile.power.status = 1f;
@@ -219,8 +217,6 @@ public class PowerGraph{
return;
}
lastFrameUpdated = state.updateId;
float powerNeeded = getPowerNeeded();
float powerProduced = getPowerProduced();
@@ -255,6 +251,8 @@ 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();
for(Building tile : graph.all){
add(tile);
@@ -265,9 +263,16 @@ public class PowerGraph{
if(build == null || build.power == null) return;
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();
}
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.consumes.getPower().buffered){
producers.add(build);
@@ -287,6 +292,8 @@ public class PowerGraph{
producers.clear();
consumers.clear();
batteries.clear();
//nothing left
entity.remove();
}
public void reflow(Building tile){
@@ -313,7 +320,7 @@ public class PowerGraph{
}
/** Note that this does not actually remove the building from the graph;
* it creates *new* graphs that contain the correct buildings. */
* it creates *new* graphs that contain the correct buildings. Doing this invalidates the graph. */
public void remove(Building tile){
//go through all the connections of this tile
@@ -345,6 +352,9 @@ public class PowerGraph{
//update the graph once so direct consumers without any connected producer lose their power
graph.update();
}
//implied empty graph here
entity.remove();
}
private boolean otherConsumersAreValid(Building build, Consume consumePower){
@@ -363,7 +373,6 @@ public class PowerGraph{
", consumers=" + consumers +
", batteries=" + batteries +
", all=" + all +
", lastFrameUpdated=" + lastFrameUpdated +
", graphID=" + graphID +
'}';
}

View File

@@ -379,11 +379,6 @@ public class PowerNode extends PowerBlock{
updatePowerGraph();
}
@Override
public void updateTile(){
power.graph.update();
}
@Override
public boolean onConfigureTileTapped(Building other){
if(linkValid(this, other)){