'use strict'; // t1183: rocket flame jitter (random frame, flip, scale, rotation). const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const FX = require('../src/flameFx.js'); function seeded (seed) { let s = seed >>> 0; return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 4294967296); } describe('FlameFx (t1183)', () => { it('runs at double the old frame rate (0.08 s → 0.04 s per frame)', () => { assert.equal(FX.FPS, 25); assert.equal(FX.FPS, 2 * (1 / 0.08)); const st = FX.create(4, seeded(1)); st.timer = 0; assert.equal(FX.advance(st, 1, seeded(2), false), 25); st.timer = 0; assert.equal(FX.advance(st, 1, seeded(2), true), 12, 'reduced motion keeps the old rate'); }); it('picks a random frame that differs from the current one, covering all frames', () => { const rand = seeded(7); const seen = new Set(); let cur = 0; for (let i = 0; i < 400; i++) { const next = FX.pickFrame(cur, 4, rand); assert.notEqual(next, cur); assert.ok(next >= 0 && next < 4); seen.add(next); cur = next; } assert.equal(seen.size, 4); assert.equal(FX.pickFrame(0, 1, rand), 0); assert.equal(FX.pickFrame(2, 4, () => 0.999999999), 3); }); it('flips about half the time and keeps scale/size/rotation within bounds, independently', () => { const rand = seeded(42); const st = FX.create(4, rand); let flips = 0; const n = 4000; const ws = []; const hs = []; for (let i = 0; i < n; i++) { FX.roll(st, rand, false); if (st.flip) flips++; assert.ok(Math.abs(st.scale - 1) <= FX.SCALE_VAR + 1e-12); assert.ok(Math.abs(st.w - 1) <= FX.WIDTH_VAR + 1e-12); assert.ok(Math.abs(st.h - 1) <= FX.HEIGHT_VAR + 1e-12); assert.ok(Math.abs(st.rot) <= FX.ROT_VAR_DEG + 1e-12); ws.push(st.w); hs.push(st.h); } assert.ok(flips / n > 0.45 && flips / n < 0.55, 'flip share ' + flips / n); // width and height vary independently (low correlation). const mean = (a) => a.reduce((x, y) => x + y, 0) / a.length; const mw = mean(ws), mh = mean(hs); let cov = 0, vw = 0, vh = 0; for (let i = 0; i < n; i++) { cov += (ws[i] - mw) * (hs[i] - mh); vw += (ws[i] - mw) ** 2; vh += (hs[i] - mh) ** 2; } assert.ok(Math.abs(cov / Math.sqrt(vw * vh)) < 0.1); }); it('the top-centre anchor never moves, whatever the flip, scale or rotation', () => { const rand = seeded(3); const st = FX.create(4, rand); for (let i = 0; i < 500; i++) { FX.roll(st, rand, false); const pose = FX.pose(st, 123.5, 77.25, 90, 90); const top = FX.topCentreOf(pose); assert.ok(Math.abs(top.x - 123.5) < 1e-9 && Math.abs(top.y - 77.25) < 1e-9, JSON.stringify(top)); assert.equal(pose.rotation, st.rot); assert.equal(pose.mirrorX, st.flip); } // Unjittered pose = the old placement (centre half a flame below the top). const plain = { frame: 0, flip: false, scale: 1, w: 1, h: 1, rot: 0 }; assert.deepEqual(FX.pose(plain, 10, 20, 30, 40), { x: 10, y: 40, w: 30, h: 40, rotation: 0, mirrorX: false, frame: 0 }); }); it('reduced motion: random frame only, no flip/scale/rotation', () => { const rand = seeded(9); const st = FX.create(4, rand); for (let i = 0; i < 50; i++) { const before = st.frame; FX.roll(st, rand, true); assert.notEqual(st.frame, before); assert.equal(st.flip, false); assert.equal(st.scale * st.w * st.h, 1); assert.equal(st.rot, 0); } }); it('a long stall rolls once and keeps the remainder', () => { const st = FX.create(4, seeded(5)); st.timer = 0; let calls = 0; const rand = () => { calls++; return 0.3; }; assert.equal(FX.advance(st, 2.01, rand, false), 50); assert.ok(calls <= 6, 'one roll, not 50: ' + calls); assert.ok(st.timer >= 0 && st.timer < 0.04); assert.equal(FX.advance(st, 0.001, rand, false) >= 0, true); }); }); describe('FlameFx create (t1183)', () => { it('a flame created under reduced motion starts unjittered', () => { const st = FX.create(4, () => 0.9, true); assert.equal(st.flip, false); assert.equal(st.rot, 0); assert.equal(st.scale * st.w * st.h, 1); }); });