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

@ -94,12 +94,10 @@ public final class PowerHandler {
* @return * @return
*/ */
public double applyPerdition(PowerHandler powerHandler, double current, long ticksPassed) { public double applyPerdition(PowerHandler powerHandler, double current, long ticksPassed) {
// float prev = current;
current -= powerLoss * ticksPassed; current -= powerLoss * ticksPassed;
if (current < 0) { if (current < 0) {
current = 0; current = 0;
} }
// powerHandler.totalLostPower += prev - current;
return current; return current;
} }
@ -115,6 +113,9 @@ public final class PowerHandler {
} }
} }
public static final PerditionCalculator DEFAULT_PERDITION = new PerditionCalculator(); 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 minEnergyReceived;
private double maxEnergyReceived; private double maxEnergyReceived;
private double maxEnergyStored; private double maxEnergyStored;
@ -128,11 +129,10 @@ public final class PowerHandler {
private PerditionCalculator perdition; private PerditionCalculator perdition;
private final PowerReceiver receiver; private final PowerReceiver receiver;
private final Type type; private final Type type;
// Debug // Tracking
// private double totalLostPower = 0; private double averageLostPower = 0;
// private double totalReceivedPower = 0; private double averageReceivedPower = 0;
// private double totalUsedPower = 0; private double averageUsedPower = 0;
// private long startTime = -1;
public PowerHandler(IPowerReceptor receptor, Type type) { public PowerHandler(IPowerReceptor receptor, Type type) {
this.receptor = receptor; this.receptor = receptor;
@ -242,13 +242,6 @@ public final class PowerHandler {
* design around this though if you are aware of the limitations. * design around this though if you are aware of the limitations.
*/ */
public void update() { 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(); applyPerdition();
applyWork(); applyWork();
validateEnergy(); validateEnergy();
@ -256,12 +249,15 @@ public final class PowerHandler {
private void applyPerdition() { private void applyPerdition() {
if (perditionTracker.markTimeIfDelay(receptor.getWorld(), 1) && energyStored > 0) { if (perditionTracker.markTimeIfDelay(receptor.getWorld(), 1) && energyStored > 0) {
double prev = energyStored;
double newEnergy = getPerdition().applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()); double newEnergy = getPerdition().applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
if (newEnergy == 0 || newEnergy < energyStored) if (newEnergy == 0 || newEnergy < energyStored)
energyStored = newEnergy; energyStored = newEnergy;
else else
energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay()); energyStored = DEFAULT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
validateEnergy(); validateEnergy();
averageLostPower = (averageLostPower * ROLLING_AVERAGE_NUMERATOR + (prev - energyStored)) * ROLLING_AVERAGE_DENOMINATOR;
} }
} }
@ -317,8 +313,8 @@ public final class PowerHandler {
validateEnergy(); validateEnergy();
// if (doUse) if (doUse)
// totalUsedPower += result; averageUsedPower = (averageUsedPower * ROLLING_AVERAGE_NUMERATOR + result) * ROLLING_AVERAGE_DENOMINATOR;
return result; return result;
} }
@ -367,6 +363,18 @@ public final class PowerHandler {
return energyStored; return energyStored;
} }
public double getAveragePowerReceived() {
return averageReceivedPower;
}
public double getAveragePowerUsed() {
return averageUsedPower;
}
public double getAveragePowerLost() {
return averageLostPower;
}
public Type getType() { public Type getType() {
return type; return type;
} }
@ -416,7 +424,7 @@ public final class PowerHandler {
used = Math.min(quantity, maxEnergyReceived); used = Math.min(quantity, maxEnergyReceived);
} }
// totalReceivedPower += used; averageReceivedPower = (averageReceivedPower * ROLLING_AVERAGE_NUMERATOR + used) * ROLLING_AVERAGE_DENOMINATOR;
return used; return used;
} }

View file

@ -8,12 +8,8 @@
package buildcraft.builders.filler.pattern; package buildcraft.builders.filler.pattern;
import buildcraft.api.core.IBox; 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.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
public class PatternFill extends FillerPattern { public class PatternFill extends FillerPattern {

View file

@ -61,6 +61,7 @@ public class PipeTransportPower extends PipeTransport {
private float[] internalPower = new float[6]; private float[] internalPower = new float[6];
public float[] internalNextPower = new float[6]; public float[] internalNextPower = new float[6];
public int maxPower = 8; public int maxPower = 8;
private double highestPower;
SafeTimeTracker tracker = new SafeTimeTracker(); SafeTimeTracker tracker = new SafeTimeTracker();
public PipeTransportPower() { public PipeTransportPower() {
@ -181,7 +182,7 @@ public class PipeTransportPower extends PipeTransport {
} }
} }
double highestPower = 0; highestPower = 0;
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
displayPower[i] = (prevDisplayPower[i] * (DISPLAY_SMOOTHING - 1.0F) + displayPower[i]) / DISPLAY_SMOOTHING; displayPower[i] = (prevDisplayPower[i] * (DISPLAY_SMOOTHING - 1.0F) + displayPower[i]) / DISPLAY_SMOOTHING;
if (displayPower[i] > highestPower) { if (displayPower[i] > highestPower) {
@ -309,18 +310,6 @@ public class PipeTransportPower extends PipeTransport {
* Power Pipes or a subclass thereof. * Power Pipes or a subclass thereof.
*/ */
public float receiveEnergy(ForgeDirection from, float val) { 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(); step();
if (this.container.pipe instanceof IPipeTransportPowerHook) { if (this.container.pipe instanceof IPipeTransportPowerHook) {
float ret = ((IPipeTransportPowerHook) this.container.pipe).receiveEnergy(from, val); float ret = ((IPipeTransportPowerHook) this.container.pipe).receiveEnergy(from, val);
@ -394,4 +383,33 @@ public class PipeTransportPower extends PipeTransport {
clientDisplayPower = packetPower.displayPower; clientDisplayPower = packetPower.displayPower;
overload = packetPower.overload ? OVERLOAD_TICKS : 0; 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;
}
} }