'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, placeRow, almost, Planet, GameCore } = require('./helpers.js'); function flyingClusterAt (game, y, colorsByCol) { // Place a grounded match, launch, then teleport the cluster to y. const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b); // Use a dummy match to create a cluster, then rebuild positions/colors. placeRow(game, 0, cols, 0); game.resolveMatches(); const clusterId = game.blocks.find((b) => b.clusterId != null).clusterId; const cluster = game.clusters[clusterId]; for (const b of game.clusterMembers(clusterId)) { game.removeBlock(b); } // removeBlock drops an empty cluster; put it back before adding new members. game.clusters[clusterId] = cluster; const members = []; for (const col of cols) { const b = game.createBlock(col, y, colorsByCol[col], 'flying'); b.clusterId = clusterId; if (colorsByCol[col] === GameCore.COLOR_GRAY) { b.color = GameCore.COLOR_GRAY; b.flame = true; } members.push(b); } cluster.phase = 'hold'; cluster.vy = 0; cluster.decel = 0; return { cluster, members }; } describe('Push-up and chain re-launch', () => { it('a manual jump hitting a flying cluster pushes that column one tile up, other columns stay', () => { const g = makeGame({ noSpawn: true }); // Flying gray row at y=6, cols 1,2,3. const { cluster, members } = flyingClusterAt(g, 6, { 1: GameCore.COLOR_GRAY, 2: GameCore.COLOR_GRAY, 3: GameCore.COLOR_GRAY }); const pushed = members.find((b) => b.col === 2); const left = members.find((b) => b.col === 1); const right = members.find((b) => b.col === 3); const manual = g.place(2, 2, 4); g.startManualLaunch(manual); // Fast-forward until the push has happened (or we time out). let pushedHappened = false; for (let i = 0; i < 400; i++) { g.tick(1 / 120); if (pushed.y >= 6.99) { pushedHappened = true; break; } } assert.ok(pushedHappened, 'expected the hit column to move up, y=' + pushed.y); assert.ok(pushed.y >= 6.99 && pushed.y < 8.5, 'hit column y=' + pushed.y); assert.ok(Math.abs(left.y - 6) < 0.3, 'left should stay, y=' + left.y); assert.ok(Math.abs(right.y - 6) < 0.3, 'right should stay, y=' + right.y); assert.equal(left.clusterId, cluster.id); assert.equal(pushed.clusterId, cluster.id); assert.equal(right.clusterId, cluster.id); }); it('a push that completes a new row of 3 turns those gray, links them, and re-launches the whole cluster the same height', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 4, riseTime: 1 } }); // Flying: col 0 and 2 have color 1 at y=6, col 1 has color 1 at y=5 (one tile below). // A gray rocket at col 4 y=6 is the original link (disjoint, so "not always touching"). const { cluster } = flyingClusterAt(g, 6, { 0: 1, 1: 1, 2: 1, 4: GameCore.COLOR_GRAY }); // Move col 1 down one tile so they are not yet a horizontal 3 at the same y. const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id); const left = g.blocks.find((b) => b.col === 0 && b.clusterId === cluster.id); const right = g.blocks.find((b) => b.col === 2 && b.clusterId === cluster.id); const gray = g.blocks.find((b) => b.col === 4 && b.clusterId === cluster.id); mid.y = 5; left.y = 6; right.y = 6; gray.y = 6; const originalHeight = cluster.jumpHeight; // Manual in col 1, will push mid from 5 to 6, forming a row of three 1s. const manual = g.place(1, 1, 3); g.startManualLaunch(manual); let relaunched = false; const midStart = mid.y; for (let i = 0; i < 500; i++) { g.tick(1 / 120); if (mid.color === GameCore.COLOR_GRAY && mid.flame && cluster.phase === 'up' && cluster.vy > 1) { relaunched = true; break; } } assert.ok(relaunched, 'expected a re-launch after the new match, mid.color=' + mid.color + ' flame=' + mid.flame + ' phase=' + cluster.phase + ' vy=' + cluster.vy + ' mid.y=' + mid.y); assert.equal(left.color, GameCore.COLOR_GRAY); assert.equal(right.color, GameCore.COLOR_GRAY); assert.equal(mid.color, GameCore.COLOR_GRAY); assert.equal(mid.clusterId, cluster.id); assert.equal(gray.clusterId, cluster.id); assert.equal(cluster.jumpHeight, originalHeight); // The whole cluster (including the distant gray) shares the new upward velocity. assert.ok(cluster.vy > 0); assert.equal(gray.state, 'flying'); }); it('if a re-launch sends every block off the top, that is fine and they become points', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 } }); const { cluster } = flyingClusterAt(g, 11, { 0: 1, 1: 1, 2: 1 }); const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id); mid.y = 10; g.blocks.find((b) => b.col === 0).y = 11; g.blocks.find((b) => b.col === 2).y = 11; const manual = g.place(1, 6, 2); g.startManualLaunch(manual); g.step(3, 1 / 120); const flying = g.blocks.filter((b) => b.clusterId === cluster.id); assert.equal(flying.length, 0, 'cluster should have left the field'); assert.ok(g.score >= 3, 'score=' + g.score); assert.equal(g.state, 'playing'); assert.ok(!g.clusters[cluster.id]); }); it('gray blocks can also disappear off the top and score points', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.4 } }); placeRow(g, 11, [0, 1, 2], 2); g.resolveMatches(); const rockets = g.blocks.filter((b) => b.col <= 2); assert.ok(rockets.length === 3 && rockets.every((b) => b.color === GameCore.COLOR_GRAY && b.state === 'flying')); g.step(2, 1 / 120); assert.equal(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY).length, 0); assert.equal(g.score, 3); assert.equal(g.blocks.length, 0); }); });