Improve performance by reducing unnecessary lighting updates (#32)

Introduce 5 second delay on changing ambient lighting when a machine shuts off. IActiveState now has a concept of wasActiveRecently to enable rendering/block updates to have some wiggle room.
This commit is contained in:
Dave Smith authored and GitHub committed 2019-03-14 12:38:16 -06:00
1 parent 8c79baccf6
commit 2c6d666b89
7 files changed
+47 -49

No files matched your search

@@ -21,6 +21,16 @@ public interface IActiveState {
*/
void setActive(boolean active);
/**
* Determine if a machine/generator was "recently" active, where "recently" is up to the interface
* implementor. This is useful for reducing rendering calls, esp. when the entity can oscillate between
* active/inactive states rapidly.
*
* The default implementation just returns current active state.
*
*/
default boolean wasActiveRecently() { return getActive(); }
/**
* Whether or not this block has a visual effect when it is on it's active state. Used for rendering.
*
@@ -298,11 +298,10 @@ public abstract class BlockMachine extends BlockContainer {
public int getLightValue(IBlockState state, IBlockAccess world, BlockPos pos) {
if (client.enableAmbientLighting) {
TileEntity tileEntity = MekanismUtils.getTileEntitySafe(world, pos);
if (tileEntity instanceof IActiveState) {
if (((IActiveState) tileEntity).getActive() && ((IActiveState) tileEntity).lightUpdate()) {
return client.ambientLightingLevel;
}
if (tileEntity instanceof IActiveState &&
((IActiveState) tileEntity).lightUpdate() &&
((IActiveState) tileEntity).wasActiveRecently()) {
return client.ambientLightingLevel;
}
}
@@ -149,12 +149,6 @@ public class TileEntityChemicalWasher extends TileEntityMachine implements IGasH
TileUtils.readTankData(dataStream, fluidTank);
TileUtils.readTankData(dataStream, inputTank);
TileUtils.readTankData(dataStream, outputTank);
if (updateDelay == 0 && clientActive != isActive) {
updateDelay = general.UPDATE_DELAY;
isActive = clientActive;
MekanismUtils.updateBlock(world, getPos());
}
}
}
@@ -196,9 +196,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt
System.arraycopy(progress, 0, factory.progress, 0, tier.processes);
factory.recipeTicks = recipeTicks;
factory.clientActive = clientActive;
factory.isActive = isActive;
factory.updateDelay = updateDelay;
factory.prevEnergy = prevEnergy;
factory.gasTank.setGas(gasTank.getGas());
factory.sorting = sorting;
@@ -156,9 +156,7 @@ public class TileEntityMetallurgicInfuser extends TileEntityOperationalMachine i
//Machine
factory.progress[0] = operatingTicks;
factory.clientActive = clientActive;
factory.isActive = isActive;
factory.updateDelay = updateDelay;
factory.controlType = controlType;
factory.prevEnergy = prevEnergy;
factory.upgradeComponent.readFrom(upgradeComponent);
@@ -9,7 +9,6 @@ import mekanism.common.Upgrade;
import mekanism.common.base.IRedstoneControl;
import mekanism.common.base.IUpgradeTile;
import mekanism.common.base.TileNetworkList;
import mekanism.common.config.MekanismConfig.general;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.security.ISecurityTile;
import mekanism.common.tile.component.TileComponentSecurity;
@@ -21,18 +20,20 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
public abstract class TileEntityMachine extends TileEntityNoisyBlock implements IUpgradeTile, IRedstoneControl,
ISecurityTile {
public int updateDelay;
public boolean isActive;
public boolean clientActive;
public double prevEnergy;
public double BASE_ENERGY_PER_TICK;
public double energyPerTick;
private long lastActive = -1;
// Number of ticks that a machine can be inactive before it's considered not
// recently active
private final int RECENT_THRESHOLD = 100;
/**
* This machine's current RedstoneControl type.
*/
@@ -54,24 +55,11 @@ public abstract class TileEntityMachine extends TileEntityNoisyBlock implements
public void onUpdate() {
super.onUpdate();
if (world.isRemote && updateDelay > 0) {
updateDelay--;
if (updateDelay == 0 && clientActive != isActive) {
isActive = clientActive;
if (world.isRemote && !isActive && lastActive > 0) {
long updateDiff = world.getTotalWorldTime() - lastActive;
if (updateDiff > RECENT_THRESHOLD) {
MekanismUtils.updateBlock(world, getPos());
}
}
if (!world.isRemote) {
if (updateDelay > 0) {
updateDelay--;
if (updateDelay == 0 && clientActive != isActive) {
Mekanism.packetHandler.sendToReceivers(
new TileEntityMessage(Coord4D.get(this), getNetworkedData(new TileNetworkList())),
new Range4D(Coord4D.get(this)));
}
lastActive = -1;
}
}
}
@@ -88,18 +76,22 @@ public abstract class TileEntityMachine extends TileEntityNoisyBlock implements
@Override
public void setActive(boolean active) {
isActive = active;
boolean stateChange = (isActive != active);
if (clientActive != active && updateDelay == 0) {
if (stateChange) {
isActive = active;
Mekanism.packetHandler
.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new TileNetworkList())),
new Range4D(Coord4D.get(this)));
updateDelay = 10;
clientActive = active;
}
}
public boolean wasActiveRecently() {
// If the machine is currently active or it flipped off within our threshold,
// we'll consider it recently active.
return isActive || (lastActive > 0 && (world.getTotalWorldTime() - lastActive) < RECENT_THRESHOLD);
}
@Override
public boolean renderUpdate() {
return true;
@@ -115,15 +107,24 @@ public abstract class TileEntityMachine extends TileEntityNoisyBlock implements
super.handlePacketData(dataStream);
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
clientActive = dataStream.readBoolean();
boolean newActive = dataStream.readBoolean();
controlType = RedstoneControl.values()[dataStream.readInt()];
energyPerTick = dataStream.readDouble();
maxEnergy = dataStream.readDouble();
if (updateDelay == 0 && clientActive != isActive) {
updateDelay = general.UPDATE_DELAY;
isActive = clientActive;
MekanismUtils.updateBlock(world, getPos());
boolean stateChange = (newActive != isActive);
isActive = newActive;
if (stateChange && !isActive) {
// Switched off; note the time
lastActive = world.getTotalWorldTime();
} else if (stateChange && isActive) {
// Switching on; if lastActive is not currently set, trigger a lighting update
// and make sure lastActive is clear
if (lastActive == -1) {
MekanismUtils.updateBlock(world, getPos());
}
lastActive = -1;
}
}
}
@@ -54,9 +54,7 @@ public abstract class TileEntityUpgradeableMachine<INPUT extends MachineInput<IN
//Machine
factory.progress[0] = operatingTicks;
factory.clientActive = clientActive;
factory.isActive = isActive;
factory.updateDelay = updateDelay;
factory.controlType = controlType;
factory.prevEnergy = prevEnergy;
factory.upgradeComponent.readFrom(upgradeComponent);