CursedSliver

cc dethrottler

Nov 13th, 2024 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.57 KB | Source Code | 0 0
  1. Game.registerMod('dethrottler', {
  2.     init: function() {
  3.         this.setup();
  4.         window.dethrottlerModObj = this;
  5.         Game.registerHook('logic', this.update);
  6.         eval('Game.Loop='+Game.Loop.toString()
  7.             .replace('setTimeout(Game.Loop,1000/Game.fps);', 'setTimeout(Game.Loop,Game.currentAvgFps / Game.fps * 1000 / Game.fps);')
  8.             .replace('setTimeout(Game.Loop,Math.max(1000/Game.fps-(Date.now()-startTimestamp),0));', 'setTimeout(Game.Loop,Game.currentAvgFps / Game.fps * Math.max(1000/Game.fps-(Date.now()-startTimestamp),0));') //FPSTweaker support
  9.         );
  10.     },
  11.     deltaTimeList: [],
  12.     deltaTimeListMaxLength: 20 * Game.fps,
  13.     setup: function() {
  14.         for (let i = 0; i < this.deltaTimeListMaxLength; i++) {
  15.             this.deltaTimeList.push(Game.fps);
  16.         }
  17.     },
  18.     loop: function(t) {
  19.         t.deltaTimeList.shift();
  20.         t.deltaTimeList.push(t.getFps());
  21.         //setTimeout(t.loop, 1 / Game.fps, t);
  22.     },
  23.     getAverage: function() {
  24.         let a = 0;
  25.         for (let i in this.deltaTimeList) {
  26.             a += this.deltaTimeList[i];
  27.         }
  28.         a /= this.deltaTimeList.length;
  29.         return a;
  30.     },
  31.     getFps: function() {
  32.         return (Game.frameNumber/((Date.now()-Game.fpsStartTime)/1000)) + 3; //+3 is to mitigate overcorrecting
  33.     },
  34.     currentAvgFps: Game.fps,
  35.     update: function() {
  36.         dethrottlerModObj.loop(dethrottlerModObj);
  37.         dethrottlerModObj.currentAvgFps = dethrottlerModObj.getAverage();
  38.         Game.currentAvgFps = dethrottlerModObj.currentAvgFps;
  39.     }
  40. });
Advertisement
Add Comment
Please, Sign In to add comment