Add the engine core: path-addressed state, data-driven turns

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
This commit is contained in:
JesseMarkowitz
2026-09-09 21:43:46 -04:00
co-authored by Claude Opus 5
parent 6c7d31371e
commit cc59b81155
21 changed files with 1679 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { eligibleEvents, selectEvent, resolveOptions } from '../src/engine/select.js';
import { createState, resolveSchema } from '../src/engine/state.js';
import { createRng } from '../src/engine/rng.js';
import { setPath } from '../src/engine/paths.js';
import { testPack } from './fixtures/test-pack.js';
const schema = resolveSchema(testPack);
const fresh = () => createState(testPack, { seed: 'select', schema });
test('a conditional event is out of the pool until its condition holds', () => {
const rich = fresh();
assert.ok(!eligibleEvents(testPack, rich).eligible.some((e) => e.id === 'hard_times'));
const broke = setPath(rich, 'money', -20);
assert.ok(eligibleEvents(testPack, broke).eligible.some((e) => e.id === 'hard_times'));
});
test('exclusions say why', () => {
const { excluded } = eligibleEvents(testPack, fresh());
const hardTimes = excluded.find((x) => x.event.id === 'hard_times');
assert.equal(hardTimes.reason, 'requires');
assert.equal(hardTimes.failed[0].path, 'money');
});
test('a once-only event leaves the pool after it fires', () => {
const state = setPath(fresh(), 'seen.first_day', 1);
const { eligible, excluded } = eligibleEvents(testPack, state);
assert.ok(!eligible.some((e) => e.id === 'first_day'));
assert.equal(excluded.find((x) => x.event.id === 'first_day').reason, 'once');
});
test('a cooldown holds an event back for the declared number of turns', () => {
let state = fresh();
state = setPath(state, 'lastSeen.audit', 5);
state = setPath(state, 'turn', 9);
assert.equal(eligibleEvents(testPack, state).excluded.find((x) => x.event.id === 'audit')?.reason, 'cooldown');
state = setPath(state, 'turn', 15);
assert.ok(eligibleEvents(testPack, state).eligible.some((e) => e.id === 'audit'));
});
test('events belonging to another stage are never drawn', () => {
const elsewhere = setPath(fresh(), 'stage', 'penthouse');
const { eligible, excluded } = eligibleEvents(testPack, elsewhere);
assert.equal(eligible.length, 0);
assert.ok(excluded.every((x) => x.reason === 'stage'));
});
test('selection is a weighted draw, so the heavy event dominates', () => {
const rng = createRng('draw');
const state = setPath(fresh(), 'seen.first_day', 1); // take the opener out of the way
const counts = {};
for (let i = 0; i < 1000; i++) {
const event = selectEvent(testPack, state, rng);
counts[event.id] = (counts[event.id] ?? 0) + 1;
}
assert.ok(counts.routine > 900, `routine (weight 100) should dominate audit (weight 5): ${JSON.stringify(counts)}`);
assert.ok(counts.audit > 0, 'a low weight is still reachable');
});
test('an option below its skill gate resolves as locked, with the reason', () => {
const event = testPack.events.find((e) => e.id === 'routine');
const resolved = resolveOptions(fresh(), event);
const charm = resolved.find((r) => r.option.id === 'charm');
assert.equal(charm.available, false);
assert.equal(charm.failed[0].path, 'skills.talking');
assert.equal(charm.hidden, false, 'locked options are shown greyed out unless the content says otherwise');
assert.equal(resolved.find((r) => r.option.id === 'work').available, true);
});
test('an option unlocks once the gate is met', () => {
const skilled = setPath(fresh(), 'skills.talking', 5);
const event = testPack.events.find((e) => e.id === 'routine');
assert.equal(resolveOptions(skilled, event).find((r) => r.option.id === 'charm').available, true);
});