2020-05-18 10:53:01 +00:00
|
|
|
import { GameRoot } from "./root";
|
|
|
|
import { createLogger } from "../core/logging";
|
|
|
|
import { globalConfig } from "../core/config";
|
|
|
|
import { performanceNow, Math_min, Math_round, Math_max } from "../core/builtins";
|
|
|
|
import { round3Digits } from "../core/utils";
|
|
|
|
|
|
|
|
const logger = createLogger("dynamic_tickrate");
|
|
|
|
|
2020-05-18 15:40:20 +00:00
|
|
|
const fpsAccumulationTime = 1000;
|
|
|
|
|
2020-05-18 10:53:01 +00:00
|
|
|
export class DynamicTickrate {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {GameRoot} root
|
|
|
|
*/
|
|
|
|
constructor(root) {
|
|
|
|
this.root = root;
|
|
|
|
|
|
|
|
this.currentTickStart = null;
|
|
|
|
this.capturedTicks = [];
|
|
|
|
this.averageTickDuration = 0;
|
|
|
|
|
2020-05-18 15:40:20 +00:00
|
|
|
this.accumulatedFps = 0;
|
|
|
|
this.accumulatedFpsLastUpdate = 0;
|
|
|
|
|
|
|
|
this.averageFps = 60;
|
|
|
|
|
2020-05-18 14:08:33 +00:00
|
|
|
this.setTickRate(60);
|
2020-05-18 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 15:40:20 +00:00
|
|
|
onFrameRendered() {
|
|
|
|
++this.accumulatedFps;
|
|
|
|
|
|
|
|
const now = performanceNow();
|
|
|
|
const timeDuration = now - this.accumulatedFpsLastUpdate;
|
|
|
|
if (timeDuration > fpsAccumulationTime) {
|
|
|
|
const avgFps = (this.accumulatedFps / fpsAccumulationTime) * 1000;
|
|
|
|
this.averageFps = avgFps;
|
|
|
|
this.accumulatedFps = 0;
|
|
|
|
this.accumulatedFpsLastUpdate = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 10:53:01 +00:00
|
|
|
/**
|
|
|
|
* Sets the tick rate to N updates per second
|
|
|
|
* @param {number} rate
|
|
|
|
*/
|
|
|
|
setTickRate(rate) {
|
|
|
|
logger.log("Applying tick-rate of", rate);
|
|
|
|
this.currentTickRate = rate;
|
|
|
|
this.deltaMs = 1000.0 / this.currentTickRate;
|
|
|
|
this.deltaSeconds = 1.0 / this.currentTickRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increases the tick rate marginally
|
|
|
|
*/
|
|
|
|
increaseTickRate() {
|
2020-05-18 15:40:20 +00:00
|
|
|
const desiredFps = this.root.app.settings.getDesiredFps();
|
|
|
|
this.setTickRate(Math_round(Math_min(desiredFps, this.currentTickRate * 1.2)));
|
2020-05-18 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decreases the tick rate marginally
|
|
|
|
*/
|
|
|
|
decreaseTickRate() {
|
2020-05-18 15:40:20 +00:00
|
|
|
const desiredFps = this.root.app.settings.getDesiredFps();
|
|
|
|
this.setTickRate(Math_round(Math_max(desiredFps / 2, this.currentTickRate * 0.8)));
|
2020-05-18 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call whenever a tick began
|
|
|
|
*/
|
|
|
|
beginTick() {
|
|
|
|
assert(this.currentTickStart === null, "BeginTick called twice");
|
|
|
|
this.currentTickStart = performanceNow();
|
|
|
|
|
2020-05-18 14:08:33 +00:00
|
|
|
if (this.capturedTicks.length > this.currentTickRate * 2) {
|
2020-05-18 10:53:01 +00:00
|
|
|
// Take only a portion of the ticks
|
|
|
|
this.capturedTicks.sort();
|
|
|
|
this.capturedTicks.splice(0, 10);
|
|
|
|
this.capturedTicks.splice(this.capturedTicks.length - 11, 10);
|
|
|
|
|
|
|
|
let average = 0;
|
|
|
|
for (let i = 0; i < this.capturedTicks.length; ++i) {
|
|
|
|
average += this.capturedTicks[i];
|
|
|
|
}
|
|
|
|
average /= this.capturedTicks.length;
|
|
|
|
|
|
|
|
this.averageTickDuration = average;
|
|
|
|
|
2020-05-18 15:40:20 +00:00
|
|
|
const desiredFps = this.root.app.settings.getDesiredFps();
|
|
|
|
|
|
|
|
if (this.averageFps > desiredFps * 0.9) {
|
|
|
|
// if (average < maxTickDuration) {
|
2020-05-18 10:53:01 +00:00
|
|
|
this.increaseTickRate();
|
2020-05-18 15:40:20 +00:00
|
|
|
} else if (this.averageFps < desiredFps * 0.7) {
|
2020-05-18 10:53:01 +00:00
|
|
|
this.decreaseTickRate();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.capturedTicks = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call whenever a tick ended
|
|
|
|
*/
|
|
|
|
endTick() {
|
|
|
|
assert(this.currentTickStart !== null, "EndTick called without BeginTick");
|
|
|
|
const duration = performanceNow() - this.currentTickStart;
|
|
|
|
this.capturedTicks.push(duration);
|
|
|
|
this.currentTickStart = null;
|
|
|
|
}
|
|
|
|
}
|