reworked ticking for many configs! maybe in settings?!

stable
Valerie 2021-07-29 12:51:31 -04:00
parent 845cb376e8
commit 8f4d21c00d
5 changed files with 103 additions and 34 deletions

View File

@ -24,9 +24,9 @@ export class Game extends Frigid implements Tickable {
return game;
}
async tick() {
async tick(seconds: number) {
for(const pawn of this.pawns) {
pawn.tick();
pawn.tick(seconds);
}
update();
}

View File

@ -20,8 +20,8 @@ export class Pawn extends Serializable implements Tickable {
job: TaskState<unknown, unknown>;
async tick() {
this.age ++;
async tick(seconds: number) {
this.age += seconds;
}
get idle() {

View File

@ -201,15 +201,77 @@ export default class Time extends Serializable {
}
}
async execTick(seconds: number) {
const doDynamicTick = true;
if(doDynamicTick) {
if(this.thing) {
await this.thing.tick(seconds);
}
this.advanceTime(seconds);
} else {
for(let i = 0; i < seconds; i ++) {
if(this.thing) {
await this.thing.tick(1);
}
this.advanceTime(1);
}
}
}
async doTick() {
this.advanceTime(3);
// the higher the multitick, the more lopsided
// ticks become in realtime, however, they rely
// on fewer setTimeouts, which helps tick scheduling
const multitick = 1;
if(multitick > 1) {
const timeout = (1000 / this.targetTPS) * multitick;
const start = ms4();
for(let tickNum = 0; tickNum < multitick; tickNum ++ ) {
const seconds = 3;
this.execTick(seconds);
this.ticksInSecond ++;
const end = ms4();
if(end > this.lastTPSCheckpoint + 1000) {
this.lastTPSCheckpoint = end;
this.tps = this.ticksInSecond;
this.ticksInSecond = 0;
}
}
const end = ms4()
const elapsed = end - start;
const wait = timeout - elapsed;
const normalizedWait = Math.floor(Math.max(wait, 0));
// process.stdout.write(`tick took ${elapsed} waiting ${normalizedWait}\n`);
if(wait < 0) {
const ticksOver = (-wait / timeout) + 1;
if(ticksOver > 1.5)
console.log(chalk.yellow('Can\'t keep up! Tick took ' + ticksOver.toFixed(2) + ' ticks (' + (timeout - wait).toFixed(4) + 'ms)'));
}
if(this.paused) return;
setTimeout(this._boundTick, normalizedWait);
} else {
// this code is from not needing a multi-tick workaround, due to scheduling.
const seconds = 3;
const timeout = 1000 / this.targetTPS;
// const start = ms4()
const start = ms4();
if(this.thing) {
await this.thing.tick();
}
this.execTick(seconds);
const end = ms4()
const elapsed = end - start;
const wait = timeout - elapsed;
@ -233,13 +295,18 @@ export default class Time extends Serializable {
}
if(this.paused) return;
setTimeout(this._boundTick, normalizedWait)
setTimeout(this._boundTick, normalizedWait);
}
}
}
export interface Tickable {
tick: () => Promise<void>
tick: (seconds: number) => Promise<void>
}
function ms4() {

View File

@ -15,6 +15,7 @@ ipc.config.silent = true;
const exec = 'qode' +
(process.platform === "win32" ? '.cmd' : '');
const args = [
// '--inspect',
'bin/app.bundle.cjs'
];

View File

@ -64,8 +64,9 @@ ProcessManager.on('reload', () => {
// HACK this is bullshit, :)
function f() {
win.repaint();
// win.repaint();
win.update();
setTimeout(f, 0);
}
f();