'use strict'; // t1192: received blocks queue and are never lost. const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame } = require('./helpers.js'); const { GameCore } = require('../src/gameCore.js'); const GRAY = GameCore.COLOR_GRAY; function lockedExcept (g, open) { for (let c = 0; c < g.width; c++) { if (open.includes(c)) continue; for (let r = 0; r < g.height; r++) g.place(c, r, (r + c) % 5, 'locked'); } } function run (g, seconds, onShot) { let shots = 0; for (let i = 0; i < Math.round(seconds * 120); i++) { g.step(1 / 120, 1 / 120); shots += g.consumeOutgoingShots().length; } return shots; } function grayCount (g, cols) { return g.blocks.filter((b) => cols.includes(b.col) && (b.color === GRAY || b.garbage)).length; } describe('Received garbage queue (t1192)', () => { it('exact scenario: 5 blocks at once, 2 open columns, all aimed at one → all 5 land', () => { for (const seed of [1, 2, 3, 4, 5, 6]) { const g = makeGame({ noSpawn: true, seed }); lockedExcept(g, [3, 7]); const before = g.blocks.length; for (let i = 0; i < 5; i++) g.ingestGarbage(3); const shots = run(g, 8); assert.equal(g.pendingGarbage(), 0, 'queue drained (seed ' + seed + ')'); assert.equal(g.blocks.length - before, 5, 'all 5 on the field (seed ' + seed + ')'); assert.equal(shots, 0, 'none culled off the top / sent back'); assert.equal(g.score, 0, 'receiver gains no points from them'); // Stacked without overlap in columns 3 and 7. for (const c of [3, 7]) { const ys = g.blocksInColumn(c).map((b) => b.y); ys.forEach((y, k) => assert.equal(y, k, 'col ' + c + ' row ' + k)); } } }); it('random aims with 2 open columns: still all 5 land', () => { const g = makeGame({ noSpawn: true, seed: 9 }); lockedExcept(g, [0, 9]); const before = g.blocks.length; for (const c of [0, 9, 0, 4, 9]) g.ingestGarbage(c); run(g, 8); assert.equal(g.blocks.length - before, 5); assert.equal(g.pendingGarbage(), 0); }); it('blocks in one column never overlap and drop only after a full cell of clearance', () => { const g = makeGame({ noSpawn: true, seed: 2 }); lockedExcept(g, [5]); for (let i = 0; i < 3; i++) g.ingestGarbage(5); assert.equal(g.pendingGarbage(), 2, 'one drops now, two wait'); let minGap = Infinity; for (let i = 0; i < 120 * 6; i++) { g.step(1 / 120, 1 / 120); const col = g.blocksInColumn(5); for (let k = 1; k < col.length; k++) minGap = Math.min(minGap, col[k].y - col[k - 1].y); } assert.ok(minGap >= 1 - 1e-6, 'min gap ' + minGap); assert.equal(g.blocksInColumn(5).length, 3); }); it('no open column: garbage waits (not dropped, not lost) and lands when room appears', () => { const g = makeGame({ noSpawn: true, seed: 3 }); lockedExcept(g, [2]); // The only open column holds a rocket that is leaving (t1184). const r = g.place(2, 0, 1); const cluster = g.launchCluster([r], 30); for (let i = 0; i < 400 && r.y < 10; i++) g.step(1 / 120, 1 / 120); assert.ok(g.departingColumns()[2]); assert.equal(g.ingestGarbage(2), null); assert.equal(g.ingestGarbage(7), null); assert.equal(g.pendingGarbage(), 2, 'both wait'); run(g, 5); assert.equal(g.state, 'playing'); assert.equal(g.pendingGarbage(), 0, 'dropped after the rocket left'); assert.equal(g.blocksInColumn(2).filter((b) => b.color === GRAY).length, 2); assert.ok(cluster); }); it('a full column (counting blocks still falling) is skipped for another open one', () => { const g = makeGame({ noSpawn: true, seed: 4 }); lockedExcept(g, [1, 6]); for (let r = 0; r < 12; r++) g.place(1, r, r % 5, 'still'); // one cell left in col 1 const before = g.blocks.length; for (let i = 0; i < 4; i++) g.ingestGarbage(1); run(g, 10); assert.equal(g.blocks.length - before, 4); assert.equal(g.blocksInColumn(1).length, 13, 'col 1 filled to the top, not beyond'); assert.equal(g.blocksInColumn(6).length, 3, 'rest went to col 6'); assert.equal(g.pendingGarbage(), 0); }); it('open wanted column wins; a free field takes all at once in distinct columns', () => { const g = makeGame({ noSpawn: true, seed: 5 }); const a = g.ingestGarbage(4); assert.equal(a.col, 4); const b = g.ingestGarbage(4); assert.ok(b && b.col !== 4, 'second picks another column instead of overlapping'); assert.equal(g.pendingGarbage(), 0); }); it('garbage is not accepted after game over (as before)', () => { const g = makeGame({ noSpawn: true }); g.state = 'gameover'; assert.equal(g.ingestGarbage(1), null); assert.equal(g.pendingGarbage(), 0); }); }); describe('Received garbage queue — offline bots (t1192)', () => { it('5 shots at once to a bot with 2 open columns: all 5 land on the bot', () => { const { Planet, Rng } = require('./helpers.js'); const Offline = require('../src/offline.js'); const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng: Rng.create(7), round: 1 }); const bot = battle.bots[0]; const core = bot.core; core.spawnInterval = 1e9; lockedExcept(core, [2, 8]); const before = core.blocks.length; for (let i = 0; i < 5; i++) { battle.pending.push({ at: battle.clock, targetId: bot.id, color: 1, fromId: 'me', landCol: 2 }); } const player = makeGame({ noSpawn: true }); for (let i = 0; i < 120 * 8; i++) { battle.clock += 1 / 120; battle._deliver(player); core.step(1 / 120, 1 / 120); core.consumeOutgoingShots(); } assert.equal(core.pendingGarbage(), 0); assert.equal(core.blocks.length - before, 5, 'all 5 on the bot field'); }); });