t1182: predict falling blocks on opponents' mini-fields
@@ -20,6 +20,7 @@ FILES=(
20
20
"$SRC_DIR/easing.js"
21
21
"$SRC_DIR/gameCore.js"
22
22
"$SRC_DIR/miniLayout.js"
23
"$SRC_DIR/miniPredict.js"
23
24
"$SRC_DIR/online.js"
24
25
"$SRC_DIR/offline.js"
25
26
"$SRC_DIR/reward.js"
@@ -20,6 +20,7 @@ FILES=(20
"$SRC_DIR/easing.js"20
"$SRC_DIR/easing.js"21
"$SRC_DIR/gameCore.js"21
"$SRC_DIR/gameCore.js"22
"$SRC_DIR/miniLayout.js"22
"$SRC_DIR/miniLayout.js"23
"$SRC_DIR/miniPredict.js"23
"$SRC_DIR/online.js"24
"$SRC_DIR/online.js"24
"$SRC_DIR/offline.js"25
"$SRC_DIR/offline.js"25
"$SRC_DIR/reward.js"26
"$SRC_DIR/reward.js"@@ -0,0 +1,221 @@
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; }
@@ -0,0 +1,221 @@1
'use strict';2
3
/**4
* t1182: client-side prediction for falling blocks on opponents' mini5
* fields. Purely visual. Between snapshots (one per poll, ~0.75 s) a6
* block whose authoritative state is 'falling' keeps descending at that7
* player's fallSpeed (13 / fallTime cells/s), with sub-cell motion. It8
* never goes below the first obstacle in its column, so a landing, lock9
* or match is never predicted — those only appear when the next10
* snapshot says so. When a snapshot arrives the shown position is11
* reconciled smoothly: a block that lags catches up a little faster, a12
* block that ran ahead falls more slowly until the truth catches up13
* (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.127
};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 same62
* snapshot (the opponent has not polled since); that must not restart63
* the extrapolation. With a sender time `t` (ms, sender clock) the64
* snapshot's moment is mapped to our clock via the smallest observed65
* arrival offset, so a late-arriving snapshot is placed at the time it66
* 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 the110
* first non-falling block (or the floor) below it in its column, chaining111
* 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 occupy130
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 downwards138
out[blk.id] = y;139
floor = y + 1;140
}141
else142
{ 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 for150
* falling blocks. Non-falling blocks are not in the map (draw them at151
* 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
else212
{ 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; }@@ -0,0 +1,162 @@
1
'use strict';
2
3
// t1182: prediction / reconcile of falling blocks on opponents' minis.
4
5
const { describe, it } = require('node:test');
6
const assert = require('node:assert/strict');
7
const MP = require('../src/miniPredict.js');
8
9
const FS = 13 / 3; // fallTime 3 → 4.33 cells/s
10
11
function snap (t, blocks)
12
{
13
return { t, blocks };
14
}
15
16
function run (st, from, to, fs, stepMs)
17
{
18
let out = null;
19
for (let now = from; now <= to + 1e-9; now += stepMs || 16)
20
{ out = MP.step(st, now, fs); }
21
return out;
22
}
23
24
describe('MiniPredict (t1182)', () =>
25
{
26
it('fall speed comes from the opponent planet (13 / fallTime)', () =>
27
{
28
assert.equal(MP.fallSpeedFor({ fallTime: 2 }), 6.5);
29
assert.equal(MP.fallSpeedFor({ fallTime: 5 }), 13 / 5);
30
assert.equal(MP.fallSpeedFor(null), 13 / 3);
31
});
32
33
it('extrapolates a falling block downwards between snapshots, sub-cell', () =>
34
{
35
const st = MP.create();
36
MP.update(st, snap(1000, [{ id: 1, col: 2, y: 10, state: 'falling' }]), 5000);
37
const a = MP.step(st, 5000, FS)[1];
38
const b = MP.step(st, 5100, FS)[1];
39
const c = MP.step(st, 5300, FS)[1];
40
assert.equal(a, 10);
41
assert.ok(Math.abs(b - (10 - FS * 0.1)) < 1e-9, 'b=' + b);
42
assert.ok(Math.abs(c - (10 - FS * 0.3)) < 1e-9, 'c=' + c);
43
assert.ok(c % 1 !== 0, 'sub-cell position');
44
});
45
46
it('never predicts a landing below the stack, and leaves non-falling blocks alone', () =>
47
{
48
const st = MP.create();
49
MP.update(st, snap(0, [
50
{ id: 1, col: 4, y: 0, state: 'still' },
51
{ id: 2, col: 4, y: 1, state: 'locked' },
52
{ id: 3, col: 4, y: 4, state: 'falling' },
53
{ id: 4, col: 4, y: 5.5, state: 'falling' }
54
]), 0);
55
const out = run(st, 0, 1500, FS);
56
assert.equal(out[3], 2, 'stops on top of the stack (y=2), no lock shown');
57
assert.equal(out[4], 3, 'the block above it stops one cell higher');
58
assert.equal(out[1], undefined, 'still blocks are drawn at their own y');
59
assert.equal(out[2], undefined);
60
});
61
62
it('caps extrapolation when snapshots stop coming', () =>
63
{
64
const st = MP.create();
65
MP.update(st, snap(0, [{ id: 1, col: 0, y: 12, state: 'falling' }]), 0);
66
const out = run(st, 0, 5000, FS, 50);
67
assert.ok(Math.abs(out[1] - (12 - FS * MP.MAX_EXTRAP_S)) < 1e-6, 'y=' + out[1]);
68
});
69
70
it('a repeated snapshot (opponent has not polled) does not restart the prediction', () =>
71
{
72
const st = MP.create();
73
const s1 = snap(1000, [{ id: 1, col: 0, y: 12, state: 'falling' }]);
74
MP.update(st, s1, 0);
75
run(st, 0, 400, FS);
76
const again = snap(1000, [{ id: 1, col: 0, y: 12, state: 'falling' }]);
77
assert.equal(MP.update(st, again, 400), false);
78
const out = MP.step(st, 416, FS);
79
assert.ok(out[1] < 12 - FS * 0.4 + 0.01, 'kept falling: ' + out[1]);
80
// Same for snapshots without a sender time (content signature).
81
const st2 = MP.create();
82
MP.update(st2, { blocks: [{ id: 1, col: 0, y: 12, state: 'falling' }] }, 0);
83
assert.equal(MP.update(st2, { blocks: [{ id: 1, col: 0, y: 12, state: 'falling' }] }, 400), false);
84
});
85
86
it('a late snapshot is placed at the time it was taken (sender clock offset)', () =>
87
{
88
const st = MP.create();
89
MP.update(st, snap(10000, [{ id: 1, col: 0, y: 12, state: 'falling' }]), 50); // offset 50 ms
90
// Next snapshot taken 750 ms later but arrives 300 ms late.
91
MP.update(st, snap(10750, [{ id: 1, col: 0, y: 12 - FS * 0.75, state: 'falling' }]), 50 + 750 + 300);
92
assert.equal(st.at, 10750 + (50 - 10000));
93
const t = MP.predict(st.snap, (1100 - st.at) / 1000, FS)[1];
94
assert.ok(Math.abs(t - (12 - FS * 1.05)) < 1e-6, 'continuous with true motion: ' + t);
95
});
96
97
it('reconciles smoothly: a lagging block catches up, a leading one never moves up', () =>
98
{
99
// Lagging: shown 0.8 cell above the new truth.
100
const st = MP.create();
101
MP.update(st, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0);
102
MP.step(st, 0, FS);
103
st.shown[1] = 10.8;
104
let prev = 10.8;
105
for (let now = 16; now <= 400; now += 16)
106
{
107
const y = MP.step(st, now, FS)[1];
108
assert.ok(y <= prev + 1e-9, 'monotonic down');
109
prev = y;
110
}
111
const truth = 10 - FS * 0.4;
112
assert.ok(Math.abs(prev - truth) < 0.02, 'caught up: ' + prev + ' vs ' + truth);
113
114
// Leading: shown 0.6 below the truth → keeps falling (slower), never jumps up.
115
const st2 = MP.create();
116
MP.update(st2, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0);
117
MP.step(st2, 0, FS);
118
st2.shown[1] = 9.4;
119
prev = 9.4;
120
for (let now = 16; now <= 800; now += 16)
121
{
122
const y = MP.step(st2, now, FS)[1];
123
assert.ok(y <= prev + 1e-9, 'never up: ' + y + ' > ' + prev);
124
prev = y;
125
}
126
assert.ok(Math.abs(prev - (10 - FS * 0.8)) < 0.05, 'converged: ' + prev);
127
});
128
129
it('snaps when far off (e.g. block jumped / was dragged) and drops blocks that landed', () =>
130
{
131
const st = MP.create();
132
MP.update(st, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0);
133
run(st, 0, 200, FS);
134
// Authoritative: block now 4 cells higher (far) → snap.
135
MP.update(st, snap(250, [{ id: 1, col: 0, y: 13.5, state: 'falling' }]), 250);
136
assert.equal(MP.step(st, 250, FS)[1], 13.5);
137
// Then it landed: not falling any more → no predicted pose.
138
MP.update(st, snap(900, [{ id: 1, col: 0, y: 0, state: 'still' }]), 900);
139
assert.equal(MP.step(st, 916, FS)[1], undefined);
140
});
141
142
it('steady 750 ms snapshots of a constant fall render as one smooth descent', () =>
143
{
144
const st = MP.create();
145
let maxStep = 0;
146
let prev = null;
147
const y0 = 12.5;
148
for (let now = 0; now <= 2200; now += 16)
149
{
150
const k = Math.floor(now / 750);
151
const taken = k * 750;
152
if (now % 750 < 16)
153
{ MP.update(st, snap(taken, [{ id: 9, col: 1, y: y0 - FS * taken / 1000, state: 'falling' }]), now + 40); }
154
const y = MP.step(st, now + 40, FS)[9];
155
if (prev != null)
156
{ maxStep = Math.max(maxStep, Math.abs(prev - y)); }
157
prev = y;
158
}
159
const perFrame = FS * 0.016;
160
assert.ok(maxStep < perFrame * 1.6, 'max per-frame move ' + maxStep.toFixed(3) + ' vs fall ' + perFrame.toFixed(3));
161
});
162
});
@@ -0,0 +1,162 @@1
'use strict';2
3
// t1182: prediction / reconcile of falling blocks on opponents' minis.4
5
const { describe, it } = require('node:test');6
const assert = require('node:assert/strict');7
const MP = require('../src/miniPredict.js');8
9
const FS = 13 / 3; // fallTime 3 → 4.33 cells/s10
11
function snap (t, blocks)12
{13
return { t, blocks };14
}15
16
function run (st, from, to, fs, stepMs)17
{18
let out = null;19
for (let now = from; now <= to + 1e-9; now += stepMs || 16)20
{ out = MP.step(st, now, fs); }21
return out;22
}23
24
describe('MiniPredict (t1182)', () =>25
{26
it('fall speed comes from the opponent planet (13 / fallTime)', () =>27
{28
assert.equal(MP.fallSpeedFor({ fallTime: 2 }), 6.5);29
assert.equal(MP.fallSpeedFor({ fallTime: 5 }), 13 / 5);30
assert.equal(MP.fallSpeedFor(null), 13 / 3);31
});32
33
it('extrapolates a falling block downwards between snapshots, sub-cell', () =>34
{35
const st = MP.create();36
MP.update(st, snap(1000, [{ id: 1, col: 2, y: 10, state: 'falling' }]), 5000);37
const a = MP.step(st, 5000, FS)[1];38
const b = MP.step(st, 5100, FS)[1];39
const c = MP.step(st, 5300, FS)[1];40
assert.equal(a, 10);41
assert.ok(Math.abs(b - (10 - FS * 0.1)) < 1e-9, 'b=' + b);42
assert.ok(Math.abs(c - (10 - FS * 0.3)) < 1e-9, 'c=' + c);43
assert.ok(c % 1 !== 0, 'sub-cell position');44
});45
46
it('never predicts a landing below the stack, and leaves non-falling blocks alone', () =>47
{48
const st = MP.create();49
MP.update(st, snap(0, [50
{ id: 1, col: 4, y: 0, state: 'still' },51
{ id: 2, col: 4, y: 1, state: 'locked' },52
{ id: 3, col: 4, y: 4, state: 'falling' },53
{ id: 4, col: 4, y: 5.5, state: 'falling' }54
]), 0);55
const out = run(st, 0, 1500, FS);56
assert.equal(out[3], 2, 'stops on top of the stack (y=2), no lock shown');57
assert.equal(out[4], 3, 'the block above it stops one cell higher');58
assert.equal(out[1], undefined, 'still blocks are drawn at their own y');59
assert.equal(out[2], undefined);60
});61
62
it('caps extrapolation when snapshots stop coming', () =>63
{64
const st = MP.create();65
MP.update(st, snap(0, [{ id: 1, col: 0, y: 12, state: 'falling' }]), 0);66
const out = run(st, 0, 5000, FS, 50);67
assert.ok(Math.abs(out[1] - (12 - FS * MP.MAX_EXTRAP_S)) < 1e-6, 'y=' + out[1]);68
});69
70
it('a repeated snapshot (opponent has not polled) does not restart the prediction', () =>71
{72
const st = MP.create();73
const s1 = snap(1000, [{ id: 1, col: 0, y: 12, state: 'falling' }]);74
MP.update(st, s1, 0);75
run(st, 0, 400, FS);76
const again = snap(1000, [{ id: 1, col: 0, y: 12, state: 'falling' }]);77
assert.equal(MP.update(st, again, 400), false);78
const out = MP.step(st, 416, FS);79
assert.ok(out[1] < 12 - FS * 0.4 + 0.01, 'kept falling: ' + out[1]);80
// Same for snapshots without a sender time (content signature).81
const st2 = MP.create();82
MP.update(st2, { blocks: [{ id: 1, col: 0, y: 12, state: 'falling' }] }, 0);83
assert.equal(MP.update(st2, { blocks: [{ id: 1, col: 0, y: 12, state: 'falling' }] }, 400), false);84
});85
86
it('a late snapshot is placed at the time it was taken (sender clock offset)', () =>87
{88
const st = MP.create();89
MP.update(st, snap(10000, [{ id: 1, col: 0, y: 12, state: 'falling' }]), 50); // offset 50 ms90
// Next snapshot taken 750 ms later but arrives 300 ms late.91
MP.update(st, snap(10750, [{ id: 1, col: 0, y: 12 - FS * 0.75, state: 'falling' }]), 50 + 750 + 300);92
assert.equal(st.at, 10750 + (50 - 10000));93
const t = MP.predict(st.snap, (1100 - st.at) / 1000, FS)[1];94
assert.ok(Math.abs(t - (12 - FS * 1.05)) < 1e-6, 'continuous with true motion: ' + t);95
});96
97
it('reconciles smoothly: a lagging block catches up, a leading one never moves up', () =>98
{99
// Lagging: shown 0.8 cell above the new truth.100
const st = MP.create();101
MP.update(st, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0);102
MP.step(st, 0, FS);103
st.shown[1] = 10.8;104
let prev = 10.8;105
for (let now = 16; now <= 400; now += 16)106
{107
const y = MP.step(st, now, FS)[1];108
assert.ok(y <= prev + 1e-9, 'monotonic down');109
prev = y;110
}111
const truth = 10 - FS * 0.4;112
assert.ok(Math.abs(prev - truth) < 0.02, 'caught up: ' + prev + ' vs ' + truth);113
114
// Leading: shown 0.6 below the truth → keeps falling (slower), never jumps up.115
const st2 = MP.create();116
MP.update(st2, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0);117
MP.step(st2, 0, FS);118
st2.shown[1] = 9.4;119
prev = 9.4;120
for (let now = 16; now <= 800; now += 16)121
{122
const y = MP.step(st2, now, FS)[1];123
assert.ok(y <= prev + 1e-9, 'never up: ' + y + ' > ' + prev);124
prev = y;125
}126
assert.ok(Math.abs(prev - (10 - FS * 0.8)) < 0.05, 'converged: ' + prev);127
});128
129
it('snaps when far off (e.g. block jumped / was dragged) and drops blocks that landed', () =>130
{131
const st = MP.create();132
MP.update(st, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0);133
run(st, 0, 200, FS);134
// Authoritative: block now 4 cells higher (far) → snap.135
MP.update(st, snap(250, [{ id: 1, col: 0, y: 13.5, state: 'falling' }]), 250);136
assert.equal(MP.step(st, 250, FS)[1], 13.5);137
// Then it landed: not falling any more → no predicted pose.138
MP.update(st, snap(900, [{ id: 1, col: 0, y: 0, state: 'still' }]), 900);139
assert.equal(MP.step(st, 916, FS)[1], undefined);140
});141
142
it('steady 750 ms snapshots of a constant fall render as one smooth descent', () =>143
{144
const st = MP.create();145
let maxStep = 0;146
let prev = null;147
const y0 = 12.5;148
for (let now = 0; now <= 2200; now += 16)149
{150
const k = Math.floor(now / 750);151
const taken = k * 750;152
if (now % 750 < 16)153
{ MP.update(st, snap(taken, [{ id: 9, col: 1, y: y0 - FS * taken / 1000, state: 'falling' }]), now + 40); }154
const y = MP.step(st, now + 40, FS)[9];155
if (prev != null)156
{ maxStep = Math.max(maxStep, Math.abs(prev - y)); }157
prev = y;158
}159
const perFrame = FS * 0.016;160
assert.ok(maxStep < perFrame * 1.6, 'max per-frame move ' + maxStep.toFixed(3) + ' vs fall ' + perFrame.toFixed(3));161
});162
});