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>
impl<T: PossibleValues, const D: usize> Grid<T, D>
Sourcepub fn new(size: [usize; D]) -> Result<Self, SizeErr>
pub fn new(size: [usize; D]) -> Result<Self, SizeErr>
Constructs a new Grid using the n-dimensional size.
Examples found in repository?
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}Sourcepub fn size(&self) -> [usize; D]
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?
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}Sourcepub fn as_inner(&self) -> &Array<Pixel<T>, D>
pub fn as_inner(&self) -> &Array<Pixel<T>, D>
Returns an immutable reference to the inner microNDArray
Sourcepub fn as_mut_inner(&mut self) -> &mut Array<Pixel<T>, D>
pub fn as_mut_inner(&mut self) -> &mut Array<Pixel<T>, D>
Returns an mutable reference to the inner microNDArray
Sourcepub fn get_item(&self, location: [usize; D]) -> Pixel<T>
pub fn get_item(&self, location: [usize; D]) -> Pixel<T>
Clones and returns a Pixel from the Grid.
Examples found in repository?
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}Sourcepub fn unidirectional_neighbors(
&self,
location: [usize; D],
) -> Vec<([usize; D], Pixel<T>)>
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?
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}Sourcepub fn neighbors(
&self,
location: [usize; D],
distance: usize,
) -> Vec<([usize; D], Pixel<T>)>
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.
Sourcepub fn check_loc(&self, location: [i128; D]) -> Option<[usize; D]>
pub fn check_loc(&self, location: [i128; D]) -> Option<[usize; D]>
Checks if a location is inside the Grid, then returns its Grid coordinates.
Sourcepub fn check_validity<F>(&mut self, test: F) -> Result<(), [usize; D]>
pub fn check_validity<F>(&mut self, test: F) -> Result<(), [usize; D]>
Checks if the Grid is valid
Sourcepub fn collapse<F, R>(
&mut self,
test: F,
effect_distance: usize,
rng: &mut R,
item: ([usize; D], Pixel<T>),
) -> Result<(), [usize; D]>
pub fn collapse<F, R>( &mut self, test: F, effect_distance: usize, rng: &mut R, item: ([usize; D], Pixel<T>), ) -> Result<(), [usize; D]>
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
Sourcepub fn wfc<F, R>(
&mut self,
test: F,
effect_distance: usize,
rng: &mut R,
chance: f32,
on_update: impl Fn(&Self),
) -> Result<(), [usize; D]>
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]>
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?
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}