mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-24 22:14:42 +00:00
49 lines
1.1 KiB
Java
49 lines
1.1 KiB
Java
package crazypants.enderio.power;
|
|
|
|
public class PerTickIntAverageCalculator {
|
|
|
|
private float lastSecondTotal = 0;
|
|
private int tickCount = 0;
|
|
private final float[] secondsCache;
|
|
private int writeIndex;
|
|
private int writeSize;
|
|
|
|
public PerTickIntAverageCalculator() {
|
|
this(5);
|
|
}
|
|
|
|
public PerTickIntAverageCalculator(int numSeconds) {
|
|
secondsCache = new float[numSeconds];
|
|
}
|
|
|
|
public float getAverage() {
|
|
int numTicks = tickCount + writeSize * 20;
|
|
if(numTicks == 0) {
|
|
return 0;
|
|
}
|
|
float totalPower = lastSecondTotal;
|
|
for (int idx=writeIndex,cnt=writeSize ; cnt-->0 ;) {
|
|
totalPower += secondsCache[idx];
|
|
if(++idx == secondsCache.length) {
|
|
idx = 0;
|
|
}
|
|
}
|
|
return totalPower / numTicks;
|
|
}
|
|
|
|
public void tick(long value) {
|
|
lastSecondTotal += value;
|
|
tickCount++;
|
|
if(tickCount == 20) {
|
|
secondsCache[writeIndex++] = lastSecondTotal;
|
|
if(writeIndex > writeSize) {
|
|
writeSize = writeIndex;
|
|
}
|
|
if(writeIndex == secondsCache.length) {
|
|
writeIndex = 0;
|
|
}
|
|
lastSecondTotal = 0;
|
|
tickCount = 0;
|
|
}
|
|
}
|
|
} |