test: shot-into-cluster, video scenario, invariant soak and gap teleports
Unit tests for t1111 (push column, attach at bottom, successive shots, two launched blocks). A reproduction of the video column (repeated launches under a flying cluster plus falling blocks). A randomized soak of thousands of ticks across several seeds, asserting no overlap, no illegal colour change and no vanish without a score. Falling-below- a-gap and hanging-chain tests for the t1112 stackTop and releaseUnsupported fixes. makeGame enables strictInvariants by default.
@@ -16,6 +16,8 @@ function makeGame (opts)
16
16
const game = new GameCore(planet, rng);
17
17
if (opts.noSpawn)
18
18
{ game.spawnInterval = 1e9; }
19
if (opts.strictInvariants !== false)
20
{ game.strictInvariants = true; }
19
21
return game;
20
22
}
21
23
@@ -16,6 +16,8 @@ function makeGame (opts)16
const game = new GameCore(planet, rng);16
const game = new GameCore(planet, rng);17
if (opts.noSpawn)17
if (opts.noSpawn)18
{ game.spawnInterval = 1e9; }18
{ game.spawnInterval = 1e9; }19
if (opts.strictInvariants !== false)20
{ game.strictInvariants = true; }19
return game;21
return game;20
}22
}21
23
@@ -0,0 +1,406 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, almost, GameCore, Planet, Rng } = require('./helpers.js');
6
7
function flyingHeld (game, y, colorsByCol)
8
{
9
const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b);
10
const cluster = {
11
id: game.nextClusterId++,
12
jumpHeight: 3,
13
vy: 0,
14
decel: 0,
15
phase: 'hold',
16
anchorY: y
17
};
18
game.clusters[cluster.id] = cluster;
19
const members = [];
20
for (const col of cols)
21
{
22
const b = game.createBlock(col, y, colorsByCol[col], 'flying');
23
b.clusterId = cluster.id;
24
b.clusterOffset = 0;
25
b.vy = 0;
26
if (colorsByCol[col] === GameCore.COLOR_GRAY)
27
{ b.flame = true; }
28
members.push(b);
29
}
30
game.prepareClusterBody(cluster);
31
return { cluster, members };
32
}
33
34
function waitUntil (g, pred, maxTicks)
35
{
36
for (let i = 0; i < (maxTicks || 800); i++)
37
{
38
g.tick(1 / 120);
39
if (pred())
40
{ return i; }
41
}
42
return -1;
43
}
44
45
function columnStack (g, col)
46
{
47
return g.blocks
48
.filter((b) => b.col === col)
49
.sort((a, b) => a.y - b.y)
50
.map((b) => ({
51
id: b.id,
52
color: b.color,
53
y: b.y,
54
state: b.state,
55
clusterId: b.clusterId,
56
offset: b.clusterOffset
57
}));
58
}
59
60
function assertPacked (blocks, label)
61
{
62
const sorted = blocks.slice().sort((a, b) => a.y - b.y);
63
for (let i = 1; i < sorted.length; i++)
64
{
65
assert.ok(sorted[i].y >= sorted[i - 1].y + 1 - 1e-6,
66
(label || 'pack') + ': overlap ' + sorted[i - 1].y + ' and ' + sorted[i].y);
67
}
68
}
69
70
describe('Shot into a flying cluster (t1111)', () =>
71
{
72
it('pushes the hit column up by 1 and attaches the shot at the bottom, grid-aligned', () =>
73
{
74
const g = makeGame({ noSpawn: true });
75
const { cluster, members } = flyingHeld(g, 6, {
76
1: GameCore.COLOR_GRAY,
77
2: GameCore.COLOR_GRAY,
78
3: GameCore.COLOR_GRAY
79
});
80
const hit = members.find((b) => b.col === 2);
81
const left = members.find((b) => b.col === 1);
82
const right = members.find((b) => b.col === 3);
83
const shot = g.place(2, 1, 4);
84
g.startManualLaunch(shot);
85
const n = waitUntil(g, () => shot.state === 'flying' && shot.clusterId === cluster.id);
86
assert.ok(n >= 0, 'shot must attach, state=' + shot.state + ' cid=' + shot.clusterId
87
+ ' y=' + shot.y + ' hit=' + hit.y);
88
assert.equal(shot.clusterId, cluster.id);
89
assert.equal(hit.clusterId, cluster.id);
90
assert.ok(almost(hit.y - shot.y, 1, 1e-6),
91
'shot sits exactly 1 under the pushed member, shot=' + shot.y + ' hit=' + hit.y);
92
assert.ok(almost(left.y, right.y, 1e-6), 'other columns stay level');
93
assert.ok(almost(left.y, shot.y, 0.05), 'shot is at the old bottom (other cols), left=' + left.y + ' shot=' + shot.y);
94
assert.ok(almost(hit.y - left.y, 1, 0.05), 'hit column rose by 1 relative to neighbours');
95
assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; '));
96
});
97
98
it('successive shots each push the column 1 and insert at the bottom, never overlapping', () =>
99
{
100
const g = makeGame({ noSpawn: true });
101
const { cluster, members } = flyingHeld(g, 7, { 4: GameCore.COLOR_GRAY });
102
const original = members[0];
103
const shots = [];
104
for (let s = 0; s < 4; s++)
105
{
106
const shot = g.createBlock(4, 0.2, s % 5, 'still');
107
g.startManualLaunch(shot);
108
shots.push(shot);
109
const n = waitUntil(g, () => shot.state === 'flying' && shot.clusterId === cluster.id, 600);
110
assert.ok(n >= 0, 'shot ' + s + ' must attach, state=' + shot.state + ' y=' + shot.y
111
+ ' stack=' + JSON.stringify(columnStack(g, 4)));
112
assert.equal(shot.clusterId, cluster.id);
113
assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; '));
114
}
115
const stack = g.clusterMembers(cluster.id)
116
.filter((b) => b.col === 4)
117
.sort((a, b) => a.y - b.y);
118
assert.equal(stack.length, 5, 'original + 4 shots, got ' + stack.length);
119
// Bottom-to-top: last shot, then earlier shots, then the original at the top.
120
assert.equal(stack[0].id, shots[3].id, 'last shot is at the bottom');
121
assert.equal(stack[4].id, original.id, 'original member is at the top');
122
for (let i = 1; i < stack.length; i++)
123
{
124
assert.ok(almost(stack[i].y - stack[i - 1].y, 1, 1e-6),
125
'integer packing, ' + stack[i - 1].y + ' then ' + stack[i].y);
126
}
127
});
128
129
it('two launched blocks in the same column never overlap; the later one attaches under the earlier', () =>
130
{
131
const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
132
const first = g.place(3, 0, 1);
133
g.startManualLaunch(first);
134
// Let the first rise a few tiles.
135
g.step(0.25, 1 / 120);
136
assert.equal(first.state, 'manual');
137
assert.ok(first.y > 1, 'first is in the air, y=' + first.y);
138
const second = g.createBlock(3, 0.2, 2, 'still');
139
g.startManualLaunch(second);
140
const n = waitUntil(g, () => second.state === 'flying' && first.state === 'flying'
141
&& first.clusterId != null && first.clusterId === second.clusterId, 800);
142
assert.ok(n >= 0, 'they must form a cluster, first=' + first.state + '@' + first.y
143
+ ' second=' + second.state + '@' + second.y);
144
assert.ok(almost(Math.abs(first.y - second.y), 1, 1e-6),
145
'packed 1 apart, first=' + first.y + ' second=' + second.y);
146
assert.ok(second.y < first.y - 0.5, 'later shot is under the earlier');
147
assert.equal(g.checkInvariants().length, 0);
148
});
149
});
150
151
describe('Video scenario: repeated launches under a cluster with falling blocks (t1111/t1112)', () =>
152
{
153
it('never overlaps, vanishes, duplicates or reorders illegally', () =>
154
{
155
const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
156
// 3rd column from the left is col 2. A flying grey+pink cluster,
157
// falling greens, and several launched pinks and a yellow — the
158
// shape of the t1111.webm clip.
159
const { cluster } = flyingHeld(g, 8, {
160
2: GameCore.COLOR_GRAY
161
});
162
cluster.phase = 'down';
163
cluster.vy = -1;
164
cluster.decel = 0;
165
g.syncClusterMembers(cluster);
166
167
const cargo = g.createBlock(2, 9, 1, 'falling'); // pink-ish color 1
168
cargo.vy = -g.planet.fallSpeed;
169
const fallingGreen = g.createBlock(2, 12, 3, 'falling');
170
fallingGreen.vy = -g.planet.fallSpeed * 0.5;
171
172
const launched = [];
173
// First pink already on the way.
174
const p1 = g.place(2, 1, 1);
175
g.startManualLaunch(p1);
176
launched.push(p1);
177
178
const seen = new Map();
179
const recordColors = () =>
180
{
181
for (const b of g.blocks)
182
{
183
if (!seen.has(b.id))
184
{ seen.set(b.id, b.color); }
185
else
186
{
187
const prev = seen.get(b.id);
188
if (prev !== b.color)
189
{
190
const ok = prev === GameCore.COLOR_GRAY || b.color === GameCore.COLOR_GRAY;
191
assert.ok(ok, 'illegal colour change id=' + b.id + ' ' + prev + ' → ' + b.color
192
+ ' at y=' + b.y);
193
seen.set(b.id, b.color);
194
}
195
}
196
}
197
};
198
recordColors();
199
200
let launchesLeft = 4;
201
const colorsToShoot = [1, 2, 1, 2]; // pink, yellow, pink, yellow
202
for (let i = 0; i < 900; i++)
203
{
204
if (launchesLeft > 0 && i > 0 && i % 40 === 0)
205
{
206
// Only launch if the floor cell is free.
207
const occupant = g.blocks.find((b) => b.col === 2 && b.y < 1);
208
if (!occupant)
209
{
210
const shot = g.createBlock(2, 0, colorsToShoot[4 - launchesLeft], 'still');
211
g.startManualLaunch(shot);
212
launched.push(shot);
213
launchesLeft--;
214
seen.set(shot.id, shot.color);
215
}
216
}
217
g.tick(1 / 120);
218
recordColors();
219
const issues = g.checkInvariants();
220
assert.equal(issues.length, 0, 'tick ' + i + ': ' + issues.join('; ')
221
+ '\n' + g.snapshot().picture
222
+ '\n' + JSON.stringify(columnStack(g, 2)));
223
}
224
225
// Everything still in col 2 is packed, no overlaps, and every
226
// launched block still exists or was scored off the top.
227
const remaining = g.blocks.filter((b) => b.col === 2);
228
assertPacked(remaining, 'final col 2');
229
for (const shot of launched)
230
{
231
const still = g.blockById(shot.id);
232
const scored = g.score > 0 || !still;
233
assert.ok(still || scored, 'shot ' + shot.id + ' vanished without scoring');
234
}
235
assert.equal(g.checkInvariants().length, 0);
236
});
237
});
238
239
describe('Falling must not teleport up through a gap (t1112)', () =>
240
{
241
it('a falling block below a hanging still lands on the stack under it, not on the hanging block', () =>
242
{
243
const g = makeGame({ noSpawn: true });
244
g.place(2, 0, 0);
245
const hanging = g.place(2, 5, 1);
246
const falling = g.createBlock(2, 2, 3, 'falling');
247
falling.vy = 0;
248
g.tick(1 / 120);
249
assert.ok(falling.y < hanging.y - 0.5,
250
'must not teleport up onto the hanging block, falling.y=' + falling.y
251
+ ' hanging.y=' + hanging.y + ' state=' + falling.state);
252
assert.ok(falling.y <= 2 + 1e-6, 'must not rise, y=' + falling.y);
253
assert.equal(g.checkInvariants().length, 0);
254
});
255
256
it('a chain of stills sitting on a released landed cluster all fall, from rest', () =>
257
{
258
const g = makeGame({ noSpawn: true });
259
const { cluster } = flyingHeld(g, 0, { 3: GameCore.COLOR_GRAY });
260
// Build a 3-high landed group and two stills on top.
261
const grey = g.clusterMembers(cluster.id)[0];
262
grey.y = 0;
263
grey.state = 'settling';
264
grey.settleTime = 0;
265
cluster.phase = 'landed';
266
const mid = g.createBlock(3, 1, GameCore.COLOR_GRAY, 'settling');
267
mid.clusterId = cluster.id;
268
mid.settleTime = 0;
269
const topGrey = g.createBlock(3, 2, GameCore.COLOR_GRAY, 'settling');
270
topGrey.clusterId = cluster.id;
271
topGrey.settleTime = 0;
272
g.prepareClusterBody(cluster);
273
const a = g.place(3, 3, 1);
274
const b = g.place(3, 4, 2);
275
// Recolor and release.
276
g.randomColor = () => 4;
277
g.step(g.settleTime + 0.05, 1 / 120);
278
// After release the original greys (now colour 4) that have no
279
// outside support fall; the stills that sat on them must fall too,
280
// not hang or teleport.
281
for (const block of [a, b, mid, topGrey])
282
{
283
if (!g.blockById(block.id))
284
{ continue; }
285
assert.notEqual(block.state, 'still',
286
'block ' + block.id + ' at y=' + block.y + ' should have fallen or launched, state=' + block.state);
287
}
288
assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; '));
289
});
290
});
291
292
describe('Randomized physics soak (t1112)', () =>
293
{
294
it('thousands of ticks of random launches, drags, matches and landings keep the invariants', () =>
295
{
296
const seeds = [1, 2, 7, 13, 42, 99, 128, 255];
297
const ticksPerSeed = 2500;
298
for (const seed of seeds)
299
{
300
const rng = Rng.create(seed);
301
const g = makeGame({
302
noSpawn: true,
303
seed,
304
planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.2 }
305
});
306
g.strictInvariants = true;
307
// A grounded mess of colours, not a ready-made match.
308
for (let c = 0; c < 10; c++)
309
{
310
const h = 1 + Math.floor(rng() * 4);
311
for (let r = 0; r < h; r++)
312
{ g.place(c, r, Math.floor(rng() * 5)); }
313
}
314
const colors = new Map();
315
for (const b of g.blocks)
316
{ colors.set(b.id, b.color); }
317
let scored = 0;
318
for (let t = 0; t < ticksPerSeed; t++)
319
{
320
const roll = rng();
321
if (roll < 0.04)
322
{
323
// Random drag in a still column.
324
const still = g.blocks.filter((b) => g.isDraggableBlock && g.isDraggableBlock(b));
325
if (still.length)
326
{
327
const b = still[Math.floor(rng() * still.length)];
328
if (g.beginDrag(b.col + 0.5, b.y + 0.5))
329
{
330
const dest = b.y + (rng() < 0.5 ? 1 : -1) * (1 + Math.floor(rng() * 3));
331
g.moveDrag(dest);
332
g.endDrag();
333
}
334
}
335
}
336
else if (roll < 0.08)
337
{
338
// Launch a top still block.
339
const tops = [];
340
for (let c = 0; c < g.width; c++)
341
{
342
const list = g.blocks.filter((b) => b.col === c && (b.state === 'still' || b.state === 'settling'));
343
if (!list.length)
344
{ continue; }
345
list.sort((a, b) => b.y - a.y);
346
tops.push(list[0]);
347
}
348
if (tops.length)
349
{
350
const b = tops[Math.floor(rng() * tops.length)];
351
if (g.beginDrag(b.col + 0.5, b.y + 0.5))
352
{
353
g.moveDrag(b.y + 1 + 0.5);
354
g.endDrag();
355
}
356
}
357
}
358
else if (roll < 0.1)
359
{
360
// Drop a falling block into a random column if there is room.
361
const col = Math.floor(rng() * g.width);
362
const top = Math.max(-1, ...g.blocks.filter((b) => b.col === col).map((b) => b.y));
363
if (top < g.height - 3)
364
{
365
const fb = g.createBlock(col, g.height - 1.2, Math.floor(rng() * 5), 'falling');
366
fb.vy = -g.planet.fallSpeed;
367
colors.set(fb.id, fb.color);
368
}
369
}
370
371
const beforeIds = new Set(g.blocks.map((b) => b.id));
372
const scoreBefore = g.score;
373
g.tick(1 / 120);
374
scored += g.score - scoreBefore;
375
376
for (const b of g.blocks)
377
{
378
if (!colors.has(b.id))
379
{ colors.set(b.id, b.color); }
380
else if (colors.get(b.id) !== b.color)
381
{
382
const prev = colors.get(b.id);
383
const ok = prev === GameCore.COLOR_GRAY || b.color === GameCore.COLOR_GRAY;
384
assert.ok(ok, 'seed=' + seed + ' tick=' + t
385
+ ' illegal colour change id=' + b.id + ' ' + prev + ' → ' + b.color
386
+ '\n' + g.snapshot().picture);
387
colors.set(b.id, b.color);
388
}
389
}
390
for (const id of beforeIds)
391
{
392
if (!g.blockById(id) && g.score === scoreBefore)
393
{
394
// cullOffTop increments score. A disappearance
395
// without a point is a vanish.
396
assert.ok(g.score > scoreBefore,
397
'seed=' + seed + ' tick=' + t + ' block ' + id + ' vanished without scoring'
398
+ '\n' + g.snapshot().picture);
399
}
400
}
401
}
402
assert.equal(g.checkInvariants().length, 0, 'seed=' + seed + ' final: '
403
+ g.checkInvariants().join('; '));
404
}
405
});
406
});
@@ -0,0 +1,406 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, almost, GameCore, Planet, Rng } = require('./helpers.js');6
7
function flyingHeld (game, y, colorsByCol)8
{9
const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b);10
const cluster = {11
id: game.nextClusterId++,12
jumpHeight: 3,13
vy: 0,14
decel: 0,15
phase: 'hold',16
anchorY: y17
};18
game.clusters[cluster.id] = cluster;19
const members = [];20
for (const col of cols)21
{22
const b = game.createBlock(col, y, colorsByCol[col], 'flying');23
b.clusterId = cluster.id;24
b.clusterOffset = 0;25
b.vy = 0;26
if (colorsByCol[col] === GameCore.COLOR_GRAY)27
{ b.flame = true; }28
members.push(b);29
}30
game.prepareClusterBody(cluster);31
return { cluster, members };32
}33
34
function waitUntil (g, pred, maxTicks)35
{36
for (let i = 0; i < (maxTicks || 800); i++)37
{38
g.tick(1 / 120);39
if (pred())40
{ return i; }41
}42
return -1;43
}44
45
function columnStack (g, col)46
{47
return g.blocks48
.filter((b) => b.col === col)49
.sort((a, b) => a.y - b.y)50
.map((b) => ({51
id: b.id,52
color: b.color,53
y: b.y,54
state: b.state,55
clusterId: b.clusterId,56
offset: b.clusterOffset57
}));58
}59
60
function assertPacked (blocks, label)61
{62
const sorted = blocks.slice().sort((a, b) => a.y - b.y);63
for (let i = 1; i < sorted.length; i++)64
{65
assert.ok(sorted[i].y >= sorted[i - 1].y + 1 - 1e-6,66
(label || 'pack') + ': overlap ' + sorted[i - 1].y + ' and ' + sorted[i].y);67
}68
}69
70
describe('Shot into a flying cluster (t1111)', () =>71
{72
it('pushes the hit column up by 1 and attaches the shot at the bottom, grid-aligned', () =>73
{74
const g = makeGame({ noSpawn: true });75
const { cluster, members } = flyingHeld(g, 6, {76
1: GameCore.COLOR_GRAY,77
2: GameCore.COLOR_GRAY,78
3: GameCore.COLOR_GRAY79
});80
const hit = members.find((b) => b.col === 2);81
const left = members.find((b) => b.col === 1);82
const right = members.find((b) => b.col === 3);83
const shot = g.place(2, 1, 4);84
g.startManualLaunch(shot);85
const n = waitUntil(g, () => shot.state === 'flying' && shot.clusterId === cluster.id);86
assert.ok(n >= 0, 'shot must attach, state=' + shot.state + ' cid=' + shot.clusterId87
+ ' y=' + shot.y + ' hit=' + hit.y);88
assert.equal(shot.clusterId, cluster.id);89
assert.equal(hit.clusterId, cluster.id);90
assert.ok(almost(hit.y - shot.y, 1, 1e-6),91
'shot sits exactly 1 under the pushed member, shot=' + shot.y + ' hit=' + hit.y);92
assert.ok(almost(left.y, right.y, 1e-6), 'other columns stay level');93
assert.ok(almost(left.y, shot.y, 0.05), 'shot is at the old bottom (other cols), left=' + left.y + ' shot=' + shot.y);94
assert.ok(almost(hit.y - left.y, 1, 0.05), 'hit column rose by 1 relative to neighbours');95
assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; '));96
});97
98
it('successive shots each push the column 1 and insert at the bottom, never overlapping', () =>99
{100
const g = makeGame({ noSpawn: true });101
const { cluster, members } = flyingHeld(g, 7, { 4: GameCore.COLOR_GRAY });102
const original = members[0];103
const shots = [];104
for (let s = 0; s < 4; s++)105
{106
const shot = g.createBlock(4, 0.2, s % 5, 'still');107
g.startManualLaunch(shot);108
shots.push(shot);109
const n = waitUntil(g, () => shot.state === 'flying' && shot.clusterId === cluster.id, 600);110
assert.ok(n >= 0, 'shot ' + s + ' must attach, state=' + shot.state + ' y=' + shot.y111
+ ' stack=' + JSON.stringify(columnStack(g, 4)));112
assert.equal(shot.clusterId, cluster.id);113
assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; '));114
}115
const stack = g.clusterMembers(cluster.id)116
.filter((b) => b.col === 4)117
.sort((a, b) => a.y - b.y);118
assert.equal(stack.length, 5, 'original + 4 shots, got ' + stack.length);119
// Bottom-to-top: last shot, then earlier shots, then the original at the top.120
assert.equal(stack[0].id, shots[3].id, 'last shot is at the bottom');121
assert.equal(stack[4].id, original.id, 'original member is at the top');122
for (let i = 1; i < stack.length; i++)123
{124
assert.ok(almost(stack[i].y - stack[i - 1].y, 1, 1e-6),125
'integer packing, ' + stack[i - 1].y + ' then ' + stack[i].y);126
}127
});128
129
it('two launched blocks in the same column never overlap; the later one attaches under the earlier', () =>130
{131
const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });132
const first = g.place(3, 0, 1);133
g.startManualLaunch(first);134
// Let the first rise a few tiles.135
g.step(0.25, 1 / 120);136
assert.equal(first.state, 'manual');137
assert.ok(first.y > 1, 'first is in the air, y=' + first.y);138
const second = g.createBlock(3, 0.2, 2, 'still');139
g.startManualLaunch(second);140
const n = waitUntil(g, () => second.state === 'flying' && first.state === 'flying'141
&& first.clusterId != null && first.clusterId === second.clusterId, 800);142
assert.ok(n >= 0, 'they must form a cluster, first=' + first.state + '@' + first.y143
+ ' second=' + second.state + '@' + second.y);144
assert.ok(almost(Math.abs(first.y - second.y), 1, 1e-6),145
'packed 1 apart, first=' + first.y + ' second=' + second.y);146
assert.ok(second.y < first.y - 0.5, 'later shot is under the earlier');147
assert.equal(g.checkInvariants().length, 0);148
});149
});150
151
describe('Video scenario: repeated launches under a cluster with falling blocks (t1111/t1112)', () =>152
{153
it('never overlaps, vanishes, duplicates or reorders illegally', () =>154
{155
const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });156
// 3rd column from the left is col 2. A flying grey+pink cluster,157
// falling greens, and several launched pinks and a yellow — the158
// shape of the t1111.webm clip.159
const { cluster } = flyingHeld(g, 8, {160
2: GameCore.COLOR_GRAY161
});162
cluster.phase = 'down';163
cluster.vy = -1;164
cluster.decel = 0;165
g.syncClusterMembers(cluster);166
167
const cargo = g.createBlock(2, 9, 1, 'falling'); // pink-ish color 1168
cargo.vy = -g.planet.fallSpeed;169
const fallingGreen = g.createBlock(2, 12, 3, 'falling');170
fallingGreen.vy = -g.planet.fallSpeed * 0.5;171
172
const launched = [];173
// First pink already on the way.174
const p1 = g.place(2, 1, 1);175
g.startManualLaunch(p1);176
launched.push(p1);177
178
const seen = new Map();179
const recordColors = () =>180
{181
for (const b of g.blocks)182
{183
if (!seen.has(b.id))184
{ seen.set(b.id, b.color); }185
else186
{187
const prev = seen.get(b.id);188
if (prev !== b.color)189
{190
const ok = prev === GameCore.COLOR_GRAY || b.color === GameCore.COLOR_GRAY;191
assert.ok(ok, 'illegal colour change id=' + b.id + ' ' + prev + ' → ' + b.color192
+ ' at y=' + b.y);193
seen.set(b.id, b.color);194
}195
}196
}197
};198
recordColors();199
200
let launchesLeft = 4;201
const colorsToShoot = [1, 2, 1, 2]; // pink, yellow, pink, yellow202
for (let i = 0; i < 900; i++)203
{204
if (launchesLeft > 0 && i > 0 && i % 40 === 0)205
{206
// Only launch if the floor cell is free.207
const occupant = g.blocks.find((b) => b.col === 2 && b.y < 1);208
if (!occupant)209
{210
const shot = g.createBlock(2, 0, colorsToShoot[4 - launchesLeft], 'still');211
g.startManualLaunch(shot);212
launched.push(shot);213
launchesLeft--;214
seen.set(shot.id, shot.color);215
}216
}217
g.tick(1 / 120);218
recordColors();219
const issues = g.checkInvariants();220
assert.equal(issues.length, 0, 'tick ' + i + ': ' + issues.join('; ')221
+ '\n' + g.snapshot().picture222
+ '\n' + JSON.stringify(columnStack(g, 2)));223
}224
225
// Everything still in col 2 is packed, no overlaps, and every226
// launched block still exists or was scored off the top.227
const remaining = g.blocks.filter((b) => b.col === 2);228
assertPacked(remaining, 'final col 2');229
for (const shot of launched)230
{231
const still = g.blockById(shot.id);232
const scored = g.score > 0 || !still;233
assert.ok(still || scored, 'shot ' + shot.id + ' vanished without scoring');234
}235
assert.equal(g.checkInvariants().length, 0);236
});237
});238
239
describe('Falling must not teleport up through a gap (t1112)', () =>240
{241
it('a falling block below a hanging still lands on the stack under it, not on the hanging block', () =>242
{243
const g = makeGame({ noSpawn: true });244
g.place(2, 0, 0);245
const hanging = g.place(2, 5, 1);246
const falling = g.createBlock(2, 2, 3, 'falling');247
falling.vy = 0;248
g.tick(1 / 120);249
assert.ok(falling.y < hanging.y - 0.5,250
'must not teleport up onto the hanging block, falling.y=' + falling.y251
+ ' hanging.y=' + hanging.y + ' state=' + falling.state);252
assert.ok(falling.y <= 2 + 1e-6, 'must not rise, y=' + falling.y);253
assert.equal(g.checkInvariants().length, 0);254
});255
256
it('a chain of stills sitting on a released landed cluster all fall, from rest', () =>257
{258
const g = makeGame({ noSpawn: true });259
const { cluster } = flyingHeld(g, 0, { 3: GameCore.COLOR_GRAY });260
// Build a 3-high landed group and two stills on top.261
const grey = g.clusterMembers(cluster.id)[0];262
grey.y = 0;263
grey.state = 'settling';264
grey.settleTime = 0;265
cluster.phase = 'landed';266
const mid = g.createBlock(3, 1, GameCore.COLOR_GRAY, 'settling');267
mid.clusterId = cluster.id;268
mid.settleTime = 0;269
const topGrey = g.createBlock(3, 2, GameCore.COLOR_GRAY, 'settling');270
topGrey.clusterId = cluster.id;271
topGrey.settleTime = 0;272
g.prepareClusterBody(cluster);273
const a = g.place(3, 3, 1);274
const b = g.place(3, 4, 2);275
// Recolor and release.276
g.randomColor = () => 4;277
g.step(g.settleTime + 0.05, 1 / 120);278
// After release the original greys (now colour 4) that have no279
// outside support fall; the stills that sat on them must fall too,280
// not hang or teleport.281
for (const block of [a, b, mid, topGrey])282
{283
if (!g.blockById(block.id))284
{ continue; }285
assert.notEqual(block.state, 'still',286
'block ' + block.id + ' at y=' + block.y + ' should have fallen or launched, state=' + block.state);287
}288
assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; '));289
});290
});291
292
describe('Randomized physics soak (t1112)', () =>293
{294
it('thousands of ticks of random launches, drags, matches and landings keep the invariants', () =>295
{296
const seeds = [1, 2, 7, 13, 42, 99, 128, 255];297
const ticksPerSeed = 2500;298
for (const seed of seeds)299
{300
const rng = Rng.create(seed);301
const g = makeGame({302
noSpawn: true,303
seed,304
planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.2 }305
});306
g.strictInvariants = true;307
// A grounded mess of colours, not a ready-made match.308
for (let c = 0; c < 10; c++)309
{310
const h = 1 + Math.floor(rng() * 4);311
for (let r = 0; r < h; r++)312
{ g.place(c, r, Math.floor(rng() * 5)); }313
}314
const colors = new Map();315
for (const b of g.blocks)316
{ colors.set(b.id, b.color); }317
let scored = 0;318
for (let t = 0; t < ticksPerSeed; t++)319
{320
const roll = rng();321
if (roll < 0.04)322
{323
// Random drag in a still column.324
const still = g.blocks.filter((b) => g.isDraggableBlock && g.isDraggableBlock(b));325
if (still.length)326
{327
const b = still[Math.floor(rng() * still.length)];328
if (g.beginDrag(b.col + 0.5, b.y + 0.5))329
{330
const dest = b.y + (rng() < 0.5 ? 1 : -1) * (1 + Math.floor(rng() * 3));331
g.moveDrag(dest);332
g.endDrag();333
}334
}335
}336
else if (roll < 0.08)337
{338
// Launch a top still block.339
const tops = [];340
for (let c = 0; c < g.width; c++)341
{342
const list = g.blocks.filter((b) => b.col === c && (b.state === 'still' || b.state === 'settling'));343
if (!list.length)344
{ continue; }345
list.sort((a, b) => b.y - a.y);346
tops.push(list[0]);347
}348
if (tops.length)349
{350
const b = tops[Math.floor(rng() * tops.length)];351
if (g.beginDrag(b.col + 0.5, b.y + 0.5))352
{353
g.moveDrag(b.y + 1 + 0.5);354
g.endDrag();355
}356
}357
}358
else if (roll < 0.1)359
{360
// Drop a falling block into a random column if there is room.361
const col = Math.floor(rng() * g.width);362
const top = Math.max(-1, ...g.blocks.filter((b) => b.col === col).map((b) => b.y));363
if (top < g.height - 3)364
{365
const fb = g.createBlock(col, g.height - 1.2, Math.floor(rng() * 5), 'falling');366
fb.vy = -g.planet.fallSpeed;367
colors.set(fb.id, fb.color);368
}369
}370
371
const beforeIds = new Set(g.blocks.map((b) => b.id));372
const scoreBefore = g.score;373
g.tick(1 / 120);374
scored += g.score - scoreBefore;375
376
for (const b of g.blocks)377
{378
if (!colors.has(b.id))379
{ colors.set(b.id, b.color); }380
else if (colors.get(b.id) !== b.color)381
{382
const prev = colors.get(b.id);383
const ok = prev === GameCore.COLOR_GRAY || b.color === GameCore.COLOR_GRAY;384
assert.ok(ok, 'seed=' + seed + ' tick=' + t385
+ ' illegal colour change id=' + b.id + ' ' + prev + ' → ' + b.color386
+ '\n' + g.snapshot().picture);387
colors.set(b.id, b.color);388
}389
}390
for (const id of beforeIds)391
{392
if (!g.blockById(id) && g.score === scoreBefore)393
{394
// cullOffTop increments score. A disappearance395
// without a point is a vanish.396
assert.ok(g.score > scoreBefore,397
'seed=' + seed + ' tick=' + t + ' block ' + id + ' vanished without scoring'398
+ '\n' + g.snapshot().picture);399
}400
}401
}402
assert.equal(g.checkInvariants().length, 0, 'seed=' + seed + ' final: '403
+ g.checkInvariants().join('; '));404
}405
});406
});