Add some stat tracking ability to the Power API

This commit is contained in:
CovertJaguar 2014-01-03 16:47:30 -08:00
parent 7fc7f25661
commit 5d9a9fa4e0
3 changed files with 57 additions and 35 deletions

View file

@ -21,7 +21,7 @@ import net.minecraftforge.common.ForgeDirection;
* If you plan emit power, you need only implement IPowerEmitter. You do not
* need a PowerHandler. Engines have a PowerHandler because they can also
* receive power from other Engines.
*
*
* See TileRefinery for a simple example of a power using machine.
*
* @see IPowerReceptor
@ -94,12 +94,10 @@ public final class PowerHandler {
* @return
*/
public double applyPerdition(PowerHandler powerHandler, double current, long ticksPassed) {
// float prev = current;
current -= powerLoss * ticksPassed;
if (current < 0) {
current = 0;
}
// powerHandler.totalLostPower += prev - current;
return current;
}
@ -115,6 +113,9 @@ public final class PowerHandler {
}
}
public static final PerditionCalculator DEFAULT_PERDITION = new PerditionCalculator();
public static final double ROLLING_AVERAGE_WEIGHT = 100.0;
public static final double ROLLING_AVERAGE_NUMERATOR = ROLLING_AVERAGE_WEIGHT - 1;
public static final double ROLLING_AVERAGE_DENOMINATOR = 1.0 / ROLLING_AVERAGE_WEIGHT;
private double minEnergyReceived;
private double maxEnergyReceived;
private double maxEnergyStored;
@ -128,11 +129,10 @@ public final class PowerHandler {
private PerditionCalculator perdition;
private final PowerReceiver receiver;
private final Type type;
// Debug
// private double totalLostPower = 0;
// private double totalReceivedPower = 0;
// private double totalUsedPower = 0;
// private long startTime = -1;
// Tracking
private double averageLostPower = 0;
private double averageReceivedPower = 0;
private double averageUsedPower = 0;
public PowerHandler(IPowerReceptor receptor, Type type) {
this.receptor = receptor;
@ -242,13 +242,6 @@ public final class PowerHandler {
* design around this though if you are aware of the limitations.
*/
public void update() {
// if (startTime == -1)
// startTime = receptor.getWorld().getTotalWorldTime();
// else {
// long duration = receptor.getWorld().getTotalWorldTime() - startTime;
// System.out.printf("Power Stats: %s - Stored: %.2f Gained: %.2f - %.2f/t Lost: %.2f - %.2f/t Used: %.2f - %.2f/t%n", receptor.getClass().getSimpleName(), energyStored, totalReceivedPower, totalReceivedPower / duration, totalLostPower, totalLostPower / duration, totalUsedPower, totalUsedPower / duration);
// }
applyPerdition();
applyWork();
validateEnergy();
@ -256,12 +249,15 @@ public final class PowerHandler {
private void applyPerdition() {
if (perditionTracker.markTimeIfDelay(receptor.getWorld(), 1) && energyStored > 0) {
double prev = energyStored;
double newEnergy = getPerdition().applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
if (newEnergy == 0 || newEnergy < energyStored)
energyStored = newEnergy;
else
energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
validateEnergy();
averageLostPower = (averageLostPower * ROLLING_AVERAGE_NUMERATOR + (prev - energyStored)) * ROLLING_AVERAGE_DENOMINATOR;
}
}
@ -317,8 +313,8 @@ public final class PowerHandler {
validateEnergy();
// if (doUse)
// totalUsedPower += result;
if (doUse)
averageUsedPower = (averageUsedPower * ROLLING_AVERAGE_NUMERATOR + result) * ROLLING_AVERAGE_DENOMINATOR;
return result;
}
@ -367,6 +363,18 @@ public final class PowerHandler {
return energyStored;
}
public double getAveragePowerReceived() {
return averageReceivedPower;
}
public double getAveragePowerUsed() {
return averageUsedPower;
}
public double getAveragePowerLost() {
return averageLostPower;
}
public Type getType() {
return type;
}
@ -416,7 +424,7 @@ public final class PowerHandler {
used = Math.min(quantity, maxEnergyReceived);
}
// totalReceivedPower += used;
averageReceivedPower = (averageReceivedPower * ROLLING_AVERAGE_NUMERATOR + used) * ROLLING_AVERAGE_DENOMINATOR;
return used;
}

View file

@ -8,12 +8,8 @@
package buildcraft.builders.filler.pattern;
import buildcraft.api.core.IBox;
import buildcraft.builders.BuilderProxyClient;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
public class PatternFill extends FillerPattern {

View file

@ -61,6 +61,7 @@ public class PipeTransportPower extends PipeTransport {
private float[] internalPower = new float[6];
public float[] internalNextPower = new float[6];
public int maxPower = 8;
private double highestPower;
SafeTimeTracker tracker = new SafeTimeTracker();
public PipeTransportPower() {
@ -181,7 +182,7 @@ public class PipeTransportPower extends PipeTransport {
}
}
double highestPower = 0;
highestPower = 0;
for (int i = 0; i < 6; i++) {
displayPower[i] = (prevDisplayPower[i] * (DISPLAY_SMOOTHING - 1.0F) + displayPower[i]) / DISPLAY_SMOOTHING;
if (displayPower[i] > highestPower) {
@ -309,18 +310,6 @@ public class PipeTransportPower extends PipeTransport {
* Power Pipes or a subclass thereof.
*/
public float receiveEnergy(ForgeDirection from, float val) {
// Keep this in reserve for if too many idiots start bypassing the API
// Verify that it is BC calling this method.
// If its someone else take all their power and run!
// Note: This should be safe for PipePowerWood subclasses, aka custom input pipes.
// StackTraceElement[] stackTrace = (new Throwable()).getStackTrace();
// String caller = stackTrace[1].getClassName();
// if (!caller.equals("buildcraft.transport.PipeTransportPower")
// && !caller.equals("buildcraft.transport.pipes.PipePowerWood")) {
// return val;
// }
step();
if (this.container.pipe instanceof IPipeTransportPowerHook) {
float ret = ((IPipeTransportPowerHook) this.container.pipe).receiveEnergy(from, val);
@ -394,4 +383,33 @@ public class PipeTransportPower extends PipeTransport {
clientDisplayPower = packetPower.displayPower;
overload = packetPower.overload ? OVERLOAD_TICKS : 0;
}
/**
* This can be use to provide a rough estimate of how much power is flowing
* through a pipe. Measured in MJ/t.
*
* @return MJ/t
*/
public double getCurrentPowerTransferRate() {
return highestPower;
}
/**
* This can be use to provide a rough estimate of how much power is
* contained in a pipe. Measured in MJ.
*
* Max should be around (throughput * internalPower.length * 2), ie 112 MJ for a Cobblestone Pipe.
*
* @return MJ
*/
public double getCurrentPowerAmount() {
double amount = 0.0;
for (double d : internalPower) {
amount += d;
}
for (double d : internalNextPower) {
amount += d;
}
return amount;
}
}