| 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.ok(pts.length >= 2); |
| 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 | }); |
| 147 | |
| 148 | |
| 149 | describe('Smooth C1 arcs (t1134)', () => |
| 150 | { |
| 151 | const field = { left: 335, top: 173, right: 335 + 550, bottom: 173 + 715, width: 550, height: 715 }; |
| 152 | |
| 153 | function entry (col) |
| 154 | { |
| 155 | return Online.cellViewport(col, 13, field); |
| 156 | } |
| 157 | |
| 158 | function cell (col, y) |
| 159 | { |
| 160 | return Online.cellViewport(col, y, field); |
| 161 | } |
| 162 | |
| 163 | it('is a C1 curve: tangents and speeds match at joints, no corners', () => |
| 164 | { |
| 165 | const start = { x: 250, y: 700 }; |
| 166 | const end = entry(3); |
| 167 | const curve = Online.makeAroundCurve(start, end, field, { clearance: 80 }); |
| 168 | assert.ok(curve.segments && curve.segments.length >= 2, 'multi-segment around-field path'); |
| 169 | const n = curve.segments.length; |
| 170 | for (let i = 1; i < n; i++) |
| 171 | { |
| 172 | const u = i / n; |
| 173 | const left = Online.curveDeriv(curve, u - 1e-6); |
| 174 | const right = Online.curveDeriv(curve, u + 1e-6); |
| 175 | const aL = Math.atan2(left.y, left.x); |
| 176 | const aR = Math.atan2(right.y, right.x); |
| 177 | let da = Math.abs(aR - aL); |
| 178 | if (da > Math.PI) |
| 179 | { da = 2 * Math.PI - da; } |
| 180 | assert.ok(da < 0.02, 'joint ' + i + ' direction jump ' + da); |
| 181 | const sL = Math.hypot(left.x, left.y); |
| 182 | const sR = Math.hypot(right.x, right.y); |
| 183 | assert.ok(Math.abs(sL - sR) / Math.max(sL, 1) < 0.05, |
| 184 | 'joint ' + i + ' speed jump ' + sL + ' vs ' + sR); |
| 185 | } |
| 186 | // Densely sampled tangent never snaps by a large angle (no corner). |
| 187 | let prev = null; |
| 188 | let maxDa = 0; |
| 189 | for (let i = 0; i <= 80; i++) |
| 190 | { |
| 191 | const d = Online.curveDeriv(curve, i / 80); |
| 192 | const a = Math.atan2(d.y, d.x); |
| 193 | if (prev != null) |
| 194 | { |
| 195 | let da = Math.abs(a - prev); |
| 196 | if (da > Math.PI) |
| 197 | { da = 2 * Math.PI - da; } |
| 198 | if (da > maxDa) |
| 199 | { maxDa = da; } |
| 200 | } |
| 201 | prev = a; |
| 202 | } |
| 203 | assert.ok(maxDa < 0.35, 'max step angle ' + maxDa + ' looks like a corner'); |
| 204 | }); |
| 205 | |
| 206 | it('leaves a mid-field cell going up and stays in launch/land corridors', () => |
| 207 | { |
| 208 | const start = cell(6, 12); |
| 209 | const end = { x: field.right + 80, y: 700 }; |
| 210 | assert.ok(start.y > field.top, 'start is inside the field (below the top edge)'); |
| 211 | assert.ok(start.y < field.top + field.height * 0.2, 'start is near the top row'); |
| 212 | const d0 = Online.curveDeriv(Online.makeAroundCurve(start, end, field, { clearance: 80 }), 0); |
| 213 | assert.ok(d0.y < 0, 'initial tangent is upward, vy=' + d0.y); |
| 214 | for (let i = 0; i <= 32; i++) |
| 215 | { |
| 216 | const p = Online.aroundFieldPoint(start, end, i / 32, field, 80); |
| 217 | assert.equal(Online.pointAllowedOnArc(p, field, start, end), true, |
| 218 | 'u=' + (i / 32) + ' at ' + p.x + ',' + p.y + ' is not allowed'); |
| 219 | } |
| 220 | }); |
| 221 | |
| 222 | it('dives into the landing column at the spawn pose with downward fallSpeed (t1135)', () => |
| 223 | { |
| 224 | const start = { x: 250, y: 700 }; |
| 225 | const end = entry(3); |
| 226 | // Spawn pose is half a tile above the canvas top (y = 13). |
| 227 | assert.ok(Math.abs(end.y - (field.top - 0.5 / 13 * field.height)) < 0.5); |
| 228 | assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5); |
| 229 | const duration = 1.2; |
| 230 | const fallPx = (13 / 3) * (field.height / 13); |
| 231 | const endSpeedPx = (fallPx * duration) / 3; |
| 232 | const curve = Online.makeAroundCurve(start, end, field, { |
| 233 | clearance: 80, endSpeedPx: endSpeedPx, duration: duration |
| 234 | }); |
| 235 | const landed = Online.curvePoint(curve, 1); |
| 236 | assert.ok(Math.abs(landed.x - end.x) < 0.5); |
| 237 | assert.ok(Math.abs(landed.y - end.y) < 0.5); |
| 238 | const d1 = Online.curveDeriv(curve, 1); |
| 239 | assert.ok(d1.y > 0, 'end tangent points down into the column'); |
| 240 | assert.ok(Math.abs(d1.x) < 20, 'end tangent is vertical, dx=' + d1.x); |
| 241 | const v1 = 0.85; |
| 242 | const pxPerSec = d1.y * v1 / duration; |
| 243 | assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.08, |
| 244 | 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx); |
| 245 | }); |
| 246 | |
| 247 | it('smoothParam has non-zero endpoint derivatives (no visual stop)', () => |
| 248 | { |
| 249 | const v0 = 0.85; |
| 250 | const v1 = 0.85; |
| 251 | const mid = Online.smoothParam(0.5, v0, v1); |
| 252 | assert.ok(mid > 0.4 && mid < 0.6); |
| 253 | const d0 = (Online.smoothParam(0.001, v0, v1) - Online.smoothParam(0, v0, v1)) / 0.001; |
| 254 | const d1 = (Online.smoothParam(1, v0, v1) - Online.smoothParam(0.999, v0, v1)) / 0.001; |
| 255 | assert.ok(d0 > 0.5, 'start rate ' + d0); |
| 256 | assert.ok(d1 > 0.5, 'end rate ' + d1); |
| 257 | assert.equal(Online.smoothParam(0, 1, 1), 0); |
| 258 | assert.equal(Online.smoothParam(1, 1, 1), 1); |
| 259 | assert.ok(Math.abs(Online.smoothParam(0.5, 1, 1) - 0.5) < 1e-9); |
| 260 | }); |
| 261 | |
| 262 | it('cellViewport matches placeTile centres (y=0 at the bottom)', () => |
| 263 | { |
| 264 | const rect = { left: 100, top: 50, width: 390, height: 507, right: 490, bottom: 557 }; |
| 265 | const bottom = Online.cellViewport(0, 0, rect); |
| 266 | assert.ok(Math.abs(bottom.x - (100 + 0.5 / 10 * 390)) < 0.01); |
| 267 | assert.ok(Math.abs(bottom.y - (50 + (1 - 0.5 / 13) * 507)) < 0.01); |
| 268 | const spawn = Online.cellViewport(4, 13, rect); |
| 269 | assert.ok(spawn.y < rect.top, 'spawn of a falling block is just above the canvas'); |
| 270 | assert.ok(rect.top - spawn.y - 0.5 / 13 * 507 < 0.01); |
| 271 | }); |
| 272 | }); |
projects-timfalken-com meteors