/* ================= LOOP ================= */ let last=performance.now(),acc=0,hudT=0,animT=0; function frame(now){ // Schedule the next frame first: one bad lookup in a day tick must not freeze the whole game. 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. 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. if(UI.panelsDue){NOC.dirty=true;UI.panelsDue=false;if(!pressing){renderPanel();renderLeft();if(!$('dash').hidden)renderDash();renderBank();}renderHint();} if(NOC.peer)nocPump(now); // A network the server runs (srvOn) has its days there: this page only shows them. if(S&&!S.lost&&$('modal').hidden&&UI.speed&&!pzOn()&&!srvOn())runClock(dt); if((UI.speed||pzOn())&&!(S&&S.lost))animT+=dt; /* paused = frozen dots, waves and clouds */ const t=animOn()?animT:0; if(!(UI.mapHid=mapHidden())&&!mapStill(t,now)){const d0=DIAG.on?performance.now():0;try{draw(t);}catch(e){tickErr(e);}UI.drawnAt=now;if(DIAG.on)DIAG.dms=performance.now()-d0;} if(DIAG.on)diagFrame(now); RW.lf=performance.now(); } // dt of real time at UI.speed: the days it adds up to. The frame calls it; so may a clock that also runs where frames // do not (src/agent/, a hidden tab), under the frame's own conditions otherwise. function runClock(dt){acc+=dt*UI.speed;let n=0; // One day in flight at a time (its routing in the worker, or resting between steps): time does not pile up behind it. // The clock waits for the day, never the other way round: a machine that cannot keep up plays slower, it does not // freeze to catch up. if(RW.job){acc=Math.min(acc,DAY_MS);return;} if(rwOn()){if(!RW.w)rwStart();if(acc>=DAY_MS){acc=Math.min(acc-DAY_MS,DAY_MS);try{rwDay();}catch(e){tickErr(e);}}} else{while(acc>=DAY_MS&&n<4&&UI.speed){acc-=DAY_MS;try{dayTick();}catch(e){tickErr(e);}n++;} if(n){UI.ticking=true;renderHUD();UI.ticking=false;UI.panelsDue=true;}}} /* On a phone the NOC dashboard (or the bank), scrolled past its top edge, covers the whole map. Drawing the map under it is work nobody sees, so it waits until the map shows again (and no payday money piles up meanwhile). */ function mapHidden(){for(const id of ['dashCard','bankCard']){const c=$(id);if(c.parentNode.hidden)continue; const r=c.getBoundingClientRect();if(r.top<=1&&r.left<=1&&r.right>=innerWidth-1&&r.bottom>=innerHeight-1)return true;}return false;} /* Paused, or with animations off, the map only changes when you move it, point at something or the network changes. Until then the last frame is still right, so it is not drawn again (phones keep cool). It is redrawn at least once a second anyway, and every frame while the land picture is still catching up with a zoom (the WebGL map has none). */ const FRAME=[]; function mapStill(t,now){const sl=UI.sel,dr=UI.drag; const v=[t,cam.x,cam.y,cam.z,CW,CH,DPR,S,R,S&&S.day,S&&S.links.length,S&&S.offers.length,S&&S.inc.length,sl&&sl.type,sl&&sl.id,UI.hover,UI.snap&&UI.snap.id,dr&&dr.mode,dr&&dr.cx,dr&&dr.cy,FLOATS.length,UI.mk?UI.mk.tx:0,UI.mk?UI.mk.mood:0,zoomMoving()]; let same=v.length===FRAME.length;for(let i=0;i({S:S&&JSON.parse(JSON.stringify(S)),cam:{...cam}})); if(!window.__TUBES_TEST__){requestAnimationFrame(frame);setTimeout(checkUpdate,4000);setInterval(checkUpdate,10*60*1000);} } // A #mp= link (a multiplayer network moving to this device) is read first: it picks the slot slotBoot opens. if(NOC.win)nocWinBoot();else if(!RW_IN)mpBoot(()=>slotBoot(()=>window.claude?.hot?.ready ? window.claude.hot.ready(boot) : boot(window.claude?.hot?.data ?? {})));