diff --git a/src/Game.ts b/src/Game.ts index 7004591..249b167 100644 --- a/src/Game.ts +++ b/src/Game.ts @@ -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(); } diff --git a/src/Pawn.ts b/src/Pawn.ts index 065ae4b..7578206 100644 --- a/src/Pawn.ts +++ b/src/Pawn.ts @@ -20,8 +20,8 @@ export class Pawn extends Serializable implements Tickable { job: TaskState; - async tick() { - this.age ++; + async tick(seconds: number) { + this.age += seconds; } get idle() { diff --git a/src/Time.ts b/src/Time.ts index 7daaca4..951b555 100644 --- a/src/Time.ts +++ b/src/Time.ts @@ -201,45 +201,112 @@ 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); - const timeout = 1000 / this.targetTPS; - // const start = ms4() - const start = ms4(); - if(this.thing) { - await this.thing.tick(); - } - 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`); + // 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(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)')); - } - - - this.ticksInSecond ++; + if(multitick > 1) { + const timeout = (1000 / this.targetTPS) * multitick; + const start = ms4(); - if(end > this.lastTPSCheckpoint + 1000) { - this.lastTPSCheckpoint = end; - this.tps = this.ticksInSecond; - this.ticksInSecond = 0; + 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(); + + this.execTick(seconds); + + 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)')); + } + + + this.ticksInSecond ++; + + if(end > this.lastTPSCheckpoint + 1000) { + this.lastTPSCheckpoint = end; + this.tps = this.ticksInSecond; + this.ticksInSecond = 0; + } + + if(this.paused) return; + setTimeout(this._boundTick, normalizedWait); } - if(this.paused) return; - setTimeout(this._boundTick, normalizedWait) + + + } } export interface Tickable { - tick: () => Promise + tick: (seconds: number) => Promise } function ms4() { diff --git a/src/hot-index.ts b/src/hot-index.ts index f5e1ee9..35ae49d 100644 --- a/src/hot-index.ts +++ b/src/hot-index.ts @@ -15,6 +15,7 @@ ipc.config.silent = true; const exec = 'qode' + (process.platform === "win32" ? '.cmd' : ''); const args = [ + // '--inspect', 'bin/app.bundle.cjs' ]; diff --git a/src/qt/index.ts b/src/qt/index.ts index f96bde0..a3c632e 100644 --- a/src/qt/index.ts +++ b/src/qt/index.ts @@ -64,8 +64,9 @@ ProcessManager.on('reload', () => { // HACK this is bullshit, :) function f() { - win.repaint(); + // win.repaint(); win.update(); setTimeout(f, 0); } -f(); \ No newline at end of file +f(); +