sandspiel Documentation
repository·master·Indexed 25 days ago
https://github.com/maxbittker/sandspielA 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.
What's inside sandspiel
- 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.
Build and run sandspiel locally
masterTo 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, usecargo watchto 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'Paint cells into the Universe
masterUse
Universe::paint(x, y, size, species)to draw a circular area of a specificSpeciesat the given coordinates.x,y: The center coordinates of the brush.size: The diameter of the brush.species: TheSpeciesenum value to apply.
Cells are only painted if the target location is currently
Species::Emptyor if the species being painted isSpecies::Empty.Advance the simulation with tick()
masterCall
Universe::tick()to advance the simulation by one generation. This method performs two main phases:- Wind Phase: Applies wind forces to cells based on the current
windsvector. - 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 internalgenerationcounter.- Wind Phase: Applies wind forces to cells based on the current
Update a cell's behavior using Species::update
masterThe
Species::updatemethod is the primary entry point for applying the physics and logic associated with a specific species. It takes the currentCelland aSandApiinstance to perform state changes in the simulation.impl Species { pub fn update(&self, cell: Cell, api: SandApi) { // ... implementation ... } }Access raw cell and wind data
masterTo render the simulation, you can access raw pointers to the underlying data buffers:
cells(): Returns a pointer to the start of theCellarray.winds(): Returns a pointer to the start of theWindarray (used for fluid dynamics).burns(): Returns a pointer to the start of theburnsarray.
Use
Universe::width()andUniverse::height()to determine the dimensions for iterating over these buffers.Initialize a new Universe
masterUseUniverse::new(width, height)to create a new cellular automata simulation environment. The universe is initialized with all cells set toSpecies::Emptyand all wind/burn vectors set to zero. The simulation uses a fixed seed for its internal RNG.Manage Undo/Redo state
masterThe
Universemaintains 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.
Reference: Cell struct
masterThe
Cellstruct represents a single unit in the cellular automata grid.#[repr(C)] pub struct Cell { species: Species, ra: u8, rb: u8, clock: u8, }Reference: Wind struct
masterThe
Windstruct represents fluid velocity and pressure at a specific coordinate. It is used for both general wind and 'burn' effects.#[repr(C)] pub struct Wind { dx: u8, dy: u8, pressure: u8, density: u8, }Reference the Species enum
masterThe
Speciesenum defines the different types of elements available in the simulation. It is represented as au8in memory. Use these variants to identify or set the type of aCell.#[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, }