cellular_snapp/lib.rs
1//! Create cellular automata.
2//!
3//! Inspired by tantan's 3D cellular automata project, the cellular library aims to allow you to create and simulate 2D and 3D cellular automata.
4//! Unlike tantan's project, this isn't bound to any one framework or game engine for displaying the automata graphically.
5//! This just does the simulation bits.
6
7//--> Imports <--
8
9use std::ops::Range;
10
11/// Create flat (2D) cellular automata.
12pub mod flat;
13
14/// Create deep (3D) cellular automata.
15pub mod deep;
16
17//--> Structs <--
18
19/// Any cellular automata has four different rules that govern how it works.
20///
21/// Any given cell has a certain amount of neighbors. Whether any cell is a neighbor of said cell is up to the neighbor method, which is either Moore or Von Neumann.
22///
23/// A cell must have a certain amount of neighbors to either become alive or stay alive.
24///
25/// Finally, cells in an automaton have a certain amount of 'states'. Live cells have a state equal to whatever value you put in here, minus 1.
26///
27/// You can think of these states as the amount of time steps (or ticks) it takes for a cell to die.
28/// Given a cell which is alive but no longer has enough neighbors to survive, its state value will be decremented to 0 (dead) each tick.
29#[derive(Clone)]
30pub struct AutomataRules {
31 to_survive: Rule,
32 to_be_born: Rule,
33 cell_states: u8,
34 neighbor_method: Method
35}
36
37//--> Enums <--
38
39/// Any cellular automata has two rules that care about neighbors.
40/// One rule says how many neighbors a cell needs to be born, and another says how many neighbors a cell needs to continue living.
41/// This enum is used to tell the automata how many neighbor counts are valid for a given rule.
42#[derive(Clone)]
43pub enum Rule {
44 /// This rule only matches a single count of neighbors.
45 Single(u8),
46 /// This rule matches a consecutive set of neighbor counts.
47 /// Note that this range is inclusive on the start and exclusive on the end, so a range of 3..5 will include the values 3 and 4.
48 Range(Range<u8>),
49 /// This rule matches a non-consecutive set of neighbor counts.
50 Many(Vec<u8>)
51}
52
53/// Any cellular automata has one of two ways to determine whether any given cell is the neighbor of any other cell.
54/// This enum allows choosing which method is used by an automaton to determine neighbors.
55#[derive(Clone)]
56pub enum Method {
57 /// The Moore method counts any cell as a neighbor of a given cell if that cell is next to it, even if they don't share a face.
58 /// More mathmatically, if any two cells have coordinates that are only off by one from each-other for any given component, they are neighbors.
59 Moore,
60 /// The Von Neumann method counts any cell as a neighbor of a given cell if the cells share a face, or are touching.
61 VonNeumann
62}
63
64//--> Functions <--
65
66impl AutomataRules {
67 /// Creates a new set of cellular automaton rules.
68 pub fn new(to_survive: Rule, to_be_born: Rule, cell_states: u8, neighbor_method: Method) -> AutomataRules {
69 AutomataRules {
70 to_survive,
71 to_be_born,
72 cell_states,
73 neighbor_method
74 }
75 }
76}
77
78//--> Tests <--
79
80#[cfg(test)]
81mod tests {}