'use strict'; /** * t1182: client-side prediction for falling blocks on opponents' mini * fields. Purely visual. Between snapshots (one per poll, ~0.75 s) a * block whose authoritative state is 'falling' keeps descending at that * player's fallSpeed (13 / fallTime cells/s), with sub-cell motion. It * never goes below the first obstacle in its column, so a landing, lock * or match is never predicted — those only appear when the next * snapshot says so. When a snapshot arrives the shown position is * reconciled smoothly: a block that lags catches up a little faster, a * block that ran ahead falls more slowly until the truth catches up * (it never moves up), and a large difference snaps. */ var MiniPredict = { FIELD_HEIGHT: 13, /** Seconds over which a lagging block closes its gap (on top of fallSpeed). */ RECONCILE_S: 0.16, /** Cells: further off than this → snap to the authoritative pose. */ SNAP_CELLS: 1.5, /** Never extrapolate further than this past the last snapshot (s). */ MAX_EXTRAP_S: 1.5, /** Share of fallSpeed used while a block that ran ahead waits for the truth. */ AHEAD_SPEED_SHARE: 0.5, /** Largest frame step used for the smoothing integrator (s). */ MAX_FRAME_S: 0.1 }; /** Arrival samples kept for the sender-clock offset estimate. */ MiniPredict.OFFSET_SAMPLES = 16; MiniPredict.create = function () { return { snap: null, at: 0, lastFrame: 0, shown: Object.create(null), offsets: [], sig: null }; }; /** Cheap content signature for snapshots without a sender time. */ MiniPredict.signature = function (snapshot) { if (!snapshot || !snapshot.blocks) { return ''; } var s = String(snapshot.blocks.length); for (var i = 0; i < snapshot.blocks.length; i++) { var b = snapshot.blocks[i]; s += '|' + b.id + ':' + b.y + ':' + b.state; } return s; }; MiniPredict.fallSpeedFor = function (planet) { var ft = planet && (planet.fallTime != null ? planet.fallTime : planet.fall_time); ft = Number(ft); if (!(ft > 0)) { ft = 3; } return this.FIELD_HEIGHT / ft; }; /** * Record an authoritative snapshot. A poll can repeat the same * snapshot (the opponent has not polled since); that must not restart * the extrapolation. With a sender time `t` (ms, sender clock) the * snapshot's moment is mapped to our clock via the smallest observed * arrival offset, so a late-arriving snapshot is placed at the time it * was really taken. Returns true when the snapshot is new. */ MiniPredict.update = function (st, snapshot, now) { if (snapshot === st.snap) { return false; } if (!snapshot) { st.snap = null; st.sig = null; return false; } var hasT = snapshot.t != null && isFinite(snapshot.t); if (st.snap && hasT && st.snap.t === snapshot.t) { st.snap = snapshot; return false; } if (!hasT) { var sig = this.signature(snapshot); if (st.snap && sig === st.sig) { st.snap = snapshot; return false; } st.sig = sig; st.snap = snapshot; st.at = now; return true; } st.offsets.push(now - snapshot.t); if (st.offsets.length > this.OFFSET_SAMPLES) { st.offsets.shift(); } var off = Math.min.apply(null, st.offsets); st.snap = snapshot; st.sig = null; // Never in the future, never older than the extrapolation cap. st.at = Math.max(now - this.MAX_EXTRAP_S * 1000, Math.min(now, snapshot.t + off)); return true; }; /** * Predicted y for every falling block at time `now`, clamped above the * first non-falling block (or the floor) below it in its column, chaining * falling blocks in the same column bottom-up. Returns id → y. */ MiniPredict.predict = function (snapshot, sinceS, fallSpeed) { var out = Object.create(null); if (!snapshot || !snapshot.blocks) { return out; } var dt = Math.max(0, Math.min(this.MAX_EXTRAP_S, sinceS || 0)); var cols = Object.create(null); var blocks = snapshot.blocks; for (var i = 0; i < blocks.length; i++) { var b = blocks[i]; (cols[b.col] = cols[b.col] || []).push(b); } for (var c in cols) { var list = cols[c].slice().sort(function (a, b) { return a.y - b.y; }); var floor = 0; // lowest y the next block may occupy for (var k = 0; k < list.length; k++) { var blk = list[k]; if (blk.state === 'falling') { var y = Math.max(floor, blk.y - fallSpeed * dt); if (y > blk.y) { y = blk.y; } // only ever predict downwards out[blk.id] = y; floor = y + 1; } else { floor = Math.max(floor, blk.y + 1); } } } return out; }; /** * Advance the shown positions to `now` and return id → display y for * falling blocks. Non-falling blocks are not in the map (draw them at * their authoritative y). */ MiniPredict.step = function (st, now, fallSpeed) { var frame = st.lastFrame ? Math.min(this.MAX_FRAME_S, Math.max(0, (now - st.lastFrame) / 1000)) : 0; st.lastFrame = now; var target = this.predict(st.snap, (now - st.at) / 1000, fallSpeed); var shown = Object.create(null); var floors = this._floors(st.snap, target); for (var id in target) { var t = target[id]; var s = st.shown[id]; if (s == null || !isFinite(s) || Math.abs(s - t) > this.SNAP_CELLS) { s = t; } else if (s > t) { // Lagging: fall at fallSpeed plus a share of the gap. s -= (fallSpeed + (s - t) / this.RECONCILE_S) * frame; if (s < t) { s = t; } } else if (s < t) { // Ran ahead: keep falling, slower, never upwards. s -= fallSpeed * this.AHEAD_SPEED_SHARE * frame; } var fl = floors[id] != null ? floors[id] : 0; if (s < fl) { s = fl; } shown[id] = s; } st.shown = shown; return shown; }; /** Floor (lowest allowed y) for each falling block given the targets. */ MiniPredict._floors = function (snapshot, target) { var out = Object.create(null); if (!snapshot || !snapshot.blocks) { return out; } var cols = Object.create(null); for (var i = 0; i < snapshot.blocks.length; i++) { var b = snapshot.blocks[i]; (cols[b.col] = cols[b.col] || []).push(b); } for (var c in cols) { var list = cols[c].slice().sort(function (a, b) { return a.y - b.y; }); var floor = 0; for (var k = 0; k < list.length; k++) { var blk = list[k]; if (blk.state === 'falling') { out[blk.id] = floor; floor = (target[blk.id] != null ? target[blk.id] : blk.y) + 1; } else { floor = Math.max(floor, blk.y + 1); } } } return out; }; if (typeof globalThis !== 'undefined') { globalThis.MiniPredict = MiniPredict; } if (typeof module !== 'undefined' && module.exports) { module.exports = MiniPredict; }