BlueGit

projects-timfalken-com meteors

Files

meteors / test / flight-path.test.js

flight-path.test.js

flight-path.test.js 17.9 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, GameCore, Planet, Rng } = require('./helpers.js');
6const Offline = require('../src/offline.js');
7const Online = require('../src/online.js');
8const MiniLayout = require('../src/miniLayout.js');
9
10describe('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
58describe('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
113describe('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
134describe('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
149describe('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 one continuous polynomial: C^∞, gradual curvature, no straight stretches (t1138)', () =>
164 {
165 const cases = [
166 { name: 'incoming left-mini → col3', start: { x: 250, y: 700 }, end: entry(3) },
167 { name: 'incoming far-left → col9', start: { x: 200, y: 800 }, end: entry(9) },
168 { name: 'outgoing mid → right mini', start: entry(6), end: { x: field.right + 80, y: 700 } },
169 { name: 'outgoing col0 → left mini', start: entry(0), end: { x: 200, y: 700 } },
170 { name: 'incoming right-mini → col1', start: { x: field.right + 90, y: 720 }, end: entry(1) },
171 { name: 'enemy→enemy behind (no field)', start: { x: 180, y: 750 }, end: { x: field.right + 90, y: 680 }, noField: true }
172 ];
173 for (const c of cases)
174 {
175 const curve = Online.makeAroundCurve(c.start, c.end, c.noField ? null : field, { clearance: 110 });
176 assert.equal(curve.degree, 7, c.name + ' is a single degree-7 Bézier');
177 assert.ok(curve.pts && curve.pts.length === 8, c.name + ' has eight control points');
178 assert.ok(!curve.segments, c.name + ' is not a chain of segments');
179 assert.ok(curve.totalLen > 50, c.name + ' has a measured arc length');
180
181 // Single polynomial ⇒ C^∞: tangent and curvature vary continuously.
182 let prevA = null;
183 let maxDa = 0;
184 const kappas = [];
185 let zeroRun = 0;
186 let maxZeroRun = 0;
187 let maxDKappa = 0;
188 let prevK = null;
189 const N = 100;
190 for (let i = 0; i <= N; i++)
191 {
192 const u = i / N;
193 const d = Online.curveDeriv(curve, u);
194 const a = Math.atan2(d.y, d.x);
195 if (prevA != null)
196 {
197 let da = Math.abs(a - prevA);
198 if (da > Math.PI)
199 { da = 2 * Math.PI - da; }
200 if (da > maxDa)
201 { maxDa = da; }
202 }
203 prevA = a;
204 const k = Online.curveCurvature(curve, u);
205 kappas.push(k);
206 // A nearly-straight vertical heading near the ends is
207 // expected (exit up, land down). The forbidden stretch
208 // is a mid-arc cruise like the old κ=0 apex segment.
209 if (u > 0.12 && u < 0.72 && Math.abs(k) < 1e-5)
210 {
211 zeroRun++;
212 if (zeroRun > maxZeroRun)
213 { maxZeroRun = zeroRun; }
214 }
215 else
216 { zeroRun = 0; }
217 if (prevK != null)
218 {
219 const dk = Math.abs(k - prevK);
220 if (dk > maxDKappa)
221 { maxDKappa = dk; }
222 }
223 prevK = k;
224 }
225 assert.ok(maxDa < 0.28, c.name + ' max step angle ' + maxDa + ' looks like a kink');
226 // No long κ≈0 cruise (the old straight apex segment). A short
227 // almost-flat inflection (a few samples) is fine.
228 assert.ok(maxZeroRun <= 8, c.name + ' straight stretch of ' + maxZeroRun + ' samples (κ≈0)');
229 // Curvature changes gradually: no spike between neighbouring samples.
230 const maxAbsK = Math.max(...kappas.map(Math.abs), 1e-9);
231 assert.ok(maxDKappa / maxAbsK < 0.85, c.name + ' curvature jump ' + maxDKappa + ' vs max |κ| ' + maxAbsK);
232 // Ends are vertical: up at the start, down at the landing.
233 const d0 = Online.curveDeriv(curve, 0);
234 const d1 = Online.curveDeriv(curve, 1);
235 assert.ok(d0.y < 0, c.name + ' starts heading up, vy=' + d0.y);
236 assert.ok(Math.abs(d0.x) < 1e-6, c.name + ' start tangent is vertical, dx=' + d0.x);
237 assert.ok(d1.y > 0, c.name + ' ends heading down, vy=' + d1.y);
238 assert.ok(Math.abs(d1.x) < 1e-6, c.name + ' end tangent is vertical, dx=' + d1.x);
239 // Peak sits above both ends (a real arch, not a sag).
240 let minY = Infinity;
241 for (let i = 0; i <= N; i++)
242 {
243 const p = Online.curvePoint(curve, i / N);
244 if (p.y < minY)
245 { minY = p.y; }
246 }
247 assert.ok(minY < Math.min(c.start.y, c.end.y) - 20, c.name + ' peak ' + minY + ' is not a high arch');
248 if (!c.noField)
249 {
250 for (let i = 0; i <= 80; i++)
251 {
252 const p = Online.curvePoint(curve, i / 80);
253 assert.equal(Online.pointAllowedOnArc(p, field, c.start, c.end), true,
254 c.name + ' u=' + (i / 80) + ' at ' + p.x + ',' + p.y + ' crosses the field');
255 }
256 }
257 // Arc-length: |dP/du| is nearly constant (= totalLen).
258 for (const u of [0, 0.25, 0.5, 0.75, 1])
259 {
260 const d = Online.curveDeriv(curve, u);
261 const speed = Math.hypot(d.x, d.y);
262 assert.ok(Math.abs(speed - curve.totalLen) / curve.totalLen < 0.08,
263 c.name + ' arc-length speed at u=' + u + ' is ' + speed + ' vs L=' + curve.totalLen);
264 }
265 }
266 });
267
268 it('leaves a mid-field cell going up and stays in launch/land corridors', () =>
269 {
270 const start = cell(6, 12);
271 const end = { x: field.right + 80, y: 700 };
272 assert.ok(start.y > field.top, 'start is inside the field (below the top edge)');
273 assert.ok(start.y < field.top + field.height * 0.2, 'start is near the top row');
274 const d0 = Online.curveDeriv(Online.makeAroundCurve(start, end, field, { clearance: 80 }), 0);
275 assert.ok(d0.y < 0, 'initial tangent is upward, vy=' + d0.y);
276 for (let i = 0; i <= 32; i++)
277 {
278 const p = Online.aroundFieldPoint(start, end, i / 32, field, 80);
279 assert.equal(Online.pointAllowedOnArc(p, field, start, end), true,
280 'u=' + (i / 32) + ' at ' + p.x + ',' + p.y + ' is not allowed');
281 }
282 });
283
284 it('dives into the landing column at the spawn pose with downward fallSpeed (t1135)', () =>
285 {
286 const start = { x: 250, y: 700 };
287 const end = entry(3);
288 // Spawn pose is half a tile above the canvas top (y = 13).
289 assert.ok(Math.abs(end.y - (field.top - 0.5 / 13 * field.height)) < 0.5);
290 assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5);
291 const duration = 1.2;
292 const fallPx = (13 / 3) * (field.height / 13);
293 const curve = Online.makeAroundCurve(start, end, field, { clearance: 110 });
294 const landed = Online.curvePoint(curve, 1);
295 assert.ok(Math.abs(landed.x - end.x) < 0.5);
296 assert.ok(Math.abs(landed.y - end.y) < 0.5);
297 const d1 = Online.curveDeriv(curve, 1);
298 assert.ok(d1.y > 0, 'end tangent points down into the column');
299 assert.ok(Math.abs(d1.x) < 1, 'end tangent is vertical, dx=' + d1.x);
300 // Under arc-length parametrisation |dP/du| = L, so arrival
301 // speed is (L/duration)*v1. flightEaseRates picks v1 to
302 // match fallSpeed, clamped so a very long path does not rush.
303 const rates = Online.flightEaseRates(curve, duration, fallPx);
304 const pxPerSec = (curve.totalLen / duration) * rates.v1;
305 const unclamped = fallPx * duration / curve.totalLen;
306 if (unclamped >= 0.28 && unclamped <= 1.15)
307 {
308 assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.02,
309 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx);
310 }
311 else
312 {
313 assert.ok(pxPerSec > 0 && rates.v1 >= 0.28 && rates.v1 <= 1.15,
314 'clamped arrival ' + pxPerSec + ' with v1=' + rates.v1);
315 }
316 });
317
318 it('smoothParam has non-zero endpoint derivatives (no visual stop)', () =>
319 {
320 const v0 = 0.85;
321 const v1 = 0.85;
322 const mid = Online.smoothParam(0.5, v0, v1);
323 assert.ok(mid > 0.4 && mid < 0.6);
324 const d0 = (Online.smoothParam(0.001, v0, v1) - Online.smoothParam(0, v0, v1)) / 0.001;
325 const d1 = (Online.smoothParam(1, v0, v1) - Online.smoothParam(0.999, v0, v1)) / 0.001;
326 assert.ok(d0 > 0.5, 'start rate ' + d0);
327 assert.ok(d1 > 0.5, 'end rate ' + d1);
328 assert.equal(Online.smoothParam(0, 1, 1), 0);
329 assert.equal(Online.smoothParam(1, 1, 1), 1);
330 assert.ok(Math.abs(Online.smoothParam(0.5, 1, 1) - 0.5) < 1e-9);
331 });
332
333 it('cellViewport matches placeTile centres (y=0 at the bottom)', () =>
334 {
335 const rect = { left: 100, top: 50, width: 390, height: 507, right: 490, bottom: 557 };
336 const bottom = Online.cellViewport(0, 0, rect);
337 assert.ok(Math.abs(bottom.x - (100 + 0.5 / 10 * 390)) < 0.01);
338 assert.ok(Math.abs(bottom.y - (50 + (1 - 0.5 / 13) * 507)) < 0.01);
339 const spawn = Online.cellViewport(4, 13, rect);
340 assert.ok(spawn.y < rect.top, 'spawn of a falling block is just above the canvas');
341 assert.ok(rect.top - spawn.y - 0.5 / 13 * 507 < 0.01);
342 });
343});
344
345
346describe('Exit-only flight start (t1137)', () =>
347{
348 function flyingCluster (game, col, y, opts)
349 {
350 opts = opts || {};
351 const cluster = {
352 id: game.nextClusterId++,
353 jumpHeight: opts.jumpHeight != null ? opts.jumpHeight : 3,
354 vy: opts.vy != null ? opts.vy : 4,
355 decel: opts.decel != null ? opts.decel : 2,
356 phase: opts.phase || (opts.vy > 0 ? 'up' : 'down'),
357 anchorY: y
358 };
359 game.clusters[cluster.id] = cluster;
360 const b = game.createBlock(col, y, opts.color != null ? opts.color : 0, 'flying');
361 b.clusterId = cluster.id;
362 b.clusterOffset = 0;
363 b.vy = cluster.vy;
364 b.flame = true;
365 return { cluster, block: b };
366 }
367
368 function resetPreview ()
369 {
370 Online._flights = [];
371 Online._flightSeen = Object.create(null);
372 Online._previewIds = Object.create(null);
373 }
374
375 it('hasLeftTheField is true only at y ≥ 13', () =>
376 {
377 assert.equal(Online.hasLeftTheField(11.6), false);
378 assert.equal(Online.hasLeftTheField(12), false);
379 assert.equal(Online.hasLeftTheField(12.99), false);
380 assert.equal(Online.hasLeftTheField(13 - 2e-9), false);
381 assert.equal(Online.hasLeftTheField(13), true);
382 assert.equal(Online.hasLeftTheField(13.01), true);
383 assert.equal(Online.hasLeftTheField(14), true);
384 assert.equal(Online.hasLeftTheField(12, 13), false);
385 assert.equal(Online.hasLeftTheField(13, 13), true);
386 });
387
388 it('a rocket that peaks below the exit produces no shot and no flight', () =>
389 {
390 const g = makeGame({ noSpawn: true, strictInvariants: false });
391 const { block } = flyingCluster(g, 3, 11.6, {
392 vy: 3, decel: 12, jumpHeight: 0.7, phase: 'up'
393 });
394 const id = block.id;
395 resetPreview();
396 let maxY = block.y;
397 for (let i = 0; i < 120; i++)
398 {
399 for (const b of g.blocks)
400 {
401 assert.equal(Online.hasLeftTheField(b.y, g.height), false,
402 'block still on the field reached y=' + b.y);
403 assert.equal(Online.previewExit({
404 id: b.id, y: b.y, col: b.col, vy: b.vy,
405 color: b.color, start: { x: 0, y: 0 }
406 }), null);
407 }
408 g.tick(1 / 60);
409 if (g.blocks.length)
410 {
411 const y = Math.max(...g.blocks.map((b) => b.y));
412 if (y > maxY)
413 { maxY = y; }
414 }
415 assert.equal(g.outgoingShots.length, 0,
416 'outgoing shot at tick ' + i + ' y=' + (g.blocks[0] && g.blocks[0].y));
417 }
418 assert.ok(maxY < 13, 'peak y=' + maxY + ' should stay below the exit');
419 assert.ok(maxY > 11.6, 'rocket did go up a bit, peak=' + maxY);
420 assert.ok(g.blocks.some((b) => b.id === id), 'the rocket fell back onto the field');
421 assert.equal(Online._flights.length, 0);
422 assert.equal(Object.keys(Online._previewIds).length, 0);
423 assert.equal(g.consumeOutgoingShots().length, 0);
424 });
425
426 it('a rocket that crosses y=13 produces exactly one shot at the exit pose', () =>
427 {
428 const g = makeGame({ noSpawn: true, strictInvariants: false });
429 const { block } = flyingCluster(g, 6, 12.2, {
430 vy: 24, decel: 0, jumpHeight: 8, phase: 'up'
431 });
432 const id = block.id;
433 const col = block.col;
434 resetPreview();
435 // Still on the field before the tick that carries it out:
436 assert.equal(Online.hasLeftTheField(block.y, g.height), false);
437 assert.equal(Online.previewExit({
438 id: block.id, y: block.y, col: block.col, vy: block.vy,
439 color: block.color, start: { x: 1, y: 2 }
440 }), null);
441 assert.equal(Online._flights.length, 0);
442
443 let shots = [];
444 for (let i = 0; i < 30 && shots.length === 0; i++)
445 {
446 g.tick(1 / 60);
447 shots = g.consumeOutgoingShots();
448 }
449 assert.equal(shots.length, 1, 'exactly one outgoing shot');
450 assert.equal(shots[0].id, id);
451 assert.equal(shots[0].col, col);
452 assert.ok(Online.hasLeftTheField(shots[0].y, g.height),
453 'shot is recorded at the exit, y=' + shots[0].y);
454 assert.equal(g.blocks.filter((b) => b.id === id).length, 0, 'block is culled');
455 assert.equal(g.consumeOutgoingShots().length, 0, 'not emitted again');
456
457 const field = { left: 100, top: 80, width: 390, height: 507, right: 490, bottom: 587 };
458 const start = Online.cellViewport(shots[0].col, shots[0].y, field);
459 const at13 = Online.cellViewport(col, 13, field);
460 assert.ok(start.y <= field.top + 0.5,
461 'exit pose is at or above the canvas top, start.y=' + start.y + ' top=' + field.top);
462 assert.ok(Math.abs(start.x - at13.x) < 0.5, 'starts in the launch column');
463 // y ≥ 13 sits at or above the y=13 spawn/exit centre.
464 assert.ok(start.y <= at13.y + 0.5);
465 assert.equal(Online.hasLeftTheField(shots[0].y, g.height), true);
466 });
467
468 it('previewExit ignores a rocket that has not left, even with an upward vy', () =>
469 {
470 resetPreview();
471 const r = Online.previewExit({
472 id: 'phantom', y: 12.4, col: 4, vy: 10, color: 1,
473 start: { x: 50, y: 50 }
474 });
475 assert.equal(r, null);
476 assert.equal(Online._flights.length, 0);
477 assert.ok(!Online._previewIds.phantom);
478 assert.ok(!Online._flightSeen.phantom);
479 });
480});