| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * t1182: client-side prediction for falling blocks on opponents' mini |
| 5 | * fields. Purely visual. Between snapshots (one per poll, ~0.75 s) a |
| 6 | * block whose authoritative state is 'falling' keeps descending at that |
| 7 | * player's fallSpeed (13 / fallTime cells/s), with sub-cell motion. It |
| 8 | * never goes below the first obstacle in its column, so a landing, lock |
| 9 | * or match is never predicted — those only appear when the next |
| 10 | * snapshot says so. When a snapshot arrives the shown position is |
| 11 | * reconciled smoothly: a block that lags catches up a little faster, a |
| 12 | * block that ran ahead falls more slowly until the truth catches up |
| 13 | * (it never moves up), and a large difference snaps. |
| 14 | */ |
| 15 | var MiniPredict = { |
| 16 | FIELD_HEIGHT: 13, |
| 17 | /** Seconds over which a lagging block closes its gap (on top of fallSpeed). */ |
| 18 | RECONCILE_S: 0.16, |
| 19 | /** Cells: further off than this → snap to the authoritative pose. */ |
| 20 | SNAP_CELLS: 1.5, |
| 21 | /** Never extrapolate further than this past the last snapshot (s). */ |
| 22 | MAX_EXTRAP_S: 1.5, |
| 23 | /** Share of fallSpeed used while a block that ran ahead waits for the truth. */ |
| 24 | AHEAD_SPEED_SHARE: 0.5, |
| 25 | /** Largest frame step used for the smoothing integrator (s). */ |
| 26 | MAX_FRAME_S: 0.1 |
| 27 | }; |
| 28 | |
| 29 | /** Arrival samples kept for the sender-clock offset estimate. */ |
| 30 | MiniPredict.OFFSET_SAMPLES = 16; |
| 31 | |
| 32 | MiniPredict.create = function () |
| 33 | { |
| 34 | return { snap: null, at: 0, lastFrame: 0, shown: Object.create(null), offsets: [], sig: null }; |
| 35 | }; |
| 36 | |
| 37 | /** Cheap content signature for snapshots without a sender time. */ |
| 38 | MiniPredict.signature = function (snapshot) |
| 39 | { |
| 40 | if (!snapshot || !snapshot.blocks) |
| 41 | { return ''; } |
| 42 | var s = String(snapshot.blocks.length); |
| 43 | for (var i = 0; i < snapshot.blocks.length; i++) |
| 44 | { |
| 45 | var b = snapshot.blocks[i]; |
| 46 | s += '|' + b.id + ':' + b.y + ':' + b.state; |
| 47 | } |
| 48 | return s; |
| 49 | }; |
| 50 | |
| 51 | MiniPredict.fallSpeedFor = function (planet) |
| 52 | { |
| 53 | var ft = planet && (planet.fallTime != null ? planet.fallTime : planet.fall_time); |
| 54 | ft = Number(ft); |
| 55 | if (!(ft > 0)) |
| 56 | { ft = 3; } |
| 57 | return this.FIELD_HEIGHT / ft; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Record an authoritative snapshot. A poll can repeat the same |
| 62 | * snapshot (the opponent has not polled since); that must not restart |
| 63 | * the extrapolation. With a sender time `t` (ms, sender clock) the |
| 64 | * snapshot's moment is mapped to our clock via the smallest observed |
| 65 | * arrival offset, so a late-arriving snapshot is placed at the time it |
| 66 | * was really taken. Returns true when the snapshot is new. |
| 67 | */ |
| 68 | MiniPredict.update = function (st, snapshot, now) |
| 69 | { |
| 70 | if (snapshot === st.snap) |
| 71 | { return false; } |
| 72 | if (!snapshot) |
| 73 | { |
| 74 | st.snap = null; |
| 75 | st.sig = null; |
| 76 | return false; |
| 77 | } |
| 78 | var hasT = snapshot.t != null && isFinite(snapshot.t); |
| 79 | if (st.snap && hasT && st.snap.t === snapshot.t) |
| 80 | { |
| 81 | st.snap = snapshot; |
| 82 | return false; |
| 83 | } |
| 84 | if (!hasT) |
| 85 | { |
| 86 | var sig = this.signature(snapshot); |
| 87 | if (st.snap && sig === st.sig) |
| 88 | { |
| 89 | st.snap = snapshot; |
| 90 | return false; |
| 91 | } |
| 92 | st.sig = sig; |
| 93 | st.snap = snapshot; |
| 94 | st.at = now; |
| 95 | return true; |
| 96 | } |
| 97 | st.offsets.push(now - snapshot.t); |
| 98 | if (st.offsets.length > this.OFFSET_SAMPLES) |
| 99 | { st.offsets.shift(); } |
| 100 | var off = Math.min.apply(null, st.offsets); |
| 101 | st.snap = snapshot; |
| 102 | st.sig = null; |
| 103 | // Never in the future, never older than the extrapolation cap. |
| 104 | st.at = Math.max(now - this.MAX_EXTRAP_S * 1000, Math.min(now, snapshot.t + off)); |
| 105 | return true; |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * Predicted y for every falling block at time `now`, clamped above the |
| 110 | * first non-falling block (or the floor) below it in its column, chaining |
| 111 | * falling blocks in the same column bottom-up. Returns id → y. |
| 112 | */ |
| 113 | MiniPredict.predict = function (snapshot, sinceS, fallSpeed) |
| 114 | { |
| 115 | var out = Object.create(null); |
| 116 | if (!snapshot || !snapshot.blocks) |
| 117 | { return out; } |
| 118 | var dt = Math.max(0, Math.min(this.MAX_EXTRAP_S, sinceS || 0)); |
| 119 | var cols = Object.create(null); |
| 120 | var blocks = snapshot.blocks; |
| 121 | for (var i = 0; i < blocks.length; i++) |
| 122 | { |
| 123 | var b = blocks[i]; |
| 124 | (cols[b.col] = cols[b.col] || []).push(b); |
| 125 | } |
| 126 | for (var c in cols) |
| 127 | { |
| 128 | var list = cols[c].slice().sort(function (a, b) { return a.y - b.y; }); |
| 129 | var floor = 0; // lowest y the next block may occupy |
| 130 | for (var k = 0; k < list.length; k++) |
| 131 | { |
| 132 | var blk = list[k]; |
| 133 | if (blk.state === 'falling') |
| 134 | { |
| 135 | var y = Math.max(floor, blk.y - fallSpeed * dt); |
| 136 | if (y > blk.y) |
| 137 | { y = blk.y; } // only ever predict downwards |
| 138 | out[blk.id] = y; |
| 139 | floor = y + 1; |
| 140 | } |
| 141 | else |
| 142 | { floor = Math.max(floor, blk.y + 1); } |
| 143 | } |
| 144 | } |
| 145 | return out; |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * Advance the shown positions to `now` and return id → display y for |
| 150 | * falling blocks. Non-falling blocks are not in the map (draw them at |
| 151 | * their authoritative y). |
| 152 | */ |
| 153 | MiniPredict.step = function (st, now, fallSpeed) |
| 154 | { |
| 155 | var frame = st.lastFrame ? Math.min(this.MAX_FRAME_S, Math.max(0, (now - st.lastFrame) / 1000)) : 0; |
| 156 | st.lastFrame = now; |
| 157 | var target = this.predict(st.snap, (now - st.at) / 1000, fallSpeed); |
| 158 | var shown = Object.create(null); |
| 159 | var floors = this._floors(st.snap, target); |
| 160 | for (var id in target) |
| 161 | { |
| 162 | var t = target[id]; |
| 163 | var s = st.shown[id]; |
| 164 | if (s == null || !isFinite(s) || Math.abs(s - t) > this.SNAP_CELLS) |
| 165 | { s = t; } |
| 166 | else if (s > t) |
| 167 | { |
| 168 | // Lagging: fall at fallSpeed plus a share of the gap. |
| 169 | s -= (fallSpeed + (s - t) / this.RECONCILE_S) * frame; |
| 170 | if (s < t) |
| 171 | { s = t; } |
| 172 | } |
| 173 | else if (s < t) |
| 174 | { |
| 175 | // Ran ahead: keep falling, slower, never upwards. |
| 176 | s -= fallSpeed * this.AHEAD_SPEED_SHARE * frame; |
| 177 | } |
| 178 | var fl = floors[id] != null ? floors[id] : 0; |
| 179 | if (s < fl) |
| 180 | { s = fl; } |
| 181 | shown[id] = s; |
| 182 | } |
| 183 | st.shown = shown; |
| 184 | return shown; |
| 185 | }; |
| 186 | |
| 187 | /** Floor (lowest allowed y) for each falling block given the targets. */ |
| 188 | MiniPredict._floors = function (snapshot, target) |
| 189 | { |
| 190 | var out = Object.create(null); |
| 191 | if (!snapshot || !snapshot.blocks) |
| 192 | { return out; } |
| 193 | var cols = Object.create(null); |
| 194 | for (var i = 0; i < snapshot.blocks.length; i++) |
| 195 | { |
| 196 | var b = snapshot.blocks[i]; |
| 197 | (cols[b.col] = cols[b.col] || []).push(b); |
| 198 | } |
| 199 | for (var c in cols) |
| 200 | { |
| 201 | var list = cols[c].slice().sort(function (a, b) { return a.y - b.y; }); |
| 202 | var floor = 0; |
| 203 | for (var k = 0; k < list.length; k++) |
| 204 | { |
| 205 | var blk = list[k]; |
| 206 | if (blk.state === 'falling') |
| 207 | { |
| 208 | out[blk.id] = floor; |
| 209 | floor = (target[blk.id] != null ? target[blk.id] : blk.y) + 1; |
| 210 | } |
| 211 | else |
| 212 | { floor = Math.max(floor, blk.y + 1); } |
| 213 | } |
| 214 | } |
| 215 | return out; |
| 216 | }; |
| 217 | |
| 218 | if (typeof globalThis !== 'undefined') |
| 219 | { globalThis.MiniPredict = MiniPredict; } |
| 220 | if (typeof module !== 'undefined' && module.exports) |
| 221 | { module.exports = MiniPredict; } |
projects-timfalken-com meteors