'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 fallSpeed 13/3 ≈ 4.33 → ~0.7s 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 fallSpeed 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; 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(0.8, 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'); }); });