kahuna/collapse_rule.rs
1use crate::{State, Space};
2
3/// Collapse rules define the relationships between a cell's possible state
4/// based on it's neighbors.
5///
6/// While this can be anything, it is recommended that collapse rules are
7/// purely subtractive in nature, either reducing or maintaining the number
8/// possible states that a cell can take on. With addative rules, runtime
9/// can be unbounded, the algorithm may (randomly) never converge on a
10/// solution.
11pub trait CollapseRule<S: State, Sp: 'static + Space<S>> {
12 /// Neighbor directions are specified as a list of coordinate deltas.
13 fn neighbor_offsets(&self) -> Box<[Sp::CoordinateDelta]>;
14 /// The collapse rule, which modifies the possible states of 'cell' based
15 /// on the states of neighboring cells.
16 ///
17 /// * `cell` - The cell state to modify
18 /// * `neighbors` - The states of neighbors in the order specified by
19 /// `NEIGHBOR_DIRECTIONS`. `Some(<state>)` if the cell exists, and `None`
20 /// otherwise.
21 fn collapse(&self, cell: &mut S, neighbors: &[Option<S>]);
22 /// The observe rule, which forces a cell into a zero-entropy state.
23 ///
24 /// * `cell` - The cell to observe
25 /// * `neighbors` - The states of neighbor cells as in `collapse()` above.
26 fn observe(&self, cell: &mut S, neighbors: &[Option<S>]);
27}