terrain_forge/algorithm.rs
1//! Algorithm trait for procedural generation
2
3use crate::{Cell, Grid};
4
5/// Trait for procedural generation algorithms.
6///
7/// All implementations must be `Send + Sync` so algorithms can be shared
8/// across threads (e.g. in a `Pipeline` or thread pool).
9///
10/// # Examples
11///
12/// ```
13/// use terrain_forge::{Grid, Algorithm};
14/// use terrain_forge::algorithms::Bsp;
15///
16/// let mut grid = Grid::new(40, 30);
17/// let algo = Bsp::default();
18/// algo.generate(&mut grid, 42);
19/// assert!(grid.count(|t| t.is_floor()) > 0);
20/// ```
21pub trait Algorithm<C: Cell = crate::Tile>: Send + Sync {
22 /// Generate content into the grid using the given seed
23 fn generate(&self, grid: &mut Grid<C>, seed: u64);
24
25 /// Algorithm name for identification
26 fn name(&self) -> &'static str;
27}
28
29impl<C: Cell> Algorithm<C> for Box<dyn Algorithm<C> + Send + Sync> {
30 fn generate(&self, grid: &mut Grid<C>, seed: u64) {
31 (**self).generate(grid, seed)
32 }
33
34 fn name(&self) -> &'static str {
35 (**self).name()
36 }
37}