'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, almost, GameCore, Planet, Rng } = require('./helpers.js'); function flyingHeld (game, y, colorsByCol) { const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b); const cluster = { id: game.nextClusterId++, jumpHeight: 3, vy: 0, decel: 0, phase: 'hold', anchorY: y }; game.clusters[cluster.id] = cluster; const members = []; for (const col of cols) { const b = game.createBlock(col, y, colorsByCol[col], 'flying'); b.clusterId = cluster.id; b.clusterOffset = 0; b.vy = 0; if (colorsByCol[col] === GameCore.COLOR_GRAY) { b.flame = true; } members.push(b); } game.prepareClusterBody(cluster); return { cluster, members }; } function waitUntil (g, pred, maxTicks) { for (let i = 0; i < (maxTicks || 800); i++) { g.tick(1 / 120); if (pred()) { return i; } } return -1; } function columnStack (g, col) { return g.blocks .filter((b) => b.col === col) .sort((a, b) => a.y - b.y) .map((b) => ({ id: b.id, color: b.color, y: b.y, state: b.state, clusterId: b.clusterId, offset: b.clusterOffset })); } function assertPacked (blocks, label) { const sorted = blocks.slice().sort((a, b) => a.y - b.y); for (let i = 1; i < sorted.length; i++) { assert.ok(sorted[i].y >= sorted[i - 1].y + 1 - 1e-6, (label || 'pack') + ': overlap ' + sorted[i - 1].y + ' and ' + sorted[i].y); } } describe('Shot into a flying cluster (t1111)', () => { it('pushes the hit column up by 1 and attaches the shot at the bottom, grid-aligned', () => { const g = makeGame({ noSpawn: true }); const { cluster, members } = flyingHeld(g, 6, { 1: GameCore.COLOR_GRAY, 2: GameCore.COLOR_GRAY, 3: GameCore.COLOR_GRAY }); const hit = members.find((b) => b.col === 2); const left = members.find((b) => b.col === 1); const right = members.find((b) => b.col === 3); const shot = g.place(2, 1, 4); g.startManualLaunch(shot); const n = waitUntil(g, () => shot.state === 'flying' && shot.clusterId === cluster.id); assert.ok(n >= 0, 'shot must attach, state=' + shot.state + ' cid=' + shot.clusterId + ' y=' + shot.y + ' hit=' + hit.y); assert.equal(shot.clusterId, cluster.id); assert.equal(hit.clusterId, cluster.id); assert.ok(almost(hit.y - shot.y, 1, 1e-6), 'shot sits exactly 1 under the pushed member, shot=' + shot.y + ' hit=' + hit.y); assert.ok(almost(left.y, right.y, 1e-6), 'other columns stay level'); assert.ok(almost(left.y, shot.y, 0.05), 'shot is at the old bottom (other cols), left=' + left.y + ' shot=' + shot.y); assert.ok(almost(hit.y - left.y, 1, 0.05), 'hit column rose by 1 relative to neighbours'); assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; ')); }); it('successive shots each push the column 1 and insert at the bottom, never overlapping', () => { const g = makeGame({ noSpawn: true }); const { cluster, members } = flyingHeld(g, 7, { 4: GameCore.COLOR_GRAY }); const original = members[0]; const shots = []; for (let s = 0; s < 4; s++) { const shot = g.createBlock(4, 0.2, s % 5, 'still'); g.startManualLaunch(shot); shots.push(shot); const n = waitUntil(g, () => shot.state === 'flying' && shot.clusterId === cluster.id, 600); assert.ok(n >= 0, 'shot ' + s + ' must attach, state=' + shot.state + ' y=' + shot.y + ' stack=' + JSON.stringify(columnStack(g, 4))); assert.equal(shot.clusterId, cluster.id); assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; ')); } const stack = g.clusterMembers(cluster.id) .filter((b) => b.col === 4) .sort((a, b) => a.y - b.y); assert.equal(stack.length, 5, 'original + 4 shots, got ' + stack.length); // Bottom-to-top: last shot, then earlier shots, then the original at the top. assert.equal(stack[0].id, shots[3].id, 'last shot is at the bottom'); assert.equal(stack[4].id, original.id, 'original member is at the top'); for (let i = 1; i < stack.length; i++) { assert.ok(almost(stack[i].y - stack[i - 1].y, 1, 1e-6), 'integer packing, ' + stack[i - 1].y + ' then ' + stack[i].y); } }); it('two launched blocks in the same column never overlap; the later one attaches under the earlier', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); const first = g.place(3, 0, 1); g.startManualLaunch(first); // Let the first rise a few tiles. g.step(0.25, 1 / 120); assert.equal(first.state, 'manual'); assert.ok(first.y > 1, 'first is in the air, y=' + first.y); const second = g.createBlock(3, 0.2, 2, 'still'); g.startManualLaunch(second); const n = waitUntil(g, () => second.state === 'flying' && first.state === 'flying' && first.clusterId != null && first.clusterId === second.clusterId, 800); assert.ok(n >= 0, 'they must form a cluster, first=' + first.state + '@' + first.y + ' second=' + second.state + '@' + second.y); assert.ok(almost(Math.abs(first.y - second.y), 1, 1e-6), 'packed 1 apart, first=' + first.y + ' second=' + second.y); assert.ok(second.y < first.y - 0.5, 'later shot is under the earlier'); assert.equal(g.checkInvariants().length, 0); }); }); describe('Video scenario: repeated launches under a cluster with falling blocks (t1111/t1112)', () => { it('never overlaps, vanishes, duplicates or reorders illegally', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); // 3rd column from the left is col 2. A flying grey+pink cluster, // falling greens, and several launched pinks and a yellow — the // shape of the t1111.webm clip. const { cluster } = flyingHeld(g, 8, { 2: GameCore.COLOR_GRAY }); cluster.phase = 'down'; cluster.vy = -1; cluster.decel = 0; g.syncClusterMembers(cluster); const cargo = g.createBlock(2, 9, 1, 'falling'); // pink-ish color 1 cargo.vy = -g.planet.fallSpeed; const fallingGreen = g.createBlock(2, 12, 3, 'falling'); fallingGreen.vy = -g.planet.fallSpeed * 0.5; const launched = []; // First pink already on the way. const p1 = g.place(2, 1, 1); g.startManualLaunch(p1); launched.push(p1); const seen = new Map(); const recordColors = () => { for (const b of g.blocks) { if (!seen.has(b.id)) { seen.set(b.id, b.color); } else { const prev = seen.get(b.id); if (prev !== b.color) { const ok = prev === GameCore.COLOR_GRAY || b.color === GameCore.COLOR_GRAY; assert.ok(ok, 'illegal colour change id=' + b.id + ' ' + prev + ' → ' + b.color + ' at y=' + b.y); seen.set(b.id, b.color); } } } }; recordColors(); let launchesLeft = 4; const colorsToShoot = [1, 2, 1, 2]; // pink, yellow, pink, yellow for (let i = 0; i < 900; i++) { if (launchesLeft > 0 && i > 0 && i % 40 === 0) { // Only launch if the floor cell is free. const occupant = g.blocks.find((b) => b.col === 2 && b.y < 1); if (!occupant) { const shot = g.createBlock(2, 0, colorsToShoot[4 - launchesLeft], 'still'); g.startManualLaunch(shot); launched.push(shot); launchesLeft--; seen.set(shot.id, shot.color); } } g.tick(1 / 120); recordColors(); const issues = g.checkInvariants(); assert.equal(issues.length, 0, 'tick ' + i + ': ' + issues.join('; ') + '\n' + g.snapshot().picture + '\n' + JSON.stringify(columnStack(g, 2))); } // Everything still in col 2 is packed, no overlaps, and every // launched block still exists or was scored off the top. const remaining = g.blocks.filter((b) => b.col === 2); assertPacked(remaining, 'final col 2'); for (const shot of launched) { const still = g.blockById(shot.id); const scored = g.score > 0 || !still; assert.ok(still || scored, 'shot ' + shot.id + ' vanished without scoring'); } assert.equal(g.checkInvariants().length, 0); }); }); describe('Falling must not teleport up through a gap (t1112)', () => { it('a falling block below a hanging still lands on the stack under it, not on the hanging block', () => { const g = makeGame({ noSpawn: true }); g.place(2, 0, 0); const hanging = g.place(2, 5, 1); const falling = g.createBlock(2, 2, 3, 'falling'); falling.vy = 0; g.tick(1 / 120); assert.ok(falling.y < hanging.y - 0.5, 'must not teleport up onto the hanging block, falling.y=' + falling.y + ' hanging.y=' + hanging.y + ' state=' + falling.state); assert.ok(falling.y <= 2 + 1e-6, 'must not rise, y=' + falling.y); assert.equal(g.checkInvariants().length, 0); }); it('a chain of stills sitting on a released landed cluster all fall, from rest', () => { const g = makeGame({ noSpawn: true }); const { cluster } = flyingHeld(g, 0, { 3: GameCore.COLOR_GRAY }); // Build a 3-high landed group and two stills on top. const grey = g.clusterMembers(cluster.id)[0]; grey.y = 0; grey.state = 'settling'; grey.settleTime = 0; cluster.phase = 'landed'; const mid = g.createBlock(3, 1, GameCore.COLOR_GRAY, 'settling'); mid.clusterId = cluster.id; mid.settleTime = 0; const topGrey = g.createBlock(3, 2, GameCore.COLOR_GRAY, 'settling'); topGrey.clusterId = cluster.id; topGrey.settleTime = 0; g.prepareClusterBody(cluster); const a = g.place(3, 3, 1); const b = g.place(3, 4, 2); // Recolor and release. g.randomColor = () => 4; g.step(g.settleTime + 0.05, 1 / 120); // After release the original greys (now colour 4) that have no // outside support fall; the stills that sat on them must fall too, // not hang or teleport. for (const block of [a, b, mid, topGrey]) { if (!g.blockById(block.id)) { continue; } assert.notEqual(block.state, 'still', 'block ' + block.id + ' at y=' + block.y + ' should have fallen or launched, state=' + block.state); } assert.equal(g.checkInvariants().length, 0, g.checkInvariants().join('; ')); }); }); describe('Randomized physics soak (t1112)', () => { it('thousands of ticks of random launches, drags, matches and landings keep the invariants', () => { const seeds = [1, 2, 7, 13, 42, 99, 128, 255]; const ticksPerSeed = 2500; for (const seed of seeds) { const rng = Rng.create(seed); const g = makeGame({ noSpawn: true, seed, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.2 } }); g.strictInvariants = true; // A grounded mess of colours, not a ready-made match. for (let c = 0; c < 10; c++) { const h = 1 + Math.floor(rng() * 4); for (let r = 0; r < h; r++) { g.place(c, r, Math.floor(rng() * 5)); } } const colors = new Map(); for (const b of g.blocks) { colors.set(b.id, b.color); } let scored = 0; for (let t = 0; t < ticksPerSeed; t++) { const roll = rng(); if (roll < 0.04) { // Random drag in a still column. const still = g.blocks.filter((b) => g.isDraggableBlock && g.isDraggableBlock(b)); if (still.length) { const b = still[Math.floor(rng() * still.length)]; if (g.beginDrag(b.col + 0.5, b.y + 0.5)) { const dest = b.y + (rng() < 0.5 ? 1 : -1) * (1 + Math.floor(rng() * 3)); g.moveDrag(dest); g.endDrag(); } } } else if (roll < 0.08) { // Launch a top still block. const tops = []; for (let c = 0; c < g.width; c++) { const list = g.blocks.filter((b) => b.col === c && (b.state === 'still' || b.state === 'settling')); if (!list.length) { continue; } list.sort((a, b) => b.y - a.y); tops.push(list[0]); } if (tops.length) { const b = tops[Math.floor(rng() * tops.length)]; if (g.beginDrag(b.col + 0.5, b.y + 0.5)) { g.moveDrag(b.y + 1 + 0.5); g.endDrag(); } } } else if (roll < 0.1) { // Drop a falling block into a random column if there is room. const col = Math.floor(rng() * g.width); const top = Math.max(-1, ...g.blocks.filter((b) => b.col === col).map((b) => b.y)); if (top < g.height - 3) { const fb = g.createBlock(col, g.height - 1.2, Math.floor(rng() * 5), 'falling'); fb.vy = -g.planet.fallSpeed; colors.set(fb.id, fb.color); } } const beforeIds = new Set(g.blocks.map((b) => b.id)); const scoreBefore = g.score; g.tick(1 / 120); scored += g.score - scoreBefore; for (const b of g.blocks) { if (!colors.has(b.id)) { colors.set(b.id, b.color); } else if (colors.get(b.id) !== b.color) { const prev = colors.get(b.id); const ok = prev === GameCore.COLOR_GRAY || b.color === GameCore.COLOR_GRAY; assert.ok(ok, 'seed=' + seed + ' tick=' + t + ' illegal colour change id=' + b.id + ' ' + prev + ' → ' + b.color + '\n' + g.snapshot().picture); colors.set(b.id, b.color); } } for (const id of beforeIds) { if (!g.blockById(id) && g.score === scoreBefore) { // cullOffTop increments score. A disappearance // without a point is a vanish. assert.ok(g.score > scoreBefore, 'seed=' + seed + ' tick=' + t + ' block ' + id + ' vanished without scoring' + '\n' + g.snapshot().picture); } } } assert.equal(g.checkInvariants().length, 0, 'seed=' + seed + ' final: ' + g.checkInvariants().join('; ')); } }); });