'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, GameCore, Planet, Rng } = require('./helpers.js'); const Offline = require('../src/offline.js'); const Online = require('../src/online.js'); const MiniLayout = require('../src/miniLayout.js'); describe('Garbage landing column (t1126)', () => { it('pickGarbageColumn keeps a preferred open column', () => { const g = makeGame({ noSpawn: true, seed: 3 }); assert.equal(g.pickGarbageColumn(4), 4); assert.equal(g.ingestGarbage(4).col, 4); }); it('pickGarbageColumn skips a full preferred column and still returns an open one', () => { const g = makeGame({ noSpawn: true, seed: 3 }); for (let r = 0; r < 13; r++) { g.place(4, r, r % 5); } const col = g.pickGarbageColumn(4); assert.notEqual(col, 4); assert.ok(col >= 0 && col < 10); const block = g.ingestGarbage(4); assert.notEqual(block.col, 4, 'ingest also skips the full preferred column'); assert.ok(block.col >= 0 && block.col < 10); }); it('offline dispatch picks land_col and ingest uses the same column', () => { const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' }); const hitRng = () => 0.99; const battle = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 }); const player = makeGame({ noSpawn: true }); // Bot shoots at the player: from_id is the bot. const bot = battle.bots[0]; bot.core.outgoingShots.push({ id: 11, color: 2, col: 3, y: 13 }); battle.tick(0.01, player); const flight = battle.flightResults.find((f) => f.from_id === bot.id && !f.miss); assert.ok(flight, 'bot hit the player'); assert.equal(typeof flight.land_col, 'number'); assert.ok(flight.land_col >= 0 && flight.land_col < 10); assert.equal(battle.pending[0].landCol, flight.land_col); const before = player.blocks.length; battle.tick(Offline.FLIGHT_DURATION + 0.05, player); assert.equal(battle.pending.length, 0); const greys = player.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); assert.ok(greys.length >= 1, 'player received grey garbage'); assert.equal(greys[greys.length - 1].col, flight.land_col, 'ingested column matches the flight land_col'); assert.ok(player.blocks.length > before); }); }); describe('Around-field flight path (t1128)', () => { const field = { left: 335, top: 173, right: 335 + 550, bottom: 173 + 715 }; it('routes a left-mini shot around the field into a given column', () => { const start = { x: 250, y: 700 }; const end = { x: field.left + (3 + 0.5) / 10 * (field.right - field.left), y: field.top + 4 }; const pts = Online.waypointsAroundField(start, end, field, 72); assert.ok(pts.length >= 2); for (let i = 0; i <= 20; i++) { const u = i / 20; const p = Online.aroundFieldPoint(start, end, u, field, 72); assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false, 'u=' + u + ' is inside the field at ' + p.x + ',' + p.y); } const landed = Online.aroundFieldPoint(start, end, 1, field, 72); assert.ok(Math.abs(landed.x - end.x) < 0.5, 'ends at the landing column x'); assert.ok(Math.abs(landed.y - end.y) < 1, 'ends at the field top'); }); it('routes a player shot around the field to a right-hand mini', () => { const start = { x: field.left + (6 + 0.5) / 10 * (field.right - field.left), y: field.top + 4 }; const end = { x: field.right + 80, y: 700 }; for (let i = 0; i <= 20; i++) { const p = Online.aroundFieldPoint(start, end, i / 20, field, 72); assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false, 'player→enemy u=' + (i / 20) + ' crosses the field'); } }); it('a path already above the field stays as a simple arc above it', () => { const start = { x: 400, y: 80 }; const end = { x: 600, y: field.top + 4 }; const pts = Online.waypointsAroundField(start, end, field, 72); assert.ok(pts.length >= 2); for (let i = 0; i <= 16; i++) { const p = Online.aroundFieldPoint(start, end, i / 16, field, 72); assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false); assert.ok(p.y <= field.top + 6, 'stays at or above the top edge, y=' + p.y); } }); }); describe('Wide-layout top padding (t1129)', () => { it('leaves room above the field on a wide screen, not on narrow or mobile', () => { const wide = MiniLayout.plan(1280, 900, 2, 70, 8); assert.equal(wide.mode, 'sides'); assert.ok(wide.fieldY >= 70 + MiniLayout.ARC_TOP_PAD - 1, 'fieldY=' + wide.fieldY + ' should be at least hud+pad=' + (70 + MiniLayout.ARC_TOP_PAD)); assert.ok(wide.fieldY - 70 >= MiniLayout.ARC_TOP_PAD * 0.9); const narrow = MiniLayout.plan(720, 800, 2, 70, 8); assert.equal(narrow.mode, 'top'); // Unchanged: top band is hud + miniH, no arc pad. assert.ok(narrow.miniH > 0); assert.ok(narrow.fieldY >= 70 + narrow.miniH - 1); const mobile = MiniLayout.plan(393, 851, 1, 60, 6); assert.equal(mobile.mode, 'top'); }); }); describe('takeLandCol consistency (t1126)', () => { it('prefers the server land_col and falls back to a remembered id', () => { Online._pendingLandCol = Object.create(null); assert.equal(Online.takeLandCol({ id: 9, land_col: 6 }), 6); Online.rememberLandCol('abc', 2); assert.equal(Online.takeLandCol({ client_id: 'abc' }), 2); // Server value wins over the remembered one. assert.equal(Online.takeLandCol({ client_id: 'abc', land_col: 8 }), 8); assert.equal(Online.takeLandCol({ id: 1 }), null); }); }); describe('Smooth C1 arcs (t1134)', () => { const field = { left: 335, top: 173, right: 335 + 550, bottom: 173 + 715, width: 550, height: 715 }; function entry (col) { return Online.cellViewport(col, 13, field); } function cell (col, y) { return Online.cellViewport(col, y, field); } it('is one continuous polynomial: C^∞, gradual curvature, no straight stretches (t1138)', () => { const cases = [ { name: 'incoming left-mini → col3', start: { x: 250, y: 700 }, end: entry(3) }, { name: 'incoming far-left → col9', start: { x: 200, y: 800 }, end: entry(9) }, { name: 'outgoing mid → right mini', start: entry(6), end: { x: field.right + 80, y: 700 } }, { name: 'outgoing col0 → left mini', start: entry(0), end: { x: 200, y: 700 } }, { name: 'incoming right-mini → col1', start: { x: field.right + 90, y: 720 }, end: entry(1) }, { name: 'enemy→enemy behind (no field)', start: { x: 180, y: 750 }, end: { x: field.right + 90, y: 680 }, noField: true } ]; for (const c of cases) { const curve = Online.makeAroundCurve(c.start, c.end, c.noField ? null : field, { clearance: 110 }); assert.equal(curve.degree, 7, c.name + ' is a single degree-7 Bézier'); assert.ok(curve.pts && curve.pts.length === 8, c.name + ' has eight control points'); assert.ok(!curve.segments, c.name + ' is not a chain of segments'); assert.ok(curve.totalLen > 50, c.name + ' has a measured arc length'); // Single polynomial ⇒ C^∞: tangent and curvature vary continuously. let prevA = null; let maxDa = 0; const kappas = []; let zeroRun = 0; let maxZeroRun = 0; let maxDKappa = 0; let prevK = null; const N = 100; for (let i = 0; i <= N; i++) { const u = i / N; const d = Online.curveDeriv(curve, u); const a = Math.atan2(d.y, d.x); if (prevA != null) { let da = Math.abs(a - prevA); if (da > Math.PI) { da = 2 * Math.PI - da; } if (da > maxDa) { maxDa = da; } } prevA = a; const k = Online.curveCurvature(curve, u); kappas.push(k); // A nearly-straight vertical heading near the ends is // expected (exit up, land down). The forbidden stretch // is a mid-arc cruise like the old κ=0 apex segment. if (u > 0.12 && u < 0.72 && Math.abs(k) < 1e-5) { zeroRun++; if (zeroRun > maxZeroRun) { maxZeroRun = zeroRun; } } else { zeroRun = 0; } if (prevK != null) { const dk = Math.abs(k - prevK); if (dk > maxDKappa) { maxDKappa = dk; } } prevK = k; } assert.ok(maxDa < 0.28, c.name + ' max step angle ' + maxDa + ' looks like a kink'); // No long κ≈0 cruise (the old straight apex segment). A short // almost-flat inflection (a few samples) is fine. assert.ok(maxZeroRun <= 8, c.name + ' straight stretch of ' + maxZeroRun + ' samples (κ≈0)'); // Curvature changes gradually: no spike between neighbouring samples. const maxAbsK = Math.max(...kappas.map(Math.abs), 1e-9); assert.ok(maxDKappa / maxAbsK < 0.85, c.name + ' curvature jump ' + maxDKappa + ' vs max |κ| ' + maxAbsK); // Ends are vertical: up at the start, down at the landing. const d0 = Online.curveDeriv(curve, 0); const d1 = Online.curveDeriv(curve, 1); assert.ok(d0.y < 0, c.name + ' starts heading up, vy=' + d0.y); assert.ok(Math.abs(d0.x) < 1e-6, c.name + ' start tangent is vertical, dx=' + d0.x); assert.ok(d1.y > 0, c.name + ' ends heading down, vy=' + d1.y); assert.ok(Math.abs(d1.x) < 1e-6, c.name + ' end tangent is vertical, dx=' + d1.x); // Peak sits above both ends (a real arch, not a sag). let minY = Infinity; for (let i = 0; i <= N; i++) { const p = Online.curvePoint(curve, i / N); if (p.y < minY) { minY = p.y; } } assert.ok(minY < Math.min(c.start.y, c.end.y) - 20, c.name + ' peak ' + minY + ' is not a high arch'); if (!c.noField) { for (let i = 0; i <= 80; i++) { const p = Online.curvePoint(curve, i / 80); assert.equal(Online.pointAllowedOnArc(p, field, c.start, c.end), true, c.name + ' u=' + (i / 80) + ' at ' + p.x + ',' + p.y + ' crosses the field'); } } // Arc-length: |dP/du| is nearly constant (= totalLen). for (const u of [0, 0.25, 0.5, 0.75, 1]) { const d = Online.curveDeriv(curve, u); const speed = Math.hypot(d.x, d.y); assert.ok(Math.abs(speed - curve.totalLen) / curve.totalLen < 0.08, c.name + ' arc-length speed at u=' + u + ' is ' + speed + ' vs L=' + curve.totalLen); } } }); it('leaves a mid-field cell going up and stays in launch/land corridors', () => { const start = cell(6, 12); const end = { x: field.right + 80, y: 700 }; assert.ok(start.y > field.top, 'start is inside the field (below the top edge)'); assert.ok(start.y < field.top + field.height * 0.2, 'start is near the top row'); const d0 = Online.curveDeriv(Online.makeAroundCurve(start, end, field, { clearance: 80 }), 0); assert.ok(d0.y < 0, 'initial tangent is upward, vy=' + d0.y); for (let i = 0; i <= 32; i++) { const p = Online.aroundFieldPoint(start, end, i / 32, field, 80); assert.equal(Online.pointAllowedOnArc(p, field, start, end), true, 'u=' + (i / 32) + ' at ' + p.x + ',' + p.y + ' is not allowed'); } }); it('dives into the landing column at the spawn pose with downward fallSpeed (t1135)', () => { const start = { x: 250, y: 700 }; const end = entry(3); // Spawn pose is half a tile above the canvas top (y = 13). assert.ok(Math.abs(end.y - (field.top - 0.5 / 13 * field.height)) < 0.5); assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5); const duration = 1.2; const fallPx = (13 / 3) * (field.height / 13); const curve = Online.makeAroundCurve(start, end, field, { clearance: 110 }); const landed = Online.curvePoint(curve, 1); assert.ok(Math.abs(landed.x - end.x) < 0.5); assert.ok(Math.abs(landed.y - end.y) < 0.5); const d1 = Online.curveDeriv(curve, 1); assert.ok(d1.y > 0, 'end tangent points down into the column'); assert.ok(Math.abs(d1.x) < 1, 'end tangent is vertical, dx=' + d1.x); // Under arc-length parametrisation |dP/du| = L, so arrival // speed is (L/duration)*v1. flightEaseRates picks v1 to // match fallSpeed, clamped so a very long path does not rush. const rates = Online.flightEaseRates(curve, duration, fallPx); const pxPerSec = (curve.totalLen / duration) * rates.v1; const unclamped = fallPx * duration / curve.totalLen; if (unclamped >= 0.28 && unclamped <= 1.15) { assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.02, 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx); } else { assert.ok(pxPerSec > 0 && rates.v1 >= 0.28 && rates.v1 <= 1.15, 'clamped arrival ' + pxPerSec + ' with v1=' + rates.v1); } }); it('smoothParam has non-zero endpoint derivatives (no visual stop)', () => { const v0 = 0.85; const v1 = 0.85; const mid = Online.smoothParam(0.5, v0, v1); assert.ok(mid > 0.4 && mid < 0.6); const d0 = (Online.smoothParam(0.001, v0, v1) - Online.smoothParam(0, v0, v1)) / 0.001; const d1 = (Online.smoothParam(1, v0, v1) - Online.smoothParam(0.999, v0, v1)) / 0.001; assert.ok(d0 > 0.5, 'start rate ' + d0); assert.ok(d1 > 0.5, 'end rate ' + d1); assert.equal(Online.smoothParam(0, 1, 1), 0); assert.equal(Online.smoothParam(1, 1, 1), 1); assert.ok(Math.abs(Online.smoothParam(0.5, 1, 1) - 0.5) < 1e-9); }); it('cellViewport matches placeTile centres (y=0 at the bottom)', () => { const rect = { left: 100, top: 50, width: 390, height: 507, right: 490, bottom: 557 }; const bottom = Online.cellViewport(0, 0, rect); assert.ok(Math.abs(bottom.x - (100 + 0.5 / 10 * 390)) < 0.01); assert.ok(Math.abs(bottom.y - (50 + (1 - 0.5 / 13) * 507)) < 0.01); const spawn = Online.cellViewport(4, 13, rect); assert.ok(spawn.y < rect.top, 'spawn of a falling block is just above the canvas'); assert.ok(rect.top - spawn.y - 0.5 / 13 * 507 < 0.01); }); }); describe('Exit-only flight start (t1137)', () => { function flyingCluster (game, col, y, 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'), anchorY: y }; game.clusters[cluster.id] = cluster; const b = game.createBlock(col, y, opts.color != null ? opts.color : 0, 'flying'); b.clusterId = cluster.id; b.clusterOffset = 0; b.vy = cluster.vy; b.flame = true; return { cluster, block: b }; } function resetPreview () { Online._flights = []; Online._flightSeen = Object.create(null); Online._previewIds = Object.create(null); } it('hasLeftTheField is true only at y ≥ 13', () => { assert.equal(Online.hasLeftTheField(11.6), false); assert.equal(Online.hasLeftTheField(12), false); assert.equal(Online.hasLeftTheField(12.99), false); assert.equal(Online.hasLeftTheField(13 - 2e-9), false); assert.equal(Online.hasLeftTheField(13), true); assert.equal(Online.hasLeftTheField(13.01), true); assert.equal(Online.hasLeftTheField(14), true); assert.equal(Online.hasLeftTheField(12, 13), false); assert.equal(Online.hasLeftTheField(13, 13), true); }); it('a rocket that peaks below the exit produces no shot and no flight', () => { const g = makeGame({ noSpawn: true, strictInvariants: false }); const { block } = flyingCluster(g, 3, 11.6, { vy: 3, decel: 12, jumpHeight: 0.7, phase: 'up' }); const id = block.id; resetPreview(); let maxY = block.y; for (let i = 0; i < 120; i++) { for (const b of g.blocks) { assert.equal(Online.hasLeftTheField(b.y, g.height), false, 'block still on the field reached y=' + b.y); assert.equal(Online.previewExit({ id: b.id, y: b.y, col: b.col, vy: b.vy, color: b.color, start: { x: 0, y: 0 } }), null); } g.tick(1 / 60); if (g.blocks.length) { const y = Math.max(...g.blocks.map((b) => b.y)); if (y > maxY) { maxY = y; } } assert.equal(g.outgoingShots.length, 0, 'outgoing shot at tick ' + i + ' y=' + (g.blocks[0] && g.blocks[0].y)); } assert.ok(maxY < 13, 'peak y=' + maxY + ' should stay below the exit'); assert.ok(maxY > 11.6, 'rocket did go up a bit, peak=' + maxY); assert.ok(g.blocks.some((b) => b.id === id), 'the rocket fell back onto the field'); assert.equal(Online._flights.length, 0); assert.equal(Object.keys(Online._previewIds).length, 0); assert.equal(g.consumeOutgoingShots().length, 0); }); it('a rocket that crosses y=13 produces exactly one shot at the exit pose', () => { const g = makeGame({ noSpawn: true, strictInvariants: false }); const { block } = flyingCluster(g, 6, 12.2, { vy: 24, decel: 0, jumpHeight: 8, phase: 'up' }); const id = block.id; const col = block.col; resetPreview(); // Still on the field before the tick that carries it out: assert.equal(Online.hasLeftTheField(block.y, g.height), false); assert.equal(Online.previewExit({ id: block.id, y: block.y, col: block.col, vy: block.vy, color: block.color, start: { x: 1, y: 2 } }), null); assert.equal(Online._flights.length, 0); let shots = []; for (let i = 0; i < 30 && shots.length === 0; i++) { g.tick(1 / 60); shots = g.consumeOutgoingShots(); } assert.equal(shots.length, 1, 'exactly one outgoing shot'); assert.equal(shots[0].id, id); assert.equal(shots[0].col, col); assert.ok(Online.hasLeftTheField(shots[0].y, g.height), 'shot is recorded at the exit, y=' + shots[0].y); assert.equal(g.blocks.filter((b) => b.id === id).length, 0, 'block is culled'); assert.equal(g.consumeOutgoingShots().length, 0, 'not emitted again'); const field = { left: 100, top: 80, width: 390, height: 507, right: 490, bottom: 587 }; const start = Online.cellViewport(shots[0].col, shots[0].y, field); const at13 = Online.cellViewport(col, 13, field); assert.ok(start.y <= field.top + 0.5, 'exit pose is at or above the canvas top, start.y=' + start.y + ' top=' + field.top); assert.ok(Math.abs(start.x - at13.x) < 0.5, 'starts in the launch column'); // y ≥ 13 sits at or above the y=13 spawn/exit centre. assert.ok(start.y <= at13.y + 0.5); assert.equal(Online.hasLeftTheField(shots[0].y, g.height), true); }); it('previewExit ignores a rocket that has not left, even with an upward vy', () => { resetPreview(); const r = Online.previewExit({ id: 'phantom', y: 12.4, col: 4, vy: 10, color: 1, start: { x: 50, y: 50 } }); assert.equal(r, null); assert.equal(Online._flights.length, 0); assert.ok(!Online._previewIds.phantom); assert.ok(!Online._flightSeen.phantom); }); });