'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js'); function flyingGrayRow (game, y, cols) { const cluster = { id: game.nextClusterId++, jumpHeight: 3, vy: -game.planet.fallSpeed, decel: 0, phase: 'down' }; game.clusters[cluster.id] = cluster; const members = []; for (const col of cols) { const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying'); b.clusterId = cluster.id; b.vy = cluster.vy; b.flame = true; members.push(b); } return { cluster, members }; } describe('Falling blocks land on flying clusters', () => { it('a falling spawn lands on a flying cluster and flies along as cargo', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); const { cluster, members } = flyingGrayRow(g, 6, [2, 3, 4]); // Freeze ascent/descent so the falling block can meet them cleanly, // then unfreeze so they move together. cluster.phase = 'hold'; cluster.vy = 0; const falling = g.createBlock(3, 10, 1, 'falling'); falling.vy = -g.planet.fallSpeed; // Fall from 10 onto 6+1=7: distance 3, at full spawn fallSpeed // 13/3 ≈ 4.33 → ~0.7s (t1124: singles are not halved). g.step(1.0, 1 / 120); assert.equal(falling.state, 'flying', 'should have attached, state=' + falling.state + ' y=' + falling.y); assert.equal(falling.clusterId, cluster.id); assert.equal(falling.flame, false, 'cargo has no flame'); assert.equal(falling.color, 1, 'keeps its color'); assert.ok(almost(falling.y, members[1].y + 1, 0.05), 'sits on top, y=' + falling.y + ' flyer=' + members[1].y); // Now let the cluster descend: cargo must move with it, same dy. cluster.phase = 'down'; cluster.vy = 0; // will ease into clusterFallSpeed const yCargo = falling.y; const yFlyer = members[1].y; g.step(0.4, 1 / 120); assert.equal(falling.clusterId, cluster.id); assert.equal(falling.state, 'flying'); assert.ok(almost(falling.y - members[1].y, yCargo - yFlyer, 0.05), 'relative offset kept: cargo=' + falling.y + ' flyer=' + members[1].y); assert.ok(falling.y < yCargo, 'must have moved down with the cluster'); assert.ok(almost(falling.vy, members[1].vy, 1e-9), 'same vy'); }); it('a falling block does not pass through a descending or rising cluster', () => { const g = makeGame({ noSpawn: true }); const { cluster } = flyingGrayRow(g, 5, [1]); cluster.phase = 'down'; cluster.vy = -g.planet.fallSpeed; const falling = g.createBlock(1, 8, 2, 'falling'); falling.vy = -g.planet.fallSpeed; // The falling single is at full fallSpeed; the cluster floats // at the halved clusterFallSpeed (t1124), so the single catches // up. 2 s is plenty for a 3-tile gap. g.step(2.0, 1 / 120); assert.notEqual(falling.state, 'falling'); assert.ok(falling.state === 'flying' || falling.state === 'settling' || falling.state === 'still', 'must not still be falling through, state=' + falling.state + ' y=' + falling.y); // Never went below the flyer without joining (flyer may have landed). const flyer = g.blocks.find((b) => b.color === GameCore.COLOR_GRAY); if (falling.state === 'flying' && flyer) { assert.ok(falling.y >= flyer.y - 1e-6, 'cargo not below its flyer'); } }); it('a rising cluster that meets a falling block picks it up', () => { const g = makeGame({ noSpawn: true }); const { cluster, members } = flyingGrayRow(g, 3, [5]); cluster.phase = 'up'; cluster.jumpHeight = 6; cluster.vy = Planet.launchVelocity(g.planet, 6); cluster.decel = Planet.launchDeceleration(g.planet, 6); members[0].vy = cluster.vy; const falling = g.createBlock(5, 7, 3, 'falling'); falling.vy = -g.planet.fallSpeed; g.step(1.6, 1 / 120); assert.equal(falling.state, 'flying', 'picked up, state=' + falling.state + ' y=' + falling.y); assert.equal(falling.clusterId, cluster.id); assert.ok(falling.y >= members[0].y, 'sits at or above the flyer, cargo=' + falling.y + ' flyer=' + members[0].y); }); }); describe('Rigid grey landing', () => { it('keeps relative positions, so part of the group can hang above a gap', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } }); // Horizontal match on a pedestal in col 1 only: cols 0 and 2 will hang. g.place(1, 0, 4); // pedestal placeRow(g, 1, [0, 1, 2], 0); g.resolveMatches(); assert.ok(g.blocks.some((b) => b.state === 'flying')); let landed = false; for (let i = 0; i < 3000; i++) { g.tick(1 / 120); const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); if (greys.length >= 3 && greys.every((b) => b.state === 'settling')) { landed = true; break; } } assert.ok(landed, 'should have landed rigid\n' + g.snapshot().picture); const at = (col) => g.blocks.find((b) => b.col === col && b.color === GameCore.COLOR_GRAY); const a = at(0); const b = at(1); const c = at(2); assert.ok(a && b && c, 'all three greys present'); // The middle one rests on the pedestal (y=1). The sides hang at the same y. assert.ok(almost(b.y, 1, 0.05), 'middle sits on pedestal, y=' + b.y); assert.ok(almost(a.y, b.y, 0.05), 'left hangs at the same height, y=' + a.y); assert.ok(almost(c.y, b.y, 0.05), 'right hangs at the same height, y=' + c.y); assert.equal(g.blockAt(0, 0), null, 'gap under the left hang remains'); assert.equal(g.blockAt(2, 0), null, 'gap under the right hang remains'); // They stay there through the settle wait. g.step(2.9, 1 / 60); assert.ok(almost(a.y, 1, 0.05) && a.state === 'settling'); }); it('after conversion, hanging blocks fall individually and supported ones stay', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }, seed: 7 }); g.place(1, 0, 4); placeRow(g, 1, [0, 1, 2], 0); g.resolveMatches(); for (let i = 0; i < 3000; i++) { g.tick(1 / 120); const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); if (greys.length >= 3 && greys.every((b) => b.state === 'settling')) { break; } } const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY); const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY); const right = g.blocks.find((b) => b.col === 2 && b.color === GameCore.COLOR_GRAY); const leftY = left.y; g.step(3.0, 1 / 120); // Conversion happens at 3s; take a short step so the fall is visible // but not finished (from rest, Terra-like ease is not instant). g.step(0.25, 1 / 120); assert.ok(mid.color >= 0 && mid.state === 'still', 'supported middle stays, state=' + mid.state); assert.ok(almost(mid.y, 1, 0.05), 'middle stays on the pedestal, y=' + mid.y); assert.equal(mid.clusterId, null); // Hanging sides fall from rest — they must not teleport to the floor. assert.equal(left.state, 'falling', 'left should still be falling after 0.25s, state=' + left.state + ' y=' + left.y); assert.ok(left.y < leftY - 0.05, 'left has started falling smoothly, y=' + left.y + ' from ' + leftY); assert.ok(left.y > 0.25, 'must not have teleported to the floor, y=' + left.y); assert.ok(Math.abs(left.vy) <= g.planet.fallSpeed + 1e-6); assert.ok(right.state === 'falling' || (right.state === 'still' && almost(right.y, 0, 0.05)), 'right falls, state=' + right.state); // Give them time to land individually. g.step(g.planet.fallTime + 0.5, 1 / 60); assert.equal(left.state, 'still'); assert.ok(almost(left.y, 0, 0.05), 'left lands on the floor, y=' + left.y); assert.equal(right.state, 'still'); assert.ok(almost(right.y, 0, 0.05)); }); }); describe('Landing bounce planet parameter', () => { it('sets bouncing on a landed block, faster at low air resistance', () => { const slow = Planet.createPlanet({ fallTime: 5, jumpsToClear: 2, riseTime: 1.5 }); const fast = Planet.createPlanet({ fallTime: 1, jumpsToClear: 2, riseTime: 1.5 }); assert.ok(fast.bounceDuration < slow.bounceDuration, 'low air (fallTime=1) must bounce faster: ' + fast.bounceDuration + ' vs ' + slow.bounceDuration); assert.ok(almost(fast.bounceDuration, 0.12 * 1, 1e-9)); assert.ok(almost(slow.bounceDuration, 0.12 * 5, 1e-9)); assert.equal(fast.bounceSquash, 0.16); assert.ok(fast.apexEaseTime < slow.apexEaseTime); assert.ok(fast.fallAccel > slow.fallAccel, 'low air eases into fall faster'); const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3 } }); const block = g.createBlock(0, 5, 1, 'falling'); block.vy = -g.planet.fallSpeed; let landed = false; for (let i = 0; i < 2000; i++) { g.tick(1 / 120); if (block.state === 'still') { landed = true; break; } } assert.ok(landed, 'should have landed'); assert.ok(almost(block.y, 0, 0.05)); assert.equal(block.bouncing, true); assert.ok(block.bounceAge >= 0 && block.bounceAge < g.planet.bounceDuration, 'bounce just started, age=' + block.bounceAge); g.step(g.planet.bounceDuration + 0.05, 1 / 60); assert.equal(block.bouncing, false); }); it('a rigid cluster bounce only on members that actually rest on something', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } }); g.place(1, 0, 4); placeRow(g, 1, [0, 1, 2], 0); g.resolveMatches(); for (let i = 0; i < 3000; i++) { g.tick(1 / 120); const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); if (greys.length >= 3 && greys.every((b) => b.state === 'settling')) { break; } } const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY); const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY); assert.equal(mid.bouncing, true, 'the member on the pedestal bounces'); assert.equal(left.bouncing, false, 'the hanging member does not bounce'); }); }); function landUntilSettling (g, minGrey) { minGrey = minGrey || 3; let landed = false; for (let i = 0; i < 4000; i++) { g.tick(1 / 120); const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); const cluster = Object.values(g.clusters)[0]; if (greys.length >= minGrey && greys.every((b) => b.state === 'settling') && cluster && cluster.phase === 'landed') { landed = true; break; } } assert.ok(landed, 'cluster should have landed as a settling group\n' + g.snapshot().picture); } describe('Recolor must not shoot blocks off the top (t1102)', () => { it('clears leftover upward velocity: hanging cargo falls from rest after recolor, and does not fly off', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }, seed: 3 }); // Pedestal in col 1, hanging sides; cargo stacked on the left hang // and on the middle (in-cluster riders). g.place(1, 0, 4); placeRow(g, 1, [0, 1, 2], 0); g.place(0, 2, 3); g.place(0, 3, 4); g.place(1, 2, 3); g.resolveMatches(); landUntilSettling(g, 3); const cargo = g.blocks.filter((b) => b.color >= 0 && b.state === 'settling'); assert.ok(cargo.length >= 2, 'expected in-cluster cargo, got ' + cargo.length); const yBefore = {}; const members = g.blocks.filter((b) => b.state === 'settling'); for (const b of members) { // Inject a leftover upward launch velocity — the kind that // would shoot blocks off the top if it survived recolor. b.vy = 40; yBefore[b.id] = b.y; } for (const id of Object.keys(g.clusters)) { g.clusters[id].vy = 40; } // Force non-matching recolor (0,1,2 cycling) so any upward motion // is a leftover-velocity bug, not a new match. let n = 0; g.randomColor = () => (n++ % 3); g.step(g.settleTime + 0.05, 1 / 120); for (const b of g.blocks) { assert.notEqual(b.state, 'flying', 'no new launch without a match, id=' + b.id); } for (const b of members) { assert.ok(b.vy <= 1e-9, 'vy must not stay upward after recolor, vy=' + b.vy + ' id=' + b.id + ' state=' + b.state); } assert.equal(g.score, 0, 'nothing should have been culled off the top'); assert.equal(Object.keys(g.clusters).length, 0, 'landed cluster is released'); // Hanging left cargo (and the left grey) must start falling down, // never climb above their pre-recolor height. g.step(0.3, 1 / 120); const lefts = g.blocks.filter((b) => b.col === 0); assert.ok(lefts.length >= 2, 'left column still has blocks'); for (const b of lefts) { assert.ok(b.y <= yBefore[b.id] + 0.02, 'left block must not rise, y=' + b.y + ' from ' + yBefore[b.id]); assert.ok(b.vy <= 1e-9, 'falling from rest, vy=' + b.vy); assert.ok(b.y > -0.05, 'must stay in the field, y=' + b.y); } const maxY = Math.max(...g.blocks.map((b) => b.y)); assert.ok(maxY < 8, 'nothing raced toward the top, maxY=' + maxY); assert.equal(g.score, 0); }); it('a genuine match after recolor launches with normal jump physics, not leftover speed', () => { const planetOpts = { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 }; const g = makeGame({ noSpawn: true, planetOpts, seed: 1 }); // Full support under a 3-wide match so all three stay still after recolor. g.place(0, 0, 4); g.place(1, 0, 4); g.place(2, 0, 4); placeRow(g, 1, [0, 1, 2], 0); const cargoA = g.place(1, 2, 3); const cargoB = g.place(1, 3, 2); g.resolveMatches(); landUntilSettling(g, 3); for (const b of g.blocks) { b.vy = 40; } for (const id of Object.keys(g.clusters)) { g.clusters[id].vy = 40; } // Recolor every grey to color 0: the three supported blocks match. g.randomColor = () => 0; const yCargo = cargoB.y; g.step(g.settleTime + 0.02, 1 / 120); const cluster = Object.values(g.clusters).find((c) => c.phase === 'up'); assert.ok(cluster, 'a new match must launch\n' + g.snapshot().picture + '\nair=' + JSON.stringify(g.snapshot().airborne)); // Recolor-on-landing is a landing-chain match (t1120): +25%. const expectedH = Planet.jumpHeight(g.planet, 3, false) * (1 + GameCore.LANDING_CHAIN_BONUS); const expectedVy = Planet.launchVelocity(g.planet, expectedH); const expectedDecel = Planet.launchDeceleration(g.planet, expectedH); assert.ok(almost(cluster.jumpHeight, expectedH, 1e-9), 'jumpHeight=' + cluster.jumpHeight + ' expected ' + expectedH); assert.equal(cluster.landingChain, 1); assert.ok(almost(cluster.vy, expectedVy, 0.35), 'vy must be the normal launch speed ' + expectedVy + ', got ' + cluster.vy + ' (leftover 40 would be wrong)'); assert.ok(almost(cluster.decel, expectedDecel, 0.15), 'decel=' + cluster.decel); assert.ok(cluster.vy < 40, 'must not keep the leftover vy'); // Riders share the same (normal) velocity. assert.equal(cargoA.state, 'flying'); assert.equal(cargoB.state, 'flying'); assert.ok(almost(cargoA.vy, cluster.vy, 1e-9)); assert.ok(almost(cargoB.vy, cluster.vy, 1e-9)); // Apex of a normal jump from the pre-launch y, not a double or leftover boost. const startY = yCargo; let apex = Math.max(cargoA.y, cargoB.y); for (let i = 0; i < 800; i++) { g.tick(1 / 240); apex = Math.max(apex, cargoA.y, cargoB.y); } assert.ok(apex <= startY + expectedH + 0.35, 'apex must follow normal jump height, apex=' + apex + ' start=' + startY + ' H=' + expectedH); // With t1119 (+50% jump) and t1120 (landing-chain +25%), a chain-1 // horizontal-3 from y≈3 clears the 13-tile field, so the apex may // be the top of the field rather than startY+H. assert.ok(apex >= Math.min(g.height - 1, startY + expectedH - 0.85) - 0.05, 'should actually rise, apex=' + apex + ' start=' + startY + ' H=' + expectedH); }); });