Grid

Struct Grid 

Source
pub struct Grid<T: PossibleValues, const D: usize> { /* private fields */ }
Expand description

A microwfc grid.

Implementations§

Source§

impl<T: PossibleValues, const D: usize> Grid<T, D>

Source

pub fn new(size: [usize; D]) -> Result<Self, SizeErr>

Constructs a new Grid using the n-dimensional size.

Examples found in repository?
examples/main.rs (line 35)
32fn main() {
33    let mut rng = thread_rng();
34    // Make a new 30-by-30 grid.
35    let mut grid: Grid<Tile, 2> = Grid::new([30, 30]).unwrap();
36    loop {
37        let r = grid.wfc(
38            |g, loc, me, probability| {
39                // We use !any(|x| ...) to get none(|x| ...) functionality
40                match *me {
41                    // Disallow water next to grass
42                    Tile::Water => (
43                        !g.unidirectional_neighbors(loc).iter().any(|x| {
44                            x.1.determined_value
45                                .as_ref()
46                                .map(|x| *x == Tile::Grass)
47                                .unwrap_or(false) // Allow unsolved pixels
48                        }),
49                        probability,
50                    ),
51                    // Dirt is always allowed
52                    Tile::Dirt => (true, probability),
53                    // Disallow grass next to water
54                    Tile::Grass => (
55                        !g.unidirectional_neighbors(loc).iter().any(|x| {
56                            x.1.determined_value
57                                .as_ref()
58                                .map(|x| *x == Tile::Water)
59                                .unwrap_or(false)
60                        }),
61                        probability,
62                    ),
63                }
64            },
65            1,
66            &mut rng,
67            0.05,
68            |grid| {
69                // Clear the screen
70                println!("\x1b[H\x1b[2J\x1b[3J");
71
72                let mut s = String::new();
73                for y in 0..grid.size()[0] {
74                    s += "\n";
75                    for x in 0..grid.size()[1] {
76                        if let Some(x) = grid.get_item([x, y]).determined_value {
77                            s += &String::from(x);
78                        } else {
79                            s += "  ";
80                        }
81                    }
82                }
83
84                println!("{}", s);
85                thread::sleep(Duration::from_millis(10));
86            },
87        );
88        if r.is_ok() {
89            break;
90        } else {
91            println!("fuck");
92        }
93    }
94}
Source

pub fn size(&self) -> [usize; D]

Returns the n-dimensional size of the Grid. In all default implementations, this returns a n-tuple where n is the dimensionality of the Grid.

Examples found in repository?
examples/main.rs (line 73)
32fn main() {
33    let mut rng = thread_rng();
34    // Make a new 30-by-30 grid.
35    let mut grid: Grid<Tile, 2> = Grid::new([30, 30]).unwrap();
36    loop {
37        let r = grid.wfc(
38            |g, loc, me, probability| {
39                // We use !any(|x| ...) to get none(|x| ...) functionality
40                match *me {
41                    // Disallow water next to grass
42                    Tile::Water => (
43                        !g.unidirectional_neighbors(loc).iter().any(|x| {
44                            x.1.determined_value
45                                .as_ref()
46                                .map(|x| *x == Tile::Grass)
47                                .unwrap_or(false) // Allow unsolved pixels
48                        }),
49                        probability,
50                    ),
51                    // Dirt is always allowed
52                    Tile::Dirt => (true, probability),
53                    // Disallow grass next to water
54                    Tile::Grass => (
55                        !g.unidirectional_neighbors(loc).iter().any(|x| {
56                            x.1.determined_value
57                                .as_ref()
58                                .map(|x| *x == Tile::Water)
59                                .unwrap_or(false)
60                        }),
61                        probability,
62                    ),
63                }
64            },
65            1,
66            &mut rng,
67            0.05,
68            |grid| {
69                // Clear the screen
70                println!("\x1b[H\x1b[2J\x1b[3J");
71
72                let mut s = String::new();
73                for y in 0..grid.size()[0] {
74                    s += "\n";
75                    for x in 0..grid.size()[1] {
76                        if let Some(x) = grid.get_item([x, y]).determined_value {
77                            s += &String::from(x);
78                        } else {
79                            s += "  ";
80                        }
81                    }
82                }
83
84                println!("{}", s);
85                thread::sleep(Duration::from_millis(10));
86            },
87        );
88        if r.is_ok() {
89            break;
90        } else {
91            println!("fuck");
92        }
93    }
94}
Source

pub fn as_inner(&self) -> &Array<Pixel<T>, D>

Returns an immutable reference to the inner microNDArray

Source

pub fn as_mut_inner(&mut self) -> &mut Array<Pixel<T>, D>

Returns an mutable reference to the inner microNDArray

Source

pub fn get_item(&self, location: [usize; D]) -> Pixel<T>

Clones and returns a Pixel from the Grid.

Examples found in repository?
examples/main.rs (line 76)
32fn main() {
33    let mut rng = thread_rng();
34    // Make a new 30-by-30 grid.
35    let mut grid: Grid<Tile, 2> = Grid::new([30, 30]).unwrap();
36    loop {
37        let r = grid.wfc(
38            |g, loc, me, probability| {
39                // We use !any(|x| ...) to get none(|x| ...) functionality
40                match *me {
41                    // Disallow water next to grass
42                    Tile::Water => (
43                        !g.unidirectional_neighbors(loc).iter().any(|x| {
44                            x.1.determined_value
45                                .as_ref()
46                                .map(|x| *x == Tile::Grass)
47                                .unwrap_or(false) // Allow unsolved pixels
48                        }),
49                        probability,
50                    ),
51                    // Dirt is always allowed
52                    Tile::Dirt => (true, probability),
53                    // Disallow grass next to water
54                    Tile::Grass => (
55                        !g.unidirectional_neighbors(loc).iter().any(|x| {
56                            x.1.determined_value
57                                .as_ref()
58                                .map(|x| *x == Tile::Water)
59                                .unwrap_or(false)
60                        }),
61                        probability,
62                    ),
63                }
64            },
65            1,
66            &mut rng,
67            0.05,
68            |grid| {
69                // Clear the screen
70                println!("\x1b[H\x1b[2J\x1b[3J");
71
72                let mut s = String::new();
73                for y in 0..grid.size()[0] {
74                    s += "\n";
75                    for x in 0..grid.size()[1] {
76                        if let Some(x) = grid.get_item([x, y]).determined_value {
77                            s += &String::from(x);
78                        } else {
79                            s += "  ";
80                        }
81                    }
82                }
83
84                println!("{}", s);
85                thread::sleep(Duration::from_millis(10));
86            },
87        );
88        if r.is_ok() {
89            break;
90        } else {
91            println!("fuck");
92        }
93    }
94}
Source

pub fn set_item(&mut self, location: [usize; D], item: Pixel<T>)

Sets a Pixel in the Grid.

Source

pub fn unidirectional_neighbors( &self, location: [usize; D], ) -> Vec<([usize; D], Pixel<T>)>

Returns unidirectional neighbors, meaning only neighbord with one common face. This means the corners will not be returned.

Examples found in repository?
examples/main.rs (line 43)
32fn main() {
33    let mut rng = thread_rng();
34    // Make a new 30-by-30 grid.
35    let mut grid: Grid<Tile, 2> = Grid::new([30, 30]).unwrap();
36    loop {
37        let r = grid.wfc(
38            |g, loc, me, probability| {
39                // We use !any(|x| ...) to get none(|x| ...) functionality
40                match *me {
41                    // Disallow water next to grass
42                    Tile::Water => (
43                        !g.unidirectional_neighbors(loc).iter().any(|x| {
44                            x.1.determined_value
45                                .as_ref()
46                                .map(|x| *x == Tile::Grass)
47                                .unwrap_or(false) // Allow unsolved pixels
48                        }),
49                        probability,
50                    ),
51                    // Dirt is always allowed
52                    Tile::Dirt => (true, probability),
53                    // Disallow grass next to water
54                    Tile::Grass => (
55                        !g.unidirectional_neighbors(loc).iter().any(|x| {
56                            x.1.determined_value
57                                .as_ref()
58                                .map(|x| *x == Tile::Water)
59                                .unwrap_or(false)
60                        }),
61                        probability,
62                    ),
63                }
64            },
65            1,
66            &mut rng,
67            0.05,
68            |grid| {
69                // Clear the screen
70                println!("\x1b[H\x1b[2J\x1b[3J");
71
72                let mut s = String::new();
73                for y in 0..grid.size()[0] {
74                    s += "\n";
75                    for x in 0..grid.size()[1] {
76                        if let Some(x) = grid.get_item([x, y]).determined_value {
77                            s += &String::from(x);
78                        } else {
79                            s += "  ";
80                        }
81                    }
82                }
83
84                println!("{}", s);
85                thread::sleep(Duration::from_millis(10));
86            },
87        );
88        if r.is_ok() {
89            break;
90        } else {
91            println!("fuck");
92        }
93    }
94}
Source

pub fn neighbors( &self, location: [usize; D], distance: usize, ) -> Vec<([usize; D], Pixel<T>)>

Returns all neighbors, including ones touching only at a single point. This does return corners.

Source

pub fn check_loc(&self, location: [i128; D]) -> Option<[usize; D]>

Checks if a location is inside the Grid, then returns its Grid coordinates.

Source

pub fn check_validity<F>(&mut self, test: F) -> Result<(), [usize; D]>
where F: Fn(&Grid<T, D>, [usize; D], &T, f32) -> (bool, f32),

Checks if the Grid is valid

Source

pub fn collapse<F, R>( &mut self, test: F, effect_distance: usize, rng: &mut R, item: ([usize; D], Pixel<T>), ) -> Result<(), [usize; D]>
where F: Fn(&Grid<T, D>, [usize; D], &T, f32) -> (bool, f32), R: Rng,

Collapses a single Pixel and updates neighbors. This will return false if the Grid is invalid. Please note that this function is not very useful, and you should use wfc instead

Source

pub fn wfc<F, R>( &mut self, test: F, effect_distance: usize, rng: &mut R, chance: f32, on_update: impl Fn(&Self), ) -> Result<(), [usize; D]>
where F: Fn(&Grid<T, D>, [usize; D], &T, f32) -> (bool, f32), R: Rng,

Performs the wave-function-collapse algorithm on the Grid. This returns if the algorithm was successful, and the state of the Grid is not guaranteed to be valid if it returns false, but there is never unsafety in reading from the Grid.

The chance parameter determines the likelyhood of a random collapse happening anywhere on the grid. In some strict environments, this can cause unsolvable grids.

Examples found in repository?
examples/main.rs (lines 37-87)
32fn main() {
33    let mut rng = thread_rng();
34    // Make a new 30-by-30 grid.
35    let mut grid: Grid<Tile, 2> = Grid::new([30, 30]).unwrap();
36    loop {
37        let r = grid.wfc(
38            |g, loc, me, probability| {
39                // We use !any(|x| ...) to get none(|x| ...) functionality
40                match *me {
41                    // Disallow water next to grass
42                    Tile::Water => (
43                        !g.unidirectional_neighbors(loc).iter().any(|x| {
44                            x.1.determined_value
45                                .as_ref()
46                                .map(|x| *x == Tile::Grass)
47                                .unwrap_or(false) // Allow unsolved pixels
48                        }),
49                        probability,
50                    ),
51                    // Dirt is always allowed
52                    Tile::Dirt => (true, probability),
53                    // Disallow grass next to water
54                    Tile::Grass => (
55                        !g.unidirectional_neighbors(loc).iter().any(|x| {
56                            x.1.determined_value
57                                .as_ref()
58                                .map(|x| *x == Tile::Water)
59                                .unwrap_or(false)
60                        }),
61                        probability,
62                    ),
63                }
64            },
65            1,
66            &mut rng,
67            0.05,
68            |grid| {
69                // Clear the screen
70                println!("\x1b[H\x1b[2J\x1b[3J");
71
72                let mut s = String::new();
73                for y in 0..grid.size()[0] {
74                    s += "\n";
75                    for x in 0..grid.size()[1] {
76                        if let Some(x) = grid.get_item([x, y]).determined_value {
77                            s += &String::from(x);
78                        } else {
79                            s += "  ";
80                        }
81                    }
82                }
83
84                println!("{}", s);
85                thread::sleep(Duration::from_millis(10));
86            },
87        );
88        if r.is_ok() {
89            break;
90        } else {
91            println!("fuck");
92        }
93    }
94}

Auto Trait Implementations§

§

impl<T, const D: usize> Freeze for Grid<T, D>

§

impl<T, const D: usize> RefUnwindSafe for Grid<T, D>
where T: RefUnwindSafe,

§

impl<T, const D: usize> Send for Grid<T, D>
where T: Send,

§

impl<T, const D: usize> Sync for Grid<T, D>
where T: Sync,

§

impl<T, const D: usize> Unpin for Grid<T, D>
where T: Unpin,

§

impl<T, const D: usize> UnwindSafe for Grid<T, D>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V