diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 0702f89..202c46f 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -200,7 +200,7 @@ network does not freeze the page once a day. `rwGo()`: the steps run on the page up to a yield, then S and what routing reads of yesterday's R (`R.load`, `R.l2route`, `R.efg`) are posted, and the day goes on in `rwDone()` when R comes back. The next day waits until the last step has run. So a day sees exactly the routing it would have had in one go: same results, same random numbers. - At a `DAY_REST`, a day that has run `RW.budget` ms (half a frame, 3 to 8) already goes on after the next frame + At a `DAY_REST`, a day that has run `RW.budget` ms (half a frame: 3 to 8, or up to 32 on a device slower than 50 frames a second) already goes on after the next frame (`RW.later`, `rwLater()`), so a phone gets its frames between the steps of a big network's day. Half a day is never saved; nothing else starts meanwhile. `tests/day-steps.test.js` plays a second page as the worker and compares every day with `dayTick()`. diff --git a/src/game/80-simulation/28-route-worker.js b/src/game/80-simulation/28-route-worker.js index 6bc1a5b..a21f290 100644 --- a/src/game/80-simulation/28-route-worker.js +++ b/src/game/80-simulation/28-route-worker.js @@ -8,6 +8,8 @@ Tests (no Worker in jsdom, __TUBES_TEST__) and any worker failure take the synchronous path. */ const RW_IN=!!self.__TUBES_WORKER__; const RW_EL=!RW_IN&&typeof document!=='undefined'?document.currentScript:null; +// RW_SLOW: a frame time (ms) above which the device does not keep up anyway, and a day may take longer slices (99-loop.js). +const RW_SLOW=20; const RW={w:null,dead:false,seq:0,gen:0,job:null,budget:8,later:f=>rwLater(f),stat:{n:0,post:0,de:0,apply:0,wait:0,work:0}}; function rwOn(){return !RW_IN&&!RW.dead&&!window.__TUBES_TEST__&&!window.__TUBES_SYNC__&&typeof Worker==='function'&&!!RW_EL;} function rwStart(){if(RW.w)return RW.w; diff --git a/src/game/99-loop.js b/src/game/99-loop.js index aadabb0..af978d3 100644 --- a/src/game/99-loop.js +++ b/src/game/99-loop.js @@ -5,8 +5,10 @@ function frame(now){ if(!window.__TUBES_TEST__)requestAnimationFrame(frame); const dt=Math.min(250,now-last);last=now; // A day's steps take half a frame at most before letting one through (rwGo()): 8 ms at 60 Hz, 3 ms on a 240 Hz screen, - // where 8 ms would be two frames lost. The frame time is followed slowly, a long frame counting as two at most. - if(dt>0){RW.fms=RW.fms?RW.fms*0.95+Math.min(dt,RW.fms*2)*0.05:dt;RW.budget=Math.min(8,Math.max(3,RW.fms/2));} + // where 8 ms would be two frames lost. A device that cannot hold 50 frames a second waits a whole slow frame at every + // rest, so a big day there would crawl: it gets half of its own frame, up to 32 ms. The frame time is followed slowly, + // a long frame counting as two at most. + if(dt>0){RW.fms=RW.fms?RW.fms*0.95+Math.min(dt,RW.fms*2)*0.05:dt;RW.budget=RW.fms>RW_SLOW?Math.min(32,RW.fms/2):Math.min(8,Math.max(3,RW.fms/2));} if(ANALYTICS.host&&S&&!S.lost&&UI.speed&&document.visibilityState==='visible'){UI.playMs=(UI.playMs||0)+dt;const m=UI.playMs/60000;for(const k of PLAY_MARKS)if(m>=k)track('play_'+k+'m',true);} if(S&&UI.started&&!S.lost&&now-(UI.lastSave||0)>5000)save(); // The panels catch up on the frame after a day, so one frame does not carry both the day's simulation and them. diff --git a/tests/frame-pacing.test.js b/tests/frame-pacing.test.js index 3169265..e495306 100644 --- a/tests/frame-pacing.test.js +++ b/tests/frame-pacing.test.js @@ -15,4 +15,17 @@ test("a day's steps follow the screen: half a frame between two, from 3 to 8 ms" assert.deepEqual(errors, []); }); +// A slow phone draws 20 frames a second: at 8 ms a slice, a big day there waited a 50 ms frame at almost every step and +// played a third slower than before the day was split. It gets half of its own frame, never more than 32 ms. +test("a device that cannot hold 50 frames a second gives a day half its frame, 32 ms at most", () => { + const { ev, errors } = load(); + ev("ACT.start({});if(UI.pendGame)ACT.pitchSkip();closeModal();UI.speed=0;"); + const run = (hz, n) => ev(`(()=>{for(let i=0;i<${n};i++){window.__now=(window.__now||1000)+1000/${hz};frame(__now);}return RW.budget;})()`); + assert.equal(run(60, 300), 8, 'a 60 Hz screen keeps 8 ms'); + assert.ok(Math.abs(run(20, 300) - 25) < 0.5, `20 frames a second: half of 50 ms, ${ev('RW.budget')}`); + assert.equal(run(8, 300), 32, 'never more than 32 ms'); + assert.equal(run(240, 400), 3, 'and back to 3 ms on a fast screen'); + assert.deepEqual(errors, []); +}); + // The diverse-paths map new offers ask about (60-requirements.js) is built a millisecond a frame, before the first one