BlueGit

projects-timfalken-com meteors

Files

meteors / test / mini-predict.test.js

mini-predict.test.js

mini-predict.test.js 5.8 KiB Download Raw
1'use strict';
2
3// t1182: prediction / reconcile of falling blocks on opponents' minis.
4
5const { describe, it } = require('node:test');
6const assert = require('node:assert/strict');
7const MP = require('../src/miniPredict.js');
8
9const FS = 13 / 3; // fallTime 3 → 4.33 cells/s
10
11function snap (t, blocks)
12{
13 return { t, blocks };
14}
15
16function 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
24describe('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});