Add ability to include the current tick in TickTask.getLag.

This commit is contained in:
asofold 2013-01-04 13:47:09 +01:00
parent 47faa014bf
commit 57ba5533aa

View File

@ -186,9 +186,21 @@ public class TickTask implements Runnable {
/**
* Get lag percentage for the last ms milliseconds.<br>
* NOTE: Will not be synchronized, still can be called from other threads.
* @return
* @param ms Past milliseconds to cover. A longer period of time may be used, up to two times if ms > lagMaxTicks * 50.
* @return Lag factor (1.0 = 20 tps, 2.0 = 10 tps), excluding the current tick.
*/
public static final float getLag(final long ms){
return getLag(ms, false);
}
/**
* Get lag percentage for the last ms milliseconds.<br>
* NOTE: Using "exact = true" is meant for checks in the main thread. If called from another thread, exact should be set to false.
* @param ms Past milliseconds to cover. A longer period of time may be used, up to two times if ms > lagMaxTicks * 50.
* @param exact If to include the currently running tick, if possible. Should only be set to true, if called from the main thread (or while the main thread is blocked).
* @return Lag factor (1.0 = 20 tps, 2.0 = 10 tps).
*/
public static final float getLag(final long ms, final boolean exact){
// TODO: Account for freezing (i.e. check timeLast, might be an extra method)!
final int tick = TickTask.tick;
if (tick == 0) return 1f;
@ -206,6 +218,16 @@ public class TickTask implements Runnable {
covered += lagMaxTicks * 50 * maxTickSq;
}
if (exact){
// Attempt to count in the current tick.
final long passed = System.currentTimeMillis() - timeLast;
if (passed > 50){
// Only count in in the case of "overtime".
covered += 50;
sum += passed;
}
}
return (float) sum / (float) covered;
}