sandspiel Documentation

repository·master·Indexed 25 days ago

https://github.com/maxbittker/sandspiel

A falling sand cellular automata game built with Rust (WASM), WebGL, and JavaScript. The project includes a simulation engine via the sandtable package (v1.0.0) featuring a Universe API for managing cell states, simulation ticks, painting species, and undo/redo history.

Tokens
1.2K
Snippets
5
Records
11
Agent score
85%

What's inside sandspiel

  1. Overview of sandspiel

    master
    sandspiel is a falling sand game built using Rust (compiled to WASM), WebGL, and JavaScript. It simulates cellular automata environments where users can interact with various elements (represented as dots/powder). The project aims to support sharing, forking creations, and eventually allowing users to upload custom elements via a programmable cellular automata API.
  2. Build and run sandspiel locally

    master

    To run the sandspiel project locally, you must first build the WebAssembly (WASM) component using wasm-pack, install the JavaScript dependencies, and then start the development server. For active development, use cargo watch to automatically rebuild the WASM component whenever changes are detected in the Rust crate.

    # build the wasm once:
    cd crate && wasm-pack build && cd ..;
    npm install;
    npm run start;
    
    # then in a separate terminal:
    cargo watch -s 'wasm-pack build'
  3. Paint cells into the Universe

    master

    Use Universe::paint(x, y, size, species) to draw a circular area of a specific Species at the given coordinates.

    • x, y: The center coordinates of the brush.
    • size: The diameter of the brush.
    • species: The Species enum value to apply.

    Cells are only painted if the target location is currently Species::Empty or if the species being painted is Species::Empty.

  4. Advance the simulation with tick()

    master

    Call Universe::tick() to advance the simulation by one generation. This method performs two main phases:

    1. Wind Phase: Applies wind forces to cells based on the current winds vector.
    2. Update Phase: Iterates through cells (using a scanline approach that alternates direction to prevent directional bias) and updates their state based on their species logic.

    Note: tick() increments the internal generation counter.

  5. Update a cell's behavior using Species::update

    master

    The Species::update method is the primary entry point for applying the physics and logic associated with a specific species. It takes the current Cell and a SandApi instance to perform state changes in the simulation.

    impl Species {
        pub fn update(&self, cell: Cell, api: SandApi) {
            // ... implementation ...
        }
    }
  6. Access raw cell and wind data

    master

    To render the simulation, you can access raw pointers to the underlying data buffers:

    • cells(): Returns a pointer to the start of the Cell array.
    • winds(): Returns a pointer to the start of the Wind array (used for fluid dynamics).
    • burns(): Returns a pointer to the start of the burns array.

    Use Universe::width() and Universe::height() to determine the dimensions for iterating over these buffers.

  7. Initialize a new Universe

    master
    Use Universe::new(width, height) to create a new cellular automata simulation environment. The universe is initialized with all cells set to Species::Empty and all wind/burn vectors set to zero. The simulation uses a fixed seed for its internal RNG.
  8. Manage Undo/Redo state

    master

    The Universe maintains an undo stack of up to 50 previous cell states. Use the following methods to manage history:

    • push_undo(): Saves the current state of all cells to the stack.
    • pop_undo(): Reverts the universe to the most recent saved state.
    • flush_undos(): Clears the entire undo history.
  9. Reference the Species enum

    master

    The Species enum defines the different types of elements available in the simulation. It is represented as a u8 in memory. Use these variants to identify or set the type of a Cell.

    #[wasm_bindgen]
    #[repr(u8)]
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub enum Species {
        Empty = 0,
        Wall = 1,
        Sand = 2,
        Water = 3,
        Stone = 13,
        Ice = 9,
        Gas = 4,
        Cloner = 5,
        Mite = 15,
        Wood = 7,
        Plant = 11,
        Fungus = 18,
        Seed = 19,
        Fire = 6,
        Lava = 8,
        Acid = 12,
        Dust = 14,
        Oil = 16,
        Rocket = 17,
    }