mwg API
    Preparing search index...

    Class SimulationRuntime<State, Command, Event, A>

    A small facade over one state + one scheduler + one RNG, for the interactive half of a turn-based simulation: the command a player just issued, dispatched through the game's own rule. advanceToInput/runScenario (simulation/Turns.ts/Scenario.ts) still cover the automatic-actor loop and fixed-command replay respectively; a game composes both against the same scheduler rather than choosing one over the other.

    Every command and event batch is journalled (pass journal: null to disable the journal entirely: dispatch then skips the append, snapshot() exposes an empty journal, and replay/undo have nothing to replay), and with history every committed state is checkpointed, all through structuredClone: Command, Event and State must be plain data. A command naming its target as a live actor object (one that owns a ReactionTable, or any other callback) cannot be copied, so carry the actor's id and resolve it in the rule. A dispatch whose command, events or checkpoint cannot be copied throws a TypeError naming the offending path and commits nothing: state, scheduler, random stream and journal stay as they were before the call. The type system cannot enforce this, since a class instance with methods is perfectly cloneable while a plain object with one function field is not.

    import { SimulationRuntime, type SimulationRuntimeRule } from '@datamoc/mwg/simulation';
    import { Scheduler, type Actor } from '@datamoc/mwg/roguelike';
    import { Generator } from '@datamoc/mwg/core';

    interface Fighter extends Actor { id: string; hp: number }
    type State = { hero: Fighter; rat: Fighter };
    type Command = 'attack';
    type Event = { type: 'damage'; targetId: string; amount: number };

    const rule: SimulationRuntimeRule<State, Command, Event, Fighter> = (state, _cmd, { random }) => {
    const amount = random.int(3) + 1;
    const rat = { ...state.rat, hp: state.rat.hp - amount };
    const next = { ...state, rat };
    return { state: next, events: [{ type: 'damage', targetId: rat.id, amount }], status: 'ready', cost: 1 };
    };

    const hero: Fighter = { id: 'hero', hp: 10 };
    const rat: Fighter = { id: 'rat', hp: 5 };
    const scheduler = new Scheduler<Fighter>();
    scheduler.add(hero);
    scheduler.add(rat);

    const runtime = new SimulationRuntime<State, Command, Event, Fighter>({
    state: { hero, rat },
    scheduler,
    random: new Generator(1),
    rule,
    actorId: (fighter) => fighter.id,
    });

    const { events } = runtime.dispatch('attack');

    Type Parameters

    Index