| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js'); |
| 6 | |
| 7 | describe('Outgoing shots and incoming garbage', () => |
| 8 | { |
| 9 | it('blocks that leave the top are recorded as outgoing shots and can be consumed', () => |
| 10 | { |
| 11 | const g = makeGame({ |
| 12 | noSpawn: true, |
| 13 | planetOpts: { jumpsToClear: 2, fallTime: 5, riseTime: 0.5 } |
| 14 | }); |
| 15 | // t1199: minimum gravity is 2 (9.75-tile jump), so the match sits on |
| 16 | // a non-matching support stack and one jump still clears the field. |
| 17 | for (let y = 0; y < 4; y++) |
| 18 | for (const c of [0, 1, 2]) g.place(c, y, 1 + (c + y) % 3); |
| 19 | placeRow(g, 4, [0, 1, 2], 0); |
| 20 | g.place(1, 5, 4); |
| 21 | g.resolveMatches(); |
| 22 | assert.equal(g.consumeOutgoingShots().length, 0); |
| 23 | g.step(2, 1 / 120); |
| 24 | const shots = g.consumeOutgoingShots(); |
| 25 | assert.equal(shots.length, 4, '3 rockets + 1 cargo, got ' + shots.length); |
| 26 | assert.ok(shots.every((s) => 'id' in s && 'color' in s && 'col' in s)); |
| 27 | assert.equal(g.consumeOutgoingShots().length, 0, 'consume clears the list'); |
| 28 | assert.equal(g.score, 4); |
| 29 | }); |
| 30 | |
| 31 | it('ingestGarbage drops a falling grey that settles 3s then recolors', () => |
| 32 | { |
| 33 | const g = makeGame({ noSpawn: true, seed: 4 }); |
| 34 | const block = g.ingestGarbage(3); |
| 35 | assert.ok(block); |
| 36 | assert.equal(block.col, 3); |
| 37 | assert.equal(block.color, GameCore.COLOR_GRAY); |
| 38 | assert.equal(block.state, 'falling'); |
| 39 | assert.ok(almost(block.y, g.height, 0.05)); |
| 40 | let landed = false; |
| 41 | for (let i = 0; i < 3000; i++) |
| 42 | { |
| 43 | g.tick(1 / 120); |
| 44 | if (block.state === 'settling') |
| 45 | { |
| 46 | landed = true; |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | assert.ok(landed, 'should settle after landing, state=' + block.state + ' y=' + block.y + ' color=' + block.color); |
| 51 | assert.ok(almost(block.y, 0, 0.08), 'landed on the floor, y=' + block.y); |
| 52 | assert.equal(block.color, GameCore.COLOR_GRAY); |
| 53 | assert.ok(block.settleTime < 0.05, 'timer starts at landing'); |
| 54 | g.step(2.9, 1 / 120); |
| 55 | assert.equal(block.state, 'settling', 'still settling at 2.9s, t=' + block.settleTime); |
| 56 | g.step(0.2, 1 / 120); |
| 57 | assert.equal(block.state, 'still'); |
| 58 | assert.ok(block.color >= 0 && block.color < 5, 'recolored, color=' + block.color); |
| 59 | }); |
| 60 | |
| 61 | it('ingestGarbage skips locked and full columns, and returns null when none are open', () => |
| 62 | { |
| 63 | const g = makeGame({ noSpawn: true }); |
| 64 | for (let c = 0; c < 10; c++) |
| 65 | { |
| 66 | for (let r = 0; r < 13; r++) |
| 67 | { g.place(c, r, r % 5); } |
| 68 | } |
| 69 | g.checkLocks(); |
| 70 | assert.equal(g.lockedColumnCount(), 10); |
| 71 | assert.equal(g.ingestGarbage(), null); |
| 72 | }); |
| 73 | |
| 74 | it('missChance defaults to the planet Power formula and can still be overridden', () => |
| 75 | { |
| 76 | const g = makeGame({ noSpawn: true }); |
| 77 | const terraMiss = Planet.missChance(Planet.defaultPlanet()); |
| 78 | assert.ok(Math.abs(g.missChance - terraMiss) < 1e-12); |
| 79 | assert.ok(Math.abs(terraMiss - 0.30) > 0.01, 'Terra Power miss is not the old flat 30%'); |
| 80 | assert.equal(GameCore.DEFAULT_MISS_CHANCE, 0.30); |
| 81 | const custom = new GameCore(Planet.defaultPlanet(), Math.random, 0.05); |
| 82 | assert.equal(custom.missChance, 0.05); |
| 83 | const clamped = new GameCore(Planet.defaultPlanet(), Math.random, 2); |
| 84 | assert.equal(clamped.missChance, 1); |
| 85 | const cinder = new GameCore(Planet.planetById('cinder'), Math.random); |
| 86 | assert.ok(Math.abs(cinder.missChance - 0.05) < 1e-12); |
| 87 | const aurel = new GameCore(Planet.planetById('aurel'), Math.random); |
| 88 | assert.ok(Math.abs(aurel.missChance - Planet.missChance(Planet.planetById('aurel'))) < 1e-12); |
| 89 | assert.ok(Math.abs(aurel.missChance - (0.60 - 0.55 / 9)) < 1e-12, 'Aurel at gravity 2 (t1199)'); |
| 90 | }); |
| 91 | |
| 92 | it('fieldSnapshot lists every block for opponent miniatures', () => |
| 93 | { |
| 94 | const g = makeGame({ noSpawn: true }); |
| 95 | g.place(0, 0, 1); |
| 96 | g.place(1, 2, 3); |
| 97 | const snap = g.fieldSnapshot(); |
| 98 | assert.equal(snap.score, 0); |
| 99 | assert.equal(snap.state, 'playing'); |
| 100 | assert.equal(snap.blocks.length, 2); |
| 101 | assert.equal(snap.blocks[0].col, 0); |
| 102 | assert.equal(snap.blocks[1].y, 2); |
| 103 | assert.equal(snap.locked, 0); |
| 104 | }); |
| 105 | }); |
projects-timfalken-com meteors