The planning docs commit to one thing above all: adding content must never
require touching the engine. This lays the foundation that makes that true.
All game state is addressable by dotted path (money, skills.negotiation,
relationships.boss.likes_you, flags.took_loan), and conditions and effects are
generic operations over those paths. The schema in the planning docs had
`requires: { skill, min }` and `effects: { money, skills, relationships }`,
both of which hardcode the state shape into the engine; the first gate wanting
a relationship threshold or a flag would have meant an engine change.
There is deliberately no "random event" category. Every turn filters the whole
event pool by stage and conditions and draws by weight, so a routine turn, a
rare interruption and the hard-times branch differ only in their data.
Also here, neither in the planning docs but both cheap and load-bearing:
- A seeded, serializable RNG. Runs replay exactly from seed plus choices, which
makes tests deterministic and playtest reports reproducible. The generator's
position is one uint32 living in the save.
- A load-time content validator. Undeclared paths, unknown character ids, ids
that cannot be path segments, events that can never fire and stages with no
fallback event are all caught on load instead of forty turns into a game.
The turn loop is a pure function, so a saved game resumes into exactly the run
it left — covered by a test that plays thirty turns, plays the same thirty with
a JSON round trip in the middle, and deep-equals the two.
72 tests, no dependencies. Reasoning recorded in docs/DECISIONS.md.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VMSFHyVPitUoosW5wyEADj
60 lines
2.3 KiB
JavaScript
60 lines
2.3 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createRng } from '../src/engine/rng.js';
|
|
|
|
test('the same seed produces the same sequence', () => {
|
|
const a = createRng('seed-one');
|
|
const b = createRng('seed-one');
|
|
const draws = Array.from({ length: 20 }, () => [a.next(), b.next()]);
|
|
for (const [x, y] of draws) assert.equal(x, y);
|
|
});
|
|
|
|
test('different seeds diverge', () => {
|
|
const a = Array.from({ length: 10 }, (_, i) => createRng('one').next());
|
|
const b = Array.from({ length: 10 }, (_, i) => createRng('two').next());
|
|
assert.notDeepEqual(a, b);
|
|
});
|
|
|
|
test('a generator resumes exactly from a saved position', () => {
|
|
const original = createRng('resume-me');
|
|
for (let i = 0; i < 7; i++) original.next();
|
|
const position = original.getPosition();
|
|
|
|
const resumed = createRng('resume-me', position);
|
|
const expected = Array.from({ length: 5 }, () => original.next());
|
|
const actual = Array.from({ length: 5 }, () => resumed.next());
|
|
assert.deepEqual(actual, expected);
|
|
});
|
|
|
|
test('a saved position survives a JSON round trip', () => {
|
|
const rng = createRng('json');
|
|
rng.next();
|
|
const revived = createRng('json', JSON.parse(JSON.stringify(rng.getPosition())));
|
|
assert.equal(revived.next(), createRng('json', rng.getPosition()).next());
|
|
});
|
|
|
|
test('draws stay in range', () => {
|
|
const rng = createRng('range');
|
|
for (let i = 0; i < 500; i++) {
|
|
const value = rng.next();
|
|
assert.ok(value >= 0 && value < 1);
|
|
assert.ok(rng.int(10) >= 0 && rng.int(10) < 10);
|
|
}
|
|
});
|
|
|
|
test('weighted picks respect weight and skip zero-weight entries', () => {
|
|
const rng = createRng('weighted');
|
|
const items = [{ id: 'common', w: 99 }, { id: 'rare', w: 1 }, { id: 'never', w: 0 }];
|
|
const counts = { common: 0, rare: 0, never: 0 };
|
|
for (let i = 0; i < 2000; i++) counts[rng.weighted(items, (i2) => i2.w).id]++;
|
|
assert.equal(counts.never, 0, 'zero weight must never be drawn');
|
|
assert.ok(counts.common > counts.rare * 10, `expected common to dominate, got ${JSON.stringify(counts)}`);
|
|
assert.ok(counts.rare > 0, 'a weight of 1 in 100 should still appear over 2000 draws');
|
|
});
|
|
|
|
test('weighted returns undefined when nothing has weight', () => {
|
|
const rng = createRng('empty');
|
|
assert.equal(rng.weighted([{ w: 0 }], (i) => i.w), undefined);
|
|
assert.equal(rng.weighted([], () => 1), undefined);
|
|
});
|