Add AverageUtil and using it for energy rate computing in assembly
tables
This commit is contained in:
parent
658e10b12d
commit
93d1fb49e6
4 changed files with 61 additions and 10 deletions
53
common/buildcraft/core/utils/AverageUtil.java
Normal file
53
common/buildcraft/core/utils/AverageUtil.java
Normal file
|
@ -0,0 +1,53 @@
|
|||
/**
|
||||
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
|
||||
* http://www.mod-buildcraft.com
|
||||
*
|
||||
* BuildCraft is distributed under the terms of the Minecraft Mod Public
|
||||
* License 1.0, or MMPL. Please check the contents of the license located in
|
||||
* http://www.mod-buildcraft.com/MMPL-1.0.txt
|
||||
*/
|
||||
package buildcraft.core.utils;
|
||||
|
||||
public class AverageUtil {
|
||||
private double[] data;
|
||||
private int pos, precise;
|
||||
private double averageRaw, tickValue;
|
||||
|
||||
public AverageUtil(int precise) {
|
||||
this.precise = precise;
|
||||
this.data = new double[precise];
|
||||
this.pos = 0;
|
||||
}
|
||||
|
||||
public double getAverage() {
|
||||
return averageRaw / precise;
|
||||
}
|
||||
|
||||
public void tick(double value) {
|
||||
internalTick(tickValue + value);
|
||||
tickValue = 0;
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
internalTick(tickValue);
|
||||
tickValue = 0;
|
||||
}
|
||||
|
||||
private void internalTick(double value) {
|
||||
pos = ++pos % precise;
|
||||
double oldValue = data[pos];
|
||||
data[pos] = value;
|
||||
if (pos == 0) {
|
||||
averageRaw = 0;
|
||||
for (double iValue : data) {
|
||||
averageRaw += iValue;
|
||||
}
|
||||
} else {
|
||||
averageRaw = averageRaw - oldValue + value;
|
||||
}
|
||||
}
|
||||
|
||||
public void push(double value) {
|
||||
tickValue += value;
|
||||
}
|
||||
}
|
|
@ -245,6 +245,7 @@ public class TileAdvancedCraftingTable extends TileLaserTableBase implements IIn
|
|||
|
||||
@Override
|
||||
public void updateEntity() {
|
||||
super.updateEntity();
|
||||
if (internalPlayer == null) {
|
||||
internalInventoryCrafting = new InternalInventoryCrafting();
|
||||
internalPlayer = new InternalPlayer();
|
||||
|
|
|
@ -81,6 +81,7 @@ public class TileAssemblyTable extends TileLaserTableBase implements IMachine, I
|
|||
|
||||
@Override
|
||||
public void updateEntity() { // WARNING: run only server-side, see canUpdate()
|
||||
super.updateEntity();
|
||||
if (currentRecipe == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -18,21 +18,20 @@ import net.minecraft.nbt.NBTTagCompound;
|
|||
import buildcraft.api.power.ILaserTarget;
|
||||
import buildcraft.core.TileBuildCraft;
|
||||
import buildcraft.core.inventory.SimpleInventory;
|
||||
import buildcraft.core.utils.AverageUtil;
|
||||
|
||||
public abstract class TileLaserTableBase extends TileBuildCraft implements ILaserTarget, IInventory {
|
||||
|
||||
public double clientRequiredEnergy = 0;
|
||||
protected SimpleInventory inv = new SimpleInventory(getSizeInventory(), "inv", 64);
|
||||
private double[] recentEnergy = new double[20];
|
||||
private double energy = 0;
|
||||
private int tick = 0;
|
||||
private int recentEnergyAverage;
|
||||
private AverageUtil recentEnergyAverageUtil = new AverageUtil(20);
|
||||
|
||||
@Override
|
||||
public void updateEntity() {
|
||||
tick++;
|
||||
tick = tick % recentEnergy.length;
|
||||
recentEnergy[tick] = 0.0f;
|
||||
super.updateEntity();
|
||||
recentEnergyAverageUtil.tick();
|
||||
}
|
||||
|
||||
public double getEnergy() {
|
||||
|
@ -77,7 +76,7 @@ public abstract class TileLaserTableBase extends TileBuildCraft implements ILase
|
|||
@Override
|
||||
public void receiveLaserEnergy(double energy) {
|
||||
this.energy += energy;
|
||||
recentEnergy[tick] += energy;
|
||||
recentEnergyAverageUtil.push(energy);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -184,10 +183,7 @@ public abstract class TileLaserTableBase extends TileBuildCraft implements ILase
|
|||
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
|
||||
int requiredEnergy = (int) (getRequiredEnergy() * 100.0);
|
||||
int currentStored = (int) (energy * 100.0);
|
||||
int lRecentEnergy = 0;
|
||||
for (double element : recentEnergy) {
|
||||
lRecentEnergy += (int) (element * 100.0 / (recentEnergy.length - 1));
|
||||
}
|
||||
int lRecentEnergy = (int) (recentEnergyAverageUtil.getAverage() * 100f);
|
||||
iCrafting.sendProgressBarUpdate(container, 0, requiredEnergy & 0xFFFF);
|
||||
iCrafting.sendProgressBarUpdate(container, 1, currentStored & 0xFFFF);
|
||||
iCrafting.sendProgressBarUpdate(container, 2, (requiredEnergy >>> 16) & 0xFFFF);
|
||||
|
|
Loading…
Add table
Reference in a new issue