parent
65529cce1a
commit
ca0e17f3dd
@ -0,0 +1,10 @@
|
|||||||
|
#ingame_HUD_DebugInfo {
|
||||||
|
position: absolute;
|
||||||
|
@include S(bottom, 5px);
|
||||||
|
@include S(left, 5px);
|
||||||
|
|
||||||
|
font-size: 15px;
|
||||||
|
display: flex;
|
||||||
|
line-height: 15px;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
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");
|
||||||
|
|
||||||
|
export class DynamicTickrate {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {GameRoot} root
|
||||||
|
*/
|
||||||
|
constructor(root) {
|
||||||
|
this.root = root;
|
||||||
|
|
||||||
|
this.setTickRate(120);
|
||||||
|
|
||||||
|
this.currentTickStart = null;
|
||||||
|
this.capturedTicks = [];
|
||||||
|
this.averageTickDuration = 0;
|
||||||
|
|
||||||
|
// Exposed
|
||||||
|
this.deltaSeconds = 0;
|
||||||
|
this.deltaMs = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
||||||
|
this.setTickRate(Math_round(Math_min(globalConfig.maximumTickRate, this.currentTickRate * 1.1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decreases the tick rate marginally
|
||||||
|
*/
|
||||||
|
decreaseTickRate() {
|
||||||
|
this.setTickRate(Math_round(Math_min(globalConfig.maximumTickRate, this.currentTickRate * 0.9)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call whenever a tick began
|
||||||
|
*/
|
||||||
|
beginTick() {
|
||||||
|
assert(this.currentTickStart === null, "BeginTick called twice");
|
||||||
|
this.currentTickStart = performanceNow();
|
||||||
|
|
||||||
|
if (this.capturedTicks.length > this.currentTickRate * 4) {
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// Calculate tick duration to cover X% of the frame
|
||||||
|
const ticksPerFrame = this.currentTickRate / 60;
|
||||||
|
const maxFrameDurationMs = 8;
|
||||||
|
const maxTickDuration = maxFrameDurationMs / ticksPerFrame;
|
||||||
|
// const maxTickDuration = (1000 / this.currentTickRate) * 0.75;
|
||||||
|
logger.log(
|
||||||
|
"Average time per tick:",
|
||||||
|
round3Digits(average) + "ms",
|
||||||
|
"allowed are",
|
||||||
|
maxTickDuration
|
||||||
|
);
|
||||||
|
this.averageTickDuration = average;
|
||||||
|
|
||||||
|
if (average < maxTickDuration) {
|
||||||
|
this.increaseTickRate();
|
||||||
|
} else {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
import { BaseHUDPart } from "../base_hud_part";
|
||||||
|
import { makeDiv, round3Digits } from "../../../core/utils";
|
||||||
|
|
||||||
|
export class HUDDebugInfo extends BaseHUDPart {
|
||||||
|
createElements(parent) {
|
||||||
|
this.element = makeDiv(parent, "ingame_HUD_DebugInfo", []);
|
||||||
|
|
||||||
|
this.tickRateElement = makeDiv(this.element, null, ["tickRate"], "Ticks /s: 120");
|
||||||
|
this.tickDurationElement = makeDiv(this.element, null, ["tickDuration"], "Update time: 0.5ms");
|
||||||
|
}
|
||||||
|
|
||||||
|
initialize() {}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
this.tickRateElement.innerText = "Tickrate: " + this.root.dynamicTickrate.currentTickRate;
|
||||||
|
this.tickDurationElement.innerText =
|
||||||
|
"Avg. Dur: " + round3Digits(this.root.dynamicTickrate.averageTickDuration) + "ms";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue