| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { makeGame, GameCore, Planet, Rng } = require('./helpers.js'); |
| 6 | const Offline = require('../src/offline.js'); |
| 7 | const Online = require('../src/online.js'); |
| 8 | const MiniLayout = require('../src/miniLayout.js'); |
| 9 | |
| 10 | describe('Garbage landing column (t1126)', () => |
| 11 | { |
| 12 | it('pickGarbageColumn keeps a preferred open column', () => |
| 13 | { |
| 14 | const g = makeGame({ noSpawn: true, seed: 3 }); |
| 15 | assert.equal(g.pickGarbageColumn(4), 4); |
| 16 | assert.equal(g.ingestGarbage(4).col, 4); |
| 17 | }); |
| 18 | |
| 19 | it('pickGarbageColumn skips a full preferred column and still returns an open one', () => |
| 20 | { |
| 21 | const g = makeGame({ noSpawn: true, seed: 3 }); |
| 22 | for (let r = 0; r < 13; r++) |
| 23 | { g.place(4, r, r % 5); } |
| 24 | const col = g.pickGarbageColumn(4); |
| 25 | assert.notEqual(col, 4); |
| 26 | assert.ok(col >= 0 && col < 10); |
| 27 | const block = g.ingestGarbage(4); |
| 28 | assert.notEqual(block.col, 4, 'ingest also skips the full preferred column'); |
| 29 | assert.ok(block.col >= 0 && block.col < 10); |
| 30 | }); |
| 31 | |
| 32 | it('offline dispatch picks land_col and ingest uses the same column', () => |
| 33 | { |
| 34 | const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' }); |
| 35 | const hitRng = () => 0.99; |
| 36 | const battle = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 }); |
| 37 | const player = makeGame({ noSpawn: true }); |
| 38 | // Bot shoots at the player: from_id is the bot. |
| 39 | const bot = battle.bots[0]; |
| 40 | bot.core.outgoingShots.push({ id: 11, color: 2, col: 3, y: 13 }); |
| 41 | battle.tick(0.01, player); |
| 42 | const flight = battle.flightResults.find((f) => f.from_id === bot.id && !f.miss); |
| 43 | assert.ok(flight, 'bot hit the player'); |
| 44 | assert.equal(typeof flight.land_col, 'number'); |
| 45 | assert.ok(flight.land_col >= 0 && flight.land_col < 10); |
| 46 | assert.equal(battle.pending[0].landCol, flight.land_col); |
| 47 | const before = player.blocks.length; |
| 48 | battle.tick(Offline.FLIGHT_DURATION + 0.05, player); |
| 49 | assert.equal(battle.pending.length, 0); |
| 50 | const greys = player.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); |
| 51 | assert.ok(greys.length >= 1, 'player received grey garbage'); |
| 52 | assert.equal(greys[greys.length - 1].col, flight.land_col, |
| 53 | 'ingested column matches the flight land_col'); |
| 54 | assert.ok(player.blocks.length > before); |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | describe('Around-field flight path (t1128)', () => |
| 59 | { |
| 60 | const field = { left: 335, top: 173, right: 335 + 550, bottom: 173 + 715 }; |
| 61 | |
| 62 | it('routes a left-mini shot around the field into a given column', () => |
| 63 | { |
| 64 | const start = { x: 250, y: 700 }; |
| 65 | const end = { |
| 66 | x: field.left + (3 + 0.5) / 10 * (field.right - field.left), |
| 67 | y: field.top + 4 |
| 68 | }; |
| 69 | const pts = Online.waypointsAroundField(start, end, field, 72); |
| 70 | assert.ok(pts.length >= 2); |
| 71 | for (let i = 0; i <= 20; i++) |
| 72 | { |
| 73 | const u = i / 20; |
| 74 | const p = Online.aroundFieldPoint(start, end, u, field, 72); |
| 75 | assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false, |
| 76 | 'u=' + u + ' is inside the field at ' + p.x + ',' + p.y); |
| 77 | } |
| 78 | const landed = Online.aroundFieldPoint(start, end, 1, field, 72); |
| 79 | assert.ok(Math.abs(landed.x - end.x) < 0.5, 'ends at the landing column x'); |
| 80 | assert.ok(Math.abs(landed.y - end.y) < 1, 'ends at the field top'); |
| 81 | }); |
| 82 | |
| 83 | it('routes a player shot around the field to a right-hand mini', () => |
| 84 | { |
| 85 | const start = { |
| 86 | x: field.left + (6 + 0.5) / 10 * (field.right - field.left), |
| 87 | y: field.top + 4 |
| 88 | }; |
| 89 | const end = { x: field.right + 80, y: 700 }; |
| 90 | for (let i = 0; i <= 20; i++) |
| 91 | { |
| 92 | const p = Online.aroundFieldPoint(start, end, i / 20, field, 72); |
| 93 | assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false, |
| 94 | 'player→enemy u=' + (i / 20) + ' crosses the field'); |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | it('a path already above the field stays as a simple arc above it', () => |
| 99 | { |
| 100 | const start = { x: 400, y: 80 }; |
| 101 | const end = { x: 600, y: field.top + 4 }; |
| 102 | const pts = Online.waypointsAroundField(start, end, field, 72); |
| 103 | assert.equal(pts.length, 2, 'no extra waypoints when both ends are above'); |
| 104 | for (let i = 0; i <= 16; i++) |
| 105 | { |
| 106 | const p = Online.aroundFieldPoint(start, end, i / 16, field, 72); |
| 107 | assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false); |
| 108 | assert.ok(p.y <= field.top + 6, 'stays at or above the top edge, y=' + p.y); |
| 109 | } |
| 110 | }); |
| 111 | }); |
| 112 | |
| 113 | describe('Wide-layout top padding (t1129)', () => |
| 114 | { |
| 115 | it('leaves room above the field on a wide screen, not on narrow or mobile', () => |
| 116 | { |
| 117 | const wide = MiniLayout.plan(1280, 900, 2, 70, 8); |
| 118 | assert.equal(wide.mode, 'sides'); |
| 119 | assert.ok(wide.fieldY >= 70 + MiniLayout.ARC_TOP_PAD - 1, |
| 120 | 'fieldY=' + wide.fieldY + ' should be at least hud+pad=' + (70 + MiniLayout.ARC_TOP_PAD)); |
| 121 | assert.ok(wide.fieldY - 70 >= MiniLayout.ARC_TOP_PAD * 0.9); |
| 122 | |
| 123 | const narrow = MiniLayout.plan(720, 800, 2, 70, 8); |
| 124 | assert.equal(narrow.mode, 'top'); |
| 125 | // Unchanged: top band is hud + miniH, no arc pad. |
| 126 | assert.ok(narrow.miniH > 0); |
| 127 | assert.ok(narrow.fieldY >= 70 + narrow.miniH - 1); |
| 128 | |
| 129 | const mobile = MiniLayout.plan(393, 851, 1, 60, 6); |
| 130 | assert.equal(mobile.mode, 'top'); |
| 131 | }); |
| 132 | }); |
| 133 | |
| 134 | describe('takeLandCol consistency (t1126)', () => |
| 135 | { |
| 136 | it('prefers the server land_col and falls back to a remembered id', () => |
| 137 | { |
| 138 | Online._pendingLandCol = Object.create(null); |
| 139 | assert.equal(Online.takeLandCol({ id: 9, land_col: 6 }), 6); |
| 140 | Online.rememberLandCol('abc', 2); |
| 141 | assert.equal(Online.takeLandCol({ client_id: 'abc' }), 2); |
| 142 | // Server value wins over the remembered one. |
| 143 | assert.equal(Online.takeLandCol({ client_id: 'abc', land_col: 8 }), 8); |
| 144 | assert.equal(Online.takeLandCol({ id: 1 }), null); |
| 145 | }); |
| 146 | }); |
projects-timfalken-com meteors