'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, almost, GameCore } = 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('Flying cluster merge (t1102)', () => { it('merges two overlapping flying clusters and averages vy by block count', () => { const g = makeGame({ noSpawn: true }); // 3-block cluster going up, 1-block cluster going down, same column, overlapping. const a = flyingCluster(g, [[3, 5], [3, 6], [3, 7]], { vy: 6, decel: 4, jumpHeight: 4, phase: 'up' }); const b = flyingCluster(g, [[3, 6.5]], { vy: -2, decel: 0, jumpHeight: 2, phase: 'down' }); assert.ok(a.cluster.id !== b.cluster.id); g.mergeFlyingClusters(); const ids = Object.keys(g.clusters); assert.equal(ids.length, 1, 'one cluster remains'); const merged = g.clusters[ids[0]]; assert.equal(g.clusterMembers(merged.id).length, 4); // Mass-weighted: (3*6 + 1*(-2)) / 4 = 16/4 = 4 assert.ok(almost(merged.vy, 4, 1e-12), 'vy=' + merged.vy); assert.equal(merged.phase, 'up', 'positive vy keeps phase up'); // decel (3*4 + 1*0)/4 = 3, jumpHeight (3*4 + 1*2)/4 = 3.5 assert.ok(almost(merged.decel, 3, 1e-12), 'decel=' + merged.decel); assert.ok(almost(merged.jumpHeight, 3.5, 1e-12), 'jumpHeight=' + merged.jumpHeight); for (const block of g.blocks) { assert.equal(block.clusterId, merged.id); assert.ok(almost(block.vy, 4, 1e-12)); assert.equal(block.state, 'flying'); } // Overlap in col 3 is packed: four blocks stacked flush from the lowest (y=5). const ys = g.blocks.filter((bl) => bl.col === 3).map((bl) => bl.y).sort((x, y) => x - y); assert.ok(almost(ys[0], 5, 1e-9)); for (let i = 1; i < ys.length; i++) { assert.ok(almost(ys[i], ys[i - 1] + 1, 1e-9), 'packed ys=' + ys); } }); it('merged cluster then moves as one with the averaged velocity', () => { const g = makeGame({ noSpawn: true }); flyingCluster(g, [[1, 4], [1, 5]], { vy: 8, decel: 0, phase: 'up' }); flyingCluster(g, [[1, 4.5], [2, 4.5]], { vy: 2, decel: 0, phase: 'up' }); g.mergeFlyingClusters(); const merged = Object.values(g.clusters)[0]; // (2*8 + 2*2)/4 = 5 assert.ok(almost(merged.vy, 5, 1e-12), 'vy=' + merged.vy); const before = g.blocks.map((b) => ({ id: b.id, y: b.y, col: b.col })); g.integrateClusters(0.1); // decel 0, phase up: they rise by vy*dt = 0.5 for (const prev of before) { const block = g.blockById(prev.id); assert.ok(almost(block.y - prev.y, 5 * 0.1, 1e-9), 'id=' + prev.id + ' dy=' + (block.y - prev.y)); assert.ok(almost(block.vy, 5, 1e-12)); } }); it('merged vy <= 0 sets phase to down', () => { const g = makeGame({ noSpawn: true }); flyingCluster(g, [[0, 8]], { vy: 1, phase: 'up' }); flyingCluster(g, [[0, 8]], { vy: -5, phase: 'down' }); g.mergeFlyingClusters(); const merged = Object.values(g.clusters)[0]; assert.ok(almost(merged.vy, (1 + -5) / 2, 1e-12)); assert.equal(merged.phase, 'down'); }); it('does not merge clusters that do not overlap', () => { const g = makeGame({ noSpawn: true }); flyingCluster(g, [[0, 3]], { vy: 4, phase: 'up' }); flyingCluster(g, [[5, 8]], { vy: -3, phase: 'down' }); g.mergeFlyingClusters(); assert.equal(Object.keys(g.clusters).length, 2); }); it('two approaching clusters merge mid-flight during a tick', () => { const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3, riseTime: 1.5, jumpsToClear: 2 } }); // Lower cluster going up quickly, upper one going down, same column, a gap // that closes within a short step. flyingCluster(g, [[4, 3]], { vy: 8, decel: 0, phase: 'up' }); flyingCluster(g, [[4, 6]], { vy: -8, decel: 0, phase: 'down' }); assert.equal(Object.keys(g.clusters).length, 2); g.step(0.3, 1 / 120); assert.equal(Object.keys(g.clusters).length, 1, 'they must have merged once their y-ranges overlapped'); const merged = Object.values(g.clusters)[0]; const members = g.clusterMembers(merged.id); assert.equal(members.length, 2); assert.equal(members[0].clusterId, members[1].clusterId); assert.ok(almost(members[0].vy, members[1].vy, 1e-12)); }); });