'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, placeRow, fillColumn, Planet, GameCore } = require('./helpers.js'); describe('Match detection and launch', () => { it('does not match two adjacent blocks of the same color', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 1); g.place(1, 0, 1); g.place(2, 0, 2); assert.equal(g.findMatchLines().length, 0); g.resolveMatches(); assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0); }); it('does not match a diagonal of three', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 1); g.place(1, 1, 1); g.place(2, 2, 1); g.place(1, 0, 2); g.place(2, 0, 2); g.place(2, 1, 2); assert.equal(g.findMatchLines().length, 0); }); it('a horizontal row of 3 turns gray, links, and launches together', () => { const g = makeGame({ noSpawn: true }); const row = placeRow(g, 0, [0, 1, 2], 0); const launched = g.resolveMatches(); assert.equal(launched.length, 1); assert.equal(launched[0].blocks.length, 3); const expected = Planet.jumpHeight(g.planet, 3, false); assert.equal(launched[0].jumpHeight, expected); for (const b of row) { assert.equal(b.color, GameCore.COLOR_GRAY); assert.equal(b.state, 'flying'); assert.equal(b.clusterId, row[0].clusterId); assert.equal(b.flame, true); assert.ok(b.vy > 0); } assert.ok(row[0].clusterId != null); assert.ok(g.clusters[row[0].clusterId]); }); it('a vertical column of 3 launches with 120% jump height', () => { const g = makeGame({ noSpawn: true }); const a = g.place(3, 0, 2); const b = g.place(3, 1, 2); const c = g.place(3, 2, 2); const launched = g.resolveMatches(); assert.equal(launched.length, 1); const expected = Planet.jumpHeight(g.planet, 3, true); assert.equal(launched[0].jumpHeight, expected); assert.ok(Math.abs(expected - Planet.jumpHeight(g.planet, 3, false) * 1.2) < 1e-12); assert.equal(a.color, GameCore.COLOR_GRAY); assert.equal(b.clusterId, a.clusterId); assert.equal(c.clusterId, a.clusterId); }); it('a horizontal row of 5 gets a 10% jump-height bonus (2 extra blocks)', () => { const g = makeGame({ noSpawn: true }); placeRow(g, 0, [0, 1, 2, 3, 4], 3); const launched = g.resolveMatches(); assert.equal(launched[0].jumpHeight, Planet.jumpHeight(g.planet, 5, false)); assert.ok(Math.abs(launched[0].jumpHeight - g.planet.baseJumpHeight * 1.10) < 1e-12); }); it('everything sitting on top of a match flies with it, keeping its color', () => { const g = makeGame({ noSpawn: true }); placeRow(g, 0, [0, 1, 2], 0); const carried = g.place(1, 1, 4); g.resolveMatches(); assert.equal(carried.state, 'flying'); assert.equal(carried.color, 4); assert.equal(carried.flame, false); assert.equal(carried.clusterId, g.blockAt(0, 0).clusterId); }); it('blocks below a vertical match stay on the ground', () => { const g = makeGame({ noSpawn: true }); const below = g.place(2, 0, 1); g.place(2, 1, 3); g.place(2, 2, 3); g.place(2, 3, 3); g.resolveMatches(); assert.equal(below.state, 'still'); assert.equal(below.color, 1); assert.equal(below.y, 0); assert.equal(g.blockAt(2, 1).state, 'flying'); }); it('two disjoint horizontal matches become two clusters', () => { const g = makeGame({ noSpawn: true }); placeRow(g, 0, [0, 1, 2], 0); placeRow(g, 0, [7, 8, 9], 1); const launched = g.resolveMatches(); assert.equal(launched.length, 2); const idA = g.blockAt(0, 0).clusterId; const idB = g.blockAt(7, 0).clusterId; assert.notEqual(idA, idB); }); it('two matches in the same columns fly as one linked cluster', () => { const g = makeGame({ noSpawn: true }); placeRow(g, 0, [0, 1, 2], 0); placeRow(g, 1, [0, 1, 2], 1); const launched = g.resolveMatches(); assert.equal(launched.length, 1); const id = g.blockAt(0, 0).clusterId; assert.equal(g.blockAt(2, 1).clusterId, id); assert.equal(g.clusterMembers(id).length, 6); }); it('tick itself finds and launches a match after a swap', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 1); g.place(0, 1, 2); g.place(1, 0, 1); g.place(2, 0, 2); g.place(2, 1, 1); // After swapping col 2, the 1 at (2,1) goes to (2,0): horizontal 1s at row 0. g.swapTo(2, 1, 0); assert.equal(g.blockAt(2, 0).color, 1); g.tick(0); g.resolveMatches(); assert.equal(g.blockAt(0, 0).state, 'flying'); assert.equal(g.blockAt(1, 0).state, 'flying'); assert.equal(g.blockAt(2, 0).state, 'flying'); }); }); describe('Launch physics', () => { it('reaches the jump height in riseTime, then eases into a constant descent', () => { const g = makeGame({ noSpawn: true }); placeRow(g, 0, [0, 1, 2], 0); const launched = g.resolveMatches(); const H = launched[0].jumpHeight; const T = g.planet.riseTime; const startY = 0; g.step(T, 1 / 240); const apex = g.blocks[0].y; assert.ok(Math.abs(apex - (startY + H)) < 0.08, 'apex y=' + apex + ' expected ' + (startY + H)); const cluster = g.clusters[g.blocks[0].clusterId]; assert.equal(cluster.phase, 'down'); // Just after the apex, speed is still below the (halved) cluster float-down. const ease = g.planet.apexEaseTime; const clusterFall = g.planet.clusterFallSpeed; assert.ok(ease > 0.1, 'apexEaseTime=' + ease); g.step(ease * 0.4, 1 / 240); const midVy = Math.abs(g.blocks[0].vy); assert.ok(midVy > 0.2, 'should have started descending, vy=' + g.blocks[0].vy); assert.ok(midVy < clusterFall - 0.15, 'should still be easing, vy=' + g.blocks[0].vy + ' clusterFall=' + clusterFall); // After the ease, descent is at constant clusterFallSpeed (t1124). g.step(ease * 0.6 + 0.05, 1 / 240); assert.ok(Math.abs(Math.abs(g.blocks[0].vy) - clusterFall) < 0.08, 'terminal vy=' + g.blocks[0].vy); assert.ok(clusterFall < g.planet.fallSpeed - 0.5, 'cluster float-down is slower than spawn fall'); const y1 = g.blocks[0].y; g.step(0.2, 1 / 120); const y2 = g.blocks[0].y; const speed = (y1 - y2) / 0.2; assert.ok(Math.abs(speed - clusterFall) < 0.15, 'fall speed ' + speed); assert.ok(y2 < y1, 'must be going down'); }); });