'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js'); function flyingCluster (game, colsAndY, opts) { opts = opts || {}; const cluster = { id: game.nextClusterId++, jumpHeight: opts.jumpHeight != null ? opts.jumpHeight : 3, vy: opts.vy != null ? opts.vy : 4, decel: opts.decel != null ? opts.decel : 2, phase: opts.phase || (opts.vy > 0 ? 'up' : 'down') }; game.clusters[cluster.id] = cluster; const members = []; for (const [col, y, color] of colsAndY) { const b = game.createBlock(col, y, color == null ? GameCore.COLOR_GRAY : color, 'flying'); b.clusterId = cluster.id; b.vy = cluster.vy; b.flame = color == null || color === GameCore.COLOR_GRAY; members.push(b); } return { cluster, members }; } describe('Spontaneous matches always become rockets (t1104)', () => { it('a row that forms on the ground after a block falls launches like a player match', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); g.place(0, 0, 1); g.place(1, 0, 1); const falling = g.createBlock(2, 6, 1, 'falling'); falling.vy = -g.planet.fallSpeed; let launched = false; for (let i = 0; i < 800; i++) { g.tick(1 / 120); const cluster = Object.values(g.clusters).find((c) => c.phase === 'up'); if (cluster && falling.color === GameCore.COLOR_GRAY) { launched = true; break; } } assert.ok(launched, 'falling block completing a ground row must become a rocket\n' + g.snapshot().picture); assert.equal(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY).length, 3); const cluster = Object.values(g.clusters).find((c) => c.phase === 'up'); assert.ok(cluster.vy > 0); const expectedH = Planet.jumpHeight(g.planet, 3, false); assert.ok(almost(cluster.jumpHeight, expectedH, 1e-9), 'jumpHeight=' + cluster.jumpHeight); }); it('a row that appears after grey settle and recolor launches', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 }, seed: 1 }); // Non-matching supports so only the recolored greys form a line. g.place(0, 0, 4); g.place(1, 0, 3); g.place(2, 0, 2); const greys = placeRow(g, 1, [0, 1, 2], GameCore.COLOR_GRAY); for (const b of greys) { b.state = 'settling'; b.settleTime = 0; b.flame = false; } g.randomColor = () => 0; g.step(g.settleTime + 0.05, 1 / 120); const cluster = Object.values(g.clusters).find((c) => c.phase === 'up'); assert.ok(cluster, 'recolored grounded row must launch\n' + g.snapshot().picture + '\nblocks=' + JSON.stringify(g.blocks.map((b) => ({ col: b.col, y: b.y, color: b.color, state: b.state })))); assert.ok(cluster.vy > 0); assert.ok(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY && b.flame).length >= 3); }); it('a match after cargo attaches to a flying cluster greys the new line and re-launches the whole cluster', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); // Two color-1 blocks at y=6 with a platform in the gap column, plus a // distant grey so the original cluster is already flying. const { cluster, members } = flyingCluster(g, [ [0, 6, 1], [1, 5, 2], [2, 6, 1], [4, 6, GameCore.COLOR_GRAY] ], { vy: 2, decel: 0, jumpHeight: 4, phase: 'up' }); const originalHeight = cluster.jumpHeight; const originalVy = cluster.vy; const falling = g.createBlock(1, 10, 1, 'falling'); falling.vy = -g.planet.fallSpeed; let relaunched = false; for (let i = 0; i < 800; i++) { g.tick(1 / 120); if (falling.clusterId === cluster.id && falling.color === GameCore.COLOR_GRAY && cluster.phase === 'up' && cluster.vy > originalVy + 0.5) { relaunched = true; break; } } assert.ok(relaunched, 'cargo completing a flying row must re-launch, falling.color=' + falling.color + ' state=' + falling.state + ' clusterId=' + falling.clusterId + ' phase=' + cluster.phase + ' vy=' + cluster.vy + '\n' + g.snapshot().picture); assert.equal(members[0].color, GameCore.COLOR_GRAY); assert.equal(members[2].color, GameCore.COLOR_GRAY); assert.equal(falling.color, GameCore.COLOR_GRAY); assert.equal(falling.clusterId, cluster.id); assert.equal(members[3].clusterId, cluster.id, 'existing grey stays linked'); assert.equal(cluster.jumpHeight, originalHeight, 'same height as the player-push re-launch'); assert.ok(cluster.vy > 0); }); it('a match that appears when two flying clusters merge greys the line and re-launches', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); const a = flyingCluster(g, [ [0, 6, 1], [1, 6, 1], [3, 6, GameCore.COLOR_GRAY] ], { vy: 5, decel: 2, jumpHeight: 4, phase: 'up' }); const b = flyingCluster(g, [ [3, 6.4, GameCore.COLOR_GRAY], [2, 6.1, 1] ], { vy: 1, decel: 2, jumpHeight: 3, phase: 'up' }); assert.ok(a.cluster.id !== b.cluster.id); const keepId = Math.min(a.cluster.id, b.cluster.id); g.mergeFlyingClusters(); const merged = g.clusters[keepId] || Object.values(g.clusters)[0]; assert.ok(merged, 'clusters should have merged'); const color1 = g.blocks.filter((bl) => bl.col <= 2); assert.ok(color1.every((bl) => bl.color === GameCore.COLOR_GRAY), 'the completed row must have turned grey: ' + color1.map((bl) => bl.color).join(',')); assert.ok(color1.every((bl) => bl.clusterId === merged.id)); assert.equal(merged.phase, 'up'); assert.ok(merged.vy > 1, 're-launched from the current position, vy=' + merged.vy); const expectedVy = Planet.launchVelocity(g.planet, merged.jumpHeight); assert.ok(almost(merged.vy, expectedVy, 0.15), 'vy=' + merged.vy + ' expected ' + expectedVy); }); });