'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, almost, GameCore } = require('./helpers.js'); describe('Thrown blocks collide with falling blocks (t1108)', () => { it('mergeManualWithFalling averages velocity, decel and jumpHeight by block count', () => { const g = makeGame({ noSpawn: true }); const thrown = g.place(4, 4, 1); g.startManualLaunch(thrown); thrown.vy = 10; thrown.manualDecel = 6; thrown.manualJumpHeight = 9; const a = g.createBlock(4, 5, 2, 'falling'); a.vy = -4; const b = g.createBlock(4, 6, 3, 'falling'); b.vy = -2; const cluster = g.mergeManualWithFalling(thrown, [a, b]); assert.ok(cluster); assert.equal(cluster.fromManual, true); assert.ok(almost(cluster.vy, (10 + -4 + -2) / 3, 1e-12), 'vy=' + cluster.vy); assert.ok(almost(cluster.decel, 6 / 3, 1e-12), 'decel=' + cluster.decel); assert.ok(almost(cluster.jumpHeight, 9 / 3, 1e-12), 'jumpHeight=' + cluster.jumpHeight); assert.equal(thrown.state, 'flying'); assert.equal(a.state, 'flying'); assert.equal(b.state, 'flying'); assert.equal(a.clusterId, cluster.id); assert.equal(b.clusterId, cluster.id); assert.ok(almost(thrown.vy, cluster.vy, 1e-12)); assert.ok(almost(a.vy, cluster.vy, 1e-12)); }); it('a thrown block merges with a falling block and they never pass through', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); const thrown = g.place(4, 1, 1); g.startManualLaunch(thrown); assert.equal(thrown.state, 'manual'); assert.ok(thrown.vy > 0); const falling = g.createBlock(4, 8, 2, 'falling'); falling.vy = -g.planet.fallSpeed; let merged = false; for (let i = 0; i < 400; i++) { g.tick(1 / 120); if (thrown.state === 'flying' && falling.state === 'flying') { assert.equal(thrown.clusterId, falling.clusterId); const cluster = g.clusters[thrown.clusterId]; assert.ok(cluster, 'a flying cluster was created'); assert.equal(cluster.fromManual, true); assert.ok(Math.abs(Math.abs(falling.y - thrown.y) - 1) < 0.05, 'members packed one tile apart, thrown=' + thrown.y + ' falling=' + falling.y); merged = true; break; } assert.notEqual(falling.state, 'still', 'falling must not land through the thrown block, y=' + falling.y + ' thrown=' + thrown.y); } assert.ok(merged, 'they must have merged, thrown=' + thrown.state + '@' + thrown.y + ' falling=' + falling.state + '@' + falling.y); }); it('a stack of two falling blocks merges with the thrown block as one cluster of three', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); const thrown = g.place(3, 0, 0); g.startManualLaunch(thrown); const a = g.createBlock(3, 7, 1, 'falling'); const b = g.createBlock(3, 8, 2, 'falling'); a.vy = -g.planet.fallSpeed; b.vy = -g.planet.fallSpeed; let merged = false; for (let i = 0; i < 400; i++) { g.tick(1 / 120); if (thrown.state === 'flying' && a.state === 'flying' && b.state === 'flying') { assert.equal(a.clusterId, thrown.clusterId); assert.equal(b.clusterId, thrown.clusterId); const cluster = g.clusters[thrown.clusterId]; assert.equal(cluster.fromManual, true); assert.equal(g.clusterMembers(cluster.id).length, 3); merged = true; break; } } assert.ok(merged, 'all three must merge, states=' + [thrown, a, b].map((x) => x.state + '@' + x.y).join(', ')); }); it('later falling cargo on a fromManual cluster also averages and does not pass through', () => { const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } }); const thrown = g.place(2, 2, 3); g.startManualLaunch(thrown); const first = g.createBlock(2, 6, 1, 'falling'); first.vy = -g.planet.fallSpeed; for (let i = 0; i < 400 && thrown.state !== 'flying'; i++) { g.tick(1 / 120); } assert.equal(thrown.state, 'flying'); assert.equal(first.state, 'flying'); const cluster = g.clusters[thrown.clusterId]; assert.equal(cluster.fromManual, true); const roof = Math.max(thrown.y, first.y); const second = g.createBlock(2, roof + 2.5, 4, 'falling'); second.vy = -g.planet.fallSpeed; let attached = false; for (let i = 0; i < 400; i++) { const n0 = g.clusterMembers(cluster.id).length; const v0 = cluster.vy; const vFall = second.vy; g.tick(1 / 120); if (second.state === 'flying' && second.clusterId === cluster.id) { // Attach averages, then this same tick's later substeps / cluster // integrate may ease vy a little. The new vy must be between the // two inputs (or just after a small decel from that average). const expected = (v0 * n0 + vFall) / (n0 + 1); assert.ok(Math.abs(cluster.vy - expected) < 0.5 || (cluster.vy - expected) * Math.sign(cluster.decel || 1) <= 0, 'second cargo averages, vy=' + cluster.vy + ' expected ~' + expected + ' from v0=' + v0 + ' n0=' + n0 + ' vFall=' + vFall); assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05, 'second cargo did not pass through, y=' + second.y); assert.equal(g.clusterMembers(cluster.id).length, n0 + 1); attached = true; break; } assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05, 'must not pass through before attach, second=' + second.y + ' cluster min=' + Math.min(thrown.y, first.y)); } assert.ok(attached, 'second falling block must attach, state=' + second.state + ' y=' + second.y); // Direct attach average, isolated from integrate. const g2 = makeGame({ noSpawn: true }); const cl = { id: g2.nextClusterId++, jumpHeight: 6, vy: 8, decel: 4, phase: 'up', fromManual: true }; g2.clusters[cl.id] = cl; const r = g2.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying'); r.clusterId = cl.id; r.vy = 8; const c1 = g2.createBlock(1, 5, 1, 'flying'); c1.clusterId = cl.id; c1.vy = 8; const extra = g2.createBlock(1, 6.05, 2, 'falling'); extra.vy = -4; assert.ok(g2.attachToFlyingCluster(extra, c1)); assert.ok(almost(cl.vy, (8 * 2 + -4) / 3, 1e-12), 'isolated average, vy=' + cl.vy); assert.ok(almost(extra.vy, cl.vy, 1e-12)); assert.ok(almost(r.vy, cl.vy, 1e-12)); assert.ok(almost(cl.decel, 4 * 2 / 3, 1e-12)); assert.ok(almost(cl.jumpHeight, 6 * 2 / 3, 1e-12)); }); it('a fast substep that would cross still merges; they never pass through', () => { const g = makeGame({ noSpawn: true }); const thrown = g.place(5, 5, 1); g.startManualLaunch(thrown); thrown.vy = 80; thrown.manualDecel = 0; const falling = g.createBlock(5, 6.2, 2, 'falling'); falling.vy = -80; g.tick(1 / 120); assert.equal(thrown.state, 'flying', 'merged, thrown=' + thrown.state); assert.equal(falling.state, 'flying'); assert.equal(thrown.clusterId, falling.clusterId); assert.ok(Math.abs(falling.y - thrown.y) >= 1 - 0.05, 'packed without overlapping, thrown=' + thrown.y + ' falling=' + falling.y); }); it('a normal match-rocket does not average falling cargo (existing attach rule)', () => { const g = makeGame({ noSpawn: true }); const cluster = { id: g.nextClusterId++, jumpHeight: 4, vy: 6, decel: 3, phase: 'hold' }; g.clusters[cluster.id] = cluster; const rocket = g.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying'); rocket.clusterId = cluster.id; rocket.vy = 6; rocket.flame = true; const cargo = g.createBlock(1, 5.05, 3, 'falling'); cargo.vy = -g.planet.fallSpeed; assert.ok(g.attachToFlyingCluster(cargo, rocket)); assert.equal(cargo.state, 'flying'); assert.equal(cargo.clusterId, cluster.id); assert.ok(almost(cluster.vy, 6, 1e-12), 'rocket vy unchanged, vy=' + cluster.vy); assert.ok(almost(cargo.vy, 6, 1e-12)); assert.equal(cluster.fromManual, undefined); }); });