Extract tile loading into separate method transitional_engine
authorunC0Rr
Mon, 13 Feb 2023 10:39:37 +0100
branchtransitional_engine
changeset 15950 168f44ef9b67
parent 15949 659c92124c26
child 15951 5f00829c55ec
Extract tile loading into separate method
rust/landgen/src/wavefront_collapse/generator.rs
rust/landgen/src/wavefront_collapse/tile_image.rs
rust/landgen/src/wavefront_collapse/transform.rs
rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
--- a/rust/landgen/src/wavefront_collapse/generator.rs	Sun Feb 12 15:10:10 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/generator.rs	Mon Feb 13 10:39:37 2023 +0100
@@ -5,7 +5,8 @@
 use png::Decoder;
 use std::collections::HashSet;
 use std::fs::File;
-use std::io::BufReader;
+use std::io::{BufReader, Result};
+use std::path::Path;
 
 pub struct WavefrontCollapseLandGenerator {
     pub size: Size,
@@ -16,13 +17,13 @@
         Self { size: *size }
     }
 
-    pub fn load_template<T: Copy + PartialEq + Default>(
-        &self,
+    fn load_image_tiles<T: Copy + PartialEq + Default>(
         parameters: &LandGenerationParameters<T>,
-    ) -> Vec<TileImage<T, String>> {
+        path: &Path,
+    ) -> Result<Vec<TileImage<T, String>>> {
         let mut result = Vec::new();
 
-        let file = File::open("sample.png").expect("file exists");
+        let file = File::open(path)?;
         let decoder = Decoder::new(BufReader::new(file));
         let mut reader = decoder.read_info().unwrap();
 
@@ -60,10 +61,26 @@
             TileImage::<T, String>::new(tiles_image, top_edge, right_edge, bottom_edge, left_edge);
 
         result.push(tile.clone());
+        result.push(tile.flipped());
+        result.push(tile.mirrored());
+        result.push(tile.mirrored().flipped());
         result.push(tile.rotated90());
         result.push(tile.rotated180());
         result.push(tile.rotated270());
 
+        Ok(result)
+    }
+
+    pub fn load_template<T: Copy + PartialEq + Default>(
+        &self,
+        parameters: &LandGenerationParameters<T>,
+    ) -> Vec<TileImage<T, String>> {
+        let mut result = Vec::new();
+
+        if let Ok(mut tiles) = Self::load_image_tiles(parameters, Path::new("sample.png")) {
+            result.append(&mut tiles);
+        }
+
         result
     }
 }
@@ -183,7 +200,10 @@
     fn test_generation() {
         let wfc_gen = WavefrontCollapseLandGenerator::new(&Size::new(2048, 1024));
         let landgen_params = LandGenerationParameters::new(0u32, 0xff000000u32, 0, true, true);
-        let land = wfc_gen.generate_land(&landgen_params, &mut [0u32, 1u32, 3u32, 5u32, 7u32, 11u32].into_iter().cycle());
+        let land = wfc_gen.generate_land(
+            &landgen_params,
+            &mut [0u32, 1u32, 3u32, 5u32, 7u32, 11u32].into_iter().cycle(),
+        );
 
         let path = Path::new(r"output.png");
         let file = File::create(path).unwrap();
--- a/rust/landgen/src/wavefront_collapse/tile_image.rs	Sun Feb 12 15:10:10 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/tile_image.rs	Mon Feb 13 10:39:37 2023 +0100
@@ -154,7 +154,7 @@
                 };
 
                 self.image.get(image_row, image_column)
-            },
+            }
             Transform::Rotate90(_) => {
                 let image_row = if self.transform.is_mirrored() {
                     column
@@ -169,7 +169,7 @@
                 };
 
                 self.image.get(image_row, image_column)
-            },
+            }
         }
     }
 }
--- a/rust/landgen/src/wavefront_collapse/transform.rs	Sun Feb 12 15:10:10 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/transform.rs	Mon Feb 13 10:39:37 2023 +0100
@@ -203,7 +203,12 @@
         );
         assert_eq!(
             Transform::default(),
-            Transform::default().rotate90().flip().rotate90().mirror().rotate180()
+            Transform::default()
+                .rotate90()
+                .flip()
+                .rotate90()
+                .mirror()
+                .rotate180()
         );
     }
 }
--- a/rust/landgen/src/wavefront_collapse/wavefront_collapse.rs	Sun Feb 12 15:10:10 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/wavefront_collapse.rs	Mon Feb 13 10:39:37 2023 +0100
@@ -148,7 +148,7 @@
                         );
                         println!("Rules are: {:?}", self.rules);*/
 
-                        //todo!("no collapse possible - what to do?")
+                        todo!("no collapse possible - what to do?")
                     }
                 }
             }