Files
TheLadder/src/engine/select.js
T
JesseMarkowitzandClaude Opus 5 cc59b81155 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
2026-09-09 21:43:46 -04:00

78 lines
2.7 KiB
JavaScript

// Choosing what happens this turn.
//
// There is deliberately no such thing as "a scripted turn" versus "a random
// event" in here. Every turn, the whole event pool is filtered by stage and
// conditions, then drawn from by weight. A routine turn is an event with a
// high weight and loose conditions; a rare interruption is one with a low
// weight and tight conditions; the "hard times" branch is one whose
// conditions include a money threshold. The engine branches on none of it.
import { check } from './conditions.js';
import { getPath } from './paths.js';
/** Events that could fire right now, with the reason excluded ones could not. */
export function eligibleEvents(pack, state) {
const eligible = [];
const excluded = [];
for (const event of pack.events ?? []) {
if (event.stage !== undefined && event.stage !== state.stage) {
excluded.push({ event, reason: 'stage' });
continue;
}
const seenCount = getPath(state, `seen.${event.id}`) ?? 0;
if (event.once && seenCount > 0) {
excluded.push({ event, reason: 'once' });
continue;
}
if (event.cooldown) {
const lastTurn = getPath(state, `lastSeen.${event.id}`);
if (lastTurn !== undefined && state.turn - lastTurn < event.cooldown) {
excluded.push({ event, reason: 'cooldown' });
continue;
}
}
const gate = check(state, event.requires);
if (!gate.passed) {
excluded.push({ event, reason: 'requires', failed: gate.failed });
continue;
}
eligible.push(event);
}
return { eligible, excluded };
}
/**
* Draw this turn's event. Returns undefined when nothing is eligible, which is
* a content bug rather than a game state — the validator checks that at least
* one unconditional event exists per stage so this cannot happen in practice.
*/
export function selectEvent(pack, state, rng) {
const { eligible } = eligibleEvents(pack, state);
return rng.weighted(eligible, (event) => event.weight ?? 1);
}
/**
* Every option on an event, in authored order, marked available or locked and
* carrying the conditions that locked it. The UI greys locked options out; the
* feedback log records them, because what a player *could not* choose is the
* strongest signal that the choice set is too narrow.
*/
export function resolveOptions(state, event) {
return (event.options ?? []).map((option) => {
const gate = check(state, option.requires);
return {
option,
available: gate.passed,
failed: gate.failed,
hidden: !gate.passed && option.whenLocked === 'hide',
};
});
}
/** Look up one option on an event by id. */
export function findOption(event, optionId) {
return (event.options ?? []).find((option) => option.id === optionId);
}