AndresCarreon commited on
Commit
09ce0b4
·
verified ·
1 Parent(s): 4b23ced

fix construction stuck-at-dirt: snap the shared clock on large drift + tab refocus (was a 0.02s/s crawl, useless after tab throttling)

Browse files
Files changed (1) hide show
  1. web/main.js +16 -6
web/main.js CHANGED
@@ -159,13 +159,23 @@ function setSim(t, { quiet = false } = {}) {
159
  return false;
160
  }
161
 
162
- // slew ±2% toward the server clock when drift exceeds 1.5 s never snap
163
- setInterval(() => {
 
 
 
 
 
 
164
  if (!LIVE || anchorPerf == null) return;
165
- const drift = serverNowEst() - C.CITY_EPOCH_S - app.simTime;
166
- if (Math.abs(drift) <= 1.5) return;
167
- setSim(app.simTime + Math.sign(drift) * Math.min(Math.abs(drift), 0.02), { quiet: true });
168
- }, 1000);
 
 
 
 
169
 
170
  // ---------------------------------------------------------------- helpers
171
  const truncate = (s, n) => (s && s.length > n ? `${s.slice(0, n - 1)}…` : s ?? "");
 
159
  return false;
160
  }
161
 
162
+ // Keep the sim clock locked to the server clock. serverNowEst() is wall-clock
163
+ // based (accurate even when rAF is throttled); app.simTime is advanced by the
164
+ // render loop and FALLS BEHIND when the tab is backgrounded or the frame rate
165
+ // dips (the dt cap). If it lags, a freshly-built building reads as "not started"
166
+ // and sits at 0% dirt until the clock catches up (June 15 bug). So: gentle slew
167
+ // for tiny drift (cars/day-night don't jump), but SNAP on large drift (after a
168
+ // throttle gap) and whenever the tab regains focus.
169
+ function syncClock() {
170
  if (!LIVE || anchorPerf == null) return;
171
+ const target = serverNowEst() - C.CITY_EPOCH_S;
172
+ const drift = target - app.simTime;
173
+ if (Math.abs(drift) <= 1.0) return;
174
+ if (Math.abs(drift) > 3) setSim(target, { quiet: true }); // snap (post-throttle)
175
+ else setSim(app.simTime + Math.sign(drift) * 0.25, { quiet: true }); // gentle catch-up
176
+ }
177
+ setInterval(syncClock, 1000);
178
+ document.addEventListener("visibilitychange", () => { if (!document.hidden) syncClock(); });
179
 
180
  // ---------------------------------------------------------------- helpers
181
  const truncate = (s, n) => (s && s.length > n ? `${s.slice(0, n - 1)}…` : s ?? "");