machinations

    Machinations-style game-economy modeling and deterministic simulation engine (rendering-agnostic) for Kagura

    Download zip
    Author
    Version
    0.6.0
    License
    Apache-2.0
    Last updated
    15 days ago
    Downloads
    11

    mizchi/machinations does not have a README file

    Activation

    pub(all) enum Activation {
    Passive
    Automatic
    Interactive
    OnTrigger
    Start
    } derive(Eq)

    When a node fires.

    Activation::equal

    fn Activation::equal(Activation, Activation) -> Bool

    Activation::not_equal

    fn Activation::not_equal(x : Activation, y : Activation) -> Bool

    Diagram

    pub(all) struct Diagram {
    nodes : Array[Node]
    resources : Array[ResourceEdge]
    states : Array[StateEdge]
    }

    A complete economy diagram. Indices into nodes are the node ids; indices into resources are the resource-edge ids referenced by state edges.

    Diagram::connect

    fn Diagram::connect(self : Diagram, from : Int, to : Int, rate? : Rate, weight? : Double) -> Int

    Connect two nodes with a resource flow. Returns the resource-edge id.

    Diagram::converter

    fn Diagram::converter(self : Diagram, label : String, activation? : Activation, x? : Double, y? : Double) -> Int

    A converter (atomically turns inputs into outputs). Default: automatic.

    Diagram::delay

    fn Diagram::delay(self : Diagram, label : String, steps? : Int, activation? : Activation, x? : Double, y? : Double) -> Int

    A delay: holds incoming resources for steps steps, then releases them.

    Diagram::drain

    fn Diagram::drain(self : Diagram, label : String, flow? : Flow, activation? : Activation, x? : Double, y? : Double) -> Int

    A drain. Default: automatic, pull.

    Diagram::gate

    fn Diagram::gate(self : Diagram, label : String, flow? : Flow, activation? : Activation, gate_mode? : GateMode, x? : Double, y? : Double) -> Int

    A gate (non-storing router). Default: automatic, pull, deterministic split.

    Diagram::new

    fn Diagram::new() -> Diagram

    An empty diagram.

    Diagram::node

    fn Diagram::node(self : Diagram, kind : NodeKind, label : String, initial? : Int, capacity? : Int?, flow? : Flow, activation? : Activation, gate_mode? : GateMode, param? : Int, x? : Double, y? : Double) -> Int

    Add a node of any kind. Returns the node id (index into nodes).

    Diagram::pool

    fn Diagram::pool(self : Diagram, label : String, initial? : Int, capacity? : Int?, flow? : Flow, activation? : Activation, x? : Double, y? : Double) -> Int

    A storage pool. Default: passive, pull.

    Diagram::queue

    fn Diagram::queue(self : Diagram, label : String, rate? : Int, activation? : Activation, x? : Double, y? : Double) -> Int

    A queue: stores incoming and releases up to rate per step (FIFO).

    Diagram::register

    fn Diagram::register(self : Diagram, label : String, value? : Int, x? : Double, y? : Double) -> Int

    A register: holds a numeric value that feeds state connections.

    Diagram::source

    fn Diagram::source(self : Diagram, label : String, flow? : Flow, activation? : Activation, x? : Double, y? : Double) -> Int

    An infinite source. Default: automatic, push.

    Diagram::state_to_edge

    fn Diagram::state_to_edge(self : Diagram, from : Int, edge : Int, kind : StateKind, factor? : Double) -> Int

    Add a state connection targeting a resource edge (Modifier / RateLabel).

    Diagram::state_to_node

    fn Diagram::state_to_node(self : Diagram, from : Int, to : Int, kind : StateKind, factor? : Double) -> Int

    Add a state connection targeting a node (Trigger / Activator).

    Diagram::trader

    fn Diagram::trader(self : Diagram, label : String, activation? : Activation, x? : Double, y? : Double) -> Int

    A trader (atomic resource exchange). Same resolution as a converter.

    Flow

    pub(all) enum Flow {
    Pull
    PullAll
    Push
    } derive(Eq)

    Direction a node drives resource flow.

    Flow::equal

    fn Flow::equal(Flow, Flow) -> Bool

    Flow::not_equal

    fn Flow::not_equal(x : Flow, y : Flow) -> Bool

    GateMode

    pub(all) enum GateMode {
    Deterministic
    Probabilistic
    } derive(Eq)

    How a Gate distributes incoming resources across its outgoing edges.

    GateMode::equal

    fn GateMode::equal(GateMode, GateMode) -> Bool

    GateMode::not_equal

    fn GateMode::not_equal(x : GateMode, y : GateMode) -> Bool

    Node

    pub(all) struct Node {
    kind : NodeKind
    label : String
    activation : Activation
    flow : Flow
    capacity : Int?
    initial : Int
    gate_mode : GateMode
    param : Int
    x : Double
    y : Double
    }

    A diagram node. Position (x, y) is layout metadata; the engine ignores it.

    NodeKind

    pub(all) enum NodeKind {
    Pool
    Source
    Drain
    Gate
    Converter
    Trader
    Delay
    Queue
    Register
    } derive(Eq)

    Node archetypes from the Machinations framework.

    NodeKind::equal

    fn NodeKind::equal(NodeKind, NodeKind) -> Bool

    NodeKind::not_equal

    fn NodeKind::not_equal(x : NodeKind, y : NodeKind) -> Bool

    Rate

    pub(all) enum Rate {
    Fixed(Int)
    Dice(Int, Int)
    Interval(Int)
    All
    } derive(Eq)

    Flow-rate label on a resource connection.

    Rate::equal

    fn Rate::equal(Rate, Rate) -> Bool

    Rate::not_equal

    fn Rate::not_equal(x : Rate, y : Rate) -> Bool

    ResourceEdge

    pub(all) struct ResourceEdge {
    from : Int
    to : Int
    rate : Rate
    weight : Double
    }

    A resource connection: moves resources from from to to at rate. weight is used for gate distribution (deterministic ratio / random weight).

    SimState

    pub(all) struct SimState {
    values : Array[Int]
    step : Int
    rng : UInt64
    last_flows : Array[Int]
    pending : Array[Bool]
    buffers : Array[Array[Int]]
    }

    Mutable simulation state, advanced one step at a time by step.

    SimState::activate

    fn SimState::activate(self : SimState, node : Int) -> Unit

    Queue an interactive activation for the next step (e.g. a click on a node).

    SimState::init

    fn SimState::init(diagram : Diagram, seed? : UInt64) -> SimState

    Create the initial simulation state for a diagram, seeded for reproducibility.

    SimState::reset

    fn SimState::reset(self : SimState, diagram : Diagram) -> Unit

    Reset state back to the diagram's initial values (keeps the RNG stream).

    StateEdge

    pub(all) struct StateEdge {
    from : Int
    to_node : Int?
    to_edge : Int?
    kind : StateKind
    factor : Double
    }

    A state connection: alters a target node or resource edge from a source node's state. to_node / to_edge select the target (exactly one is set).

    StateKind

    pub(all) enum StateKind {
    Modifier
    Trigger
    Activator
    RateLabel
    } derive(Eq)

    Kind of a state (non-resource) connection.

    StateKind::equal

    fn StateKind::equal(StateKind, StateKind) -> Bool

    StateKind::not_equal

    fn StateKind::not_equal(x : StateKind, y : StateKind) -> Bool

    next_int

    fn next_int(state : SimState, n : Int) -> Int

    Uniform integer in [0, n). Returns 0 for n <= 0.

    next_u64

    fn next_u64(state : SimState) -> UInt64

    Advance the RNG and return the next 64-bit value. Mutates state.rng.

    roll_dice

    fn roll_dice(state : SimState, count : Int, sides : Int) -> Int

    Roll count dice with sides faces (1..sides) and return the sum.

    run

    fn run(diagram : Diagram, state : SimState, count : Int) -> Unit

    Run count steps.

    step

    fn step(diagram : Diagram, state : SimState) -> Unit

    Advance the simulation by one step, mutating state.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io