Skip to content

Commit

Permalink
feat: add a targeted fps property and new command for adjusting fps
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraviolet-jordan committed Apr 4, 2024
1 parent 790913b commit 4bead82
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Create your account on the 2004scape website.

---

## Commands

`::debug` Shows performance metrics (FPS, frame times).

`::fps` Set a targeted FPS (ex. `::fps 30`)

---

## First Time Installation

```shell
Expand Down
9 changes: 7 additions & 2 deletions src/js/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3901,10 +3901,9 @@ class Game extends Client {
}

if ((key === 13 || key === 10) && this.chatTyped.length > 0) {
// if (this.rights) {
if (this.chatTyped === '::clientdrop' /* && super.frame*/) {
await this.tryReconnect();
} else if (this.chatTyped === '::noclip') {
} else if (this.rights && this.chatTyped === '::noclip') {
for (let level: number = 0; level < CollisionMap.LEVELS; level++) {
for (let x: number = 1; x < CollisionMap.SIZE - 1; x++) {
for (let z: number = 1; z < CollisionMap.SIZE - 1; z++) {
Expand All @@ -3917,6 +3916,12 @@ class Game extends Client {
}
} else if (this.chatTyped === '::debug') {
Client.showDebug = !Client.showDebug;
} else if (this.chatTyped.startsWith('::fps ')) {
try {
this.setTargetedFramerate(parseInt(this.chatTyped.substring(6), 10));
} catch (e) {
/* empty */
}
}

if (this.chatTyped.startsWith('::')) {
Expand Down
21 changes: 19 additions & 2 deletions src/js/jagex2/client/GameShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default abstract class GameShell {
protected frameTime: number[] = [];
protected redrawScreen: boolean = true;
protected resizeToFit: boolean = false;
protected tfps: number = 50; // custom

protected ingame: boolean = false;

Expand Down Expand Up @@ -131,11 +132,15 @@ export default abstract class GameShell {
e.preventDefault();
};

if (this.isMobile && GameShell.getParameter('detail') === 'low') {
this.tfps = 30;
}

await this.showProgress(0, 'Loading...');
await this.load();

for (let i: number = 0; i < 10; i++) {
this.otim[i] = Date.now();
this.otim[i] = performance.now();
}

let ntime: number;
Expand All @@ -159,7 +164,7 @@ export default abstract class GameShell {
ratio = 300;
delta = 1;

ntime = Date.now();
ntime = performance.now();
const otim: number = this.otim[opos];

if (otim === 0) {
Expand Down Expand Up @@ -215,6 +220,14 @@ export default abstract class GameShell {

this.frameTime[this.fpos] = (performance.now() - time) / 1000;
this.fpos = (this.fpos + 1) % this.frameTime.length;

// this is custom for targeting specific fps (on mobile).
if (this.tfps < 50) {
const tfps: number = 1000 / this.tfps - (performance.now() - ntime);
if (tfps > 0) {
await sleep(tfps);
}
}
}
if (this.state === -1) {
this.shutdown();
Expand All @@ -230,6 +243,10 @@ export default abstract class GameShell {
this.deltime = (1000 / rate) | 0;
}

protected setTargetedFramerate(rate: number): void {
this.tfps = Math.max(Math.min(50, rate | 0), 0);
}

protected start(): void {
if (this.state >= 0) {
this.state = 0;
Expand Down
20 changes: 10 additions & 10 deletions src/js/jagex2/client/InputTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class InputTracking {
static setEnabled = (): void => {
this.outBuffer = Packet.alloc(1);
this.oldBuffer = null;
this.lastTime = Date.now();
this.lastTime = performance.now();
this.enabled = true;
};

Expand Down Expand Up @@ -45,7 +45,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand All @@ -66,7 +66,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand All @@ -85,7 +85,7 @@ export default class InputTracking {
if (!(this.enabled && x >= 0 && x < 789 && y >= 0 && y < 532)) {
return;
}
const now: number = Date.now();
const now: number = performance.now();
if (now - this.lastMoveTime >= 50) {
this.lastMoveTime = now;
this.trackedCount++;
Expand Down Expand Up @@ -124,7 +124,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand Down Expand Up @@ -152,7 +152,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand Down Expand Up @@ -180,7 +180,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand All @@ -196,7 +196,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand All @@ -212,7 +212,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand All @@ -228,7 +228,7 @@ export default class InputTracking {
return;
}
this.trackedCount++;
const now: number = Date.now();
const now: number = performance.now();
let delta: number = ((now - this.lastTime) / 10) | 0;
if (delta > 250) {
delta = 250;
Expand Down

0 comments on commit 4bead82

Please sign in to comment.