Pass full path to WFC for tile loading transitional_engine
authorunC0Rr
Thu, 29 Aug 2024 12:56:16 +0200
branchtransitional_engine
changeset 16053 3402b2185698
parent 16052 0fd23fc57947
child 16054 274a5afc2aec
Pass full path to WFC for tile loading
rust/landgen/src/wavefront_collapse/generator.rs
rust/lib-hwengine-future/src/lib.rs
rust/mapgen/src/lib.rs
--- a/rust/landgen/src/wavefront_collapse/generator.rs	Wed Aug 28 17:16:23 2024 +0200
+++ b/rust/landgen/src/wavefront_collapse/generator.rs	Thu Aug 29 12:56:16 2024 +0200
@@ -6,7 +6,7 @@
 use std::collections::HashSet;
 use std::fs::File;
 use std::io::{BufReader, Result};
-use std::path::Path;
+use std::path::{Path, PathBuf};
 
 #[derive(Clone)]
 pub struct EdgeDescription {
@@ -53,21 +53,23 @@
 
 pub struct WavefrontCollapseLandGenerator {
     pub template: TemplateDescription,
+    data_path: PathBuf,
 }
 
 impl WavefrontCollapseLandGenerator {
-    pub fn new(template: TemplateDescription) -> Self {
-        Self { template }
+    pub fn new(template: TemplateDescription, data_path: &Path) -> Self {
+        Self { template, data_path: data_path.to_owned() }
     }
 
     fn load_image_tiles<T: Copy + PartialEq + Default>(
+        &self,
         parameters: &LandGenerationParameters<T>,
         tile_description: &TileDescription,
     ) -> Result<Vec<TileImage<T, String>>> {
         let mut result = Vec::new();
 
         let file = File::open(
-            Path::new("../share/hedgewars/Data/Tiles")
+            self.data_path.join("Tiles")
                 .join(&tile_description.name)
                 .as_path(),
         )?;
@@ -160,7 +162,7 @@
         let mut result = Vec::new();
 
         for tile_description in self.template.tiles.iter() {
-            if let Ok(mut tiles) = Self::load_image_tiles(parameters, tile_description) {
+            if let Ok(mut tiles) = self.load_image_tiles(parameters, tile_description) {
                 result.append(&mut tiles);
             } else {
                 eprintln!("Failed to load a tile!");
--- a/rust/lib-hwengine-future/src/lib.rs	Wed Aug 28 17:16:23 2024 +0200
+++ b/rust/lib-hwengine-future/src/lib.rs	Thu Aug 29 12:56:16 2024 +0200
@@ -64,7 +64,7 @@
     let yaml_templates =
         fs::read_to_string(data_path.join(Path::new("wfc_templates.yaml")).as_path())
             .expect("Error reading map templates file");
-    let mut map_gen = MapGenerator::<WfcTemplate>::new();
+    let mut map_gen = MapGenerator::<WfcTemplate>::new(data_path);
     map_gen.import_yaml_templates(&yaml_templates);
 
     let distance_divisor = feature_size.pow(2) / 8 + 10;
@@ -96,7 +96,7 @@
     let data_path = Path::new(&data_path);
 
     let theme_name: &str = unsafe { CStr::from_ptr(theme_name) }.to_str().unwrap();
-    let map_gen = MapGenerator::<()>::new();
+    let map_gen = MapGenerator::<()>::new(data_path);
 
     let theme = Theme::load(
         data_path
--- a/rust/mapgen/src/lib.rs	Wed Aug 28 17:16:23 2024 +0200
+++ b/rust/mapgen/src/lib.rs	Thu Aug 29 12:56:16 2024 +0200
@@ -4,6 +4,7 @@
 use self::theme::Theme;
 use crate::template::outline::TemplateCollectionDesc as OutlineTemplateCollectionDesc;
 use crate::template::wavefront_collapse::TemplateCollectionDesc as WfcTemplateCollectionDesc;
+use std::path::{Path, PathBuf};
 
 use land2d::Land2D;
 use landgen::{
@@ -32,12 +33,14 @@
 #[derive(Debug)]
 pub struct MapGenerator<T> {
     pub(crate) templates: HashMap<TemplateType, Vec<T>>,
+    data_path: PathBuf,
 }
 
 impl<T> MapGenerator<T> {
-    pub fn new() -> Self {
+    pub fn new(data_path: &Path) -> Self {
         Self {
             templates: HashMap::new(),
+            data_path: data_path.to_owned(),
         }
     }
 
@@ -158,7 +161,7 @@
     }
 
     pub fn build_generator(&self, template: WfcTemplate) -> impl LandGenerator {
-        WavefrontCollapseLandGenerator::new(template)
+        WavefrontCollapseLandGenerator::new(template, &self.data_path)
     }
 }