Skip to content

Commit

Permalink
Overhaul solar neutron activator to use better sun/rain detection. Re…
Browse files Browse the repository at this point in the history
…duce network traffic by 50% by ensuring we only send updates every second
  • Loading branch information
dizzyd committed Mar 19, 2019
1 parent 0453584 commit 3fd7540
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 55 deletions.
Expand Up @@ -51,7 +51,7 @@ public GuiSolarNeutronActivator(InventoryPlayer inventory, TileEntitySolarNeutro
guiElements.add(new GuiProgress(new IProgressInfoHandler() {
@Override
public double getProgress() {
return tileEntity.isActive ? 1 : 0;
return tileEntity.getProgress();
}
}, ProgressBar.LARGE_RIGHT, this, MekanismUtils.getResource(ResourceType.GUI, "GuiBlank.png"), 62, 38));
}
Expand Down
Expand Up @@ -52,19 +52,14 @@ public class TileEntitySolarNeutronActivator extends TileEntityContainerBlock im
public static final int MAX_GAS = 10000;
public GasTank inputTank = new GasTank(MAX_GAS);
public GasTank outputTank = new GasTank(MAX_GAS);
public int updateDelay;

public boolean isActive;

public boolean clientActive;

public int gasOutput = 256;

public SolarNeutronRecipe cachedRecipe;
private SolarNeutronRecipe cachedRecipe;

private boolean isActive;
private boolean needsRainCheck;

/**
* This machine's current RedstoneControl type.
*/
public RedstoneControl controlType = RedstoneControl.DISABLED;

public TileComponentUpgrade upgradeComponent = new TileComponentUpgrade(this, 3);
Expand All @@ -77,45 +72,46 @@ public TileEntitySolarNeutronActivator() {
}

@Override
public void onUpdate() {
if (world.isRemote && updateDelay > 0) {
updateDelay--;
public void validate() {
super.validate();

if (updateDelay == 0 && clientActive != isActive) {
isActive = clientActive;
MekanismUtils.updateBlock(world, getPos());
}
}
// Cache the flag to know if rain matters where this block is placed
needsRainCheck = world.provider.getBiomeForCoords(getPos()).canRain();
}

@Override
public void onUpdate() {
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)));
}
}

TileUtils.receiveGas(inventory.get(0), inputTank);
TileUtils.drawGas(inventory.get(1), outputTank);

SolarNeutronRecipe recipe = getRecipe();

boolean sky =
((!world.isRaining() && !world.isThundering()) || isDesert()) && !world.provider.isNether() && world
.canSeeSky(getPos().up()); // TODO Check isNether call, maybe it should be hasSkyLight
// TODO: Ideally the neutron activator should use the sky brightness to determine throughput; but
// changing this would dramatically affect a lot of setups with Fusion reactors which can take
// a long time to relight. I don't want to be chased by a mob right now, so just doing basic
// rain checks.
boolean seesSun = world.isDaytime() && world.canSeeSky(getPos().up(4)) && !world.provider.isNether();
if (needsRainCheck) {
seesSun &= !(world.isRaining() || world.isThundering());
}

if (world.isDaytime() && sky && canOperate(recipe) && MekanismUtils.canFunction(this)) {
if (seesSun && canOperate(recipe) && MekanismUtils.canFunction(this)) {
setActive(true);

int operations = operate(recipe);
operate(recipe);
} else {
setActive(false);
}

TileUtils.emitGas(this, outputTank, gasOutput);

// Every 20 ticks (once a second), send update to client. Note that this is a 50% reduction in network
// traffic from previous implementation that send the update every 10 ticks.
if (world.getTotalWorldTime() % 20 == 0) {
Mekanism.packetHandler
.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new TileNetworkList())),
new Range4D(Coord4D.get(this)));
}
}
}

Expand All @@ -126,10 +122,6 @@ public int getUpgradedUsage() {
return possibleProcess;
}

public boolean isDesert() {
return world.provider.getBiomeForCoords(getPos()) instanceof BiomeDesert;
}

public SolarNeutronRecipe getRecipe() {
GasInput input = getInput();

Expand All @@ -148,12 +140,8 @@ public boolean canOperate(SolarNeutronRecipe recipe) {
return recipe != null && recipe.canOperate(inputTank, outputTank);
}

public int operate(SolarNeutronRecipe recipe) {
int operations = getUpgradedUsage();

recipe.operate(inputTank, outputTank, operations);

return operations;
public void operate(SolarNeutronRecipe recipe) {
recipe.operate(inputTank, outputTank, getUpgradedUsage());
}

@Override
Expand All @@ -165,11 +153,6 @@ public void handlePacketData(ByteBuf dataStream) {
controlType = RedstoneControl.values()[dataStream.readInt()];
TileUtils.readTankData(dataStream, inputTank);
TileUtils.readTankData(dataStream, outputTank);
if (updateDelay == 0 && clientActive != isActive) {
updateDelay = general.UPDATE_DELAY;
isActive = clientActive;
MekanismUtils.updateBlock(world, getPos());
}
}
}

Expand Down Expand Up @@ -321,15 +304,13 @@ public boolean getActive() {

@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;
}
}

Expand All @@ -340,7 +321,7 @@ public boolean renderUpdate() {

@Override
public boolean lightUpdate() {
return true;
return false;
}

@Override
Expand Down Expand Up @@ -369,4 +350,11 @@ public List<String> getInfo(Upgrade upgrade) {
public AxisAlignedBB getRenderBoundingBox() {
return INFINITE_EXTENT_AABB;
}

public double getProgress() {
if (isActive) {
return .16 * (1 + (world.getTotalWorldTime() % 6));
}
return 0;
}
}

0 comments on commit 3fd7540

Please sign in to comment.